246 lines
8.1 KiB
TypeScript
246 lines
8.1 KiB
TypeScript
import React from 'react';
|
|
import {
|
|
CartesianGrid,
|
|
ComposedChart,
|
|
Line,
|
|
LineChart,
|
|
ResponsiveContainer,
|
|
Tooltip,
|
|
XAxis,
|
|
YAxis,
|
|
} from 'recharts';
|
|
import { isDeviceHistoryMetric } from './deviceHistoryUtils.ts';
|
|
import { IOT_HISTORY_METRIC_META } from './iotHistoryMetricMeta.ts';
|
|
import type { IotDeviceHistoryPoint, IotHistoryMetric, IotSensorHistoryPoint } from './types.ts';
|
|
|
|
export interface IotSensorHistoryModalProps {
|
|
open: boolean;
|
|
metric: IotHistoryMetric | null;
|
|
onClose: () => void;
|
|
points: IotSensorHistoryPoint[];
|
|
deviceHistoryPoints?: IotDeviceHistoryPoint[];
|
|
/** @deprecated Use {@link deviceHistoryPoints} */
|
|
fanHistoryPoints?: IotDeviceHistoryPoint[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
const timeTick = (ms: number) =>
|
|
new Date(ms).toLocaleString('id-ID', {
|
|
timeZone: 'Asia/Jakarta',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false,
|
|
});
|
|
|
|
const timeTooltipLabel = (ms: number) =>
|
|
new Date(ms).toLocaleString('id-ID', {
|
|
timeZone: 'Asia/Jakarta',
|
|
dateStyle: 'medium',
|
|
timeStyle: 'short',
|
|
});
|
|
|
|
const IotSensorHistoryModal: React.FC<IotSensorHistoryModalProps> = ({
|
|
open,
|
|
metric,
|
|
onClose,
|
|
points,
|
|
deviceHistoryPoints: deviceHistoryPointsProp,
|
|
fanHistoryPoints,
|
|
loading,
|
|
error,
|
|
}) => {
|
|
if (!open || !metric) return null;
|
|
|
|
const meta = IOT_HISTORY_METRIC_META[metric];
|
|
const isDeviceChart = isDeviceHistoryMetric(metric);
|
|
const deviceHistoryPoints = deviceHistoryPointsProp ?? fanHistoryPoints ?? [];
|
|
|
|
const chartData = isDeviceChart
|
|
? deviceHistoryPoints.map((p) => ({
|
|
t: p.atMs,
|
|
setpoint: p.setpoint === null ? null : Number(p.setpoint.toFixed(4)),
|
|
status: p.status === null ? null : p.status,
|
|
}))
|
|
: points.map((p) => ({
|
|
t: p.atMs,
|
|
y: Number(p.value.toFixed(4)),
|
|
}));
|
|
|
|
const hasData = chartData.length > 0;
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4"
|
|
role="dialog"
|
|
aria-modal="true"
|
|
aria-labelledby="iot-sensor-history-title"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className="max-h-[90vh] w-full max-w-3xl overflow-auto rounded-xl border border-slate-200 bg-white p-4 shadow-xl"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="mb-3 flex items-start justify-between gap-3">
|
|
<div>
|
|
<h2 id="iot-sensor-history-title" className="text-lg font-semibold text-slate-800">
|
|
{meta.title}
|
|
</h2>
|
|
<p className="mt-1 text-sm text-slate-500">{meta.subtitle}</p>
|
|
{isDeviceChart && (
|
|
<p className="mt-2 flex flex-wrap gap-3 text-xs text-slate-600">
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="inline-block h-0.5 w-4 bg-blue-600" aria-hidden="true" />
|
|
Set point (°C)
|
|
</span>
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="inline-block h-0.5 w-4 bg-emerald-600" aria-hidden="true" />
|
|
Status (Aktif / Mati)
|
|
</span>
|
|
</p>
|
|
)}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="rounded-lg border border-slate-200 px-3 py-1.5 text-sm font-medium text-slate-600 hover:bg-slate-50"
|
|
onClick={onClose}
|
|
>
|
|
Tutup
|
|
</button>
|
|
</div>
|
|
|
|
{loading && <div className="py-16 text-center text-slate-500">Memuat data…</div>}
|
|
{!loading && error && (
|
|
<div className="rounded-lg bg-red-50 px-3 py-2 text-sm text-red-700">{error}</div>
|
|
)}
|
|
{!loading && !error && !hasData && (
|
|
<div className="py-16 text-center text-slate-500">
|
|
Tidak ada data dalam 24 jam terakhir.
|
|
</div>
|
|
)}
|
|
{!loading && !error && hasData && isDeviceChart && (
|
|
<div className="h-80 w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<ComposedChart data={chartData} margin={{ top: 8, right: 48, left: 0, bottom: 8 }}>
|
|
<CartesianGrid strokeDasharray="3 3" stroke="#e5e7eb" />
|
|
<XAxis
|
|
dataKey="t"
|
|
type="number"
|
|
domain={['dataMin', 'dataMax']}
|
|
tickFormatter={timeTick}
|
|
stroke="#64748b"
|
|
fontSize={11}
|
|
minTickGap={28}
|
|
/>
|
|
<YAxis
|
|
yAxisId="temp"
|
|
stroke="#2563eb"
|
|
fontSize={11}
|
|
width={48}
|
|
tickFormatter={(v) => `${v}`}
|
|
label={{
|
|
value: '°C',
|
|
angle: -90,
|
|
position: 'insideLeft',
|
|
fill: '#2563eb',
|
|
fontSize: 11,
|
|
}}
|
|
/>
|
|
<YAxis
|
|
yAxisId="status"
|
|
orientation="right"
|
|
domain={[0, 1]}
|
|
ticks={[0, 1]}
|
|
stroke="#16a34a"
|
|
fontSize={11}
|
|
width={44}
|
|
tickFormatter={(v) => (Number(v) === 1 ? 'Aktif' : 'Mati')}
|
|
/>
|
|
<Tooltip
|
|
labelFormatter={(ms) => timeTooltipLabel(ms as number)}
|
|
formatter={(value: number, name: string) => {
|
|
if (name === 'Status') {
|
|
return [Number(value) === 1 ? 'Aktif' : 'Mati', name];
|
|
}
|
|
if (value === null || value === undefined || !Number.isFinite(Number(value))) {
|
|
return ['—', name];
|
|
}
|
|
return [`${Number(value).toFixed(1)} °C`, name];
|
|
}}
|
|
/>
|
|
<Line
|
|
yAxisId="temp"
|
|
type="monotone"
|
|
dataKey="setpoint"
|
|
stroke="#2563eb"
|
|
strokeWidth={2}
|
|
dot={false}
|
|
connectNulls
|
|
name="Set point"
|
|
/>
|
|
<Line
|
|
yAxisId="status"
|
|
type="stepAfter"
|
|
dataKey="status"
|
|
stroke="#16a34a"
|
|
strokeWidth={2}
|
|
dot={false}
|
|
connectNulls
|
|
name="Status"
|
|
/>
|
|
</ComposedChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
)}
|
|
{!loading && !error && hasData && !isDeviceChart && (
|
|
<div className="h-80 w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<LineChart data={chartData} margin={{ top: 8, right: 12, left: 0, bottom: 8 }}>
|
|
<CartesianGrid strokeDasharray="3 3" stroke="#e5e7eb" />
|
|
<XAxis
|
|
dataKey="t"
|
|
type="number"
|
|
domain={['dataMin', 'dataMax']}
|
|
tickFormatter={timeTick}
|
|
stroke="#64748b"
|
|
fontSize={11}
|
|
minTickGap={28}
|
|
/>
|
|
<YAxis
|
|
stroke="#64748b"
|
|
fontSize={11}
|
|
width={48}
|
|
tickFormatter={(v) => `${v}`}
|
|
label={{
|
|
value: meta.yAxisLabel,
|
|
angle: -90,
|
|
position: 'insideLeft',
|
|
fill: '#64748b',
|
|
fontSize: 11,
|
|
}}
|
|
/>
|
|
<Tooltip
|
|
formatter={(value: number) => [meta.formatTooltip(value), meta.seriesName]}
|
|
labelFormatter={(ms) => timeTooltipLabel(ms as number)}
|
|
/>
|
|
<Line
|
|
type="monotone"
|
|
dataKey="y"
|
|
stroke={meta.stroke}
|
|
strokeWidth={2}
|
|
dot={false}
|
|
name={meta.seriesName}
|
|
/>
|
|
</LineChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default IotSensorHistoryModal;
|