Files
dashboard/components/iotPanel/IotPanelEnvironmentColumn.tsx
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

239 lines
9.1 KiB
TypeScript

import React from 'react';
import { SENSOR_ICON_HUMIDITY, SENSOR_ICON_WATER, baseCard, sensorCard } from './constants.tsx';
import IotPanelExperienceTempCard from './IotPanelExperienceTempCard.tsx';
import type { IotComputedDashboard, IotHistoryMetric } from './types.ts';
type SensorTile =
| { iconEmoji: string; label: string; value: string; historyMetric?: IotHistoryMetric }
| { iconClass: string; label: string; value: string; historyMetric?: IotHistoryMetric }
| { iconText: string; label: string; value: string; historyMetric?: IotHistoryMetric };
export interface IotPanelEnvironmentColumnProps {
computed: IotComputedDashboard;
onSensorHistoryClick?: (metric: IotHistoryMetric) => void;
sensorHistoryLoading?: boolean;
}
const IotPanelEnvironmentColumn: React.FC<IotPanelEnvironmentColumnProps> = ({
computed,
onSensorHistoryClick,
sensorHistoryLoading = false,
}) => {
const sensorTiles: SensorTile[] = [
{
iconEmoji: SENSOR_ICON_HUMIDITY,
label: 'Kelembapan',
value: computed.humidity,
historyMetric: 'humidity',
},
{
iconClass: 'fa-solid fa-temperature-quarter text-orange-500',
label: 'Suhu Luar',
value: computed.outsideTemp,
historyMetric: 'outsideTemp',
},
{ iconClass: 'fa-solid fa-dove text-amber-600', label: 'Umur Ayam', value: computed.dayAge },
{ iconText: 'NH3', label: 'NH3', value: computed.ammonia, historyMetric: 'ammonia' },
{ iconText: 'CO2', label: 'CO2', value: computed.co2, historyMetric: 'co2' },
{ iconEmoji: SENSOR_ICON_WATER, label: 'Air', value: computed.water, historyMetric: 'water' },
{
iconClass: 'fa-solid fa-wind text-indigo-600',
label: 'Angin',
value: computed.wind,
historyMetric: 'wind',
},
{
iconClass: 'fa-solid fa-sun text-yellow-500',
label: 'Lux',
value: computed.lux,
historyMetric: 'lux',
},
];
const tempCells: {
label: string;
value: string;
metric: IotHistoryMetric;
alarmHighlight: boolean;
}[] = [
{
label: '1',
value: computed.temp1,
metric: 'temp1',
alarmHighlight: computed.temp1AlarmHighlight,
},
{
label: '2',
value: computed.temp2,
metric: 'temp2',
alarmHighlight: computed.temp2AlarmHighlight,
},
{
label: '3',
value: computed.temp3,
metric: 'temp3',
alarmHighlight: computed.temp3AlarmHighlight,
},
];
const tempValueClass = (alarmHighlight: boolean) =>
`text-2xl font-semibold ${alarmHighlight ? 'text-red-600' : ''}`;
const avgTempValueClass = computed.avgTempAlarmHighlight
? 'mt-2 text-3xl font-bold text-red-600'
: 'mt-2 text-3xl font-bold text-slate-700';
return (
<section className="space-y-3">
<article className={baseCard}>
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
<section>
<button
type="button"
className="h-full w-full rounded-lg bg-red-50/40 p-3 text-left outline-none ring-red-500/30 transition hover:bg-red-50/70 focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-60"
onClick={() => onSensorHistoryClick?.('avgTemp')}
disabled={!onSensorHistoryClick || sensorHistoryLoading}
aria-busy={sensorHistoryLoading}
aria-label="Buka riwayat suhu rata-rata 24 jam"
>
<h3 className="text-base font-semibold">
<i
className="fa-solid fa-temperature-half mr-2 text-red-500"
aria-hidden="true"
></i>
Suhu Rata-Rata
</h3>
<p className={avgTempValueClass}>{computed.avgTemp}</p>
</button>
</section>
<section>
{onSensorHistoryClick ? (
<button
type="button"
className="h-full w-full rounded-lg bg-emerald-50/40 p-3 text-left outline-none ring-emerald-500/30 transition hover:bg-emerald-50/70 focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-60"
onClick={() => onSensorHistoryClick('setPoint')}
disabled={sensorHistoryLoading}
aria-busy={sensorHistoryLoading}
aria-label="Buka riwayat suhu set point 24 jam"
>
<h3 className="text-base font-semibold">
<i className="fa-solid fa-temperature-half mr-2 text-emerald-600"></i>
Suhu Set Point
</h3>
<p className="mt-2 text-3xl font-bold text-emerald-600">{computed.setPoint}</p>
</button>
) : (
<div className="h-full rounded-lg bg-emerald-50/40 p-3">
<h3 className="text-base font-semibold">
<i className="fa-solid fa-temperature-half mr-2 text-emerald-600"></i>
Suhu Set Point
</h3>
<p className="mt-2 text-3xl font-bold text-emerald-600">{computed.setPoint}</p>
</div>
)}
</section>
</div>
<div className="mt-3 grid grid-cols-3 gap-2">
{tempCells.map((item) =>
onSensorHistoryClick ? (
<button
key={item.label}
type="button"
className="rounded-lg border border-slate-200 bg-slate-50 p-2 text-center outline-none ring-slate-400/30 transition hover:bg-slate-100 focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-60"
onClick={() => onSensorHistoryClick(item.metric)}
disabled={sensorHistoryLoading}
aria-busy={sensorHistoryLoading}
aria-label={`Buka riwayat suhu sensor ${item.label} (24 jam)`}
>
<h4 className="text-sm text-slate-500">
<span className="inline-flex items-center justify-center gap-1">
<i className="fa-solid fa-temperature-half" aria-hidden="true"></i>
<span>{item.label}</span>
</span>
</h4>
<p className={tempValueClass(item.alarmHighlight)}>{item.value}</p>
</button>
) : (
<section
key={item.label}
className="rounded-lg border border-slate-200 bg-slate-50 p-2 text-center"
>
<h4 className="text-sm text-slate-500">
<span className="inline-flex items-center justify-center gap-1">
<i className="fa-solid fa-temperature-half" aria-hidden="true"></i>
<span>{item.label}</span>
</span>
</h4>
<p className={tempValueClass(item.alarmHighlight)}>{item.value}</p>
</section>
)
)}
</div>
</article>
<article className={baseCard}>
<div className="grid grid-cols-2 gap-2 lg:grid-cols-3">
{sensorTiles.map((item, idx) => {
const metric = item.historyMetric;
const clickable = Boolean(metric && onSensorHistoryClick);
const inner = (
<>
<h4 className="text-sm text-slate-500">
{'iconEmoji' in item && item.iconEmoji ? (
<span className="mr-1 inline-block text-base leading-none" aria-hidden="true">
{item.iconEmoji}
</span>
) : 'iconText' in item && item.iconText ? (
<span className="mr-1 inline-block font-semibold text-slate-600">
{item.iconText}
</span>
) : (
<i className={`${(item as { iconClass: string }).iconClass} mr-1`}></i>
)}
</h4>
<p
className={`mt-2 text-2xl font-semibold ${
'historyMetric' in item &&
item.historyMetric === 'humidity' &&
computed.humidityAlarmHighlight
? 'text-red-600'
: 'text-slate-700'
}`}
>
{item.value}
</p>
</>
);
if (!clickable) {
return (
<article key={`${item.label}-${idx}`} className={sensorCard}>
{inner}
</article>
);
}
return (
<button
key={`${item.label}-${idx}`}
type="button"
className={`${sensorCard} w-full cursor-pointer text-left outline-none ring-slate-400/30 transition hover:bg-slate-50 focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-60`}
onClick={() => onSensorHistoryClick?.(metric!)}
disabled={sensorHistoryLoading}
aria-busy={sensorHistoryLoading}
aria-label={`Buka riwayat ${item.label} 24 jam`}
>
{inner}
</button>
);
})}
</div>
</article>
<IotPanelExperienceTempCard computed={computed} />
</section>
);
};
export default IotPanelEnvironmentColumn;