2021-01-05 16:17:44 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2021-05-20 22:00:46 +00:00
|
|
|
import getServers, { Context as ServersContext, Filters } from '@/api/admin/servers/getServers';
|
2021-01-05 16:17:44 +00:00
|
|
|
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
2021-07-14 22:59:37 +00:00
|
|
|
import AdminTable, { ContentWrapper, Loading, NoItems, Pagination, TableBody, TableHead, TableHeader } from '@/components/admin/AdminTable';
|
2020-10-04 20:25:58 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2021-01-05 16:17:44 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { AdminContext } from '@/state/admin';
|
|
|
|
import { NavLink, useRouteMatch } from 'react-router-dom';
|
2020-08-22 22:49:18 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
|
|
|
|
2021-01-05 16:17:44 +00:00
|
|
|
const RowCheckbox = ({ id }: { id: number }) => {
|
|
|
|
const isChecked = AdminContext.useStoreState(state => state.servers.selectedServers.indexOf(id) >= 0);
|
|
|
|
const appendSelectedServer = AdminContext.useStoreActions(actions => actions.servers.appendSelectedServer);
|
|
|
|
const removeSelectedServer = AdminContext.useStoreActions(actions => actions.servers.removeSelectedServer);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminCheckbox
|
|
|
|
name={id.toString()}
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
appendSelectedServer(id);
|
|
|
|
} else {
|
|
|
|
removeSelectedServer(id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
const ServersContainer = () => {
|
2021-01-05 16:17:44 +00:00
|
|
|
const match = useRouteMatch();
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(ServersContext);
|
2021-01-05 16:17:44 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2021-07-14 22:59:37 +00:00
|
|
|
const { data: servers, error, isValidating } = getServers([ 'node', 'user' ]);
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
const length = servers?.items?.length || 0;
|
|
|
|
|
|
|
|
const setSelectedServers = AdminContext.useStoreActions(actions => actions.servers.setSelectedServers);
|
|
|
|
const selectedServerLength = AdminContext.useStoreState(state => state.servers.selectedServers.length);
|
|
|
|
|
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSelectedServers(e.currentTarget.checked ? (servers?.items?.map(server => server.id) || []) : []);
|
|
|
|
};
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
const onSearch = (query: string): Promise<void> => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (query.length < 2) {
|
|
|
|
setFilters(null);
|
|
|
|
} else {
|
|
|
|
setFilters({ name: query });
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-05 16:17:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setSelectedServers([]);
|
|
|
|
}, [ page ]);
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!error) {
|
|
|
|
clearFlashes('servers');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAndAddHttpError({ key: 'servers', error });
|
|
|
|
}, [ error ]);
|
|
|
|
|
2020-08-22 22:49:18 +00:00
|
|
|
return (
|
2021-01-07 16:44:24 +00:00
|
|
|
<AdminContentBlock title={'Servers'}>
|
2021-01-05 16:17:44 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
2021-01-10 18:34:14 +00:00
|
|
|
<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`}>Servers</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>All servers available on the system.</p>
|
2020-10-04 20:25:58 +00:00
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:34:14 +00:00
|
|
|
<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 Server
|
|
|
|
</Button>
|
|
|
|
</NavLink>
|
|
|
|
</div>
|
2020-08-22 22:49:18 +00:00
|
|
|
</div>
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
<FlashMessageRender byKey={'servers'} css={tw`mb-4`}/>
|
|
|
|
|
|
|
|
<AdminTable>
|
2021-05-20 22:00:46 +00:00
|
|
|
<ContentWrapper
|
|
|
|
checked={selectedServerLength === (length === 0 ? -1 : length)}
|
|
|
|
onSelectAllClick={onSelectAllClick}
|
|
|
|
onSearch={onSearch}
|
|
|
|
>
|
2021-07-14 22:59:37 +00:00
|
|
|
<Pagination data={servers} onPageSelect={setPage}>
|
|
|
|
<div css={tw`overflow-x-auto`}>
|
|
|
|
<table css={tw`w-full table-auto`}>
|
|
|
|
<TableHead>
|
|
|
|
<TableHeader name={'Identifier'} direction={sort === 'uuidShort' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('uuidShort')}/>
|
|
|
|
<TableHeader name={'Name'} direction={sort === 'name' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('name')}/>
|
|
|
|
<TableHeader name={'Owner'} direction={sort === 'owner_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('owner_id')}/>
|
|
|
|
<TableHeader name={'Node'} direction={sort === 'node_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('node_id')}/>
|
|
|
|
<TableHeader name={'Status'} direction={sort === 'status' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('status')}/>
|
|
|
|
</TableHead>
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
{ servers !== undefined && !error && !isValidating && length > 0 &&
|
|
|
|
servers.items.map(server => (
|
|
|
|
<tr key={server.id} css={tw`h-14 hover:bg-neutral-600`}>
|
|
|
|
<td css={tw`pl-6`}>
|
|
|
|
<RowCheckbox id={server.id}/>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={server.identifier}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{server.identifier}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`${match.url}/${server.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
{server.name}
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
{/* TODO: Have permission check for displaying user information. */}
|
|
|
|
<td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`/admin/users/${server.relations.user?.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
<div css={tw`text-sm text-neutral-200`}>
|
|
|
|
{server.relations.user?.firstName} {server.relations.user?.lastName}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`text-sm text-neutral-400`}>
|
|
|
|
{server.relations.user?.email}
|
|
|
|
</div>
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
{/* TODO: Have permission check for displaying node information. */}
|
|
|
|
<td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`/admin/nodes/${server.relations.node?.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
<div css={tw`text-sm text-neutral-200`}>
|
|
|
|
{server.relations.node?.name}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`text-sm text-neutral-400`}>
|
|
|
|
{server.relations.node?.fqdn}
|
|
|
|
</div>
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 whitespace-nowrap`}>
|
|
|
|
{server.status === 'installing' ?
|
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}>
|
|
|
|
Installing
|
|
|
|
</span>
|
|
|
|
:
|
|
|
|
server.status === 'transferring' ?
|
2021-05-20 22:00:46 +00:00
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}>
|
2021-07-14 22:59:37 +00:00
|
|
|
Transferring
|
2021-05-20 22:00:46 +00:00
|
|
|
</span>
|
2021-07-14 22:59:37 +00:00
|
|
|
: server.status === 'suspended' ?
|
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-red-200 text-red-800`}>
|
|
|
|
Suspended
|
2021-01-05 16:17:44 +00:00
|
|
|
</span>
|
2021-07-14 22:59:37 +00:00
|
|
|
:
|
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}>
|
|
|
|
Active
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</TableBody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
{ servers === undefined || (error && isValidating) ?
|
|
|
|
<Loading/>
|
|
|
|
:
|
|
|
|
length < 1 ?
|
|
|
|
<NoItems/>
|
|
|
|
:
|
|
|
|
null
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</Pagination>
|
2021-05-20 22:00:46 +00:00
|
|
|
</ContentWrapper>
|
2021-01-05 16:17:44 +00:00
|
|
|
</AdminTable>
|
2020-08-22 22:49:18 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const [ page, setPage ] = useState<number>(1);
|
2021-05-20 22:00:46 +00:00
|
|
|
const [ filters, setFilters ] = useState<Filters | null>(null);
|
|
|
|
const [ sort, setSortState ] = useState<string | null>(null);
|
|
|
|
const [ sortDirection, setSortDirection ] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const setSort = (newSort: string | null) => {
|
|
|
|
if (sort === newSort) {
|
|
|
|
setSortDirection(!sortDirection);
|
|
|
|
} else {
|
|
|
|
setSortState(newSort);
|
|
|
|
setSortDirection(false);
|
|
|
|
}
|
|
|
|
};
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
return (
|
2021-05-20 22:00:46 +00:00
|
|
|
<ServersContext.Provider value={{ page, setPage, filters, setFilters, sort, setSort, sortDirection, setSortDirection }}>
|
|
|
|
<ServersContainer/>
|
2021-01-05 16:17:44 +00:00
|
|
|
</ServersContext.Provider>
|
|
|
|
);
|
|
|
|
};
|