import { useStore } from '@tanstack/react-form';
import { jdbcConnectionString, serverDatabaseShortName } from '@/lib/databases';
import { Eye } from 'lucide-react';
import { useAppForm, Form, type AppForm } from '@/components/form';
import {
type AdminServerDatabase,
deleteAdminServerDatabaseInput,
rotateAdminServerDatabasePasswordInput,
useDeleteAdminServerDatabase,
useRotateAdminServerDatabasePassword,
} from '@/api/admin/servers/queries';
import { Dialog } from '@/components/elements/dialog';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import Label from '@/components/elements/Label';
import { TextInput } from '@/components/form/controls';
import Button from '@/components/elements/Button';
import CopyOnClick from '@/components/elements/CopyOnClick';
import { DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions';
import { relationshipAttributes } from '@/api/relationships';
export interface ServerDatabaseRowProps {
serverId: number;
database: AdminServerDatabase;
}
type ServerDatabaseDialogProps = ServerDatabaseRowProps & {
open: boolean;
onClose: () => void;
};
function DeleteDatabaseConfirmForm({
form,
databaseName,
onCancel,
}: {
form: AppForm<{ confirm: string }>;
databaseName: string;
onCancel: () => void;
}) {
return (
value === databaseName || value === serverDatabaseShortName(databaseName)
? undefined
: 'The database name must be provided.',
}}
>
{(field) => (
)}
Delete Database
);
}
function DeleteServerDatabaseDialog({ serverId, database, open, onClose }: ServerDatabaseDialogProps) {
const deleteDatabase = useDeleteAdminServerDatabase();
const databaseAttributes = database.attributes;
const form = useAppForm({
defaultValues: { confirm: '' },
onSubmit: async () => {
try {
await deleteDatabase.mutateAsync(
deleteAdminServerDatabaseInput(serverId, databaseAttributes.id, databaseAttributes.name)
);
onClose();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
return (
);
}
function ServerDatabaseConnectionDialog({ serverId, database, open, onClose }: ServerDatabaseDialogProps) {
const rotatePassword = useRotateAdminServerDatabasePassword();
const databaseAttributes = database.attributes;
const connectionString = `${databaseAttributes.host.address}:${databaseAttributes.host.port}`;
const password = relationshipAttributes(databaseAttributes.relationships?.password)?.password;
const jdbcConnection = jdbcConnectionString({
connectionString,
name: databaseAttributes.name,
password,
username: databaseAttributes.username,
});
const rotate = () => {
rotatePassword.mutate(rotateAdminServerDatabasePasswordInput(serverId, databaseAttributes.id));
};
return (
);
}
export function ServerDatabaseActions({ serverId, database }: ServerDatabaseRowProps) {
const { name } = database.attributes;
return (
(
)}
>
{({ open, onClose }) => (
)}
}>
{({ open, onClose }) => (
)}
);
}