2021-05-28 14:30:55 +05:00
|
|
|
import { useState, useEffect } from 'react';
|
2021-07-26 14:44:00 +05:00
|
|
|
import {CaretUpOutlined, CaretDownOutlined, CaretRightOutlined} from '@ant-design/icons'
|
|
|
|
import moment from 'moment';
|
2021-05-24 15:19:38 +05:00
|
|
|
|
2021-07-26 14:44:00 +05:00
|
|
|
export const formatNumber = (value, format) =>
|
|
|
|
Number.isInteger(format) && Number.isFinite(value)
|
|
|
|
? (+value).toFixed(format)
|
|
|
|
: (+value).toPrecision(4)
|
|
|
|
|
2021-08-13 10:29:34 +05:00
|
|
|
export const ValueDisplay = ({prefix, value, suffix, isArrowVisible, format, enumeration}) => {
|
2021-05-24 17:57:13 +05:00
|
|
|
const [val, setVal] = useState('---')
|
2021-07-26 14:44:00 +05:00
|
|
|
const [arrowState, setArrowState] = useState({
|
|
|
|
preVal: NaN,
|
|
|
|
preTimestamp: Date.now(),
|
|
|
|
direction: 0,
|
|
|
|
})
|
2021-05-24 17:57:13 +05:00
|
|
|
|
|
|
|
useEffect(()=>{
|
2021-08-12 17:47:16 +05:00
|
|
|
if(value === undefined || value === null || value === '-' || value === '--'){
|
2021-07-26 14:44:00 +05:00
|
|
|
setVal('---')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:29:34 +05:00
|
|
|
if(enumeration && enumeration[value]){
|
|
|
|
setVal(enumeration[value])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-26 14:44:00 +05:00
|
|
|
if(Number.isFinite(+value)){
|
|
|
|
if ((isArrowVisible) && (arrowState.preTimestamp + 1000 < Date.now()))
|
|
|
|
{
|
|
|
|
let direction = 0
|
|
|
|
if (value > arrowState.preVal)
|
|
|
|
direction = 1
|
|
|
|
if (value < arrowState.preVal)
|
|
|
|
direction = -1
|
|
|
|
|
|
|
|
setArrowState({
|
|
|
|
preVal: value,
|
|
|
|
preTimestamp: Date.now(),
|
|
|
|
direction: direction,
|
|
|
|
})
|
2021-05-28 14:30:55 +05:00
|
|
|
}
|
2021-07-26 14:44:00 +05:00
|
|
|
|
|
|
|
setVal(formatNumber(value, format))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:47:16 +05:00
|
|
|
if(value.length > 4){
|
|
|
|
let valueDate = moment(value)
|
|
|
|
if(valueDate.isValid()){
|
|
|
|
setVal(valueDate.format(format))
|
|
|
|
return
|
|
|
|
}
|
2021-05-28 14:30:55 +05:00
|
|
|
}
|
|
|
|
|
2021-07-26 14:44:00 +05:00
|
|
|
setVal(value)
|
2021-08-13 10:29:34 +05:00
|
|
|
},[value, isArrowVisible, arrowState, format, enumeration])
|
2021-04-16 15:50:01 +05:00
|
|
|
|
2021-05-28 14:30:55 +05:00
|
|
|
let arrow = null
|
2021-07-26 14:44:00 +05:00
|
|
|
if(isArrowVisible)
|
|
|
|
switch (arrowState.direction){
|
|
|
|
case 0:
|
|
|
|
arrow = <CaretRightOutlined style={{color:"#0008"}}/>
|
|
|
|
break
|
|
|
|
case 1:
|
|
|
|
arrow = <CaretUpOutlined style={{color:"#0008"}}/>
|
|
|
|
break
|
|
|
|
case -1:
|
|
|
|
arrow = <CaretDownOutlined style={{color:"#0008"}}/>
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2021-05-28 14:30:55 +05:00
|
|
|
|
|
|
|
return(<span className='display_value'>{prefix} {val} {suffix}{arrow}</span>)
|
2021-04-16 15:50:01 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Display = (props)=>{
|
|
|
|
const {label} = props
|
|
|
|
|
|
|
|
return <div className={props.className}>
|
|
|
|
<div className='display_label'>{label}</div>
|
|
|
|
<div style={{display:"flex", flexGrow:1}}>
|
|
|
|
<ValueDisplay {...props}/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
}
|