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

184 lines
8.8 KiB
TypeScript
Raw Normal View History

import React, { useContext, useEffect } from 'react';
2021-01-04 18:50:43 +00:00
import AdminCheckbox from '@/components/admin/AdminCheckbox';
2021-01-04 19:44:44 +00:00
import CopyOnClick from '@/components/elements/CopyOnClick';
import getUsers, { Context as UsersContext, Filters } from '@/api/admin/users/getUsers';
import AdminTable, { TableBody, TableHead, TableHeader, Pagination, Loading, NoItems, ContentWrapper, useTableHooks } from '@/components/admin/AdminTable';
2020-10-04 20:25:58 +00:00
import Button from '@/components/elements/Button';
2021-01-04 18:50:43 +00:00
import FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { AdminContext } from '@/state/admin';
import { NavLink, useRouteMatch } from 'react-router-dom';
import tw from 'twin.macro';
import AdminContentBlock from '@/components/admin/AdminContentBlock';
const RowCheckbox = ({ id }: { id: number }) => {
2021-01-04 19:44:44 +00:00
const isChecked = AdminContext.useStoreState(state => state.users.selectedUsers.indexOf(id) >= 0);
const appendSelectedUser = AdminContext.useStoreActions(actions => actions.users.appendSelectedUser);
const removeSelectedUser = AdminContext.useStoreActions(actions => actions.users.removeSelectedUser);
2021-01-04 18:50:43 +00:00
return (
<AdminCheckbox
name={id.toString()}
checked={isChecked}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
if (e.currentTarget.checked) {
appendSelectedUser(id);
} else {
removeSelectedUser(id);
}
}}
/>
);
};
const UsersContainer = () => {
const match = useRouteMatch();
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(UsersContext);
2021-01-04 18:50:43 +00:00
const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data: users, error, isValidating } = getUsers();
useEffect(() => {
if (!error) {
clearFlashes('users');
return;
}
2021-01-08 17:02:49 +00:00
clearAndAddHttpError({ key: 'users', error });
2021-01-04 18:50:43 +00:00
}, [ error ]);
const length = users?.items?.length || 0;
2021-01-04 19:44:44 +00:00
const setSelectedUsers = AdminContext.useStoreActions(actions => actions.users.setSelectedUsers);
const selectedUserLength = AdminContext.useStoreState(state => state.users.selectedUsers.length);
2021-01-04 18:50:43 +00:00
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
setSelectedUsers(e.currentTarget.checked ? (users?.items?.map(user => user.id) || []) : []);
};
const onSearch = (query: string): Promise<void> => {
return new Promise((resolve) => {
if (query.length < 2) {
setFilters(null);
} else {
setFilters({ username: query });
}
return resolve();
});
};
2021-01-04 18:50:43 +00:00
useEffect(() => {
setSelectedUsers([]);
}, [ page ]);
return (
2021-01-07 16:44:24 +00:00
<AdminContentBlock title={'Users'}>
2021-01-04 18:50:43 +00:00
<div css={tw`w-full flex flex-row items-center mb-8`}>
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
2020-10-04 20:25:58 +00:00
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Users</h2>
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>All registered users on the system.</p>
2020-10-04 20:25:58 +00:00
</div>
<div css={tw`flex ml-auto pl-4`}>
<NavLink to={`${match.url}/new`}>
<Button type={'button'} size={'large'} css={tw`h-10 px-4 py-0 whitespace-nowrap`}>
New User
</Button>
</NavLink>
</div>
</div>
2021-01-04 18:50:43 +00:00
<FlashMessageRender byKey={'users'} css={tw`mb-4`}/>
<AdminTable>
<ContentWrapper
checked={selectedUserLength === (length === 0 ? -1 : length)}
onSelectAllClick={onSelectAllClick}
onSearch={onSearch}
>
<Pagination data={users} 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 === 'email' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('email')}/>
<TableHeader name={'Username'} direction={sort === 'username' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('username')}/>
<TableHeader name={'Status'}/>
<TableHeader name={'Role'} direction={sort === 'admin_role_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('admin_role_id')}/>
</TableHead>
<TableBody>
{ users !== undefined && !error && !isValidating && length > 0 &&
users.items.map(user => (
<tr key={user.id} css={tw`h-14 hover:bg-neutral-600`}>
<td css={tw`pl-6`}>
<RowCheckbox id={user.id}/>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
<CopyOnClick text={user.id.toString()}>
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{user.id}</code>
</CopyOnClick>
</td>
<td css={tw`px-6 whitespace-nowrap`}>
<NavLink to={`${match.url}/${user.id}`}>
<div css={tw`flex items-center`}>
<div css={tw`flex-shrink-0 h-10 w-10`}>
<img css={tw`h-10 w-10 rounded-full`} alt="" src={user.avatarURL + '?s=40'}/>
</div>
<div css={tw`ml-4`}>
<div css={tw`text-sm text-neutral-200`}>
{user.firstName} {user.lastName}
2021-01-04 19:44:44 +00:00
</div>
<div css={tw`text-sm text-neutral-400`}>
{user.email}
</div>
</div>
</div>
</NavLink>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{user.username}</td>
<td css={tw`px-6 whitespace-nowrap`}>
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}>
Active
</span>
</td>
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{user.roleName || 'None'}</td>
</tr>
))
}
</TableBody>
</table>
{ users === undefined || (error && isValidating) ?
<Loading/>
:
length < 1 ?
<NoItems/>
:
null
}
</div>
</Pagination>
</ContentWrapper>
2021-01-04 18:50:43 +00:00
</AdminTable>
</AdminContentBlock>
);
};
2021-01-04 18:50:43 +00:00
export default () => {
2021-07-19 20:34:10 +00:00
const hooks = useTableHooks<Filters>();
2021-01-04 18:50:43 +00:00
return (
2021-07-19 20:34:10 +00:00
<UsersContext.Provider value={hooks}>
2021-01-04 18:50:43 +00:00
<UsersContainer/>
</UsersContext.Provider>
);
};