ui(admin): add "working" React admin ui

This commit is contained in:
Matthew Penner 2022-12-15 19:06:14 -07:00
parent d1c7494933
commit 5402584508
No known key found for this signature in database
199 changed files with 13387 additions and 151 deletions

View file

@ -0,0 +1,73 @@
import type { Actions } from 'easy-peasy';
import { useStoreActions } from 'easy-peasy';
import { useState } from 'react';
import tw from 'twin.macro';
import deleteMount from '@/api/admin/mounts/deleteMount';
import Button from '@/components/elements/Button';
import ConfirmationModal from '@/components/elements/ConfirmationModal';
import type { ApplicationStore } from '@/state';
interface Props {
mountId: number;
onDeleted: () => void;
}
export default ({ mountId, onDeleted }: Props) => {
const [visible, setVisible] = useState(false);
const [loading, setLoading] = useState(false);
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
(actions: Actions<ApplicationStore>) => actions.flashes,
);
const onDelete = () => {
setLoading(true);
clearFlashes('mount');
deleteMount(mountId)
.then(() => {
setLoading(false);
onDeleted();
})
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'mount', error });
setLoading(false);
setVisible(false);
});
};
return (
<>
<ConfirmationModal
visible={visible}
title={'Delete mount?'}
buttonText={'Yes, delete mount'}
onConfirmed={onDelete}
showSpinnerOverlay={loading}
onModalDismissed={() => setVisible(false)}
>
Are you sure you want to delete this mount? Deleting a mount will not delete files on any nodes.
</ConfirmationModal>
<Button type={'button'} size={'xsmall'} color={'red'} onClick={() => setVisible(true)}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
css={tw`h-5 w-5`}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
/>
</svg>
</Button>
</>
);
};

View file

@ -0,0 +1,142 @@
import type { Action, Actions } from 'easy-peasy';
import { action, createContextStore, useStoreActions } from 'easy-peasy';
import type { FormikHelpers } from 'formik';
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import tw from 'twin.macro';
import type { Mount } from '@/api/admin/mounts/getMounts';
import getMount from '@/api/admin/mounts/getMount';
import updateMount from '@/api/admin/mounts/updateMount';
import AdminContentBlock from '@/components/admin/AdminContentBlock';
import MountDeleteButton from '@/components/admin/mounts/MountDeleteButton';
import MountForm from '@/components/admin/mounts/MountForm';
import Spinner from '@/components/elements/Spinner';
import FlashMessageRender from '@/components/FlashMessageRender';
import type { ApplicationStore } from '@/state';
interface ctx {
mount: Mount | undefined;
setMount: Action<ctx, Mount | undefined>;
}
export const Context = createContextStore<ctx>({
mount: undefined,
setMount: action((state, payload) => {
state.mount = payload;
}),
});
const MountEditContainer = () => {
const navigate = useNavigate();
const params = useParams<'id'>();
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
(actions: Actions<ApplicationStore>) => actions.flashes,
);
const [loading, setLoading] = useState(true);
const mount = Context.useStoreState(state => state.mount);
const setMount = Context.useStoreActions(actions => actions.setMount);
const submit = (
{ name, description, source, target, readOnly, userMountable }: any,
{ setSubmitting }: FormikHelpers<any>,
) => {
if (mount === undefined) {
return;
}
clearFlashes('mount');
updateMount(mount.id, name, description, source, target, readOnly === '1', userMountable === '1')
.then(() =>
setMount({
...mount,
name,
description,
source,
target,
readOnly: readOnly === '1',
userMountable: userMountable === '1',
}),
)
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'mount', error });
})
.then(() => setSubmitting(false));
};
useEffect(() => {
clearFlashes('mount');
getMount(Number(params.id))
.then(mount => setMount(mount))
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'mount', error });
})
.then(() => setLoading(false));
}, []);
if (loading || mount === undefined) {
return (
<AdminContentBlock>
<FlashMessageRender byKey={'mount'} css={tw`mb-4`} />
<div css={tw`w-full flex flex-col items-center justify-center`} style={{ height: '24rem' }}>
<Spinner size={'base'} />
</div>
</AdminContentBlock>
);
}
return (
<AdminContentBlock title={'Mount - ' + mount.name}>
<div css={tw`w-full flex flex-row items-center mb-8`}>
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{mount.name}</h2>
{(mount.description || '').length < 1 ? (
<p css={tw`text-base text-neutral-400`}>
<span css={tw`italic`}>No description</span>
</p>
) : (
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
{mount.description}
</p>
)}
</div>
</div>
<FlashMessageRender byKey={'mount'} css={tw`mb-4`} />
<MountForm
action={'Save Changes'}
title={'Edit Mount'}
initialValues={{
name: mount.name,
description: mount.description || '',
source: mount.source,
target: mount.target,
readOnly: mount.readOnly ? '1' : '0',
userMountable: mount.userMountable ? '1' : '0',
}}
onSubmit={submit}
>
<div css={tw`flex`}>
<MountDeleteButton mountId={mount.id} onDeleted={() => navigate('/admin/mounts')} />
</div>
</MountForm>
</AdminContentBlock>
);
};
export default () => {
return (
<Context.Provider>
<MountEditContainer />
</Context.Provider>
);
};

