import type { Column, ColumnDef } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { cn, decimalString, formatBytes, hourWithSeconds } from "@/lib/utils" import type { SystemdRecord } from "@/types" import { ServiceStatus, ServiceStatusLabels, ServiceSubState, ServiceSubStateLabels } from "@/lib/enums" import { ActivityIcon, ArrowUpDownIcon, ClockIcon, CpuIcon, MemoryStickIcon, TerminalSquareIcon, } from "lucide-react" import { Badge } from "../ui/badge" import { t } from "@lingui/core/macro" // import { $allSystemsById } from "@/lib/stores" // import { useStore } from "@nanostores/react" function getSubStateColor(subState: ServiceSubState) { switch (subState) { case ServiceSubState.Running: return "bg-green-500" case ServiceSubState.Failed: return "bg-red-500" case ServiceSubState.Dead: return "bg-yellow-500" default: return "bg-zinc-500" } } export const systemdTableCols: ColumnDef[] = [ { id: "name", sortingFn: (a, b) => a.original.name.localeCompare(b.original.name), accessorFn: (record) => record.name, header: ({ column }) => , cell: ({ getValue }) => { return {getValue() as string} }, }, // { // id: "system", // accessorFn: (record) => record.system, // sortingFn: (a, b) => { // const allSystems = $allSystemsById.get() // const systemNameA = allSystems[a.original.system]?.name ?? "" // const systemNameB = allSystems[b.original.system]?.name ?? "" // return systemNameA.localeCompare(systemNameB) // }, // header: ({ column }) => , // cell: ({ getValue }) => { // const allSystems = useStore($allSystemsById) // return {allSystems[getValue() as string]?.name ?? ""} // }, // }, { id: "state", accessorFn: (record) => record.state, header: ({ column }) => , cell: ({ getValue }) => { const statusValue = getValue() as ServiceStatus const statusLabel = ServiceStatusLabels[statusValue] || "Unknown" return ( {statusLabel} ) }, }, { id: "sub", accessorFn: (record) => record.sub, header: ({ column }) => , cell: ({ getValue }) => { const subState = getValue() as ServiceSubState const subStateLabel = ServiceSubStateLabels[subState] || "Unknown" return ( {subStateLabel} ) }, }, { id: "cpu", accessorFn: (record) => { if (record.sub !== ServiceSubState.Running) { return -1 } return record.cpu }, invertSorting: true, header: ({ column }) => , cell: ({ getValue }) => { const val = getValue() as number if (val < 0) { return N/A } return {`${decimalString(val, val >= 10 ? 1 : 2)}%`} }, }, { id: "cpuPeak", accessorFn: (record) => { if (record.sub !== ServiceSubState.Running) { return -1 } return record.cpuPeak ?? 0 }, invertSorting: true, header: ({ column }) => , cell: ({ getValue }) => { const val = getValue() as number if (val < 0) { return N/A } return {`${decimalString(val, val >= 10 ? 1 : 2)}%`} }, }, { id: "memory", accessorFn: (record) => record.memory, invertSorting: true, header: ({ column }) => , cell: ({ getValue }) => { const val = getValue() as number if (!val) { return N/A } const formatted = formatBytes(val, false, undefined, false) return ( {`${decimalString(formatted.value, formatted.value >= 10 ? 1 : 2)} ${formatted.unit}`} ) }, }, { id: "memPeak", accessorFn: (record) => record.memPeak, invertSorting: true, header: ({ column }) => , cell: ({ getValue }) => { const val = getValue() as number if (!val) { return N/A } const formatted = formatBytes(val, false, undefined, false) return ( {`${decimalString(formatted.value, formatted.value >= 10 ? 1 : 2)} ${formatted.unit}`} ) }, }, { id: "updated", invertSorting: true, accessorFn: (record) => record.updated, header: ({ column }) => , cell: ({ getValue }) => { const timestamp = getValue() as number return ( {hourWithSeconds(new Date(timestamp).toISOString())} ) }, }, ] function HeaderButton({ column, name, Icon }: { column: Column; name: string; Icon: React.ElementType }) { const isSorted = column.getIsSorted() return ( ) } export function getStatusColor(status: ServiceStatus) { switch (status) { case ServiceStatus.Active: return "bg-green-500" case ServiceStatus.Failed: return "bg-red-500" case ServiceStatus.Reloading: case ServiceStatus.Activating: case ServiceStatus.Deactivating: return "bg-yellow-500" default: return "bg-zinc-500" } }