import { useMemo } from 'react';
import { Link, useNavigate } from '@tanstack/react-router';
import { getCoreRowModel, useReactTable, type ColumnDef, type Table } from '@tanstack/react-table';
import { Server } from 'lucide-react';
import { useAdminNodeServers } from '@/api/admin/nodes/queries';
import type { AdminServer } from '@/api/admin/servers/queries';
import { useNodeDetail } from '@/components/admin/nodes/useNodeDetail';
import ServerEditAction from '@/components/admin/servers/ServerEditAction';
import { getPageSearch, usePageSearch } from '@/router/search';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import Code from '@/components/elements/Code';
import Spinner from '@/components/elements/Spinner';
import DataTable from '@/components/elements/table/DataTable';
import { actionsColumn, RowActions } from '@/components/elements/table/RowActions';
import DataTablePagination from '@/components/elements/table/DataTablePagination';
import { Alert } from '@/components/elements/alert';
import Button from '@/components/elements/Button';
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty';
import { httpErrorToHuman } from '@/api/http';
import { relationshipAttributes } from '@/api/relationships';
const nodeServerColumns = [
{
id: 'name',
header: 'Server',
cell: ({ row }) => (
{row.original.attributes.name}
{row.original.attributes.description || 'No description'}
),
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'owner',
header: 'Owner',
cell: ({ row }) => {
const owner = relationshipAttributes(row.original.attributes.relationships?.user);
return owner ? (
{owner.username}
{owner.email}
) : (
User #{row.original.attributes.user}
);
},
meta: { headerClassName: 'hidden w-44 @xl:table-cell', cellClassName: 'hidden w-44 @xl:table-cell' },
},
{
id: 'service',
header: 'Service',
cell: ({ row }) => {
const attributes = row.original.attributes;
const egg = relationshipAttributes(attributes.relationships?.egg);
const service = egg?.name ?? `Egg #${attributes.egg}`;
return (
{service}
);
},
meta: { headerClassName: 'hidden w-40 @2xl:table-cell', cellClassName: 'hidden w-40 @2xl:table-cell' },
},
{
id: 'identifier',
header: 'Identifier',
cell: ({ row }) => (
{row.original.attributes.identifier}
ID {row.original.attributes.id}
),
meta: {
headerClassName: 'hidden w-28 text-right @sm:table-cell',
cellClassName: 'hidden w-28 text-right @sm:table-cell',
},
},
actionsColumn(1, (server) => (
)),
] satisfies ColumnDef[];
type NodeServersQuery = ReturnType;
interface NodeServerListProps {
query: NodeServersQuery;
table: Table;
}
const NodeServerList = ({ query, table }: NodeServerListProps) => {
const { data: servers, error, isFetching: loading, refetch } = query;
if (error && !loading) {
return (
{httpErrorToHuman(error)}
refetch()}>
Retry
);
}
if (!servers) {
return ;
}
return (
<>
No servers
No servers are running on this node yet.
}
/>
>
);
};
export default function NodeServersTab() {
const { node } = useNodeDetail();
const { attributes } = node;
const navigate = useNavigate();
const page = usePageSearch();
const query = useAdminNodeServers(attributes.id, page);
const servers = query.data;
const pagination = { pageIndex: Math.max(page - 1, 0), pageSize: servers?.meta.pagination.per_page ?? 50 };
const data = useMemo(() => servers?.data ?? [], [servers?.data]);
const table = useReactTable({
data,
columns: nodeServerColumns,
getCoreRowModel: getCoreRowModel(),
getRowId: (server) => String(server.attributes.id),
manualPagination: true,
rowCount: servers?.meta.pagination.total ?? 0,
state: { pagination },
onPaginationChange: (updater) => {
const next = updater instanceof Function ? updater(pagination) : updater;
if (next.pageIndex !== pagination.pageIndex) {
void navigate({
to: '/panel/nodes/$id/servers',
params: { id: attributes.id },
search: getPageSearch(next.pageIndex + 1),
replace: true,
viewTransition: false,
});
}
},
});
return (
);
}