import { useState, useContext, useRef } from 'react'; import { List, ListItem, Divider, Box, IconButton, Button, Checkbox, Menu, MenuItem, Typography, } from '@material-ui/core'; import EditIcon from '@material-ui/icons/Edit'; import MoreVertIcon from '@material-ui/icons/MoreVert'; import CheckIcon from '@material-ui/icons/Check'; 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, TEXT, BLUE, RED, GREY, BLACK, LARGE_MOBILE_BREAKPOINT, } 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'; import putFetch from '../lib/fetchers/putFetch'; const useStyles = makeStyles((theme) => ({ root: { width: '100%', overflow: 'auto', height: '78vh', paddingRight: '.3em', [theme.breakpoints.down(LARGE_MOBILE_BREAKPOINT)]: { height: '100%', overflow: 'hidden', }, }, 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, }, checkbox: { paddingTop: '.8em', }, menuItem: { backgroundColor: GREY, paddingRight: '3em', '&:hover': { backgroundColor: GREY, }, }, editButton: { borderRadius: 8, backgroundColor: GREY, '&:hover': { backgroundColor: GREY, }, }, editButtonBox: { paddingTop: '.3em', }, dropdown: { paddingTop: '.8em', paddingBottom: '.8em', }, power: { color: BLACK, }, on: { color: BLUE, }, off: { color: RED, }, all: { paddingTop: '.5em', paddingLeft: '.3em', }, })); const selectDecorator = (state: string): Colours => { switch (state) { case 'running': return 'ok'; case 'shut off': return 'off'; case 'crashed': return 'error'; default: return 'warning'; } }; type ButtonLabels = 'on' | 'off'; const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const [anchorEl, setAnchorEl] = useState(null); const [showCheckbox, setShowCheckbox] = useState(false); const [allSelected, setAllSelected] = useState(false); const [selected, setSelected] = useState([]); const buttonLabels = useRef([]); const { uuid } = useContext(AnvilContext); const classes = useStyles(); const { data, isLoading } = PeriodicFetch( `${process.env.NEXT_PUBLIC_API_URL}/get_servers?anvil_uuid=${uuid}`, ); const setButtons = (filtered: AnvilServer[]) => { buttonLabels.current = []; if ( filtered.filter((item: AnvilServer) => item.server_state === 'running') .length ) { buttonLabels.current.push('off'); } if ( filtered.filter((item: AnvilServer) => item.server_state === 'shut off') .length ) { buttonLabels.current.push('on'); } }; const handleClick = (event: React.MouseEvent): void => { setAnchorEl(event.currentTarget); }; const handlePower = (label: ButtonLabels) => { setAnchorEl(null); if (selected.length) { putFetch(`${process.env.NEXT_PUBLIC_API_URL}/set_power`, { server_uuid_list: selected, is_on: label === 'on', }); } }; const handleChange = (server_uuid: string): void => { const index = selected.indexOf(server_uuid); if (index === -1) selected.push(server_uuid); else selected.splice(index, 1); const filtered = data.servers.filter( (server: AnvilServer) => selected.indexOf(server.server_uuid) !== -1, ); setButtons(filtered); setSelected([...selected]); }; const anvilIndex = anvil.findIndex((a) => a.anvil_uuid === uuid); const filteredHosts = hostsSanitizer(anvil[anvilIndex]?.hosts); return ( setShowCheckbox(!showCheckbox)} > {showCheckbox ? : } {showCheckbox && ( <> setAnchorEl(null)} > {buttonLabels.current.map((label: ButtonLabels) => { return ( handlePower(label)} className={classes.menuItem} key={label} > {label.replace(/^[a-z]/, (c) => c.toUpperCase())} ); })} { if (!allSelected) { setButtons(data.servers); setSelected( data.servers.map( (server: AnvilServer) => server.server_uuid, ), ); } else { setButtons([]); setSelected([]); } setAllSelected(!allSelected); }} /> )} {!isLoading ? ( {data?.servers.map((server: AnvilServer) => { return ( <> handleChange(server.server_uuid)} > {showCheckbox && ( s === server.server_uuid) !== undefined } /> )} {server.server_state !== 'shut off' && server.server_state !== 'crashed' && filteredHosts.map( ( host: AnvilStatusHost, index: number, ): JSX.Element => ( <> {index !== filteredHosts.length - 1 && ( )} ), )} ); })} ) : ( )} ); }; export default Servers;