import { useMemo } from 'react'; import { useStore } from '@tanstack/react-form'; import { Link, useNavigate } from '@tanstack/react-router'; import { getCoreRowModel, useReactTable, type ColumnDef } from '@tanstack/react-table'; import { ArrowLeft, Egg, HardDrive } from 'lucide-react'; import type { AppForm } from '@/components/form'; import { useAppForm, Form } from '@/components/form'; import { httpErrorToHuman } from '@/api/http'; import { type AdminMountEgg, type AdminMountNode, type AdminMountWithRelations, deleteAdminMountInput, detachAdminMountEggInput, detachAdminMountNodeInput, type MountValues, updateAdminMountInput, useAdminMount, useDeleteAdminMount, useDetachAdminMountEgg, useDetachAdminMountNode, useUpdateAdminMount, } from '@/api/admin/mounts/queries'; import { mountDetailRoute } from '@/router/routeTree'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import ExtensionFormFields from '@/components/admin/extensions/ExtensionFormFields'; import { initialExtensionValues, useExtensionPayload } from '@/extensions/forms'; import AttachEggsModal from '@/components/admin/mounts/AttachEggsModal'; import AttachNodesModal from '@/components/admin/mounts/AttachNodesModal'; import { validateMountName } from '@/components/admin/mounts/mountForm'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import Icon from '@/components/elements/Icon'; import Button from '@/components/elements/Button'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import Spinner from '@/components/elements/Spinner'; import { Dialog } from '@/components/elements/dialog'; import { ServerError } from '@/components/elements/ScreenBlock'; import DataTable from '@/components/elements/table/DataTable'; import { actionsColumn, DeleteAction, EditLinkAction, RowActions } from '@/components/elements/table/RowActions'; import ListToolbar from '@/components/elements/ListToolbar'; import { NewButton } from '@/components/elements/NewButton'; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty'; import { relationshipData } from '@/api/relationships'; type MountForm = AppForm; const mountToValues = (mount: AdminMountWithRelations): MountValues => ({ name: mount.attributes.name, description: mount.attributes.description ?? '', source: mount.attributes.source, target: mount.attributes.target, readOnly: mount.attributes.read_only, userMountable: mount.attributes.user_mountable, extensions: initialExtensionValues(), }); const MountDetailsCard = ({ form, mount, error, onDelete, }: { form: MountForm; mount: AdminMountWithRelations; error: unknown; onDelete: () => void; }) => (
validateMountName(value), }} > {(field) => ( )}
value.length <= 191 ? undefined : 'A description must not exceed 191 characters.', }} > {(field) => ( )}
(value.length >= 1 ? undefined : 'A source path must be provided.'), }} > {(field) => ( )}
(value.length >= 1 ? undefined : 'A target path must be provided.'), }} > {(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{() => ( )}
Save Changes
); const AttachedEggsCard = ({ eggs, onDetach, onAttach, }: { eggs: AdminMountEgg[]; onDetach: (egg: AdminMountEgg) => void; onAttach: () => void; }) => { const columns = useMemo( () => [ { id: 'name', header: 'Egg', cell: ({ row }) => ( {row.original.attributes.name} ), }, { id: 'id', header: 'ID', cell: ({ row }) => ( {row.original.attributes.id} ), meta: { headerClassName: 'hidden sm:table-cell w-16 text-right', cellClassName: 'hidden sm:table-cell w-16 text-right', }, }, actionsColumn(2, (egg) => ( ( )} onConfirmed={(_event, close) => { close(); onDetach(egg); }} > This will detach {egg.attributes.name} from this mount. Servers using that egg will no longer be offered this mount. )), ] satisfies ColumnDef[], [onDetach] ); const table = useReactTable({ data: eggs, columns, getCoreRowModel: getCoreRowModel(), getRowId: (egg) => String(egg.attributes.id), }); return ( Attach eggs No eggs attached Servers using an attached egg are offered this mount. Attach eggs } /> ); }; const AttachedNodesCard = ({ nodes, onDetach, onAttach, }: { nodes: AdminMountNode[]; onDetach: (node: AdminMountNode) => void; onAttach: () => void; }) => { const columns = useMemo( () => [ { id: 'name', header: 'Node', cell: ({ row }) => (
{row.original.attributes.name}

{row.original.attributes.fqdn}

), }, actionsColumn(2, (node) => ( ( )} onConfirmed={(_event, close) => { close(); onDetach(node); }} > This will detach {node.attributes.name} from this mount. Servers on that node will no longer be offered this mount. )), ] satisfies ColumnDef[], [onDetach] ); const table = useReactTable({ data: nodes, columns, getCoreRowModel: getCoreRowModel(), getRowId: (node) => String(node.attributes.id), }); return ( Attach nodes No nodes attached Servers on an attached node are offered this mount. Attach nodes } /> ); }; const DeleteMountDialog = ({ mount, onClose, onDeleted, }: { mount: AdminMountWithRelations; onClose: () => void; onDeleted: () => Promise; }) => { const deleteMount = useDeleteAdminMount(); const deleteForm = useAppForm({ defaultValues: { confirm: '' }, onSubmit: async () => { try { await deleteMount.mutateAsync(deleteAdminMountInput(mount.attributes.id, mount.attributes.name)); await onDeleted(); onClose(); } catch { // Error toast is handled by the mutation. } }, }); const deleteSubmitting = useStore(deleteForm.store, (state) => state.isSubmitting); const close = () => { deleteForm.reset(); onClose(); }; return (

Deleting a mount is a permanent action, it cannot be undone. This will permanently delete the{' '} {mount.attributes.name} mount and detach it from every egg and node.

value === mount.attributes.name ? undefined : 'The mount name must be provided.', }} > {(field) => ( )}
Delete Mount
); }; const MountDetailContent = ({ mount }: { mount: AdminMountWithRelations }) => { const navigate = useNavigate(); const mountId = mount.attributes.id; const updateMount = useUpdateAdminMount(); const detachMountEgg = useDetachAdminMountEgg(); const detachMountNode = useDetachAdminMountNode(); const { withExtensionPayload } = useExtensionPayload('admin.mount'); const editForm = useAppForm({ defaultValues: mountToValues(mount), onSubmit: async ({ value }) => { try { await updateMount.mutateAsync(updateAdminMountInput(mountId, withExtensionPayload(value, mount))); editForm.reset({ ...value, extensions: initialExtensionValues() }); } catch { // Error toast is handled by the mutation. } }, }); const onDetachEgg = (egg: AdminMountEgg) => { detachMountEgg.mutate(detachAdminMountEggInput(mountId, egg.attributes.id, egg.attributes.name)); }; const onDetachNode = (node: AdminMountNode) => { detachMountNode.mutate(detachAdminMountNodeInput(mountId, node.attributes.id, node.attributes.name)); }; const { attributes } = mount; return ( Back to Mounts
( )} > {({ open, onClose }) => open && ( navigate({ to: '/panel/mounts' })} /> ) }
( )} > {({ open, onClose }) => open && ( ) } ( )} > {({ open, onClose }) => open && ( ) }
); }; export default function MountDetailContainer() { const loadedMount = mountDetailRoute.useLoaderData(); const mountId = loadedMount.attributes.id; const { data: mount = loadedMount, error } = useAdminMount(mountId); if (error) { return ; } if (!mount) { return ( ); } return ; }