View file

@ -0,0 +1,133 @@
import type { FormikHelpers } from 'formik';
import { Field as FormikField, Form, Formik } from 'formik';
import tw from 'twin.macro';
import { boolean, object, string } from 'yup';
import AdminBox from '@/components/admin/AdminBox';
import Button from '@/components/elements/Button';
import Field from '@/components/elements/Field';
import Label from '@/components/elements/Label';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
interface Values {
name: string;
description: string;
source: string;
target: string;
readOnly: string;
userMountable: string;
}
interface Props {
action: string;
title: string;
initialValues?: Values;
onSubmit: (values: Values, helpers: FormikHelpers<Values>) => void;
children?: React.ReactNode;
}
function MountForm({ action, title, initialValues, children, onSubmit }: Props) {
const submit = (values: Values, helpers: FormikHelpers<Values>) => {
onSubmit(values, helpers);
};
if (!initialValues) {
initialValues = {
name: '',
description: '',
source: '',
target: '',
readOnly: '0',
userMountable: '0',
};
}
return (
<Formik
onSubmit={submit}
initialValues={initialValues}
validationSchema={object().shape({
name: string().required().min(1),
description: string().max(255, ''),
source: string().max(255, ''),
target: string().max(255, ''),
readOnly: boolean(),
userMountable: boolean(),
})}
>
{({ isSubmitting, isValid }) => (
<AdminBox title={title} css={tw`relative`}>
<SpinnerOverlay visible={isSubmitting} />
<Form css={tw`mb-0`}>
<div>
<Field id={'name'} name={'name'} label={'Name'} type={'text'} />
</div>
<div css={tw`mt-6`}>
<Field id={'description'} name={'description'} label={'Description'} type={'text'} />
</div>
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
<Field id={'source'} name={'source'} label={'Source'} type={'text'} />
</div>
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
<Field id={'target'} name={'target'} label={'Target'} type={'text'} />
</div>
</div>
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
<Label htmlFor={'readOnly'}>Permissions</Label>
<div>
<label css={tw`inline-flex items-center mr-2`}>
<FormikField name={'readOnly'} type={'radio'} value={'0'} />
<span css={tw`ml-2`}>Writable</span>
</label>
<label css={tw`inline-flex items-center ml-2`}>
<FormikField name={'readOnly'} type={'radio'} value={'1'} />
<span css={tw`ml-2`}>Read Only</span>
</label>
</div>
</div>
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
<Label htmlFor={'userMountable'}>User Mountable</Label>
<div>
<label css={tw`inline-flex items-center mr-2`}>
<FormikField name={'userMountable'} type={'radio'} value={'0'} />
<span css={tw`ml-2`}>Admin Only</span>
</label>
<label css={tw`inline-flex items-center ml-2`}>
<FormikField name={'userMountable'} type={'radio'} value={'1'} />
<span css={tw`ml-2`}>Users</span>
</label>
</div>
</div>
</div>
<div css={tw`w-full flex flex-row items-center mt-6`}>
{children}
<div css={tw`flex ml-auto`}>
<Button type={'submit'} disabled={isSubmitting || !isValid}>
{action}
</Button>
</div>
</div>
</Form>
</AdminBox>
)}
</Formik>
);
}
export default MountForm;

