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,
} 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 (
{state.kind === 'loading' ? (
) : state.kind === 'unavailable' ? (
{unavailableLabels[state.reason]}
) : (
[
{
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 }) => (
))
)}
);
}
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 (