1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useEffect } from 'react' ;
22import { Server } from '@/api/server/getServer' ;
33import getServers from '@/api/getServers' ;
44import ServerRow from '@/components/dashboard/ServerRow' ;
55import Spinner from '@/components/elements/Spinner' ;
66import PageContentBlock from '@/components/elements/PageContentBlock' ;
77import useFlash from '@/plugins/useFlash' ;
8- import { httpErrorToHuman } from '@/api/http' ;
9- import FlashMessageRender from '@/components/FlashMessageRender' ;
108import { useStoreState } from 'easy-peasy' ;
119import { usePersistedState } from '@/plugins/usePersistedState' ;
1210import Switch from '@/components/elements/Switch' ;
1311import tw from 'twin.macro' ;
12+ import useSWR from 'swr' ;
13+ import { PaginatedResult } from '@/api/http' ;
1414
1515export default ( ) => {
16- const { addError, clearFlashes } = useFlash ( ) ;
17- const [ servers , setServers ] = useState < Server [ ] > ( [ ] ) ;
18- const [ loading , setLoading ] = useState ( true ) ;
16+ const { clearFlashes, clearAndAddHttpError } = useFlash ( ) ;
1917 const { rootAdmin } = useStoreState ( state => state . user . data ! ) ;
2018 const [ showAdmin , setShowAdmin ] = usePersistedState ( 'show_all_servers' , false ) ;
2119
22- const loadServers = ( ) => {
23- clearFlashes ( ) ;
24- setLoading ( true ) ;
25-
26- getServers ( undefined , showAdmin )
27- . then ( data => setServers ( data . items ) )
28- . catch ( error => {
29- console . error ( error ) ;
30- addError ( { message : httpErrorToHuman ( error ) } ) ;
31- } )
32- . then ( ( ) => setLoading ( false ) ) ;
33- } ;
20+ const { data : servers , error } = useSWR < PaginatedResult < Server > > (
21+ [ '/api/client/servers' , showAdmin ] ,
22+ ( ) => getServers ( undefined , showAdmin )
23+ ) ;
3424
3525 useEffect ( ( ) => {
36- loadServers ( ) ;
37- } , [ showAdmin ] ) ;
26+ if ( error ) clearAndAddHttpError ( { key : 'dashboard' , error } ) ;
27+ if ( ! error ) clearFlashes ( 'dashboard' ) ;
28+ } , [ error ] ) ;
3829
3930 return (
40- < PageContentBlock >
41- < FlashMessageRender css = { tw `mb-4` } />
31+ < PageContentBlock showFlashKey = { 'dashboard' } >
4232 { rootAdmin &&
4333 < div css = { tw `mb-2 flex justify-end items-center` } >
4434 < p css = { tw `uppercase text-xs text-neutral-400 mr-2` } >
@@ -51,14 +41,12 @@ export default () => {
5141 />
5242 </ div >
5343 }
54- { loading ?
44+ { ! servers ?
5545 < Spinner centered size = { 'large' } />
5646 :
57- servers . length > 0 ?
58- servers . map ( ( server , index ) => (
59- < div key = { server . uuid } css = { index > 0 ? tw `mt-2` : undefined } >
60- < ServerRow server = { server } />
61- </ div >
47+ servers . items . length > 0 ?
48+ servers . items . map ( ( server , index ) => (
49+ < ServerRow key = { server . uuid } server = { server } css = { index > 0 ? tw `mt-2` : undefined } />
6250 ) )
6351 :
6452 < p css = { tw `text-center text-sm text-neutral-400` } >
0 commit comments