import React from 'react'; import { Link } from '@tanstack/react-router'; import { useServerDetail } from '@/components/admin/servers/useServerDetail'; import type { AdminServer } from '@/api/admin/servers/queries'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import Code from '@/components/elements/Code'; import CopyOnClick from '@/components/elements/CopyOnClick'; import { relationshipAttributes, relationshipData } from '@/api/relationships'; type ServerAttributes = AdminServer['attributes']; type AllocationAttributes = { id: number; ip: string; port: number; alias?: string | null; }; type NamedRef = { id: number; name: string }; type UserRef = { id: number; username: string; email: string; first_name?: string; last_name?: string }; type NodeRef = { id: number; name: string; fqdn: string; relationships?: { location?: { attributes: LocationRef | null } | undefined }; }; type LocationRef = { id: number; short: string; long?: string | null }; const Row = ({ label, children }: { label: string; children: React.ReactNode }) => (
{label} {children}
); const AssignmentRow = ({ label, children }: { label: string; children: React.ReactNode }) => (
{label}
{children}
); const assignmentLinkClass = 'block truncate text-sm font-medium text-foreground transition-colors hover:text-accent'; const formatLimit = (value: number, unit: string, unlimited = 'Unlimited') => value === 0 ? {unlimited} : {`${value}${unit}`}; const getDefaultAllocation = ( relationships: ServerAttributes['relationships'], allocationId: number ): AllocationAttributes | undefined => relationshipData(relationships?.allocations) .map((allocation) => allocation.attributes) .find((allocation) => allocation.id === allocationId); const getOwnerDisplayName = (user: UserRef | undefined): string => user ? [user.first_name, user.last_name].filter(Boolean).join(' ') : ''; const hasConnectionAlias = (allocation: AllocationAttributes | undefined): allocation is AllocationAttributes => !!allocation?.alias && allocation.alias !== allocation.ip; function ExternalIdentifierValue({ externalId }: { externalId: string | null | undefined }) { if (!externalId) { return Not Set; } return {externalId}; } function EggValue({ egg }: { egg: NamedRef | undefined }) { if (!egg) { return Unknown; } return ( {egg.name} ); } function CpuPinningValue({ threads }: { threads: string | null | undefined }) { if (!threads) { return Not Set; } return {threads}; } function SwapValue({ swap }: { swap: number }) { if (swap === 0) { return Not Set; } if (swap === -1) { return Unlimited; } return {`${swap}MiB`}; } function DefaultConnectionValue({ allocation }: { allocation: AllocationAttributes | undefined }) { if (!allocation) { return Unknown; } return {`${allocation.ip}:${allocation.port}`}; } function ConnectionAliasValue({ allocation }: { allocation: AllocationAttributes | undefined }) { if (!hasConnectionAlias(allocation)) { return No Alias Assigned; } return {`${allocation.alias}:${allocation.port}`}; } function InformationCard({ attributes, allocation, egg, }: { attributes: ServerAttributes; allocation: AllocationAttributes | undefined; egg: NamedRef | undefined; }) { return ( {attributes.id} {attributes.uuid} {attributes.name} {formatLimit(attributes.limits.cpu, '%')} {formatLimit(attributes.limits.memory, 'MiB')} {' / '} {formatLimit(attributes.limits.disk, 'MiB')} {attributes.limits.io} ); } function StatusCard({ attributes }: { attributes: ServerAttributes }) { if (!attributes.suspended && !attributes.status) { return null; } return ( {attributes.suspended &&

This server is currently suspended.

} {attributes.container.installed === 0 && (

This server is currently installing.

)} {attributes.status === 'install_failed' && (

This server failed to install.

)}
); } function OwnerValue({ user, ownerName }: { user: UserRef | undefined; ownerName: string }) { if (!user) { return Unknown; } return ( <> {ownerName ? `${ownerName} (${user.username})` : user.username}

{user.email}

); } function NodeValue({ node }: { node: NodeRef | undefined }) { if (!node) { return Unknown; } return ( <> {node.name}

{node.fqdn}

); } function LocationValue({ location }: { location: LocationRef | undefined }) { if (!location) { return Unknown; } return ( <> {location.short} {location.long &&

{location.long}

} ); } function AssignmentCard({ user, node, location, ownerName, }: { user: UserRef | undefined; node: NodeRef | undefined; location: LocationRef | undefined; ownerName: string; }) { return (
); } export default function ServerAboutTab() { const { server } = useServerDetail(); const { attributes } = server; const relationships = attributes.relationships; const allocation = getDefaultAllocation(relationships, attributes.allocation); const user = relationshipAttributes(relationships?.user) as UserRef | undefined; const node = relationshipAttributes(relationships?.node) as NodeRef | undefined; const egg = relationshipAttributes(relationships?.egg) as NamedRef | undefined; const location = relationshipAttributes(node?.relationships?.location) as LocationRef | undefined; const ownerName = getOwnerDisplayName(user); return (
); }