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