refactor: add Decorator component

main
Josue 4 years ago committed by Tsu-ba-me
parent 844e201c0b
commit 231c6dfdda
  1. 42
      striker-ui/components/Anvils/AnvilList.tsx
  2. 31
      striker-ui/components/Decorator.tsx
  3. 31
      striker-ui/components/Network/Network.tsx
  4. 37
      striker-ui/components/Nodes/AnvilNode.tsx
  5. 44
      striker-ui/components/Servers.tsx
  6. 28
      striker-ui/components/SharedStorage/SharedStorageNode.tsx

@ -1,17 +1,11 @@
import { useContext, useEffect } from 'react'; import { useContext, useEffect } from 'react';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { ClassNameMap } from '@material-ui/styles';
import { List, Box, Divider, ListItem } from '@material-ui/core'; import { List, Box, Divider, ListItem } from '@material-ui/core';
import { import { HOVER, DIVIDER } from '../../lib/consts/DEFAULT_THEME';
BLUE,
PURPLE_OFF,
RED_ON,
HOVER,
DIVIDER,
} from '../../lib/consts/DEFAULT_THEME';
import Anvil from './Anvil'; import Anvil from './Anvil';
import { AnvilContext } from '../AnvilContext'; import { AnvilContext } from '../AnvilContext';
import sortAnvils from './sortAnvils'; import sortAnvils from './sortAnvils';
import Decorator, { Colours } from '../Decorator';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
@ -35,34 +29,18 @@ const useStyles = makeStyles((theme) => ({
anvil: { anvil: {
paddingLeft: 0, paddingLeft: 0,
}, },
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
optimal: {
backgroundColor: BLUE,
},
notReady: {
backgroundColor: PURPLE_OFF,
},
degraded: {
backgroundColor: RED_ON,
},
})); }));
const selectDecorator = ( const selectDecorator = (state: string): Colours => {
state: string,
): keyof ClassNameMap<'optimal' | 'notReady' | 'degraded'> => {
switch (state) { switch (state) {
case 'optimal': case 'optimal':
return 'optimal'; return 'ok';
case 'not_ready': case 'not_ready':
return 'notReady'; return 'warning';
case 'degraded': case 'degraded':
return 'degraded'; return 'error';
default: default:
return 'optimal'; return 'off';
} }
}; };
@ -88,11 +66,7 @@ const AnvilList = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
> >
<Box display="flex" flexDirection="row" width="100%"> <Box display="flex" flexDirection="row" width="100%">
<Box p={1}> <Box p={1}>
<div <Decorator colour={selectDecorator(anvil.anvil_state)} />
className={`${classes.decorator} ${
classes[selectDecorator(anvil.anvil_state)]
}`}
/>
</Box> </Box>
<Box p={1} flexGrow={1} className={classes.anvil}> <Box p={1} flexGrow={1} className={classes.anvil}>
<Anvil anvil={anvil} /> <Anvil anvil={anvil} />

@ -0,0 +1,31 @@
import { makeStyles } from '@material-ui/core/styles';
import { BLUE, GREY, PURPLE_OFF, RED_ON } from '../lib/consts/DEFAULT_THEME';
export type Colours = 'ok' | 'off' | 'error' | 'warning';
const useStyles = makeStyles(() => ({
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
ok: {
backgroundColor: BLUE,
},
warning: {
backgroundColor: PURPLE_OFF,
},
error: {
backgroundColor: RED_ON,
},
off: {
backgroundColor: GREY,
},
}));
const Decorator = ({ colour }: { colour: Colours }): JSX.Element => {
const classes = useStyles();
return <div className={`${classes.decorator} ${classes[colour]}`} />;
};
export default Decorator;

