Files
pterodactyl-panel/resources/scripts/components/server/databases/DatabaseTable.tsx
T

228 lines
8.8 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import type { ColumnDef } from '@tanstack/react-table';
import { Eye } from 'lucide-react';
import { useStore } from '@tanstack/react-form';
import {
deleteServerDatabaseInput,
type ServerDatabase,
useDeleteServerDatabase,
} from '@/api/server/databases/queries';
import { useCurrentServerUuid } from '@/api/server/queries';
import Button from '@/components/elements/Button';
import Can from '@/components/elements/Can';
import CopyOnClick from '@/components/elements/CopyOnClick';
import Label from '@/components/elements/Label';
import SecretInput from '@/components/elements/SecretInput';
2026-10-05 18:10:18 -04:00
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
import { actionsColumn, DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions';
import { Dialog } from '@/components/elements/dialog';
import { Form, useAppForm } from '@/components/form';
import { TextInput } from '@/components/form/controls';
import RotatePasswordButton from '@/components/server/databases/RotatePasswordButton';
import { jdbcConnectionString, serverDatabaseShortName } from '@/lib/databases';
import { relationshipAttributes } from '@/api/relationships';
function DeleteDatabaseDialog({
database,
open,
onClose,
}: {
database: ServerDatabase;
open: boolean;
onClose: () => void;
}) {
const uuid = useCurrentServerUuid()!;
const deleteDatabase = useDeleteServerDatabase();
const { attributes } = database;
const form = useAppForm({
defaultValues: { confirm: '' },
onSubmit: async () => {
try {
await deleteDatabase.mutateAsync(deleteServerDatabaseInput(uuid, database));
onClose();
form.reset();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(form.store, (state) => state.isSubmitting) || deleteDatabase.isPending;
const close = () => {
onClose();
form.reset();
};
return (
<Dialog
open={open}
2026-10-08 16:53:20 -04:00
title='Confirm database deletion'
2026-10-05 18:10:18 -04:00
preventExternalClose={isSubmitting}
hideCloseIcon={isSubmitting}
onClose={close}
>
<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 is permanent. This will delete <strong>{attributes.name}</strong> and all associated
data.
</p>
2026-10-08 16:53:20 -04:00
<Form form={form} className='m-0 mt-6'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='confirm'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
value === attributes.name || value === serverDatabaseShortName(attributes.name)
? undefined
: 'The database name must be provided.',
}}
>
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
type='text'
id='confirm_name'
label='Confirm Database Name'
description='Enter the database name to confirm deletion.'
2026-10-05 18:10:18 -04:00
/>
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6 text-right'>
<Button type='button' isSecondary className='mr-2' onClick={close}>
2026-10-05 18:10:18 -04:00
Cancel
</Button>
<form.AppForm>
2026-10-08 16:53:20 -04:00
<form.SubmitButton color='red'>Delete Database</form.SubmitButton>
2026-10-05 18:10:18 -04:00
</form.AppForm>
</div>
</Form>
</Dialog>
);
}
function DatabaseConnectionDialog({
database,
open,
onClose,
}: {
database: ServerDatabase;
open: boolean;
onClose: () => void;
}) {
const { attributes } = database;
const connectionString = `${attributes.host.address}:${attributes.host.port}`;
const password = relationshipAttributes(attributes.relationships?.password)?.password;
const jdbcConnection = jdbcConnectionString({
connectionString,
name: attributes.name,
password,
username: attributes.username,
});
return (
2026-10-08 16:53:20 -04:00
<Dialog open={open} title='Database connection details' onClose={onClose}>
2026-10-05 18:10:18 -04:00
<div>
<Label>Endpoint</Label>
<CopyOnClick text={connectionString}>
2026-10-08 16:53:20 -04:00
<TextInput type='text' readOnly value={connectionString} />
2026-10-05 18:10:18 -04:00
</CopyOnClick>
</div>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<Label>Connections from</Label>
2026-10-08 16:53:20 -04:00
<TextInput type='text' readOnly value={attributes.connections_from} />
2026-10-05 18:10:18 -04:00
</div>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<Label>Username</Label>
<CopyOnClick text={attributes.username}>
2026-10-08 16:53:20 -04:00
<TextInput type='text' readOnly value={attributes.username} />
2026-10-05 18:10:18 -04:00
</CopyOnClick>
</div>
2026-10-08 16:53:20 -04:00
<Can action='database.view_password'>
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<Label>Password</Label>
2026-10-08 16:53:20 -04:00
<SecretInput value={password} label='Password' />
2026-10-05 18:10:18 -04:00
</div>
</Can>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<Label>JDBC Connection String</Label>
<CopyOnClick text={jdbcConnection} showInNotification={false}>
2026-10-08 16:53:20 -04:00
<TextInput type='text' readOnly value={jdbcConnection} />
2026-10-05 18:10:18 -04:00
</CopyOnClick>
</div>
2026-10-08 16:53:20 -04:00
<div className='mt-6 flex justify-end gap-2'>
<Can action='database.update'>
2026-10-05 18:10:18 -04:00
<RotatePasswordButton databaseId={attributes.id} />
</Can>
<Button isSecondary onClick={onClose}>
Close
</Button>
</div>
</Dialog>
);
}
const DatabaseActions = ({ database }: { database: ServerDatabase }) => (
<RowActions>
<Dialog.Trigger
trigger={({ onClick }) => (
<RowActionButton
icon={Eye}
2026-10-08 16:53:20 -04:00
label='Connection details'
2026-10-05 18:10:18 -04:00
aria-label={`View ${database.attributes.name} connection details`}
onClick={onClick}
/>
)}
>
{({ open, onClose }) => <DatabaseConnectionDialog database={database} open={open} onClose={onClose} />}
</Dialog.Trigger>
2026-10-08 16:53:20 -04:00
<Can action='database.delete'>
2026-10-05 18:10:18 -04:00
<Dialog.Trigger
trigger={({ onClick }) => (
<DeleteAction aria-label={`Delete ${database.attributes.name}`} onClick={onClick} />
)}
>
{({ open, onClose }) => <DeleteDatabaseDialog database={database} open={open} onClose={onClose} />}
</Dialog.Trigger>
</Can>
</RowActions>
);
export const databaseColumns: ColumnDef<ServerDatabase>[] = [
{
id: 'name',
accessorFn: (database) => database.attributes.name,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Database' />,
2026-10-05 18:10:18 -04:00
cell: ({ row }) => (
<CopyOnClick text={row.original.attributes.name}>
2026-10-08 16:53:20 -04:00
<span className='font-medium'>{row.original.attributes.name}</span>
2026-10-05 18:10:18 -04:00
</CopyOnClick>
),
},
{
id: 'endpoint',
accessorFn: (database) => `${database.attributes.host.address}:${database.attributes.host.port}`,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Endpoint' />,
2026-10-05 18:10:18 -04:00
cell: ({ getValue }) => (
<CopyOnClick text={getValue<string>()}>
2026-10-08 16:53:20 -04:00
<code className='whitespace-nowrap text-xs'>{getValue<string>()}</code>
2026-10-05 18:10:18 -04:00
</CopyOnClick>
),
meta: { headerClassName: 'hidden md:table-cell', cellClassName: 'hidden md:table-cell' },
},
{
id: 'connections_from',
accessorFn: (database) => database.attributes.connections_from,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Connections from' />,
2026-10-05 18:10:18 -04:00
meta: { headerClassName: 'hidden lg:table-cell', cellClassName: 'hidden lg:table-cell' },
},
{
id: 'username',
accessorFn: (database) => database.attributes.username,
2026-10-08 16:53:20 -04:00
header: ({ column }) => <DataTableColumnHeader column={column} title='Username' />,
2026-10-05 18:10:18 -04:00
cell: ({ getValue }) => (
<CopyOnClick text={getValue<string>()}>
2026-10-08 16:53:20 -04:00
<code className='text-xs'>{getValue<string>()}</code>
2026-10-05 18:10:18 -04:00
</CopyOnClick>
),
meta: { headerClassName: 'hidden xl:table-cell', cellClassName: 'hidden xl:table-cell' },
},
actionsColumn<ServerDatabase>(2, (database) => <DatabaseActions database={database} />),
];