useTimeseriesUnixToIso.ts25 lines · main
| 1 | import { useMemo } from 'react' |
| 2 | |
| 3 | import { |
| 4 | isUnixMicro, |
| 5 | unixMicroToIsoTimestamp, |
| 6 | } from '@/components/interfaces/Settings/Logs/Logs.utils' |
| 7 | |
| 8 | /** |
| 9 | * Convenience hook for converting timeseries timestamp from unix microsecond to iso |
| 10 | * |
| 11 | * memoized |
| 12 | */ |
| 13 | const useTimeseriesUnixToIso = (data: any[], timestampKey: string) => { |
| 14 | return useMemo(() => { |
| 15 | // check if need to convert or not |
| 16 | if (data.length === 0) return data |
| 17 | if (!isUnixMicro(data[0][timestampKey])) return data |
| 18 | |
| 19 | return data?.map((d) => { |
| 20 | d[timestampKey] = unixMicroToIsoTimestamp(d[timestampKey]) |
| 21 | return d |
| 22 | }) |
| 23 | }, [JSON.stringify(data)]) |
| 24 | } |
| 25 | export default useTimeseriesUnixToIso |