2022-01-24 21:16:50 +05:00
|
|
|
import moment from 'moment'
|
2022-04-29 16:39:49 +05:00
|
|
|
import { useState, useEffect, memo, ReactNode } from 'react'
|
2021-07-26 14:44:00 +05:00
|
|
|
import {CaretUpOutlined, CaretDownOutlined, CaretRightOutlined} from '@ant-design/icons'
|
2021-05-24 15:19:38 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
import '@styles/display.css'
|
|
|
|
|
2022-04-29 16:39:49 +05:00
|
|
|
export const formatNumber = (value?: unknown, format?: number) =>
|
2021-07-26 14:44:00 +05:00
|
|
|
Number.isInteger(format) && Number.isFinite(value)
|
2022-04-29 16:39:49 +05:00
|
|
|
? Number(value).toFixed(format)
|
|
|
|
: Number(value).toPrecision(4)
|
2021-07-26 14:44:00 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
const iconStyle = { color:'#0008' }
|
|
|
|
const displayValueStyle = { display: 'flex', flexGrow: 1 }
|
|
|
|
|
2022-04-29 16:39:49 +05:00
|
|
|
export type ValueDisplayProps = {
|
|
|
|
prefix?: ReactNode
|
|
|
|
suffix?: ReactNode
|
|
|
|
format?: number | string
|
|
|
|
isArrowVisible?: boolean
|
|
|
|
enumeration?: Record<string, string>
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DisplayProps = ValueDisplayProps & {
|
|
|
|
className?: string
|
|
|
|
label?: ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ValueDisplay = memo<ValueDisplayProps>(({ prefix, value, suffix, isArrowVisible, format, enumeration }) => {
|
|
|
|
const [val, setVal] = useState<string>('---')
|
2022-01-24 21:16:50 +05:00
|
|
|
const [arrowState, setArrowState] = useState({
|
2021-07-26 14:44:00 +05:00
|
|
|
preVal: NaN,
|
|
|
|
preTimestamp: Date.now(),
|
|
|
|
direction: 0,
|
|
|
|
})
|
2021-05-24 17:57:13 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
useEffect(() => {
|
|
|
|
setVal((preVal) => {
|
|
|
|
if ((value ?? '-') === '-' || value === '--') return '---'
|
|
|
|
if (enumeration?.[value]) return enumeration[value]
|
2021-07-26 14:44:00 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
if (Number.isFinite(+value)) {
|
|
|
|
if (isArrowVisible && (arrowState.preTimestamp + 1000 < Date.now())) {
|
|
|
|
let direction = 0
|
2022-04-29 16:39:49 +05:00
|
|
|
if (+value > arrowState.preVal)
|
2022-01-24 21:16:50 +05:00
|
|
|
direction = 1
|
2022-04-29 16:39:49 +05:00
|
|
|
if (+value < arrowState.preVal)
|
2022-01-24 21:16:50 +05:00
|
|
|
direction = -1
|
2021-08-13 10:29:34 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
setArrowState({
|
2022-04-29 16:39:49 +05:00
|
|
|
preVal: +value,
|
2022-01-24 21:16:50 +05:00
|
|
|
preTimestamp: Date.now(),
|
|
|
|
direction: direction,
|
|
|
|
})
|
|
|
|
}
|
2021-07-26 14:44:00 +05:00
|
|
|
|
2022-04-29 16:39:49 +05:00
|
|
|
return formatNumber(value, Number(format))
|
2021-05-28 14:30:55 +05:00
|
|
|
}
|
2021-07-26 14:44:00 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
if (value.length > 4) {
|
|
|
|
const valueDate = moment(value)
|
|
|
|
if (valueDate.isValid())
|
2022-04-29 16:39:49 +05:00
|
|
|
return valueDate.format(String(format))
|
2021-08-12 17:47:16 +05:00
|
|
|
}
|
2021-05-28 14:30:55 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
return 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:
|
2022-01-24 21:16:50 +05:00
|
|
|
arrow = <CaretRightOutlined style={iconStyle}/>
|
2021-07-26 14:44:00 +05:00
|
|
|
break
|
|
|
|
case 1:
|
2022-01-24 21:16:50 +05:00
|
|
|
arrow = <CaretUpOutlined style={iconStyle}/>
|
2021-07-26 14:44:00 +05:00
|
|
|
break
|
|
|
|
case -1:
|
2022-01-24 21:16:50 +05:00
|
|
|
arrow = <CaretDownOutlined style={iconStyle}/>
|
2021-07-26 14:44:00 +05:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
2022-01-24 21:16:50 +05:00
|
|
|
}
|
2021-05-28 14:30:55 +05:00
|
|
|
|
2022-01-24 21:16:50 +05:00
|
|
|
return(
|
|
|
|
<span className={'display_value'}>
|
|
|
|
{prefix} {val} {suffix}{arrow}
|
|
|
|
</span>
|
|
|
|
)
|
2022-04-29 16:39:49 +05:00
|
|
|
})
|
2021-04-16 15:50:01 +05:00
|
|
|
|
2022-04-29 16:39:49 +05:00
|
|
|
export const Display = memo<DisplayProps>(({ className, label, ...other })=> (
|
2022-01-24 21:16:50 +05:00
|
|
|
<div className={className}>
|
|
|
|
<div className={'display_label'}>{label}</div>
|
|
|
|
<div style={displayValueStyle}>
|
|
|
|
<ValueDisplay {...other}/>
|
2021-04-16 15:50:01 +05:00
|
|
|
</div>
|
2022-01-24 21:16:50 +05:00
|
|
|
</div>
|
|
|
|
))
|