2026-10-05 18:10:18 -04:00
|
|
|
import { useStore } from '@tanstack/react-form';
|
|
|
|
|
import { useAppForm, Form } from '@/components/form';
|
|
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
|
import { useCurrentServerUuid } from '@/api/server/queries';
|
|
|
|
|
import { createServerDatabaseInput, useCreateServerDatabase } from '@/api/server/databases/queries';
|
2020-07-04 17:15:49 -07:00
|
|
|
import Button from '@/components/elements/Button';
|
2026-10-05 18:10:18 -04:00
|
|
|
import { NewButton } from '@/components/elements/NewButton';
|
|
|
|
|
import type { WithClassname } from '@/components/types';
|
2019-07-16 21:43:11 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
const CreateDatabaseDialogContent = ({ onClose }: { onClose: () => void }) => {
|
|
|
|
|
const uuid = useCurrentServerUuid()!;
|
|
|
|
|
const createDatabase = useCreateServerDatabase();
|
2019-07-16 21:43:11 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
const form = useAppForm({
|
|
|
|
|
defaultValues: { databaseName: '', connectionsFrom: '' },
|
|
|
|
|
onSubmit: async ({ value }) => {
|
|
|
|
|
try {
|
|
|
|
|
await createDatabase.mutateAsync(
|
|
|
|
|
createServerDatabaseInput(uuid, {
|
|
|
|
|
databaseName: value.databaseName,
|
|
|
|
|
connectionsFrom: value.connectionsFrom || '%',
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
onClose();
|
|
|
|
|
} catch {
|
|
|
|
|
// Error toast is handled by the mutation.
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-07-09 22:41:09 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
const isSubmitting = useStore(form.store, (state) => state.isSubmitting) || createDatabase.isPending;
|
2019-07-09 22:41:09 -07:00
|
|
|
|
|
|
|
|
return (
|
2026-10-05 18:10:18 -04:00
|
|
|
<Dialog
|
|
|
|
|
open
|
|
|
|
|
title={'Create new database'}
|
|
|
|
|
preventExternalClose={isSubmitting}
|
|
|
|
|
hideCloseIcon={isSubmitting}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
form.reset();
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
|
|
|
|
<Form form={form} className={'m-0'}>
|
|
|
|
|
<form.AppField
|
|
|
|
|
name={'databaseName'}
|
|
|
|
|
validators={{
|
|
|
|
|
onChange: ({ value }) =>
|
|
|
|
|
value.length < 1
|
|
|
|
|
? 'A database name must be provided.'
|
|
|
|
|
: value.length < 3
|
|
|
|
|
? 'Database name must be at least 3 characters.'
|
|
|
|
|
: value.length > 48
|
|
|
|
|
? 'Database name must not exceed 48 characters.'
|
|
|
|
|
: /^[\w\-.]{3,48}$/.test(value)
|
|
|
|
|
? undefined
|
|
|
|
|
: 'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.TextField
|
|
|
|
|
type={'string'}
|
|
|
|
|
id={'database_name'}
|
|
|
|
|
label={'Database Name'}
|
|
|
|
|
description={'A descriptive name for your database instance.'}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</form.AppField>
|
|
|
|
|
<div className={'mt-6'}>
|
|
|
|
|
<form.AppField
|
|
|
|
|
name={'connectionsFrom'}
|
|
|
|
|
validators={{
|
|
|
|
|
onChange: ({ value }) =>
|
|
|
|
|
/^[\w\-/.%:]*$/.test(value) ? undefined : 'A valid host address must be provided.',
|
2022-06-26 15:13:52 -04:00
|
|
|
}}
|
|
|
|
|
>
|
2026-10-05 18:10:18 -04:00
|
|
|
{(field) => (
|
|
|
|
|
<field.TextField
|
2022-06-26 15:13:52 -04:00
|
|
|
type={'string'}
|
2026-10-05 18:10:18 -04:00
|
|
|
id={'connections_from'}
|
|
|
|
|
label={'Connections From'}
|
|
|
|
|
description={
|
|
|
|
|
'Where connections should be allowed from. Leave blank to allow connections from anywhere.'
|
|
|
|
|
}
|
2022-06-26 15:13:52 -04:00
|
|
|
/>
|
2026-10-05 18:10:18 -04:00
|
|
|
)}
|
|
|
|
|
</form.AppField>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={'flex flex-wrap justify-end mt-6'}>
|
|
|
|
|
<Button type={'button'} isSecondary className={'w-full sm:w-auto sm:mr-2'} onClick={onClose}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<form.AppForm>
|
|
|
|
|
<form.SubmitButton className={'w-full mt-4 sm:w-auto sm:mt-0'}>
|
|
|
|
|
Create Database
|
|
|
|
|
</form.SubmitButton>
|
|
|
|
|
</form.AppForm>
|
|
|
|
|
</div>
|
|
|
|
|
</Form>
|
|
|
|
|
</Dialog>
|
2019-07-09 22:41:09 -07:00
|
|
|
);
|
|
|
|
|
};
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
const CreateDatabaseButton = ({ className }: WithClassname) => (
|
|
|
|
|
<Dialog.Trigger
|
|
|
|
|
trigger={({ onClick }) => (
|
|
|
|
|
<NewButton className={className} onClick={onClick}>
|
|
|
|
|
New database
|
|
|
|
|
</NewButton>
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{({ open, onClose }) => open && <CreateDatabaseDialogContent onClose={onClose} />}
|
|
|
|
|
</Dialog.Trigger>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default CreateDatabaseButton;
|