Files
2026-11-23 14:16:00 +00:00

171 lines
6.8 KiB
TypeScript

import type { ColumnDef } from '@tanstack/react-table';
import { useStore } from '@tanstack/react-form';
import { Link } from '@tanstack/react-router';
import { type AdminMount, deleteAdminMountInput, useDeleteAdminMount } from '@/api/admin/mounts/queries';
import Button from '@/components/elements/Button';
import { Dialog } from '@/components/elements/dialog';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
import { actionsColumn, DeleteAction, EditLinkAction, RowActions } from '@/components/elements/table/RowActions';
import { Form, useAppForm } from '@/components/form';
import Translate from '@/components/elements/Translate';
import { type Translation, useTranslation } from '@/sdk/localization';
interface MountDialogProps {
mount: AdminMount;
open: boolean;
onClose: () => void;
}
function DeleteMountDialog({ mount, open, onClose }: MountDialogProps) {
const { t } = useTranslation();
const { attributes } = mount;
const deleteMount = useDeleteAdminMount();
const deleteForm = useAppForm({
defaultValues: { confirm: '' },
onSubmit: async () => {
try {
await deleteMount.mutateAsync(deleteAdminMountInput(attributes.id, attributes.name));
onClose();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(deleteForm.store, (state) => state.isSubmitting);
return (
<Dialog
open={open}
title={t('Confirm mount deletion')}
preventExternalClose={isSubmitting}
hideCloseIcon={isSubmitting}
onClose={onClose}
>
<SpinnerOverlay visible={isSubmitting} />
<p className='text-sm'>
<Translate
i18nKey='Deleting a mount is permanent. This will permanently delete <bold>{{name}}</bold> and remove it from attached eggs and nodes.'
values={{ name: attributes.name }}
components={{ bold: <strong /> }}
/>
</p>
<Form form={deleteForm} className='m-0 mt-6'>
<deleteForm.AppField
name='confirm'
validators={{
onChange: ({ value }) =>
value === attributes.name ? undefined : t('The mount name must be provided.'),
}}
>
{(field) => (
<field.TextField
type='text'
id={`confirm_${attributes.uuid}`}
label={t('Confirm mount name')}
description={t('Enter the name of this mount to confirm deletion.')}
/>
)}
</deleteForm.AppField>
<div className='mt-6 flex justify-end gap-2'>
<Button.Text type='button' isSecondary onClick={onClose}>
{t('Cancel')}
</Button.Text>
<deleteForm.AppForm>
<deleteForm.SubmitButton color='red'>{t('Delete mount')}</deleteForm.SubmitButton>
</deleteForm.AppForm>
</div>
</Form>
</Dialog>
);
}
const MountIdentityCell = ({ mount }: { mount: AdminMount }) => (
<div className='w-0 min-w-full'>
<Link
to='/panel/mounts/$id'
params={{ id: mount.attributes.id }}
title={mount.attributes.name}
className='block truncate font-medium text-foreground no-underline transition-colors hover:text-accent'
>
{mount.attributes.name}
</Link>
{mount.attributes.description ? (
<p className='mt-0.5 truncate text-xs text-muted-foreground' title={mount.attributes.description}>
{mount.attributes.description}
</p>
) : null}
</div>
);
const MountPathCell = ({ path }: { path: string }) => (
<code className='block w-0 min-w-full truncate text-xs' title={path}>
{path}
</code>
);
const MountActionsCell = ({ mount }: { mount: AdminMount }) => {
const { t } = useTranslation();
return (
<RowActions>
<EditLinkAction
aria-label={t('Edit {{name}}', { name: mount.attributes.name })}
to='/panel/mounts/$id'
params={{ id: mount.attributes.id }}
/>
<Dialog.Trigger
trigger={({ onClick }) => (
<DeleteAction
aria-label={t('Delete {{name}}', { name: mount.attributes.name })}
onClick={onClick}
/>
)}
>
{({ open, onClose }) => <DeleteMountDialog mount={mount} open={open} onClose={onClose} />}
</Dialog.Trigger>
</RowActions>
);
};
export const mountColumns = (t: Translation['t']) =>
[
{
id: 'name',
accessorFn: (mount) => mount.attributes.name,
header: ({ column }) => <DataTableColumnHeader column={column} title={t('Mount')} />,
cell: ({ row }) => <MountIdentityCell mount={row.original} />,
enableSorting: true,
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'source',
header: t('Source'),
cell: ({ row }) => <MountPathCell path={row.original.attributes.source} />,
enableSorting: false,
meta: { headerClassName: 'hidden w-44 @2xl:table-cell', cellClassName: 'hidden w-44 @2xl:table-cell' },
},
{
id: 'target',
header: t('Target'),
cell: ({ row }) => <MountPathCell path={row.original.attributes.target} />,
enableSorting: false,
meta: { headerClassName: 'hidden w-44 @4xl:table-cell', cellClassName: 'hidden w-44 @4xl:table-cell' },
},
{
id: 'eggs_count',
header: t('Eggs'),
cell: ({ row }) => <span className='tabular-nums'>{row.original.attributes.eggs_count}</span>,
enableSorting: false,
meta: { headerClassName: 'hidden w-20 @sm:table-cell', cellClassName: 'hidden w-20 @sm:table-cell' },
},
{
id: 'nodes_count',
header: t('Nodes'),
cell: ({ row }) => <span className='tabular-nums'>{row.original.attributes.nodes_count}</span>,
enableSorting: false,
meta: { headerClassName: 'hidden w-20 @xl:table-cell', cellClassName: 'hidden w-20 @xl:table-cell' },
},
actionsColumn<AdminMount>(2, (mount) => <MountActionsCell mount={mount} />),
] satisfies ColumnDef<AdminMount>[];