View file

@ -0,0 +1,241 @@
import type { ChangeEvent } from 'react';
import { useContext, useEffect } from 'react';
import { NavLink } from 'react-router-dom';
import tw from 'twin.macro';
import type { Filters } from '@/api/admin/mounts/getMounts';
import getMounts, { Context as MountsContext } from '@/api/admin/mounts/getMounts';
import AdminCheckbox from '@/components/admin/AdminCheckbox';
import AdminContentBlock from '@/components/admin/AdminContentBlock';
import AdminTable, {
TableBody,
TableHead,
TableHeader,
TableRow,
Pagination,
Loading,
NoItems,
ContentWrapper,
useTableHooks,
} from '@/components/admin/AdminTable';
import Button from '@/components/elements/Button';
import CopyOnClick from '@/components/elements/CopyOnClick';
import FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { AdminContext } from '@/state/admin';
const RowCheckbox = ({ id }: { id: number }) => {
const isChecked = AdminContext.useStoreState(state => state.mounts.selectedMounts.indexOf(id) >= 0);
const appendSelectedMount = AdminContext.useStoreActions(actions => actions.mounts.appendSelectedMount);
const removeSelectedMount = AdminContext.useStoreActions(actions => actions.mounts.removeSelectedMount);
return (
<AdminCheckbox
name={id.toString()}
checked={isChecked}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
if (e.currentTarget.checked) {
appendSelectedMount(id);
} else {
removeSelectedMount(id);
}
}}
/>
);
};
const MountsContainer = () => {
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(MountsContext);
const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data: mounts, error, isValidating } = getMounts();
useEffect(() => {
if (!error) {
clearFlashes('mounts');
return;
}
clearAndAddHttpError({ key: 'mounts', error });
}, [error]);
const length = mounts?.items?.length || 0;
const setSelectedMounts = AdminContext.useStoreActions(actions => actions.mounts.setSelectedMounts);
const selectedMountsLength = AdminContext.useStoreState(state => state.mounts.selectedMounts.length);
const onSelectAllClick = (e: ChangeEvent<HTMLInputElement>) => {
setSelectedMounts(e.currentTarget.checked ? mounts?.items?.map(mount => mount.id) || [] : []);
};
const onSearch = (query: string): Promise<void> => {
return new Promise(resolve => {
if (query.length < 2) {
setFilters(null);
} else {
setFilters({ name: query });
}
return resolve();
});
};
useEffect(() => {
setSelectedMounts([]);
}, [page]);
return (
<AdminContentBlock title={'Mounts'}>
<div css={tw`w-full flex flex-row items-center mb-8`}>
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Mounts</h2>
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
Configure and manage additional mount points for servers.
</p>
</div>
<div css={tw`flex ml-auto pl-4`}>
<NavLink to={`/admin/mounts/new`}>
<Button type={'button'} size={'large'} css={tw`h-10 px-4 py-0 whitespace-nowrap`}>
New Mount
</Button>
</NavLink>
</div>
</div>
<FlashMessageRender byKey={'mounts'} css={tw`mb-4`} />
<AdminTable>
<ContentWrapper
checked={selectedMountsLength === (length === 0 ? -1 : length)}
onSelectAllClick={onSelectAllClick}
onSearch={onSearch}
>
<Pagination data={mounts} onPageSelect={setPage}>
<div css={tw`overflow-x-auto`}>
<table css={tw`w-full table-auto`}>
<TableHead>
<TableHeader
name={'ID'}
direction={sort === 'id' ? (sortDirection ? 1 : 2) : null}
onClick={() => setSort('id')}
/>
<TableHeader
name={'Name'}
direction={sort === 'name' ? (sortDirection ? 1 : 2) : null}
onClick={() => setSort('name')}
/>
<TableHeader
name={'Source Path'}
direction={sort === 'source' ? (sortDirection ? 1 : 2) : null}
onClick={() => setSort('source')}
/>
<TableHeader
name={'Target Path'}
direction={sort === 'target' ? (sortDirection ? 1 : 2) : null}
onClick={() => setSort('target')}
/>
<th css={tw`px-6 py-2`} />
<th css={tw`px-6 py-2`} />
</TableHead>
<TableBody>
{mounts !== undefined &&
!error &&
!isValidating &&
length > 0 &&
mounts.items.map(mount => (
<TableRow key={mount.id}>
<td css={tw`pl-6`}>
<RowCheckbox id={mount.id} />
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
<CopyOnClick text={mount.id.toString()}>
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
{mount.id}
</code>
</CopyOnClick>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
<NavLink
to={`/admin/mounts/${mount.id}`}
css={tw`text-primary-400 hover:text-primary-300`}
>
{mount.name}
</NavLink>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
<CopyOnClick text={mount.source.toString()}>
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
{mount.source}
</code>
</CopyOnClick>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
<CopyOnClick text={mount.target.toString()}>
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
{mount.target}
</code>
</CopyOnClick>
</td>
<td css={tw`px-6 whitespace-nowrap`}>
{mount.readOnly ? (
<span
css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}
>
Read Only
</span>
) : (
<span
css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}
>
Writable
</span>
)}
</td>
<td css={tw`px-6 whitespace-nowrap`}>
{mount.userMountable ? (
<span
css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}
>
Mountable
</span>
) : (
<span
css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}
>
Admin Only
</span>
)}
</td>
</TableRow>
))}
</TableBody>
</table>
{mounts === undefined || (error && isValidating) ? (
<Loading />
) : length < 1 ? (
<NoItems />
) : null}
</div>
</Pagination>
</ContentWrapper>
</AdminTable>
</AdminContentBlock>
);
};
export default () => {
const hooks = useTableHooks<Filters>();
return (
<MountsContext.Provider value={hooks}>
<MountsContainer />
</MountsContext.Provider>
);
};

