"use client"; import { type ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, getSortedRowModel, type PaginationState, type SortingState, useReactTable, } from "@tanstack/react-table"; import type { inferRouterOutputs } from "@trpc/server"; import { ArrowUpDown, Eye, FolderOpen, HardDrive, Loader2, Trash2, } from "lucide-react"; import { useMemo, useState } from "react"; import { toast } from "sonner"; import { FilesExplorerModal } from "@/components/dashboard/docker/files/files-explorer-modal"; import { AlertBlock } from "@/components/shared/alert-block"; import { CodeEditor } from "@/components/shared/code-editor"; import { DialogAction } from "@/components/shared/dialog-action"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { AppRouter } from "@/server/api/root"; import { api } from "@/utils/api"; type VolumeRow = inferRouterOutputs["dockerVolume"]["getVolumes"][number]; interface Props { serverId?: string; } const SIZE_UNITS: Record = { b: 1, kb: 1e3, mb: 1e6, gb: 1e9, tb: 1e12, }; const parseSize = (size?: string) => { if (!size) return -1; const match = /^([\d.]+)\s*([a-zA-Z]+)$/.exec(size.trim()); if (!match?.[1] || !match[2]) return -1; return Number(match[1]) * (SIZE_UNITS[match[2].toLowerCase()] ?? 1); }; const SortableHeader = ({ column, title, }: { column: { getIsSorted: () => false | "asc" | "desc"; toggleSorting: (asc: boolean) => void; }; title: string; }) => ( ); const ShowVolumeConfig = ({ volumeName, serverId, }: { volumeName: string; serverId?: string; }) => { const [open, setOpen] = useState(false); const { data, isLoading, error } = api.dockerVolume.getVolumeConfig.useQuery( { volumeName, serverId }, { enabled: open }, ); return ( Volume Config docker volume inspect output for "{volumeName}" {error ? ( {error.message} ) : isLoading ? (
Loading...
) : (
								
							
)}
); }; export const ShowVolumes = ({ serverId }: Props) => { const utils = api.useUtils(); const [sorting, setSorting] = useState([ { id: "Name", desc: false }, ]); const [globalFilter, setGlobalFilter] = useState(""); const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10, }); const { data: volumes, isLoading } = api.dockerVolume.getVolumes.useQuery({ serverId, }); const { data: sizes, isLoading: isLoadingSizes } = api.dockerVolume.getVolumesSize.useQuery({ serverId }); const sizeByName = useMemo( () => new Map((sizes ?? []).map((entry) => [entry.name, entry.size])), [sizes], ); const { mutateAsync: removeVolume } = api.dockerVolume.removeVolume.useMutation(); const filteredData = useMemo(() => { const list = volumes ?? []; if (!globalFilter.trim()) { return list; } const query = globalFilter.toLowerCase(); return list.filter((volume) => volume.Name.toLowerCase().includes(query)); }, [volumes, globalFilter]); const columns = useMemo[]>( () => [ { accessorKey: "Name", header: ({ column }) => , cell: ({ row }) => (
{row.original.Name}
), }, { accessorKey: "Driver", header: ({ column }) => ( ), cell: ({ row }) => ( {row.original.Driver} ), }, { accessorKey: "Scope", header: ({ column }) => ( ), cell: ({ row }) => ( {row.original.Scope} ), }, { id: "Size", header: ({ column }) => , sortingFn: (a, b) => parseSize(sizeByName.get(a.original.Name) ?? undefined) - parseSize(sizeByName.get(b.original.Name) ?? undefined), cell: ({ row }) => isLoadingSizes ? ( ) : ( {sizeByName.get(row.original.Name) ?? "-"} ), }, { accessorKey: "Mountpoint", header: ({ column }) => ( ), cell: ({ row }) => (
{row.original.Mountpoint}
), }, { id: "actions", enableSorting: false, header: () =>
Actions
, cell: ({ row }) => (
{ try { await removeVolume({ volumeName: row.original.Name, serverId, }); toast.success("Volume deleted"); await utils.dockerVolume.getVolumes.invalidate(); } catch (error) { toast.error("Error deleting volume", { description: error instanceof Error ? error.message : "Unknown error", }); } }} >
), }, ], [serverId, removeVolume, utils, sizeByName, isLoadingSizes], ); const table = useReactTable({ data: filteredData, columns, state: { sorting, pagination, }, onSortingChange: setSorting, onPaginationChange: setPagination, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getPaginationRowModel: getPaginationRowModel(), }); return (
Volumes Manage the Docker volumes of the selected server. {isLoading ? (
Loading...
) : !volumes?.length ? (

No volumes found

Docker volumes created on this server will appear here.

) : ( <>
setGlobalFilter(e.target.value)} className="max-w-xs" />
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ))} )) ) : ( No volumes match your filters. )}
{table.getPageCount() > 1 && (
Page {table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount()}
)} )}
); };