AMSHistoryModal.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { useState, useEffect } from 'react';
  2. import { useQuery } from '@tanstack/react-query';
  3. import { X, Droplets, Thermometer, TrendingUp, TrendingDown, Minus } from 'lucide-react';
  4. import {
  5. LineChart,
  6. Line,
  7. XAxis,
  8. YAxis,
  9. CartesianGrid,
  10. Tooltip,
  11. ResponsiveContainer,
  12. Legend,
  13. ReferenceLine,
  14. } from 'recharts';
  15. import { api, type AMSHistoryResponse } from '../api/client';
  16. import { parseUTCDate, applyTimeFormat, type TimeFormat } from '../utils/date';
  17. import { useTranslation } from 'react-i18next';
  18. import { useTheme } from '../contexts/ThemeContext';
  19. interface AMSHistoryModalProps {
  20. isOpen: boolean;
  21. onClose: () => void;
  22. printerId: number;
  23. printerName: string;
  24. amsId: number;
  25. amsLabel: string;
  26. initialMode?: 'humidity' | 'temperature';
  27. thresholds?: {
  28. humidityGood: number;
  29. humidityFair: number;
  30. tempGood: number;
  31. tempFair: number;
  32. };
  33. }
  34. type TimeRange = '6h' | '24h' | '48h' | '7d';
  35. const TIME_RANGES: { value: TimeRange; label: string; hours: number }[] = [
  36. { value: '6h', label: '6h', hours: 6 },
  37. { value: '24h', label: '24h', hours: 24 },
  38. { value: '48h', label: '48h', hours: 48 },
  39. { value: '7d', label: '7d', hours: 168 },
  40. ];
  41. export function AMSHistoryModal({
  42. isOpen,
  43. onClose,
  44. printerId,
  45. printerName,
  46. amsId,
  47. amsLabel,
  48. initialMode = 'humidity',
  49. thresholds,
  50. }: AMSHistoryModalProps) {
  51. const { t } = useTranslation();
  52. const { mode: themeMode } = useTheme();
  53. const [timeRange, setTimeRange] = useState<TimeRange>('24h');
  54. const [mode, setMode] = useState<'humidity' | 'temperature'>(initialMode);
  55. const isDark = themeMode === 'dark';
  56. const { data: settings } = useQuery({
  57. queryKey: ['settings'],
  58. queryFn: api.getSettings,
  59. });
  60. const timeFormat: TimeFormat = settings?.time_format || 'system';
  61. // Close on Escape key
  62. useEffect(() => {
  63. if (!isOpen) return;
  64. const handleKeyDown = (e: KeyboardEvent) => {
  65. if (e.key === 'Escape') onClose();
  66. };
  67. window.addEventListener('keydown', handleKeyDown);
  68. return () => window.removeEventListener('keydown', handleKeyDown);
  69. }, [isOpen, onClose]);
  70. const hours = TIME_RANGES.find(r => r.value === timeRange)?.hours || 24;
  71. const { data, isLoading, error } = useQuery<AMSHistoryResponse>({
  72. queryKey: ['ams-history', printerId, amsId, hours],
  73. queryFn: () => api.getAMSHistory(printerId, amsId, hours),
  74. enabled: isOpen,
  75. refetchInterval: 60000, // Refresh every minute
  76. });
  77. if (!isOpen) return null;
  78. // Format data for chart
  79. const rawPoints = data?.data.map(point => {
  80. const date = parseUTCDate(point.recorded_at) || new Date();
  81. return {
  82. time: date.getTime(),
  83. humidity: point.humidity,
  84. temperature: point.temperature,
  85. };
  86. }) || [];
  87. // Pad edges so the line extends across the full time window
  88. const domainStart = Date.now() - hours * 60 * 60 * 1000;
  89. const domainEnd = Date.now();
  90. const chartData = [...rawPoints];
  91. if (chartData.length > 0) {
  92. const first = chartData[0];
  93. if (first.time > domainStart) {
  94. chartData.unshift({ ...first, time: domainStart });
  95. }
  96. const last = chartData[chartData.length - 1];
  97. if (last.time < domainEnd) {
  98. chartData.push({ ...last, time: domainEnd });
  99. }
  100. }
  101. // Get thresholds
  102. const humidityGood = thresholds?.humidityGood || 40;
  103. const humidityFair = thresholds?.humidityFair || 60;
  104. const tempGood = thresholds?.tempGood || 30;
  105. const tempFair = thresholds?.tempFair || 35;
  106. // Current values (last data point)
  107. const lastPoint = chartData[chartData.length - 1];
  108. const currentHumidity = lastPoint?.humidity;
  109. const currentTemp = lastPoint?.temperature;
  110. // Trend calculation (compare first and last 20% of data)
  111. const getTrend = (values: (number | null)[]) => {
  112. const filtered = values.filter((v): v is number => v != null);
  113. if (filtered.length < 4) return 'stable';
  114. const firstQuarter = filtered.slice(0, Math.floor(filtered.length / 4));
  115. const lastQuarter = filtered.slice(-Math.floor(filtered.length / 4));
  116. const firstAvg = firstQuarter.reduce((a, b) => a + b, 0) / firstQuarter.length;
  117. const lastAvg = lastQuarter.reduce((a, b) => a + b, 0) / lastQuarter.length;
  118. const diff = lastAvg - firstAvg;
  119. if (Math.abs(diff) < 2) return 'stable';
  120. return diff > 0 ? 'up' : 'down';
  121. };
  122. const humidityTrend = getTrend(chartData.map(d => d.humidity));
  123. const tempTrend = getTrend(chartData.map(d => d.temperature));
  124. const TrendIcon = ({ trend }: { trend: string }) => {
  125. if (trend === 'up') return <TrendingUp className="w-4 h-4 text-red-400" />;
  126. if (trend === 'down') return <TrendingDown className="w-4 h-4 text-green-400" />;
  127. return <Minus className="w-4 h-4 text-gray-400 dark:text-bambu-gray" />;
  128. };
  129. // Get status color for current value
  130. const getHumidityColor = (value: number | undefined | null) => {
  131. if (value == null) return '#9ca3af';
  132. if (value <= humidityGood) return '#22a352';
  133. if (value <= humidityFair) return '#d4a017';
  134. return '#c62828';
  135. };
  136. const getTempColor = (value: number | undefined | null) => {
  137. if (value == null) return '#9ca3af';
  138. if (value <= tempGood) return '#22a352';
  139. if (value <= tempFair) return '#d4a017';
  140. return '#c62828';
  141. };
  142. // Theme-aware styles (using isDark since dark: prefix doesn't work in portals)
  143. const modalBg = isDark ? '#2d2d2d' : '#ffffff';
  144. const cardBg = isDark ? '#1d1d1d' : '#f3f4f6';
  145. const borderColor = isDark ? '#3d3d3d' : '#e5e7eb';
  146. const textPrimary = isDark ? '#ffffff' : '#111827';
  147. const textSecondary = isDark ? '#9ca3af' : '#4b5563';
  148. return (
  149. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50" onClick={onClose}>
  150. <div
  151. className="rounded-xl w-full max-w-4xl max-h-[90vh] overflow-hidden shadow-xl"
  152. style={{ backgroundColor: modalBg }}
  153. onClick={e => e.stopPropagation()}
  154. >
  155. {/* Header */}
  156. <div
  157. className="flex items-center justify-between px-6 py-4 border-b"
  158. style={{ borderColor }}
  159. >
  160. <div>
  161. <h2 className="text-lg font-semibold" style={{ color: textPrimary }}>
  162. {amsLabel} {t('common.history', 'History')}
  163. </h2>
  164. <p className="text-sm" style={{ color: textSecondary }}>{printerName}</p>
  165. </div>
  166. <button
  167. onClick={onClose}
  168. className="p-2 rounded-lg transition-colors"
  169. style={{ color: textSecondary }}
  170. >
  171. <X className="w-5 h-5" />
  172. </button>
  173. </div>
  174. {/* Content */}
  175. <div className="p-6 space-y-6 overflow-y-auto max-h-[calc(90vh-80px)]">
  176. {/* Time Range & Mode Selector */}
  177. <div className="flex items-center justify-between max-[550px]:flex-col max-[550px]:items-start max-[550px]:gap-3">
  178. <div className="inline-flex gap-1 rounded-lg p-1 max-w-full flex-wrap w-fit" style={{ backgroundColor: cardBg }}>
  179. <button
  180. onClick={() => setMode('humidity')}
  181. className={`flex items-center gap-2 px-3 py-1.5 text-sm rounded-md transition-colors ${
  182. mode === 'humidity' ? 'bg-blue-600 text-white' : ''
  183. }`}
  184. style={mode !== 'humidity' ? { color: textSecondary } : undefined}
  185. >
  186. <Droplets className="w-4 h-4" />
  187. {t('common.humidity', 'Humidity')}
  188. </button>
  189. <button
  190. onClick={() => setMode('temperature')}
  191. className={`flex items-center gap-2 px-3 py-1.5 text-sm rounded-md transition-colors ${
  192. mode === 'temperature' ? 'bg-orange-600 text-white' : ''
  193. }`}
  194. style={mode !== 'temperature' ? { color: textSecondary } : undefined}
  195. >
  196. <Thermometer className="w-4 h-4" />
  197. {t('common.temperature', 'Temperature')}
  198. </button>
  199. </div>
  200. <div className="inline-flex gap-1 rounded-lg p-1 max-w-full flex-wrap w-fit" style={{ backgroundColor: cardBg }}>
  201. {TIME_RANGES.map(range => (
  202. <button
  203. key={range.value}
  204. onClick={() => setTimeRange(range.value)}
  205. className={`px-3 py-1 text-sm rounded-md transition-colors ${
  206. timeRange === range.value ? 'bg-bambu-green text-white' : ''
  207. }`}
  208. style={timeRange !== range.value ? { color: textSecondary } : undefined}
  209. >
  210. {range.label}
  211. </button>
  212. ))}
  213. </div>
  214. </div>
  215. {/* Stats Cards */}
  216. <div className="grid grid-cols-4 gap-4 max-[550px]:grid-cols-2">
  217. {mode === 'humidity' ? (
  218. <>
  219. <div className="rounded-lg p-4 max-[550px]:order-2" style={{ backgroundColor: cardBg }}>
  220. <p className="text-xs" style={{ color: textSecondary }}>{t('common.current', 'Current')}</p>
  221. <div className="flex items-center gap-2">
  222. <p className="text-2xl font-bold" style={{ color: getHumidityColor(currentHumidity) }}>
  223. {currentHumidity != null ? `${currentHumidity}%` : '—'}
  224. </p>
  225. <TrendIcon trend={humidityTrend} />
  226. </div>
  227. </div>
  228. <div className="rounded-lg p-4 max-[550px]:order-4" style={{ backgroundColor: cardBg }}>
  229. <p className="text-xs" style={{ color: textSecondary }}>{t('common.average', 'Average')}</p>
  230. <p className="text-2xl font-bold" style={{ color: textPrimary }}>
  231. {data?.avg_humidity != null ? `${data.avg_humidity}%` : '—'}
  232. </p>
  233. </div>
  234. <div className="rounded-lg p-4 max-[550px]:order-1" style={{ backgroundColor: cardBg }}>
  235. <p className="text-xs" style={{ color: textSecondary }}>{t('common.min', 'Min')}</p>
  236. <p className="text-2xl font-bold text-green-500">
  237. {data?.min_humidity != null ? `${data.min_humidity}%` : '—'}
  238. </p>
  239. </div>
  240. <div className="rounded-lg p-4 max-[550px]:order-3" style={{ backgroundColor: cardBg }}>
  241. <p className="text-xs" style={{ color: textSecondary }}>{t('common.max', 'Max')}</p>
  242. <p className="text-2xl font-bold text-red-500">
  243. {data?.max_humidity != null ? `${data.max_humidity}%` : '—'}
  244. </p>
  245. </div>
  246. </>
  247. ) : (
  248. <>
  249. <div className="rounded-lg p-4 max-[550px]:order-2" style={{ backgroundColor: cardBg }}>
  250. <p className="text-xs" style={{ color: textSecondary }}>{t('common.current', 'Current')}</p>
  251. <div className="flex items-center gap-2">
  252. <p className="text-2xl font-bold" style={{ color: getTempColor(currentTemp) }}>
  253. {currentTemp != null ? `${currentTemp}°C` : '—'}
  254. </p>
  255. <TrendIcon trend={tempTrend} />
  256. </div>
  257. </div>
  258. <div className="rounded-lg p-4 max-[550px]:order-4" style={{ backgroundColor: cardBg }}>
  259. <p className="text-xs" style={{ color: textSecondary }}>{t('common.average', 'Average')}</p>
  260. <p className="text-2xl font-bold" style={{ color: textPrimary }}>
  261. {data?.avg_temperature != null ? `${data.avg_temperature}°C` : '—'}
  262. </p>
  263. </div>
  264. <div className="rounded-lg p-4 max-[550px]:order-1" style={{ backgroundColor: cardBg }}>
  265. <p className="text-xs" style={{ color: textSecondary }}>{t('common.min', 'Min')}</p>
  266. <p className="text-2xl font-bold text-blue-500">
  267. {data?.min_temperature != null ? `${data.min_temperature}°C` : '—'}
  268. </p>
  269. </div>
  270. <div className="rounded-lg p-4 max-[550px]:order-3" style={{ backgroundColor: cardBg }}>
  271. <p className="text-xs" style={{ color: textSecondary }}>{t('common.max', 'Max')}</p>
  272. <p className="text-2xl font-bold text-red-500">
  273. {data?.max_temperature != null ? `${data.max_temperature}°C` : '—'}
  274. </p>
  275. </div>
  276. </>
  277. )}
  278. </div>
  279. {/* Chart */}
  280. <div className="rounded-lg p-4" style={{ backgroundColor: cardBg }}>
  281. {isLoading ? (
  282. <div className="h-[300px] flex items-center justify-center" style={{ color: textSecondary }}>
  283. {t('common.loading', 'Loading...')}
  284. </div>
  285. ) : error ? (
  286. <div className="h-[300px] flex items-center justify-center text-red-500">
  287. {t('common.error', 'Error loading data')}
  288. </div>
  289. ) : chartData.length === 0 ? (
  290. <div className="h-[300px] flex items-center justify-center" style={{ color: textSecondary }}>
  291. {t('common.noData', 'No data available for this time range')}
  292. </div>
  293. ) : (
  294. <ResponsiveContainer width="100%" height={300}>
  295. <LineChart data={chartData}>
  296. <CartesianGrid strokeDasharray="3 3" stroke={isDark ? '#3d3d3d' : '#e5e7eb'} />
  297. <XAxis
  298. dataKey="time"
  299. type="number"
  300. domain={[Date.now() - hours * 60 * 60 * 1000, Date.now()]}
  301. tickFormatter={(ts) => {
  302. const date = new Date(ts);
  303. if (hours > 24) {
  304. return date.toLocaleDateString([], { day: 'numeric', month: 'short' });
  305. }
  306. return date.toLocaleTimeString([], applyTimeFormat({ hour: '2-digit', minute: '2-digit' }, timeFormat));
  307. }}
  308. stroke={isDark ? '#9ca3af' : '#6b7280'}
  309. tick={{ fontSize: 12 }}
  310. />
  311. <YAxis
  312. stroke={isDark ? '#9ca3af' : '#6b7280'}
  313. tick={{ fontSize: 12 }}
  314. domain={mode === 'humidity' ? [0, 100] : ['auto', 'auto']}
  315. tickFormatter={(value) => mode === 'humidity' ? `${value}%` : `${value}°C`}
  316. />
  317. <Tooltip
  318. contentStyle={{
  319. backgroundColor: isDark ? '#2d2d2d' : '#ffffff',
  320. border: `1px solid ${isDark ? '#3d3d3d' : '#e5e7eb'}`,
  321. borderRadius: '8px',
  322. color: isDark ? '#fff' : '#000',
  323. }}
  324. labelFormatter={(ts) => new Date(ts).toLocaleString(undefined, applyTimeFormat({
  325. year: 'numeric',
  326. month: 'short',
  327. day: 'numeric',
  328. hour: '2-digit',
  329. minute: '2-digit',
  330. }, timeFormat))}
  331. formatter={(value) => [
  332. mode === 'humidity' ? `${value ?? 0}%` : `${value ?? 0}°C`,
  333. mode === 'humidity' ? 'Humidity' : 'Temperature'
  334. ]}
  335. />
  336. <Legend />
  337. {/* Threshold lines */}
  338. {mode === 'humidity' ? (
  339. <>
  340. <ReferenceLine y={humidityGood} stroke="#22a352" strokeDasharray="5 5" label={{ value: 'Good', fill: '#22a352', fontSize: 10 }} />
  341. <ReferenceLine y={humidityFair} stroke="#d4a017" strokeDasharray="5 5" label={{ value: 'Fair', fill: '#d4a017', fontSize: 10 }} />
  342. </>
  343. ) : (
  344. <>
  345. <ReferenceLine y={tempGood} stroke="#22a352" strokeDasharray="5 5" label={{ value: 'Good', fill: '#22a352', fontSize: 10 }} />
  346. <ReferenceLine y={tempFair} stroke="#d4a017" strokeDasharray="5 5" label={{ value: 'Fair', fill: '#d4a017', fontSize: 10 }} />
  347. </>
  348. )}
  349. <Line
  350. type="monotone"
  351. dataKey={mode}
  352. name={mode === 'humidity' ? 'Humidity' : 'Temperature'}
  353. stroke={mode === 'humidity' ? '#3b82f6' : '#f97316'}
  354. strokeWidth={2}
  355. dot={false}
  356. activeDot={{ r: 4 }}
  357. connectNulls={true}
  358. />
  359. </LineChart>
  360. </ResponsiveContainer>
  361. )}
  362. </div>
  363. {/* Info */}
  364. <div className="text-xs text-center" style={{ color: textSecondary }}>
  365. {t('amsHistory.recordingInfo', 'Data is recorded every 5 minutes while the printer is connected')}
  366. </div>
  367. </div>
  368. </div>
  369. </div>
  370. );
  371. }