import { useContext } from 'react'; import { List, ListItem, Divider, Box } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import { Panel } from './Panels'; import PeriodicFetch from '../lib/fetchers/periodicFetch'; import { HeaderText, BodyText } from './Text'; import { HOVER, DIVIDER } from '../lib/consts/DEFAULT_THEME'; import { AnvilContext } from './AnvilContext'; import serverState from '../lib/consts/SERVERS'; import Decorator, { Colours } from './Decorator'; import Spinner from './Spinner'; import hostsSanitizer from '../lib/sanitizers/hostsSanitizer'; const useStyles = makeStyles((theme) => ({ root: { width: '100%', overflow: 'auto', height: '78vh', [theme.breakpoints.down('md')]: { height: '100%', }, }, divider: { background: DIVIDER, }, verticalDivider: { height: '75%', paddingTop: '1em', }, button: { '&:hover': { backgroundColor: HOVER, }, paddingLeft: 0, }, headerPadding: { paddingLeft: '.3em', }, hostsBox: { padding: '1em', paddingRight: 0, }, hostBox: { paddingTop: 0, }, })); const selectDecorator = (state: string): Colours => { switch (state) { case 'running': return 'ok'; case 'shut off': return 'off'; case 'crashed': return 'error'; default: return 'warning'; } }; const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const { uuid } = useContext(AnvilContext); const classes = useStyles(); const { data, isLoading } = PeriodicFetch( `${process.env.NEXT_PUBLIC_API_URL}/get_servers?anvil_uuid=${uuid}`, ); const anvilIndex = anvil.findIndex((a) => a.anvil_uuid === uuid); const filteredHosts = hostsSanitizer(anvil[anvilIndex]?.hosts); return (
{!isLoading ? ( {data && data.servers.map((server: AnvilServer) => { return ( <> {server.server_state !== 'shut off' && server.server_state !== 'crashed' && filteredHosts.map( ( host: AnvilStatusHost, index: number, ): JSX.Element => ( <> {index !== filteredHosts.length - 1 && ( )} ), )} ); })} ) : ( )}
); }; export default Servers;