1- import React , { useEffect , useRef , useState } from 'react' ;
1+ import React , { memo , useEffect , useRef , useState } from 'react' ;
22import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
33import { faEthernet , faHdd , faMemory , faMicrochip , faServer } from '@fortawesome/free-solid-svg-icons' ;
44import { Link } from 'react-router-dom' ;
@@ -8,14 +8,29 @@ import { bytesToHuman, megabytesToHuman } from '@/helpers';
88import tw from 'twin.macro' ;
99import GreyRowBox from '@/components/elements/GreyRowBox' ;
1010import Spinner from '@/components/elements/Spinner' ;
11+ import styled from 'styled-components/macro' ;
12+ import { IconProp } from '@fortawesome/fontawesome-svg-core' ;
13+ import isEqual from 'react-fast-compare' ;
1114
1215// Determines if the current value is in an alarm threshold so we can show it in red rather
1316// than the more faded default style.
14- const isAlarmState = ( current : number , limit : number ) : boolean => {
15- const limitInBytes = limit * 1024 * 1024 ;
17+ const isAlarmState = ( current : number , limit : number ) : boolean => limit > 0 && ( current / ( limit * 1024 * 1024 ) >= 0.90 ) ;
1618
17- return current / limitInBytes >= 0.90 ;
18- } ;
19+ interface IconProps {
20+ icon : IconProp ;
21+ usage : number ;
22+ limit : string ;
23+ isAlarm : boolean ;
24+ }
25+
26+ const Icon = memo ( styled ( FontAwesomeIcon ) < { $alarm : boolean } > `
27+ ${ props => props . $alarm ? tw `text-red-400` : tw `text-neutral-500` } ;
28+ ` , isEqual ) ;
29+
30+ const IconDescription = styled . p < { $alarm : boolean } > `
31+ ${ tw `text-sm ml-2` } ;
32+ ${ props => props . $alarm ? tw `text-white` : tw `text-neutral-400` } ;
33+ ` ;
1934
2035export default ( { server, className } : { server : Server ; className ?: string } ) => {
2136 const interval = useRef < number > ( null ) ;
@@ -96,62 +111,26 @@ export default ({ server, className }: { server: Server; className?: string }) =
96111 :
97112 < React . Fragment >
98113 < div css = { tw `flex-1 flex md:ml-4 sm:flex hidden justify-center` } >
99- < FontAwesomeIcon
100- icon = { faMicrochip }
101- css = { [
102- ! alarms . cpu && tw `text-neutral-500` ,
103- alarms . cpu && tw `text-red-400` ,
104- ] }
105- />
106- < p
107- css = { [
108- tw `text-sm ml-2` ,
109- ! alarms . cpu && tw `text-neutral-400` ,
110- alarms . cpu && tw `text-white` ,
111- ] }
112- >
114+ < Icon icon = { faMicrochip } $alarm = { alarms . cpu } />
115+ < IconDescription $alarm = { alarms . cpu } >
113116 { stats . cpuUsagePercent } %
114- </ p >
117+ </ IconDescription >
115118 </ div >
116119 < div css = { tw `flex-1 ml-4 sm:block hidden` } >
117120 < div css = { tw `flex justify-center` } >
118- < FontAwesomeIcon
119- icon = { faMemory }
120- css = { [
121- ! alarms . memory && tw `text-neutral-500` ,
122- alarms . memory && tw `text-red-400` ,
123- ] }
124- />
125- < p
126- css = { [
127- tw `text-sm ml-2` ,
128- ! alarms . memory && tw `text-neutral-400` ,
129- alarms . memory && tw `text-white` ,
130- ] }
131- >
121+ < Icon icon = { faMemory } $alarm = { alarms . memory } />
122+ < IconDescription $alarm = { alarms . memory } >
132123 { bytesToHuman ( stats . memoryUsageInBytes ) }
133- </ p >
124+ </ IconDescription >
134125 </ div >
135126 < p css = { tw `text-xs text-neutral-600 text-center mt-1` } > of { memorylimit } </ p >
136127 </ div >
137128 < div css = { tw `flex-1 ml-4 sm:block hidden` } >
138129 < div css = { tw `flex justify-center` } >
139- < FontAwesomeIcon
140- icon = { faHdd }
141- css = { [
142- ! alarms . disk && tw `text-neutral-500` ,
143- alarms . disk && tw `text-red-400` ,
144- ] }
145- />
146- < p
147- css = { [
148- tw `text-sm ml-2` ,
149- ! alarms . disk && tw `text-neutral-400` ,
150- alarms . disk && tw `text-white` ,
151- ] }
152- >
130+ < Icon icon = { faHdd } $alarm = { alarms . disk } />
131+ < IconDescription $alarm = { alarms . disk } >
153132 { bytesToHuman ( stats . diskUsageInBytes ) }
154- </ p >
133+ </ IconDescription >
155134 </ div >
156135 < p css = { tw `text-xs text-neutral-600 text-center mt-1` } > of { disklimit } </ p >
157136 </ div >
0 commit comments