import { createContext, useContext } from 'react'; import { Cpu, EthernetPort, HardDrive, MemoryStick, Server as ServerIcon } from 'lucide-react'; import Spinner from '@/components/elements/Spinner'; import { ServerRowMetricDescription, ServerRowMetricIcon } from './ServerRowMetrics'; import { bytesToString } from '@/lib/formatters'; import { cn } from '@/lib/cn'; import Slot from '@/extensions/Slot'; import type { AccountServer } from '@/api/account/servers/queries'; import type { ComponentPartProps, ComponentParts, DefaultComponentProps, ServerCardModel, ServerCardState, } from '@/extensions/componentTypes'; export const ServerCardContext = createContext<{ model: ServerCardModel; server: AccountServer } | null>(null); type PartProps = ComponentPartProps<'dashboard.serverCard'>; function Identity({ model }: PartProps) { const context = useContext(ServerCardContext)!; return (

{model.name}

{!!model.description && (

{model.description}

)}
); } function Address({ model }: PartProps) { return (

{model.address}

); } const unavailableLabels = { suspended: 'Suspended', 'connection-error': 'Connection Error', maintenance: 'Under Maintenance', transferring: 'Transferring', installing: 'Installing', 'restoring-backup': 'Restoring Backup', unavailable: 'Unavailable', } as const; const unavailableBadgeColors = { suspended: 'bg-destructive text-destructive-foreground', 'connection-error': 'bg-destructive text-destructive-foreground', maintenance: 'bg-warning text-warning-foreground', transferring: 'bg-popover text-foreground', installing: 'bg-popover text-foreground', 'restoring-backup': 'bg-popover text-foreground', unavailable: 'bg-popover text-foreground', } as const; type UnavailableReason = Extract['reason']; function UnavailableBadge({ reason }: { reason: UnavailableReason }) { return (
{unavailableLabels[reason]}
); } function MetricsContent({ state }: { state: ServerCardState }) { if (state.kind === 'loading') { return ; } if (state.kind === 'unavailable') { return ; } return ( <> {[ { id: 'cpu', icon: Cpu, metric: state.cpu, value: `${state.cpu.value.toFixed(2)} %`, limit: state.cpu.limit === 0 ? 'Unlimited' : `${state.cpu.limit} %`, }, { id: 'memory', icon: MemoryStick, metric: state.memory, value: bytesToString(state.memory.value), limit: state.memory.limit === 0 ? 'Unlimited' : bytesToString(state.memory.limit), }, { id: 'disk', icon: HardDrive, metric: state.disk, value: bytesToString(state.disk.value), limit: state.disk.limit === 0 ? 'Unlimited' : bytesToString(state.disk.limit), }, ].map(({ id, icon, metric, value, limit }) => (
{value}

of {limit}

))} ); } function Metrics({ model }: PartProps) { return (
); } export const serverCardParts: ComponentParts<'dashboard.serverCard'> = { identity: Identity, address: Address, metrics: Metrics, }; export function DefaultServerCard({ className, parts }: DefaultComponentProps<'dashboard.serverCard'>) { const { model } = useContext(ServerCardContext)!; const IdentityPart = parts?.identity ?? Identity; const AddressPart = parts?.address ?? Address; const MetricsPart = parts?.metrics ?? Metrics; return (
); }