import type { ChangeEvent } from 'react'; import { useContext, useEffect } from 'react'; import { NavLink } from 'react-router-dom'; import tw from 'twin.macro'; import type { Filters } from '@/api/admin/nests/getNests'; import getNests, { Context as NestsContext } from '@/api/admin/nests/getNests'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper, useTableHooks, } from '@/components/admin/AdminTable'; import CopyOnClick from '@/components/elements/CopyOnClick'; import NewNestButton from '@/components/admin/nests/NewNestButton'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { AdminContext } from '@/state/admin'; const RowCheckbox = ({ id }: { id: number }) => { const isChecked = AdminContext.useStoreState(state => state.nests.selectedNests.indexOf(id) >= 0); const appendSelectedNest = AdminContext.useStoreActions(actions => actions.nests.appendSelectedNest); const removeSelectedNest = AdminContext.useStoreActions(actions => actions.nests.removeSelectedNest); return ( ) => { if (e.currentTarget.checked) { appendSelectedNest(id); } else { removeSelectedNest(id); } }} /> ); }; const NestsContainer = () => { const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(NestsContext); const { clearFlashes, clearAndAddHttpError } = useFlash(); const { data: nests, error, isValidating } = getNests(); useEffect(() => { if (!error) { clearFlashes('nests'); return; } clearAndAddHttpError({ key: 'nests', error }); }, [error]); const length = nests?.items?.length || 0; const setSelectedNests = AdminContext.useStoreActions(actions => actions.nests.setSelectedNests); const selectedNestsLength = AdminContext.useStoreState(state => state.nests.selectedNests.length); const onSelectAllClick = (e: ChangeEvent) => { setSelectedNests(e.currentTarget.checked ? nests?.items?.map(nest => nest.id) || [] : []); }; const onSearch = (query: string): Promise => { return new Promise(resolve => { if (query.length < 2) { setFilters(null); } else { setFilters({ name: query }); } return resolve(); }); }; useEffect(() => { setSelectedNests([]); }, [page]); return (

Nests

All nests currently available on this system.

setSort('id')} /> setSort('name')} /> {nests !== undefined && !error && !isValidating && length > 0 && nests.items.map(nest => ( ))}
{nest.id} {nest.name} {nest.description}
{nests === undefined || (error && isValidating) ? ( ) : length < 1 ? ( ) : null}
); }; export default () => { const hooks = useTableHooks(); return ( ); };