misc_pterodactyl-panel/resources/scripts/components/server/databases/DatabaseRow.tsx

185 lines
9.1 KiB
TypeScript
Raw Permalink Normal View History

2019-07-17 05:15:14 +00:00
import React, { useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2020-08-26 05:09:54 +00:00
import { faDatabase, faEye, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
2019-07-17 05:15:14 +00:00
import Modal from '@/components/elements/Modal';
2020-03-19 04:32:07 +00:00
import { Form, Formik, FormikHelpers } from 'formik';
2019-07-17 05:15:14 +00:00
import Field from '@/components/elements/Field';
import { object, string } from 'yup';
import FlashMessageRender from '@/components/FlashMessageRender';
import { ServerContext } from '@/state/server';
import deleteServerDatabase from '@/api/server/databases/deleteServerDatabase';
2019-07-17 05:15:14 +00:00
import { httpErrorToHuman } from '@/api/http';
2019-07-27 22:17:50 +00:00
import RotatePasswordButton from '@/components/server/databases/RotatePasswordButton';
import Can from '@/components/elements/Can';
import { ServerDatabase } from '@/api/server/databases/getServerDatabases';
2020-04-10 17:56:25 +00:00
import useFlash from '@/plugins/useFlash';
2020-07-05 00:15:49 +00:00
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import Label from '@/components/elements/Label';
import Input from '@/components/elements/Input';
import GreyRowBox from '@/components/elements/GreyRowBox';
import CopyOnClick from '@/components/elements/CopyOnClick';
2019-07-17 05:15:14 +00:00
interface Props {
2020-04-10 17:56:25 +00:00
database: ServerDatabase;
2019-07-17 05:15:14 +00:00
className?: string;
}
2020-04-10 17:56:25 +00:00
export default ({ database, className }: Props) => {
2020-08-26 05:09:54 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
2020-04-10 17:56:25 +00:00
const { addError, clearFlashes } = useFlash();
const [ visible, setVisible ] = useState(false);
const [ connectionVisible, setConnectionVisible ] = useState(false);
2019-07-17 05:15:14 +00:00
2020-04-10 17:56:25 +00:00
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
const removeDatabase = ServerContext.useStoreActions(actions => actions.databases.removeDatabase);
2019-07-27 22:17:50 +00:00
2019-07-17 05:15:14 +00:00
const schema = object().shape({
confirm: string()
.required('The database name must be provided.')
.oneOf([ database.name.split('_', 2)[1], database.name ], 'The database name must be provided.'),
2019-07-17 05:15:14 +00:00
});
2020-03-19 04:32:07 +00:00
const submit = (values: { confirm: string }, { setSubmitting }: FormikHelpers<{ confirm: string }>) => {
2019-07-17 05:15:14 +00:00
clearFlashes();
2020-04-10 17:56:25 +00:00
deleteServerDatabase(uuid, database.id)
2019-07-17 05:15:14 +00:00
.then(() => {
setVisible(false);
2020-04-10 17:56:25 +00:00
setTimeout(() => removeDatabase(database.id), 150);
2019-07-17 05:15:14 +00:00
})
.catch(error => {
console.error(error);
setSubmitting(false);
2020-04-10 17:56:25 +00:00
addError({ key: 'database:delete', message: httpErrorToHuman(error) });
2019-07-17 05:15:14 +00:00
});
};
2020-07-05 00:15:49 +00:00
return (
2020-07-05 00:15:49 +00:00
<>
2019-07-17 05:15:14 +00:00
<Formik
onSubmit={submit}
initialValues={{ confirm: '' }}
validationSchema={schema}
2020-07-05 00:15:49 +00:00
isInitialValid={false}
2019-07-17 05:15:14 +00:00
>
{
({ isSubmitting, isValid, resetForm }) => (
<Modal
visible={visible}
dismissable={!isSubmitting}
showSpinnerOverlay={isSubmitting}
onDismissed={() => {
setVisible(false);
resetForm();
}}
2019-07-17 05:15:14 +00:00
>
2020-07-05 00:15:49 +00:00
<FlashMessageRender byKey={'database:delete'} css={tw`mb-6`}/>
<h2 css={tw`text-2xl mb-6`}>Confirm database deletion</h2>
<p css={tw`text-sm`}>
2019-07-17 05:15:14 +00:00
Deleting a database is a permanent action, it cannot be undone. This will permanetly
delete the <strong>{database.name}</strong> database and remove all associated data.
</p>
2020-07-05 00:15:49 +00:00
<Form css={tw`m-0 mt-6`}>
2019-07-17 05:15:14 +00:00
<Field
type={'text'}
id={'confirm_name'}
name={'confirm'}
label={'Confirm Database Name'}
description={'Enter the database name to confirm deletion.'}
/>
2020-07-05 00:15:49 +00:00
<div css={tw`mt-6 text-right`}>
<Button
2019-07-17 05:15:14 +00:00
type={'button'}
2020-07-05 00:15:49 +00:00
isSecondary
css={tw`mr-2`}
2019-07-17 05:15:14 +00:00
onClick={() => setVisible(false)}
>
Cancel
2020-07-05 00:15:49 +00:00
</Button>
<Button
2019-07-17 05:15:14 +00:00
type={'submit'}
2020-07-05 00:15:49 +00:00
color={'red'}
2019-07-17 05:15:14 +00:00
disabled={!isValid}
>
Delete Database
2020-07-05 00:15:49 +00:00
</Button>
2019-07-17 05:15:14 +00:00
</div>
</Form>
</Modal>
)
}
</Formik>
2019-07-17 05:29:00 +00:00
<Modal visible={connectionVisible} onDismissed={() => setConnectionVisible(false)}>
2020-07-05 00:15:49 +00:00
<FlashMessageRender byKey={'database-connection-modal'} css={tw`mb-6`}/>
<h3 css={tw`mb-6 text-2xl`}>Database connection details</h3>
<div>
<Label>Endpoint</Label>
<CopyOnClick text={database.connectionString}><Input type={'text'} readOnly value={database.connectionString} /></CopyOnClick>
</div>
<div css={tw`mt-6`}>
<Label>Connections from</Label>
<Input type={'text'} readOnly value={database.allowConnectionsFrom} />
</div>
<div css={tw`mt-6`}>
<Label>Username</Label>
<CopyOnClick text={database.username}><Input type={'text'} readOnly value={database.username} /></CopyOnClick>
</div>
<Can action={'database.view_password'}>
<div css={tw`mt-6`}>
2020-07-05 00:15:49 +00:00
<Label>Password</Label>
2020-12-09 05:42:56 +00:00
<CopyOnClick text={database.password}><Input type={'text'} readOnly value={database.password}/></CopyOnClick>
</div>
</Can>
2020-07-05 00:15:49 +00:00
<div css={tw`mt-6`}>
<Label>JDBC Connection String</Label>
<CopyOnClick text={`jdbc:mysql://${database.username}:${database.password}@${database.connectionString}/${database.name}`}>
<Input
type={'text'}
readOnly
value={`jdbc:mysql://${database.username}:${database.password}@${database.connectionString}/${database.name}`}
/>
</CopyOnClick>
2019-07-17 05:29:00 +00:00
</div>
2020-07-05 00:15:49 +00:00
<div css={tw`mt-6 text-right`}>
<Can action={'database.update'}>
<RotatePasswordButton databaseId={database.id} onUpdate={appendDatabase}/>
</Can>
2020-07-05 00:15:49 +00:00
<Button isSecondary onClick={() => setConnectionVisible(false)}>
2019-07-17 05:29:00 +00:00
Close
2020-07-05 00:15:49 +00:00
</Button>
2019-07-17 05:29:00 +00:00
</div>
</Modal>
<GreyRowBox $hoverable={false} className={className} css={tw`mb-2`}>
<div css={tw`hidden md:block`}>
2020-07-05 00:15:49 +00:00
<FontAwesomeIcon icon={faDatabase} fixedWidth/>
2019-07-17 05:15:14 +00:00
</div>
2020-07-05 00:15:49 +00:00
<div css={tw`flex-1 ml-4`}>
<CopyOnClick text={database.name}><p css={tw`text-lg`}>{database.name}</p></CopyOnClick>
2019-07-17 05:15:14 +00:00
</div>
<div css={tw`ml-8 text-center hidden md:block`}>
<CopyOnClick text={database.connectionString}><p css={tw`text-sm`}>{database.connectionString}</p></CopyOnClick>
2020-07-05 00:15:49 +00:00
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Endpoint</p>
2019-07-17 05:15:14 +00:00
</div>
<div css={tw`ml-8 text-center hidden md:block`}>
2020-07-05 00:15:49 +00:00
<p css={tw`text-sm`}>{database.allowConnectionsFrom}</p>
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Connections from</p>
2019-07-17 05:15:14 +00:00
</div>
<div css={tw`ml-8 text-center hidden md:block`}>
<CopyOnClick text={database.username}><p css={tw`text-sm`}>{database.username}</p></CopyOnClick>
2020-07-05 00:15:49 +00:00
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Username</p>
2019-07-17 05:15:14 +00:00
</div>
2020-07-05 00:15:49 +00:00
<div css={tw`ml-8`}>
<Button isSecondary css={tw`mr-2`} onClick={() => setConnectionVisible(true)}>
<FontAwesomeIcon icon={faEye} fixedWidth/>
</Button>
<Can action={'database.delete'}>
2020-07-05 00:15:49 +00:00
<Button color={'red'} isSecondary onClick={() => setVisible(true)}>
<FontAwesomeIcon icon={faTrashAlt} fixedWidth/>
</Button>
</Can>
2019-07-17 05:15:14 +00:00
</div>
2020-07-05 00:15:49 +00:00
</GreyRowBox>
</>
);
};