Files
pterodactyl-panel/resources/scripts/components/admin/databases/DatabaseHostTable.tsx
T

184 lines
6.9 KiB
TypeScript
Raw 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';
interface DatabaseHostDialogProps {
host: AdminDatabaseHost;
open: boolean;
onClose: () => void;
}
function DeleteDatabaseHostDialog({ host, open, onClose }: DatabaseHostDialogProps) {
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-10-08 16:53:20 -04:00
title='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-10-05 18:10:18 -04:00
Deleting a database host is permanent. This will permanently delete{' '}
<strong>{host.attributes.name}</strong>.
</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 }) =>
value === host.attributes.name ? undefined : 'The host name must be provided.',
}}
>
{(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-10-08 16:53:20 -04:00
label='Confirm host name'
description='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-10-05 18:10:18 -04:00
Cancel
</Button.Text>
<deleteForm.AppForm>
2026-10-08 16:53:20 -04:00
<deleteForm.SubmitButton color='red'>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 }) => {
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-10-05 18:10:18 -04:00
{host.attributes.node_id === null ? 'Global host' : `Node #${host.attributes.node_id}`}
</span>
);
};
const HostActionsCell = ({ host }: { host: AdminDatabaseHost }) => (
<RowActions>
<EditLinkAction
aria-label={`Edit ${host.attributes.name}`}
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 }}
/>
<Dialog.Trigger
trigger={({ onClick }) => <DeleteAction aria-label={`Delete ${host.attributes.name}`} onClick={onClick} />}
>
{({ open, onClose }) => <DeleteDatabaseHostDialog host={host} open={open} onClose={onClose} />}
</Dialog.Trigger>
</RowActions>
);
export const databaseHostColumns = [
{
id: 'name',
accessorFn: (host) => host.attributes.name,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Database host' />,
2026-10-05 18:10:18 -04:00
cell: ({ row }) => <HostIdentityCell host={row.original} />,
enableSorting: true,
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'node',
header: '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: 'Databases',
2026-10-08 16:53:20 -04:00
cell: ({ row }) => <span className='tabular-nums'>{row.original.attributes.databases_count}</span>,
2026-10-05 18:10:18 -04:00
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,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Created' />,
2026-10-05 18:10:18 -04:00
cell: ({ row }) => (
<time
dateTime={row.original.attributes.created_at}
2026-10-08 16:53:20 -04:00
className='whitespace-nowrap text-xs text-muted-foreground'
2026-10-05 18:10:18 -04:00
>
{dayjs(row.original.attributes.created_at).format('MMM D, YYYY')}
</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>[];