misc_pterodactyl-panel/resources/scripts/components/server/users/UsersContainer.tsx

66 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { useEffect, useState } from 'react';
2019-11-03 20:20:11 +00:00
import { ServerContext } from '@/state/server';
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import Spinner from '@/components/elements/Spinner';
import AddSubuserButton from '@/components/server/users/AddSubuserButton';
import UserRow from '@/components/server/users/UserRow';
import FlashMessageRender from '@/components/FlashMessageRender';
import getServerSubusers from '@/api/server/users/getServerSubusers';
import { httpErrorToHuman } from '@/api/http';
2020-03-29 21:19:17 +00:00
import Can from '@/components/elements/Can';
2020-10-14 08:54:17 +00:00
import ServerContentBlock from '@/components/elements/ServerContentBlock';
2020-07-04 23:26:07 +00:00
import tw from 'twin.macro';
2019-11-03 20:20:11 +00:00
export default () => {
const [loading, setLoading] = useState(true);
2022-11-25 20:25:03 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const subusers = ServerContext.useStoreState(state => state.subusers.data);
const setSubusers = ServerContext.useStoreActions(actions => actions.subusers.setSubusers);
2019-11-03 20:20:11 +00:00
const permissions = useStoreState((state: ApplicationStore) => state.permissions.data);
const getPermissions = useStoreActions((actions: Actions<ApplicationStore>) => actions.permissions.getPermissions);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2019-11-03 20:20:11 +00:00
useEffect(() => {
clearFlashes('users');
getServerSubusers(uuid)
2022-11-25 20:25:03 +00:00
.then(subusers => {
setSubusers(subusers);
setLoading(false);
})
2022-11-25 20:25:03 +00:00
.catch(error => {
2019-11-03 20:20:11 +00:00
console.error(error);
addError({ key: 'users', message: httpErrorToHuman(error) });
2019-11-03 20:20:11 +00:00
});
}, []);
2019-11-03 20:20:11 +00:00
useEffect(() => {
2022-11-25 20:25:03 +00:00
getPermissions().catch(error => {
addError({ key: 'users', message: httpErrorToHuman(error) });
console.error(error);
});
}, []);
if (!subusers.length && (loading || !Object.keys(permissions).length)) {
return <Spinner size={'large'} centered />;
}
2019-11-03 20:20:11 +00:00
return (
<ServerContentBlock title={'Users'}>
<FlashMessageRender byKey={'users'} css={tw`mb-4`} />
{!subusers.length ? (
<p css={tw`text-center text-sm text-neutral-300`}>It looks like you don&apos;t have any subusers.</p>
) : (
2022-11-25 20:25:03 +00:00
subusers.map(subuser => <UserRow key={subuser.uuid} subuser={subuser} />)
)}
2020-03-29 21:19:17 +00:00
<Can action={'user.create'}>
2020-07-04 23:26:07 +00:00
<div css={tw`flex justify-end mt-6`}>
<AddSubuserButton />
2020-03-29 21:19:17 +00:00
</div>
</Can>
2020-10-14 08:54:17 +00:00
</ServerContentBlock>
2019-11-03 20:20:11 +00:00
);
};