Files

198 lines
7.7 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import type { ColumnDef } from '@tanstack/react-table';
import { useStore } from '@tanstack/react-form';
import { Link } from '@tanstack/react-router';
import {
type AdminDatabaseHost,
deleteAdminDatabaseHostInput,
useDeleteAdminDatabaseHost,
} from '@/api/admin/database-hosts/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 dayjs from '@/lib/dayjs';
import { relationshipAttributes } from '@/api/relationships';
2026-11-23 14:16:00 +00:00
import Translate from '@/components/elements/Translate';
import { useTranslation, type Translation } from '@/sdk/localization';
2026-10-05 18:10:18 -04:00
interface DatabaseHostDialogProps {
host: AdminDatabaseHost;
open: boolean;
onClose: () => void;
}
function DeleteDatabaseHostDialog({ host, open, onClose }: DatabaseHostDialogProps) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation();
2026-10-05 18:10:18 -04:00
const deleteDatabaseHost = useDeleteAdminDatabaseHost();
const deleteForm = useAppForm({
defaultValues: { confirm: '' },
onSubmit: async () => {
try {
await deleteDatabaseHost.mutateAsync(
deleteAdminDatabaseHostInput(host.attributes.id, host.attributes.name)
);
onClose();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(deleteForm.store, (state) => state.isSubmitting);
return (
<Dialog
open={open}
2026-11-23 14:16:00 +00:00
title={t('Confirm database host deletion')}
2026-10-05 18:10:18 -04:00
preventExternalClose={isSubmitting}
hideCloseIcon={isSubmitting}
onClose={onClose}
>
<SpinnerOverlay visible={isSubmitting} />
2026-10-08 16:53:20 -04:00
<p className='text-sm'>
2026-11-23 14:16:00 +00:00
<Translate
i18nKey='Deleting a database host is permanent. This will permanently delete <bold>{{name}}</bold>.'
values={{ name: host.attributes.name }}
components={{ bold: <strong /> }}
/>
2026-10-05 18:10:18 -04:00
</p>
2026-10-08 16:53:20 -04:00
<Form form={deleteForm} className='m-0 mt-6'>
2026-10-05 18:10:18 -04:00
<deleteForm.AppField
2026-10-08 16:53:20 -04:00
name='confirm'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
2026-11-23 14:16:00 +00:00
value === host.attributes.name ? undefined : t('The host name must be provided.'),
2026-10-05 18:10:18 -04:00
}}
>
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
type='text'
2026-10-05 18:10:18 -04:00
id={`confirm_${host.attributes.id}`}
2026-11-23 14:16:00 +00:00
label={t('Confirm host name')}
description={t('Enter the name of this host to confirm deletion.')}
2026-10-05 18:10:18 -04:00
/>
)}
</deleteForm.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6 flex justify-end gap-2'>
<Button.Text type='button' isSecondary onClick={onClose}>
2026-11-23 14:16:00 +00:00
{t('Cancel')}
2026-10-05 18:10:18 -04:00
</Button.Text>
<deleteForm.AppForm>
2026-11-23 14:16:00 +00:00
<deleteForm.SubmitButton color='red'>{t('Delete host')}</deleteForm.SubmitButton>
2026-10-05 18:10:18 -04:00
</deleteForm.AppForm>
</div>
</Form>
</Dialog>
);
}
const HostIdentityCell = ({ host }: { host: AdminDatabaseHost }) => {
const { name, username, host: address, port } = host.attributes;
const connection = `${username}@${address}:${port}`;
return (
2026-10-08 16:53:20 -04:00
<div className='w-0 min-w-full'>
2026-10-05 18:10:18 -04:00
<Link
2026-10-08 16:53:20 -04:00
to='/panel/databases/$id'
2026-10-05 18:10:18 -04:00
params={{ id: host.attributes.id }}
title={name}
2026-10-08 16:53:20 -04:00
className='block truncate font-medium text-foreground no-underline transition-colors hover:text-accent'
2026-10-05 18:10:18 -04:00
>
{name}
</Link>
2026-10-08 16:53:20 -04:00
<p className='mt-0.5 truncate font-mono text-xs text-muted-foreground' title={connection}>
2026-10-05 18:10:18 -04:00
{connection}
</p>
</div>
);
};
const HostNodeCell = ({ host }: { host: AdminDatabaseHost }) => {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation();
2026-10-05 18:10:18 -04:00
const node = relationshipAttributes(host.attributes.relationships?.node);
2026-10-08 16:53:20 -04:00
if (node) {
2026-10-05 18:10:18 -04:00
return (
<Link
2026-10-08 16:53:20 -04:00
to='/panel/nodes/$id'
2026-10-05 18:10:18 -04:00
params={{ id: node.id }}
title={node.name}
2026-10-08 16:53:20 -04:00
className='block w-0 min-w-full truncate text-foreground no-underline hover:text-accent'
2026-10-05 18:10:18 -04:00
>
{node.name}
</Link>
);
2026-10-08 16:53:20 -04:00
}
2026-10-05 18:10:18 -04:00
return (
2026-10-08 16:53:20 -04:00
<span className='text-muted-foreground'>
2026-11-23 14:16:00 +00:00
{host.attributes.node_id === null ? t('Global host') : t('Node #{{id}}', { id: host.attributes.node_id })}
2026-10-05 18:10:18 -04:00
</span>
);
};
2026-11-23 14:16:00 +00:00
const HostActionsCell = ({ host }: { host: AdminDatabaseHost }) => {
const { t } = useTranslation();
2026-10-05 18:10:18 -04:00
2026-11-23 14:16:00 +00:00
return (
<RowActions>
<EditLinkAction
aria-label={t('Edit {{name}}', { name: host.attributes.name })}
to='/panel/databases/$id'
params={{ id: host.attributes.id }}
/>
<Dialog.Trigger
trigger={({ onClick }) => (
<DeleteAction aria-label={t('Delete {{name}}', { name: host.attributes.name })} onClick={onClick} />
)}
2026-10-05 18:10:18 -04:00
>
2026-11-23 14:16:00 +00:00
{({ open, onClose }) => <DeleteDatabaseHostDialog host={host} open={open} onClose={onClose} />}
</Dialog.Trigger>
</RowActions>
);
};
export const databaseHostColumns = (t: Translation['t']) =>
[
{
id: 'name',
accessorFn: (host) => host.attributes.name,
header: ({ column }) => <DataTableColumnHeader column={column} title={t('Database host')} />,
cell: ({ row }) => <HostIdentityCell host={row.original} />,
enableSorting: true,
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'node',
header: t('Node'),
cell: ({ row }) => <HostNodeCell host={row.original} />,
enableSorting: false,
meta: { headerClassName: 'hidden w-36 @lg:table-cell', cellClassName: 'hidden w-36 @lg:table-cell' },
},
{
id: 'databases_count',
header: t('Databases'),
cell: ({ row }) => <span className='tabular-nums'>{row.original.attributes.databases_count}</span>,
enableSorting: false,
meta: { headerClassName: 'hidden w-28 @xl:table-cell', cellClassName: 'hidden w-28 @xl:table-cell' },
},
{
id: 'created_at',
accessorFn: (host) => host.attributes.created_at,
header: ({ column }) => <DataTableColumnHeader column={column} title={t('Created')} />,
cell: ({ row }) => (
<time
dateTime={row.original.attributes.created_at}
className='whitespace-nowrap text-xs text-muted-foreground'
>
2026-11-23 14:51:03 +00:00
{dayjs(row.original.attributes.created_at).format('ll')}
2026-11-23 14:16:00 +00:00
</time>
),
enableSorting: true,
sortDescFirst: true,
meta: { headerClassName: 'hidden w-28 @2xl:table-cell', cellClassName: 'hidden w-28 @2xl:table-cell' },
},
actionsColumn<AdminDatabaseHost>(2, (host) => <HostActionsCell host={host} />),
] satisfies ColumnDef<AdminDatabaseHost>[];