2026-10-05 18:10:18 -04:00
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' ;
2026-10-06 22:32:56 +01:00
import SecretInput from '@/components/elements/SecretInput' ;
2026-10-05 18:10:18 -04:00
import { DeleteAction , RowActionButton , RowActions } from '@/components/elements/table/RowActions' ;
import { relationshipAttributes } from '@/api/relationships' ;
2026-11-23 14:16:00 +00:00
import Translate from '@/components/elements/Translate' ;
import { useTranslation } from '@/sdk/localization' ;
2026-10-05 18:10:18 -04:00
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 ;
}) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
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 === databaseName || value === serverDatabaseShortName ( databaseName )
? undefined
2026-11-23 14:16:00 +00:00
: t ( 'The database 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'
id = 'confirm_name'
2026-11-23 14:16:00 +00:00
label = { t ( 'Confirm Database Name' )}
description = { t ( '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 = { onCancel }>
2026-11-23 14:16:00 +00:00
{ t ( 'Cancel' )}
2026-10-05 18:10:18 -04:00
</ Button >
< form.AppForm >
2026-11-23 14:16:00 +00:00
< form.SubmitButton color = 'red' >{ t ( 'Delete Database' )}</ form.SubmitButton >
2026-10-05 18:10:18 -04:00
</ form.AppForm >
</ div >
</ Form >
);
}
function DeleteServerDatabaseDialog ({ serverId , database , open , onClose } : ServerDatabaseDialogProps ) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
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 (
< Dialog
open = { open }
2026-11-23 14:16:00 +00:00
title = { t ( 'Confirm database 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 is a permanent action, it cannot be undone. This will permanently delete the <bold>{{name}}</bold> database and remove all associated data.'
values = {{ name : databaseAttributes.name }}
components = {{ bold : < strong /> }}
/>
2026-10-05 18:10:18 -04:00
</ p >
< DeleteDatabaseConfirmForm form = { form } databaseName = { databaseAttributes . name } onCancel = { onClose } />
</ Dialog >
);
}
function ServerDatabaseConnectionDialog ({ serverId , database , open , onClose } : ServerDatabaseDialogProps ) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
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 (
2026-11-23 14:16:00 +00:00
< Dialog open = { open } title = { t ( 'Database connection details' )} onClose = { onClose }>
2026-10-05 18:10:18 -04:00
< div >
2026-11-23 14:16:00 +00:00
< Label >{ t ( 'Endpoint' )}</ Label >
2026-10-05 18:10:18 -04:00
< 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-11-23 14:16:00 +00:00
< Label >{ t ( 'Connections from' )}</ Label >
2026-10-08 16:53:20 -04:00
< TextInput type = 'text' readOnly value = { databaseAttributes . connections_from } />
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'mt-6' >
2026-11-23 14:16:00 +00:00
< Label >{ t ( 'Max Connections' )}</ Label >
< TextInput type = 'text' readOnly value = { databaseAttributes . max_connections ?? t ( 'Unlimited' )} />
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'mt-6' >
2026-11-23 14:16:00 +00:00
< Label >{ t ( 'Username' )}</ Label >
2026-10-05 18:10:18 -04:00
< CopyOnClick text = { databaseAttributes . username }>
2026-10-08 16:53:20 -04:00
< TextInput type = 'text' readOnly value = { databaseAttributes . username } />
2026-10-05 18:10:18 -04:00
</ CopyOnClick >
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'mt-6' >
2026-11-23 14:16:00 +00:00
< Label >{ t ( 'Password' )}</ Label >
< SecretInput value = { password } label = { t ( 'Password' )} />
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'mt-6' >
2026-11-23 14:16:00 +00:00
< Label >{ t ( 'JDBC Connection String' )}</ Label >
2026-10-05 18:10:18 -04:00
< 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 text-right' >
2026-10-05 18:10:18 -04:00
< Button
isSecondary
2026-10-08 16:53:20 -04:00
color = 'primary'
className = 'mr-2'
2026-10-05 18:10:18 -04:00
onClick = { rotate }
isLoading = { rotatePassword . isPending }
>
2026-11-23 14:16:00 +00:00
{ t ( 'Rotate Password' )}
2026-10-05 18:10:18 -04:00
</ Button >
< Button isSecondary onClick = { onClose }>
2026-11-23 14:16:00 +00:00
{ t ( 'Close' )}
2026-10-05 18:10:18 -04:00
</ Button >
</ div >
</ Dialog >
);
}
export function ServerDatabaseActions ({ serverId , database } : ServerDatabaseRowProps ) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
const { name } = database . attributes ;
return (
< RowActions >
< Dialog.Trigger
trigger = {({ onClick }) => (
< RowActionButton
icon = { Eye }
2026-11-23 14:16:00 +00:00
label = { t ( 'Connection details' )}
aria-label = { t ( 'View {{name}} connection details' , { name })}
2026-10-05 18:10:18 -04:00
onClick = { onClick }
/>
)}
>
{({ open , onClose }) => (
< ServerDatabaseConnectionDialog
serverId = { serverId }
database = { database }
open = { open }
onClose = { onClose }
/>
)}
</ Dialog.Trigger >
2026-11-23 14:16:00 +00:00
< Dialog.Trigger
trigger = {({ onClick }) => (
< DeleteAction aria-label = { t ( 'Delete {{name}}' , { name })} onClick = { onClick } />
)}
>
2026-10-05 18:10:18 -04:00
{({ open , onClose }) => (
< DeleteServerDatabaseDialog serverId = { serverId } database = { database } open = { open } onClose = { onClose } />
)}
</ Dialog.Trigger >
</ RowActions >
);
}