Path: blob/develop/resources/scripts/components/dashboard/DashboardContainer.tsx
7428 views
import { useEffect, useState } from 'react';1import { Server } from '@/api/server/getServer';2import getServers from '@/api/getServers';3import ServerRow from '@/components/dashboard/ServerRow';4import Spinner from '@/components/elements/Spinner';5import PageContentBlock from '@/components/elements/PageContentBlock';6import useFlash from '@/plugins/useFlash';7import { useStoreState } from 'easy-peasy';8import { usePersistedState } from '@/plugins/usePersistedState';9import Switch from '@/components/elements/Switch';10import tw from 'twin.macro';11import useSWR from 'swr';12import { PaginatedResult } from '@/api/http';13import Pagination from '@/components/elements/Pagination';14import { useLocation } from 'react-router-dom';1516export default () => {17const { search } = useLocation();18const defaultPage = Number(new URLSearchParams(search).get('page') || '1');1920const [page, setPage] = useState(!isNaN(defaultPage) && defaultPage > 0 ? defaultPage : 1);21const { clearFlashes, clearAndAddHttpError } = useFlash();22const uuid = useStoreState(state => state.user.data!.uuid);23const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);24const [showOnlyAdmin, setShowOnlyAdmin] = usePersistedState(`${uuid}:show_all_servers`, false);2526const { data: servers, error } = useSWR<PaginatedResult<Server>>(27['/api/client/servers', showOnlyAdmin && rootAdmin, page],28() => getServers({ page, type: showOnlyAdmin && rootAdmin ? 'admin' : undefined }),29);3031useEffect(() => {32if (!servers) return;33if (servers.pagination.currentPage > 1 && !servers.items.length) {34setPage(1);35}36}, [servers?.pagination.currentPage]);3738useEffect(() => {39// Don't use react-router to handle changing this part of the URL, otherwise it40// triggers a needless re-render. We just want to track this in the URL incase the41// user refreshes the page.42window.history.replaceState(null, document.title, `/${page <= 1 ? '' : `?page=${page}`}`);43}, [page]);4445useEffect(() => {46if (error) clearAndAddHttpError({ key: 'dashboard', error });47if (!error) clearFlashes('dashboard');48}, [error]);4950return (51<PageContentBlock title={'Dashboard'} showFlashKey={'dashboard'}>52{rootAdmin && (53<div css={tw`mb-2 flex justify-end items-center`}>54<p css={tw`uppercase text-xs text-neutral-400 mr-2`}>55{showOnlyAdmin ? "Showing others' servers" : 'Showing your servers'}56</p>57<Switch58name={'show_all_servers'}59defaultChecked={showOnlyAdmin}60onChange={() => setShowOnlyAdmin(s => !s)}61/>62</div>63)}64{!servers ? (65<Spinner centered size={'large'} />66) : (67<Pagination data={servers} onPageSelect={setPage}>68{({ items }) =>69items.length > 0 ? (70items.map((server, index) => (71<ServerRow key={server.uuid} server={server} css={index > 0 ? tw`mt-2` : undefined} />72))73) : (74<p css={tw`text-center text-sm text-neutral-400`}>75{showOnlyAdmin76? 'There are no other servers to display.'77: 'There are no servers associated with your account.'}78</p>79)80}81</Pagination>82)}83</PageContentBlock>84);85};868788