import React, { useContext, useEffect } from 'react'; import { NavLink } from 'react-router-dom'; import tw from 'twin.macro'; import getServers, { Context as ServersContext, Filters } from '@/api/admin/servers/getServers'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { ContentWrapper, Loading, NoItems, Pagination, TableBody, TableHead, TableHeader, useTableHooks } from '@/components/admin/AdminTable'; import CopyOnClick from '@/components/elements/CopyOnClick'; import { AdminContext } from '@/state/admin'; import useFlash from '@/plugins/useFlash'; function 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 ( ) => { if (e.currentTarget.checked) { appendSelectedServer(id); } else { removeSelectedServer(id); } }} /> ); } interface Props { filters?: Filters; } function ServersTable ({ filters }: Props) { const { clearFlashes, clearAndAddHttpError } = useFlash(); const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(ServersContext); const { data: servers, error, isValidating } = getServers([ 'node', 'user' ]); 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) => { setSelectedServers(e.currentTarget.checked ? (servers?.items?.map(server => server.id) || []) : []); }; const onSearch = (query: string): Promise => { return new Promise((resolve) => { if (query.length < 2) { setFilters(filters || null); } else { setFilters({ ...filters, name: query }); } return resolve(); }); }; useEffect(() => { setSelectedServers([]); }, [ page ]); useEffect(() => { if (!error) { clearFlashes('servers'); return; } clearAndAddHttpError({ key: 'servers', error }); }, [ error ]); return (
setSort('uuidShort')}/> setSort('name')}/> setSort('owner_id')}/> setSort('node_id')}/> setSort('status')}/> { servers !== undefined && !error && !isValidating && length > 0 && servers.items.map(server => ( {/* TODO: Have permission check for displaying user information. */} {/* TODO: Have permission check for displaying node information. */} )) }
{server.identifier} {server.name}
{server.relations.user?.email}
{server.relations.user?.uuid.split('-')[0]}
{server.relations.node?.name}
{server.relations.node?.fqdn}
{server.status === 'installing' ? Installing : server.status === 'transferring' ? Transferring : server.status === 'suspended' ? Suspended : Active }
{ servers === undefined || (error && isValidating) ? : length < 1 ? : null }
); } export default ({ filters }: Props) => { const hooks = useTableHooks(filters); return ( ); };