2021-05-20 22:00:46 +00:00
|
|
|
import { Filters } from '@/api/admin/servers/getServers';
|
2021-01-05 21:52:49 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
import getNodes, { Context as NodesContext } from '@/api/admin/nodes/getNodes';
|
|
|
|
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 21:52:49 +00:00
|
|
|
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
|
|
|
import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper } from '@/components/admin/AdminTable';
|
|
|
|
import Button from '@/components/elements/Button';
|
2021-01-06 16:52:07 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
|
|
|
import { bytesToHuman, megabytesToBytes } from '@/helpers';
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
const RowCheckbox = ({ id }: { id: number}) => {
|
|
|
|
const isChecked = AdminContext.useStoreState(state => state.nodes.selectedNodes.indexOf(id) >= 0);
|
|
|
|
const appendSelectedNode = AdminContext.useStoreActions(actions => actions.nodes.appendSelectedNode);
|
|
|
|
const removeSelectedNode = AdminContext.useStoreActions(actions => actions.nodes.removeSelectedNode);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminCheckbox
|
|
|
|
name={id.toString()}
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
appendSelectedNode(id);
|
|
|
|
} else {
|
|
|
|
removeSelectedNode(id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const NodesContainer = () => {
|
|
|
|
const match = useRouteMatch();
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(NodesContext);
|
2021-01-05 21:52:49 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2021-01-06 16:52:07 +00:00
|
|
|
const { data: nodes, error, isValidating } = getNodes([ 'location' ]);
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!error) {
|
|
|
|
clearFlashes('nodes');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:02:49 +00:00
|
|
|
clearAndAddHttpError({ key: 'nodes', error });
|
2021-01-05 21:52:49 +00:00
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
const length = nodes?.items?.length || 0;
|
|
|
|
|
|
|
|
const setSelectedNodes = AdminContext.useStoreActions(actions => actions.nodes.setSelectedNodes);
|
|
|
|
const selectedNodesLength = AdminContext.useStoreState(state => state.nodes.selectedNodes.length);
|
|
|
|
|
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSelectedNodes(e.currentTarget.checked ? (nodes?.items?.map(node => node.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 21:52:49 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setSelectedNodes([]);
|
|
|
|
}, [ page ]);
|
2020-08-22 22:49:18 +00:00
|
|
|
|
|
|
|
return (
|
2021-01-07 16:44:24 +00:00
|
|
|
<AdminContentBlock title={'Nodes'}>
|
2021-01-05 21:52:49 +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`}>Nodes</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>All nodes 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 Node
|
|
|
|
</Button>
|
|
|
|
</NavLink>
|
|
|
|
</div>
|
2020-08-22 22:49:18 +00:00
|
|
|
</div>
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
<FlashMessageRender byKey={'nodes'} css={tw`mb-4`}/>
|
|
|
|
|
|
|
|
<AdminTable>
|
|
|
|
{ nodes === undefined || (error && isValidating) ?
|
|
|
|
<Loading/>
|
|
|
|
:
|
|
|
|
length < 1 ?
|
|
|
|
<NoItems/>
|
|
|
|
:
|
|
|
|
<ContentWrapper
|
|
|
|
checked={selectedNodesLength === (length === 0 ? -1 : length)}
|
|
|
|
onSelectAllClick={onSelectAllClick}
|
2021-05-20 22:00:46 +00:00
|
|
|
onSearch={onSearch}
|
2021-01-05 21:52:49 +00:00
|
|
|
>
|
|
|
|
<Pagination data={nodes} onPageSelect={setPage}>
|
|
|
|
<div css={tw`overflow-x-auto`}>
|
|
|
|
<table css={tw`w-full table-auto`}>
|
|
|
|
<TableHead>
|
2021-05-20 22:00:46 +00:00
|
|
|
<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={'Location'} direction={sort === 'location_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('location_id')}/>
|
|
|
|
<TableHeader name={'FQDN'} direction={sort === 'fqdn' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('fqdn')}/>
|
|
|
|
<TableHeader name={'Total Memory'} direction={sort === 'memory' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('memory')}/>
|
|
|
|
<TableHeader name={'Total Disk'} direction={sort === 'disk' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('disk')}/>
|
2021-03-23 23:47:24 +00:00
|
|
|
<TableHeader/>
|
|
|
|
<TableHeader/>
|
2021-01-05 21:52:49 +00:00
|
|
|
</TableHead>
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
{
|
|
|
|
nodes.items.map(node => (
|
|
|
|
<TableRow key={node.id}>
|
|
|
|
<td css={tw`pl-6`}>
|
|
|
|
<RowCheckbox id={node.id}/>
|
|
|
|
</td>
|
|
|
|
|
2021-01-06 16:52:07 +00:00
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={node.id.toString()}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{node.id}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
2021-01-05 21:52:49 +00:00
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`${match.url}/${node.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
{node.name}
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
2021-01-06 16:52:07 +00:00
|
|
|
|
|
|
|
{/* TODO: Have permission check for displaying location information. */}
|
|
|
|
<td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`/admin/locations/${node.relations.location?.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
<div css={tw`text-sm text-neutral-200`}>
|
|
|
|
{node.relations.location?.short}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`text-sm text-neutral-400`}>
|
|
|
|
{node.relations.location?.long}
|
|
|
|
</div>
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={node.fqdn}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{node.fqdn}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{bytesToHuman(megabytesToBytes(node.memory))}</td>
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{bytesToHuman(megabytesToBytes(node.disk))}</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 whitespace-nowrap`}>
|
2021-03-23 23:47:24 +00:00
|
|
|
{node.scheme === 'https' ?
|
2021-01-06 16:52:07 +00:00
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}>
|
|
|
|
Secure
|
|
|
|
</span>
|
|
|
|
:
|
|
|
|
<span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-red-200 text-red-800`}>
|
|
|
|
Non-Secure
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</td>
|
2021-03-23 23:47:24 +00:00
|
|
|
|
|
|
|
<td css={tw`px-6 whitespace-nowrap`}>
|
|
|
|
{/* TODO: Change color based off of online/offline status */}
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" css={[ tw`h-5 w-5`, node.scheme === 'https' ? tw`text-green-200` : tw`text-red-300` ]}>
|
|
|
|
<path clipRule="evenodd" fillRule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" />
|
|
|
|
</svg>
|
|
|
|
</td>
|
2021-01-05 21:52:49 +00:00
|
|
|
</TableRow>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</TableBody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</Pagination>
|
|
|
|
</ContentWrapper>
|
|
|
|
}
|
|
|
|
</AdminTable>
|
2020-08-22 22:49:18 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-05 21:52:49 +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 21:52:49 +00:00
|
|
|
|
|
|
|
return (
|
2021-05-20 22:00:46 +00:00
|
|
|
<NodesContext.Provider value={{ page, setPage, filters, setFilters, sort, setSort, sortDirection, setSortDirection }}>
|
2021-01-05 21:52:49 +00:00
|
|
|
<NodesContainer/>
|
|
|
|
</NodesContext.Provider>
|
|
|
|
);
|
|
|
|
};
|