optimization
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
This commit is contained in:
58
src/features/daily-report/hooks/useTelemetryCharts.ts
Normal file
58
src/features/daily-report/hooks/useTelemetryCharts.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
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(
|
||||
() => filteredTelemetry.map(t => t.label),
|
||||
[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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user