2020-07-15 03:48:41 +00:00
|
|
|
import React, { 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');
|
|
|
|
|
|
|
|
const [ page, setPage ] = useState((!isNaN(defaultPage) && defaultPage > 0) ? defaultPage : 1);
|
2020-07-12 18:15:54 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2021-01-21 04:07:52 +00:00
|
|
|
const uuid = useStoreState(state => state.user.data!.uuid);
|
|
|
|
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
|
|
|
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>>(
|
2020-07-26 17:43:46 +00:00
|
|
|
[ '/api/client/servers', showOnlyAdmin, page ],
|
2020-10-16 04:21:38 +00:00
|
|
|
() => getServers({ page, type: showOnlyAdmin ? '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);
|
|
|
|
}
|
|
|
|
}, [ servers?.pagination.currentPage ]);
|
|
|
|
|
|
|
|
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}`}`);
|
|
|
|
}, [ page ]);
|
|
|
|
|
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');
|
|
|
|
}, [ error ]);
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
return (
|
2020-09-08 03:26:18 +00:00
|
|
|
<PageContentBlock title={'Dashboard'} showFlashKey={'dashboard'}>
|
2020-04-26 00:52:32 +00:00
|
|
|
{rootAdmin &&
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`mb-2 flex justify-end items-center`}>
|
|
|
|
<p css={tw`uppercase text-xs text-neutral-400 mr-2`}>
|
2021-03-05 17:39:47 +00:00
|
|
|
{showOnlyAdmin ? 'Showing others\' servers' : 'Showing your servers'}
|
2019-12-22 23:31:12 +00:00
|
|
|
</p>
|
2020-04-26 00:52:32 +00:00
|
|
|
<Switch
|
|
|
|
name={'show_all_servers'}
|
2020-07-26 17:43:46 +00:00
|
|
|
defaultChecked={showOnlyAdmin}
|
|
|
|
onChange={() => setShowOnlyAdmin(s => !s)}
|
2020-04-26 00:52:32 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
2020-07-12 18:15:54 +00:00
|
|
|
{!servers ?
|
2020-07-03 21:50:37 +00:00
|
|
|
<Spinner centered size={'large'}/>
|
2020-04-26 00:52:32 +00:00
|
|
|
:
|
2020-07-15 03:48:41 +00:00
|
|
|
<Pagination data={servers} onPageSelect={setPage}>
|
|
|
|
{({ items }) => (
|
|
|
|
items.length > 0 ?
|
|
|
|
items.map((server, index) => (
|
|
|
|
<ServerRow
|
|
|
|
key={server.uuid}
|
|
|
|
server={server}
|
|
|
|
css={index > 0 ? tw`mt-2` : undefined}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
:
|
|
|
|
<p css={tw`text-center text-sm text-neutral-400`}>
|
2020-07-26 17:43:46 +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>
|
|
|
|
)}
|
|
|
|
</Pagination>
|
2019-08-17 23:03:10 +00:00
|
|
|
}
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-08-17 23:03:10 +00:00
|
|
|
);
|
|
|
|
};
|