2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2019-08-17 23:03:10 +00:00
|
|
|
import { Server } from '@/api/server/getServer';
|
|
|
|
import getServers from '@/api/getServers';
|
|
|
|
import ServerRow from '@/components/dashboard/ServerRow';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2020-04-26 00:52:32 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { useStoreState } from 'easy-peasy';
|
|
|
|
import { usePersistedState } from '@/plugins/usePersistedState';
|
|
|
|
import Switch from '@/components/elements/Switch';
|
2020-07-03 21:50:37 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-12 18:15:54 +00:00
|
|
|
import useSWR from 'swr';
|
|
|
|
import { PaginatedResult } from '@/api/http';
|
2020-07-15 03:48:41 +00:00
|
|
|
import Pagination from '@/components/elements/Pagination';
|
2021-03-21 19:07:24 +00:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2019-06-26 04:28:56 +00:00
|
|
|
|
2019-08-17 23:03:10 +00:00
|
|
|
export default () => {
|
2021-03-21 19:07:24 +00:00
|
|
|
const { search } = useLocation();
|
|
|
|
const defaultPage = Number(new URLSearchParams(search).get('page') || '1');
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const [page, setPage] = useState(!isNaN(defaultPage) && defaultPage > 0 ? defaultPage : 1);
|
2020-07-12 18:15:54 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = useStoreState(state => state.user.data!.uuid);
|
|
|
|
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
2022-06-26 19:13:52 +00:00
|
|
|
const [showOnlyAdmin, setShowOnlyAdmin] = usePersistedState(`${uuid}:show_all_servers`, false);
|
2019-08-17 23:03:10 +00:00
|
|
|
|
2020-07-12 18:15:54 +00:00
|
|
|
const { data: servers, error } = useSWR<PaginatedResult<Server>>(
|
2022-06-26 19:13:52 +00:00
|
|
|
['/api/client/servers', showOnlyAdmin && rootAdmin, page],
|
2022-11-25 20:25:03 +00:00
|
|
|
() => getServers({ page, type: showOnlyAdmin && rootAdmin ? 'admin' : undefined }),
|
2020-07-12 18:15:54 +00:00
|
|
|
);
|
2019-08-17 23:03:10 +00:00
|
|
|
|
2021-03-21 19:07:24 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!servers) return;
|
|
|
|
if (servers.pagination.currentPage > 1 && !servers.items.length) {
|
|
|
|
setPage(1);
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [servers?.pagination.currentPage]);
|
2021-03-21 19:07:24 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Don't use react-router to handle changing this part of the URL, otherwise it
|
|
|
|
// triggers a needless re-render. We just want to track this in the URL incase the
|
|
|
|
// user refreshes the page.
|
|
|
|
window.history.replaceState(null, document.title, `/${page <= 1 ? '' : `?page=${page}`}`);
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [page]);
|
2021-03-21 19:07:24 +00:00
|
|
|
|
2019-08-17 23:03:10 +00:00
|
|
|
useEffect(() => {
|
2020-07-12 18:15:54 +00:00
|
|
|
if (error) clearAndAddHttpError({ key: 'dashboard', error });
|
|
|
|
if (!error) clearFlashes('dashboard');
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [error]);
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
return (
|
2020-09-08 03:26:18 +00:00
|
|
|
<PageContentBlock title={'Dashboard'} showFlashKey={'dashboard'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{rootAdmin && (
|
|
|
|
<div css={tw`mb-2 flex justify-end items-center`}>
|
|
|
|
<p css={tw`uppercase text-xs text-neutral-400 mr-2`}>
|
|
|
|
{showOnlyAdmin ? "Showing others' servers" : 'Showing your servers'}
|
|
|
|
</p>
|
|
|
|
<Switch
|
|
|
|
name={'show_all_servers'}
|
|
|
|
defaultChecked={showOnlyAdmin}
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange={() => setShowOnlyAdmin(s => !s)}
|
2022-06-26 19:13:52 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!servers ? (
|
|
|
|
<Spinner centered size={'large'} />
|
|
|
|
) : (
|
2020-07-15 03:48:41 +00:00
|
|
|
<Pagination data={servers} onPageSelect={setPage}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{({ items }) =>
|
|
|
|
items.length > 0 ? (
|
2020-07-15 03:48:41 +00:00
|
|
|
items.map((server, index) => (
|
2022-06-26 19:13:52 +00:00
|
|
|
<ServerRow key={server.uuid} server={server} css={index > 0 ? tw`mt-2` : undefined} />
|
2020-07-15 03:48:41 +00:00
|
|
|
))
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
2020-07-15 03:48:41 +00:00
|
|
|
<p css={tw`text-center text-sm text-neutral-400`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{showOnlyAdmin
|
|
|
|
? 'There are no other servers to display.'
|
|
|
|
: 'There are no servers associated with your account.'}
|
2020-07-15 03:48:41 +00:00
|
|
|
</p>
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
|
|
|
}
|
2020-07-15 03:48:41 +00:00
|
|
|
</Pagination>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-08-17 23:03:10 +00:00
|
|
|
);
|
|
|
|
};
|