Files
GreenHomeUI/src/features/daily-report/hooks/useTelemetryCharts.ts
alireza 3644d57206
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
optimize
2025-12-20 01:50:14 +03:30

64 lines
1.7 KiB
TypeScript

import { useMemo } from 'react'
import { NormalizedTelemetry, ChartConfig, ChartData } from '../types'
import { DataGap } from '../utils'
import { insertGapsInData, calcMinMax } from '../utils'
import { BASE_CHART_CONFIGS } from '../chart-config'
type UseTelemetryChartsParams = {
filteredTelemetry: NormalizedTelemetry[]
filteredDataGaps: DataGap[]
}
export function useTelemetryCharts({
filteredTelemetry,
filteredDataGaps,
}: UseTelemetryChartsParams) {
const timestamps = useMemo(
() => filteredTelemetry.map(t => t.timestamp),
[filteredTelemetry]
)
const chartLabels = useMemo(
() => {
const labels = filteredTelemetry
.map(t => t.label)
return labels
},
[filteredTelemetry]
)
const charts = useMemo(() => {
return BASE_CHART_CONFIGS.map((cfg): ChartData => {
const raw = filteredTelemetry.map(cfg.getValue)
const data = insertGapsInData(raw, timestamps, filteredDataGaps)
let yAxisMin = cfg.yAxisMin
let yAxisMax = cfg.yAxisMax
if (cfg.key === 'temp') {
const mm = calcMinMax(data, 0, 40, 10)
yAxisMin = mm.min
yAxisMax = mm.max
} else if (cfg.key === 'lux') {
const mm = calcMinMax(data, 0, 2000, 1000)
yAxisMin = mm.min
yAxisMax = mm.max
}
return {
...cfg,
data,
yAxisMin,
yAxisMax: yAxisMax ?? yAxisMin,
}
})
}, [filteredTelemetry, timestamps, filteredDataGaps])
return {
charts,
chartLabels,
}
}