View file

@ -0,0 +1,51 @@
import type { Actions } from 'easy-peasy';
import { useStoreActions } from 'easy-peasy';
import type { FormikHelpers } from 'formik';
import { useNavigate } from 'react-router-dom';
import tw from 'twin.macro';
import AdminContentBlock from '@/components/admin/AdminContentBlock';
import FlashMessageRender from '@/components/FlashMessageRender';
import MountForm from '@/components/admin/mounts/MountForm';
import createMount from '@/api/admin/mounts/createMount';
import type { ApplicationStore } from '@/state';
export default () => {
const navigate = useNavigate();
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
(actions: Actions<ApplicationStore>) => actions.flashes,
);
const submit = (
{ name, description, source, target, readOnly, userMountable }: any,
{ setSubmitting }: FormikHelpers<any>,
) => {
clearFlashes('mount:create');
createMount(name, description, source, target, readOnly === '1', userMountable === '1')
.then(mount => navigate(`/admin/mounts/${mount.id}`))
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'mount:create', error });
})
.then(() => setSubmitting(false));
};
return (
<AdminContentBlock title={'New Mount'}>
<div css={tw`w-full flex flex-row items-center mb-8`}>
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>New Mount</h2>
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
Add a new mount to the panel.
</p>
</div>
</div>
<FlashMessageRender byKey={'mount:create'} css={tw`mb-4`} />
<MountForm action={'Create'} title={'Create Mount'} onSubmit={submit} />
</AdminContentBlock>
);
};