Correctly render suspended server in dashboard listing; closes #2613

This commit is contained in:
Dane Everitt 2020-10-31 12:07:20 -07:00
parent ff64220741
commit 87a8238c56
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -41,8 +41,9 @@ const StatusIndicatorBox = styled(GreyRowBox)<{ $status: ServerPowerState | unde
export default ({ server, className }: { server: Server; className?: string }) => { export default ({ server, className }: { server: Server; className?: string }) => {
const interval = useRef<number>(null); const interval = useRef<number>(null);
const [ stats, setStats ] = useState<ServerStats | null>(null);
const [ statsError, setStatsError ] = useState(false); const [ statsError, setStatsError ] = useState(false);
const [ isSuspended, setIsSuspended ] = useState(server.isSuspended);
const [ stats, setStats ] = useState<ServerStats | null>(null);
const getStats = () => { const getStats = () => {
setStatsError(false); setStatsError(false);
@ -55,6 +56,14 @@ export default ({ server, className }: { server: Server; className?: string }) =
}; };
useEffect(() => { useEffect(() => {
setIsSuspended(stats?.isSuspended || server.isSuspended);
}, [ stats?.isSuspended, server.isSuspended ]);
useEffect(() => {
// Don't waste a HTTP request if there is nothing important to show to the user because
// the server is suspended.
if (isSuspended) return;
getStats().then(() => { getStats().then(() => {
// @ts-ignore // @ts-ignore
interval.current = setInterval(() => getStats(), 20000); interval.current = setInterval(() => getStats(), 20000);
@ -63,7 +72,7 @@ export default ({ server, className }: { server: Server; className?: string }) =
return () => { return () => {
interval.current && clearInterval(interval.current); interval.current && clearInterval(interval.current);
}; };
}, []); }, [ isSuspended ]);
const alarms = { cpu: false, memory: false, disk: false }; const alarms = { cpu: false, memory: false, disk: false };
if (stats) { if (stats) {
@ -101,9 +110,13 @@ export default ({ server, className }: { server: Server; className?: string }) =
</p> </p>
</div> </div>
<div css={tw`hidden col-span-7 lg:col-span-4 sm:flex items-baseline justify-center`}> <div css={tw`hidden col-span-7 lg:col-span-4 sm:flex items-baseline justify-center`}>
{!stats ? {(!stats || isSuspended) ?
!statsError ? isSuspended ?
<Spinner size={'small'}/> <div css={tw`flex-1 text-center`}>
<span css={tw`bg-red-500 rounded px-2 py-1 text-red-100 text-xs`}>
{server.isSuspended ? 'Suspended' : 'Connection Error'}
</span>
</div>
: :
server.isInstalling ? server.isInstalling ?
<div css={tw`flex-1 text-center`}> <div css={tw`flex-1 text-center`}>
@ -112,11 +125,7 @@ export default ({ server, className }: { server: Server; className?: string }) =
</span> </span>
</div> </div>
: :
<div css={tw`flex-1 text-center`}> <Spinner size={'small'}/>
<span css={tw`bg-red-500 rounded px-2 py-1 text-red-100 text-xs`}>
{server.isSuspended ? 'Suspended' : 'Connection Error'}
</span>
</div>
: :
<React.Fragment> <React.Fragment>
<div css={tw`flex-1 flex md:ml-4 sm:flex hidden justify-center`}> <div css={tw`flex-1 flex md:ml-4 sm:flex hidden justify-center`}>