"use client"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { DataTable } from "@app/components/ui/data-table"; import { ExtendedColumnDef } from "@app/components/ui/data-table"; import { Button } from "@app/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { toast } from "@app/hooks/useToast"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { ArrowRight, ArrowUpDown, ArrowUpRight, MoreHorizontal } from "lucide-react"; import { useTranslations } from "next-intl"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useMemo, useState, useTransition } from "react"; import { Badge } from "./ui/badge"; import { InfoPopup } from "./ui/info-popup"; export type ClientRow = { id: number; name: string; subnet: string; // siteIds: string; mbIn: string; mbOut: string; orgId: string; online: boolean; olmVersion?: string; olmUpdateAvailable: boolean; userId: string | null; username: string | null; userEmail: string | null; }; type ClientTableProps = { userClients: ClientRow[]; orgId: string; }; export default function UserDevicesTable({ userClients }: ClientTableProps) { const router = useRouter(); const t = useTranslations(); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedClient, setSelectedClient] = useState( null ); const api = createApiClient(useEnvContext()); const [isRefreshing, startTransition] = useTransition(); const defaultUserColumnVisibility = { client: false, subnet: false }; const refreshData = () => { startTransition(() => { try { router.refresh(); } catch (error) { toast({ title: t("error"), description: t("refreshError"), variant: "destructive" }); } }); }; const deleteClient = (clientId: number) => { api.delete(`/client/${clientId}`) .catch((e) => { console.error("Error deleting client", e); toast({ variant: "destructive", title: "Error deleting client", description: formatAxiosError(e, "Error deleting client") }); }) .then(() => { startTransition(() => { router.refresh(); setIsDeleteModalOpen(false); }); }); }; // Check if there are any rows without userIds in the current view's data const hasRowsWithoutUserId = useMemo(() => { return userClients.some((client) => !client.userId); }, [userClients]); const columns: ExtendedColumnDef[] = useMemo(() => { const baseColumns: ExtendedColumnDef[] = [ { accessorKey: "name", enableHiding: false, friendlyName: "Name", header: ({ column }) => { return ( ); } }, { accessorKey: "userId", friendlyName: "User", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const r = row.original; return r.userId ? ( ) : ( "-" ); } }, // { // accessorKey: "siteName", // header: ({ column }) => { // return ( // // ); // }, // cell: ({ row }) => { // const r = row.original; // return ( // // // // ); // } // }, { accessorKey: "online", friendlyName: "Connectivity", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const originalRow = row.original; if (originalRow.online) { return (
Connected
); } else { return (
Disconnected
); } } }, { accessorKey: "mbIn", friendlyName: "Data In", header: ({ column }) => { return ( ); } }, { accessorKey: "mbOut", friendlyName: "Data Out", header: ({ column }) => { return ( ); } }, { accessorKey: "client", friendlyName: t("client"), header: ({ column }) => { return ( ); }, cell: ({ row }) => { const originalRow = row.original; return (
Olm {originalRow.olmVersion && ( v{originalRow.olmVersion} )}
{originalRow.olmUpdateAvailable && ( )}
); } }, { accessorKey: "subnet", friendlyName: "Address", header: ({ column }) => { return ( ); } } ]; // Only include actions column if there are rows without userIds if (hasRowsWithoutUserId) { baseColumns.push({ id: "actions", enableHiding: false, header: () => , cell: ({ row }) => { const clientRow = row.original; return !clientRow.userId ? (
{/* */} {/* */} {/* View settings */} {/* */} {/* */} { setSelectedClient(clientRow); setIsDeleteModalOpen(true); }} > Delete
) : null; } }); } return baseColumns; }, [hasRowsWithoutUserId, t]); return ( <> {selectedClient && ( { setIsDeleteModalOpen(val); setSelectedClient(null); }} dialog={

{t("deleteClientQuestion")}

{t("clientMessageRemove")}

} buttonText="Confirm Delete Client" onConfirm={async () => deleteClient(selectedClient!.id)} string={selectedClient.name} title="Delete Client" /> )} ); }