@ -1,13 +1,13 @@
import { useContext } from 'react'; import { useContext } from 'react';
import { Box, Divider } from '@material-ui/core'; import { Box, Divider } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { ClassNameMap } from '@material-ui/styles';
import { Panel } from '../Panels'; import { Panel } from '../Panels';
import { HeaderText, BodyText } from '../Text'; import { HeaderText, BodyText } from '../Text';
import PeriodicFetch from '../../lib/fetchers/periodicFetch'; import PeriodicFetch from '../../lib/fetchers/periodicFetch';
import { BLUE, PURPLE_OFF, DIVIDER } from '../../lib/consts/DEFAULT_THEME'; import { DIVIDER } from '../../lib/consts/DEFAULT_THEME';
import processNetworkData from './processNetwork'; import processNetworkData from './processNetwork';
import { AnvilContext } from '../AnvilContext'; import { AnvilContext } from '../AnvilContext';
import Decorator, { Colours } from '../Decorator';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
container: { container: {
@ -29,29 +29,16 @@ const useStyles = makeStyles((theme) => ({
divider: { divider: {
background: DIVIDER, background: DIVIDER,
}, },
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
optimal: {
backgroundColor: BLUE,
},
degraded: {
backgroundColor: PURPLE_OFF,
},
})); }));
const selectDecorator = ( const selectDecorator = (state: string): Colours => {
state: string,
): keyof ClassNameMap<'optimal' | 'degraded'> => {
switch (state) { switch (state) {
case 'optimal': case 'optimal':
return 'optimal'; return 'ok';
case 'degraded': case 'degraded':
return 'degraded'; return 'warning';
default: default:
return 'degraded'; return 'warning';
} }
}; };
@ -80,11 +67,7 @@ const Network = (): JSX.Element => {
className={classes.root} className={classes.root}
> >
<Box p={1} className={classes.noPaddingLeft}> <Box p={1} className={classes.noPaddingLeft}>
<div <Decorator colour={selectDecorator(bond.bond_state)} />
className={`${classes.decorator} ${
classes[selectDecorator(bond.bond_state)]
}`}
/>
</Box> </Box>
<Box p={1} flexGrow={1} className={classes.noPaddingLeft}> <Box p={1} flexGrow={1} className={classes.noPaddingLeft}>
<BodyText text={bond.bond_name} /> <BodyText text={bond.bond_name} />

@ -1,12 +1,10 @@
import { Box, Switch } from '@material-ui/core'; import { Box, Switch } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { ClassNameMap } from '@material-ui/styles';
import { InnerPanel, PanelHeader } from '../Panels'; import { InnerPanel, PanelHeader } from '../Panels';
import { ProgressBar } from '../Bars'; import { ProgressBar } from '../Bars';
import { BodyText } from '../Text'; import { BodyText } from '../Text';
import { BLUE, RED_ON, TEXT, PURPLE_OFF } from '../../lib/consts/DEFAULT_THEME';
import nodeState from '../../lib/consts/NODES'; import nodeState from '../../lib/consts/NODES';
import Decorator, { Colours } from '../Decorator';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
@ -34,41 +32,22 @@ const useStyles = makeStyles((theme) => ({
label: { label: {
paddingTop: '5px', paddingTop: '5px',
}, },
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
decoratorBox: { decoratorBox: {
paddingRight: '5px', paddingRight: '5px',
}, },
ready: {
backgroundColor: BLUE,
},
onAccessible: {
backgroundColor: PURPLE_OFF,
},
off: {
backgroundColor: TEXT,
},
unknown: {
backgroundColor: RED_ON,
},
})); }));
const selectDecorator = ( const selectDecorator = (state: string): Colours => {
state: string,
): keyof ClassNameMap<'unknown' | 'off' | 'onAccessible' | 'ready'> => {
switch (state) { switch (state) {
case 'ready': case 'ready':
return 'ready'; return 'ok';
case 'off': case 'off':
return 'off'; return 'off';
case 'accessible': case 'accessible':
case 'on': case 'on':
return 'onAccessible'; return 'warning';
default: default:
return 'unknown'; return 'error';
} }
}; };
@ -91,11 +70,7 @@ const AnvilNode = ({
<BodyText text={node.node_name} /> <BodyText text={node.node_name} />
</Box> </Box>
<Box className={classes.decoratorBox}> <Box className={classes.decoratorBox}>
<div <Decorator colour={selectDecorator(node.state)} />
className={`${classes.decorator} ${
classes[selectDecorator(node.state)]
}`}
/>
</Box> </Box>
<Box> <Box>
<BodyText <BodyText

@ -1,20 +1,13 @@
import { useContext } from 'react'; import { useContext } from 'react';
import { List, ListItem, Divider, Box } from '@material-ui/core'; import { List, ListItem, Divider, Box } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { ClassNameMap } from '@material-ui/styles';
import { Panel } from './Panels'; import { Panel } from './Panels';
import PeriodicFetch from '../lib/fetchers/periodicFetch'; import PeriodicFetch from '../lib/fetchers/periodicFetch';
import { HeaderText, BodyText } from './Text'; import { HeaderText, BodyText } from './Text';
import { import { HOVER, DIVIDER } from '../lib/consts/DEFAULT_THEME';
BLUE,
GREY,
HOVER,
DIVIDER,
PURPLE_OFF,
RED_ON,
} from '../lib/consts/DEFAULT_THEME';
import { AnvilContext } from './AnvilContext'; import { AnvilContext } from './AnvilContext';
import serverState from '../lib/consts/SERVERS'; import serverState from '../lib/consts/SERVERS';
import Decorator, { Colours } from './Decorator';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
@ -37,35 +30,16 @@ const useStyles = makeStyles((theme) => ({
noPaddingLeft: { noPaddingLeft: {
paddingLeft: 0, paddingLeft: 0,
}, },
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
running: {
backgroundColor: BLUE,
},
shut_off: {
backgroundColor: GREY,
},
crashed: {
backgroundColor: RED_ON,
},
warning: {
backgroundColor: PURPLE_OFF,
},
})); }));
const selectDecorator = ( const selectDecorator = (state: string): Colours => {
state: string,
): keyof ClassNameMap<'running' | 'shut_off' | 'crashed' | 'warning'> => {
switch (state) { switch (state) {
case 'running': case 'running':
return 'running'; return 'ok';
case 'shut_off': case 'shut_off':
return 'shut_off'; return 'off';
case 'crashed': case 'crashed':
return 'crashed'; return 'error';
default: default:
return 'warning'; return 'warning';
} }
@ -95,10 +69,8 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
> >
<Box display="flex" flexDirection="row" width="100%"> <Box display="flex" flexDirection="row" width="100%">
<Box p={1} className={classes.noPaddingLeft}> <Box p={1} className={classes.noPaddingLeft}>
<div <Decorator
className={`${classes.decorator} ${ colour={selectDecorator(server.server_state)}
classes[selectDecorator(server.server_state)]
}`}
/> />
</Box> </Box>
<Box p={1} flexGrow={1} className={classes.noPaddingLeft}> <Box p={1} flexGrow={1} className={classes.noPaddingLeft}>

@ -1,18 +1,9 @@
import { Box } from '@material-ui/core'; import { Box } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { ClassNameMap } from '@material-ui/styles';
import * as prettyBytes from 'pretty-bytes'; import * as prettyBytes from 'pretty-bytes';
import { AllocationBar } from '../Bars'; import { AllocationBar } from '../Bars';
import { BodyText } from '../Text'; import { BodyText } from '../Text';
import { BLUE, RED_ON } from '../../lib/consts/DEFAULT_THEME'; import Decorator from '../Decorator';
const selectDecorator = (
state: boolean,
): keyof ClassNameMap<'mounted' | 'notMounted'> => {
return state ? 'mounted' : 'notMounted';
};
const useStyles = makeStyles(() => ({ const useStyles = makeStyles(() => ({
fs: { fs: {
@ -24,20 +15,9 @@ const useStyles = makeStyles(() => ({
paddingLeft: '10px', paddingLeft: '10px',
paddingRight: '10px', paddingRight: '10px',
}, },
decorator: {
width: '20px',
height: '100%',
borderRadius: 2,
},
decoratorBox: { decoratorBox: {
paddingRight: '5px', paddingRight: '5px',
}, },
mounted: {
backgroundColor: BLUE,
},
notMounted: {
backgroundColor: RED_ON,
},
})); }));
const SharedStorageNode = ({ const SharedStorageNode = ({
@ -53,11 +33,7 @@ const SharedStorageNode = ({
<BodyText text={node.nodeInfo?.node_name || 'Not Available'} /> <BodyText text={node.nodeInfo?.node_name || 'Not Available'} />
</Box> </Box>
<Box className={classes.decoratorBox}> <Box className={classes.decoratorBox}>
<div <Decorator colour={node.is_mounted ? 'ok' : 'error'} />
className={`${classes.decorator} ${
classes[selectDecorator(node.is_mounted)]
}`}
/>
</Box> </Box>
<Box> <Box>
<BodyText text={node.is_mounted ? 'Mounted' : 'Not Mounted'} /> <BodyText text={node.is_mounted ? 'Mounted' : 'Not Mounted'} />

Loading…
Cancel
Save