SpoolBuddySettingsPage.tsx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. import { useState, useCallback, useRef, useEffect } from 'react';
  2. import { useQuery } from '@tanstack/react-query';
  3. import { useOutletContext } from 'react-router-dom';
  4. import { useTranslation } from 'react-i18next';
  5. import type { SpoolBuddyOutletContext } from '../../components/spoolbuddy/SpoolBuddyLayout';
  6. import { spoolbuddyApi, type SpoolBuddyDevice } from '../../api/client';
  7. import { DiagnosticModal } from '../../components/spoolbuddy/DiagnosticModal';
  8. import { FileText, Wand2, Zap } from 'lucide-react';
  9. function formatUptime(seconds: number): string {
  10. if (seconds < 60) return `${seconds}s`;
  11. if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
  12. const h = Math.floor(seconds / 3600);
  13. const m = Math.floor((seconds % 3600) / 60);
  14. return `${h}h ${m}m`;
  15. }
  16. function formatDateTime(iso: string | null): string {
  17. if (!iso) return '-';
  18. try {
  19. const d = new Date(iso);
  20. return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) + ' ' +
  21. d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' });
  22. } catch {
  23. return '-';
  24. }
  25. }
  26. const BLANK_OPTIONS = [
  27. { label: 'Off', value: 0 },
  28. { label: '1m', value: 60 },
  29. { label: '2m', value: 120 },
  30. { label: '5m', value: 300 },
  31. { label: '10m', value: 600 },
  32. { label: '30m', value: 1800 },
  33. ];
  34. // --- Device Tab ---
  35. function DeviceTab({ device }: { device: SpoolBuddyDevice }) {
  36. const { t } = useTranslation();
  37. const [diagnosticOpen, setDiagnosticOpen] = useState<'nfc' | 'scale' | 'read_tag' | null>(null);
  38. const [backendUrl, setBackendUrl] = useState('');
  39. const [apiToken, setApiToken] = useState('');
  40. const [systemBusy, setSystemBusy] = useState(false);
  41. const [systemMsg, setSystemMsg] = useState<{ type: 'ok' | 'error'; text: string } | null>(null);
  42. useEffect(() => {
  43. if (!backendUrl && device.backend_url) {
  44. setBackendUrl(device.backend_url);
  45. }
  46. }, [device.backend_url, backendUrl]);
  47. const saveConfig = async () => {
  48. if (!backendUrl.trim()) {
  49. setSystemMsg({ type: 'error', text: t('spoolbuddy.settings.systemFieldsRequired', 'Backend URL is required.') });
  50. return;
  51. }
  52. setSystemBusy(true);
  53. setSystemMsg(null);
  54. try {
  55. await spoolbuddyApi.updateSystemConfig(
  56. device.device_id,
  57. backendUrl.trim(),
  58. apiToken.trim() || undefined
  59. );
  60. setSystemMsg({ type: 'ok', text: t('spoolbuddy.settings.systemQueued', 'Config queued.') });
  61. } catch (e) {
  62. setSystemMsg({ type: 'error', text: e instanceof Error ? e.message : t('common.error', 'Error') });
  63. } finally {
  64. setSystemBusy(false);
  65. }
  66. };
  67. return (
  68. <div className="space-y-4">
  69. {/* About */}
  70. <div className="bg-zinc-800 rounded-lg p-4">
  71. <div className="flex items-center gap-3 mb-2">
  72. <img src="/img/spoolbuddy_logo_dark_small.png" alt="SpoolBuddy" className="h-7 w-auto" />
  73. </div>
  74. <p className="text-xs text-zinc-500 mb-1">Part of Bambuddy</p>
  75. <span className="text-xs text-zinc-500">github.com/maziggy/bambuddy</span>
  76. </div>
  77. {/* NFC Reader + Device Info side by side */}
  78. <div className="grid grid-cols-2 gap-3">
  79. {/* NFC Reader */}
  80. <div className="bg-zinc-800 rounded-lg p-3">
  81. <h3 className="text-sm font-semibold text-zinc-300 mb-2">
  82. {t('spoolbuddy.settings.nfcReader', 'NFC Reader')}
  83. </h3>
  84. <div className="space-y-1.5 text-xs">
  85. <div className="flex justify-between">
  86. <span className="text-zinc-500">{t('spoolbuddy.settings.type', 'Type')}</span>
  87. <span className="text-zinc-300 font-mono">
  88. {device.nfc_reader_type || 'N/A'}
  89. </span>
  90. </div>
  91. <div className="flex justify-between">
  92. <span className="text-zinc-500">{t('spoolbuddy.settings.connection', 'Connection')}</span>
  93. <span className="text-zinc-300 font-mono">
  94. {device.nfc_connection || 'N/A'}
  95. </span>
  96. </div>
  97. <div className="flex justify-between items-center">
  98. <span className="text-zinc-500">{t('spoolbuddy.status.status', 'Status')}</span>
  99. <div className="flex items-center gap-1.5">
  100. <div className={`w-2 h-2 rounded-full ${
  101. device.nfc_ok ? 'bg-green-500' : device.nfc_reader_type ? 'bg-red-500' : 'bg-zinc-600'
  102. }`} />
  103. <span className={
  104. device.nfc_ok ? 'text-green-400' : device.nfc_reader_type ? 'text-red-400' : 'text-zinc-500'
  105. }>
  106. {device.nfc_ok
  107. ? t('spoolbuddy.status.nfcReady', 'Ready')
  108. : device.nfc_reader_type
  109. ? t('common.error', 'Error')
  110. : t('spoolbuddy.settings.notConnected', 'N/A')}
  111. </span>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. {/* Device Info */}
  117. <div className="bg-zinc-800 rounded-lg p-3">
  118. <h3 className="text-sm font-semibold text-zinc-300 mb-2">
  119. {t('spoolbuddy.settings.deviceInfo', 'Device Info')}
  120. </h3>
  121. <div className="space-y-1.5 text-xs">
  122. <div className="flex justify-between">
  123. <span className="text-zinc-500">{t('spoolbuddy.settings.hostname', 'Host')}</span>
  124. <span className="text-zinc-300 truncate ml-2">{device.hostname}</span>
  125. </div>
  126. <div className="flex justify-between">
  127. <span className="text-zinc-500">IP</span>
  128. <span className="text-zinc-300">{device.ip_address}</span>
  129. </div>
  130. <div className="flex justify-between">
  131. <span className="text-zinc-500">{t('spoolbuddy.settings.uptime', 'Uptime')}</span>
  132. <span className="text-zinc-300">{formatUptime(device.uptime_s)}</span>
  133. </div>
  134. <div className="flex justify-between items-center">
  135. <span className="text-zinc-500">{t('spoolbuddy.status.status', 'Status')}</span>
  136. <div className="flex items-center gap-1.5">
  137. <div className={`w-2 h-2 rounded-full ${device.online ? 'bg-green-500' : 'bg-zinc-600'}`} />
  138. <span className={device.online ? 'text-green-400' : 'text-zinc-500'}>
  139. {device.online ? t('spoolbuddy.status.online', 'Online') : t('spoolbuddy.status.offline', 'Offline')}
  140. </span>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. {/* Device ID (full width, below cards) */}
  147. <div className="bg-zinc-800 rounded-lg px-3 py-2 flex justify-between items-center text-xs">
  148. <span className="text-zinc-500">Device ID</span>
  149. <span className="text-zinc-400 font-mono">{device.device_id}</span>
  150. </div>
  151. <div className="grid grid-cols-1 lg:grid-cols-2 gap-3">
  152. {/* Backend/Auth Config */}
  153. <div className="bg-zinc-800 rounded-lg p-4 space-y-3">
  154. <h3 className="text-sm font-semibold text-zinc-300">
  155. {t('spoolbuddy.settings.systemConfig', 'Backend & Auth')}
  156. </h3>
  157. <div className="space-y-2">
  158. <label className="text-xs text-zinc-500 block">
  159. {t('spoolbuddy.settings.backendUrl', 'Bambuddy Backend URL')}
  160. </label>
  161. <input
  162. value={backendUrl}
  163. onChange={(e) => setBackendUrl(e.target.value)}
  164. placeholder="http://192.168.1.100:5000"
  165. className="w-full px-3 py-2 rounded bg-zinc-900 border border-zinc-700 text-zinc-100 text-sm"
  166. />
  167. </div>
  168. <div className="space-y-2">
  169. <label className="text-xs text-zinc-500 block">
  170. {t('spoolbuddy.settings.apiToken', 'API Token')}
  171. </label>
  172. <input
  173. type="password"
  174. value={apiToken}
  175. onChange={(e) => setApiToken(e.target.value)}
  176. placeholder={t('spoolbuddy.settings.apiTokenPlaceholder', 'Enter API token')}
  177. className="w-full px-3 py-2 rounded bg-zinc-900 border border-zinc-700 text-zinc-100 text-sm"
  178. />
  179. </div>
  180. <div className="flex gap-2">
  181. <button
  182. onClick={saveConfig}
  183. disabled={systemBusy}
  184. className="px-3 py-2 rounded bg-green-700 hover:bg-green-600 disabled:bg-zinc-700 text-sm font-medium text-zinc-100"
  185. >
  186. {t('spoolbuddy.settings.saveConfig', 'Save Config')}
  187. </button>
  188. </div>
  189. {systemMsg && (
  190. <div className={`text-xs ${systemMsg.type === 'ok' ? 'text-green-400' : 'text-red-400'}`}>
  191. {systemMsg.text}
  192. </div>
  193. )}
  194. </div>
  195. {/* Diagnostic Buttons */}
  196. <div className="bg-zinc-800 rounded-lg p-4 space-y-3">
  197. {/* NFC Diagnostic Button */}
  198. <button
  199. onClick={() => setDiagnosticOpen('nfc')}
  200. className="w-full bg-blue-700 hover:bg-blue-600 transition-colors rounded-lg p-3 text-left"
  201. >
  202. <div className="flex items-center gap-2 mb-1">
  203. <Wand2 className="w-4 h-4 text-blue-300" />
  204. <span className="text-sm font-semibold text-blue-100">
  205. {t('spoolbuddy.settings.nfcDiagnostic', 'NFC Diagnostic')}
  206. </span>
  207. </div>
  208. <p className="text-xs text-blue-200/70">
  209. {t('spoolbuddy.settings.testNfc', 'Test reader')}
  210. </p>
  211. </button>
  212. {/* Scale Diagnostic Button */}
  213. <button
  214. onClick={() => setDiagnosticOpen('scale')}
  215. className="w-full bg-yellow-700 hover:bg-yellow-600 transition-colors rounded-lg p-3 text-left"
  216. >
  217. <div className="flex items-center gap-2 mb-1">
  218. <Zap className="w-4 h-4 text-yellow-300" />
  219. <span className="text-sm font-semibold text-yellow-100">
  220. {t('spoolbuddy.settings.scaleDiagnostic', 'Scale Diagnostic')}
  221. </span>
  222. </div>
  223. <p className="text-xs text-yellow-200/70">
  224. {t('spoolbuddy.settings.testScale', 'Test accuracy')}
  225. </p>
  226. </button>
  227. {/* Read Tag Diagnostic Button */}
  228. <button
  229. onClick={() => setDiagnosticOpen('read_tag')}
  230. className="w-full bg-emerald-700 hover:bg-emerald-600 transition-colors rounded-lg p-3 text-left"
  231. >
  232. <div className="flex items-center gap-2 mb-1">
  233. <FileText className="w-4 h-4 text-emerald-300" />
  234. <span className="text-sm font-semibold text-emerald-100">
  235. {t('spoolbuddy.settings.readTagDiagnostic', 'Read Tag Diagnostic')}
  236. </span>
  237. </div>
  238. <p className="text-xs text-emerald-200/70">
  239. {t('spoolbuddy.settings.testReadTag', 'Run read_tag.py')}
  240. </p>
  241. </button>
  242. </div>
  243. </div>
  244. {/* Diagnostic Modal */}
  245. {diagnosticOpen && device && (
  246. <DiagnosticModal
  247. type={diagnosticOpen}
  248. deviceId={device.device_id}
  249. onClose={() => setDiagnosticOpen(null)}
  250. />
  251. )}
  252. </div>
  253. );
  254. }
  255. // --- Display Tab ---
  256. function DisplayTab({ device, onBrightnessChange, onBlankTimeoutChange }: {
  257. device: SpoolBuddyDevice;
  258. onBrightnessChange: (value: number) => void;
  259. onBlankTimeoutChange: (value: number) => void;
  260. }) {
  261. const { t } = useTranslation();
  262. const [brightness, setBrightness] = useState(device.display_brightness);
  263. const [blankTimeout, setBlankTimeout] = useState(device.display_blank_timeout);
  264. const [saved, setSaved] = useState(false);
  265. const debounceRef = useRef<ReturnType<typeof setTimeout>>(undefined);
  266. const savedTimerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
  267. // Sync local state when device data updates from server
  268. useEffect(() => {
  269. setBrightness(device.display_brightness);
  270. setBlankTimeout(device.display_blank_timeout);
  271. }, [device.display_brightness, device.display_blank_timeout]);
  272. const showSaved = useCallback(() => {
  273. setSaved(true);
  274. if (savedTimerRef.current) clearTimeout(savedTimerRef.current);
  275. savedTimerRef.current = setTimeout(() => setSaved(false), 1500);
  276. }, []);
  277. const sendDisplayUpdate = useCallback((b: number, bt: number) => {
  278. if (debounceRef.current) clearTimeout(debounceRef.current);
  279. debounceRef.current = setTimeout(() => {
  280. spoolbuddyApi.updateDisplay(device.device_id, b, bt)
  281. .then(() => showSaved())
  282. .catch((e) => console.error('Failed to update display:', e));
  283. }, 300);
  284. }, [device.device_id, showSaved]);
  285. const handleBrightnessChange = (value: number) => {
  286. setBrightness(value);
  287. onBrightnessChange(value); // Instant layout update
  288. sendDisplayUpdate(value, blankTimeout);
  289. };
  290. const handleBlankTimeoutChange = (value: number) => {
  291. setBlankTimeout(value);
  292. onBlankTimeoutChange(value); // Instant layout update
  293. sendDisplayUpdate(brightness, value);
  294. };
  295. return (
  296. <div className="space-y-4">
  297. {/* Brightness */}
  298. <div className="bg-zinc-800 rounded-lg p-4">
  299. <div className="flex items-center justify-between mb-3">
  300. <h3 className="text-sm font-semibold text-zinc-300">
  301. {t('spoolbuddy.settings.brightness', 'Brightness')}
  302. </h3>
  303. {saved && (
  304. <span className="text-xs text-green-400 flex items-center gap-1 animate-pulse">
  305. <svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={3}>
  306. <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
  307. </svg>
  308. {t('spoolbuddy.settings.saved', 'Saved')}
  309. </span>
  310. )}
  311. </div>
  312. <div className="flex items-center gap-3">
  313. <svg className="w-4 h-4 text-zinc-500 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
  314. <path strokeLinecap="round" strokeLinejoin="round" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
  315. </svg>
  316. <input
  317. type="range"
  318. min={0}
  319. max={100}
  320. value={brightness}
  321. onChange={(e) => handleBrightnessChange(parseInt(e.target.value))}
  322. className="flex-1 h-2 bg-zinc-700 rounded-lg appearance-none cursor-pointer accent-green-500"
  323. />
  324. <span className="text-sm font-mono text-zinc-400 w-10 text-right">{brightness}%</span>
  325. </div>
  326. {!device.has_backlight && (
  327. <p className="text-xs text-zinc-600 mt-2">
  328. {t('spoolbuddy.settings.noBacklight', 'No DSI backlight detected. Brightness control requires a DSI display.')}
  329. </p>
  330. )}
  331. </div>
  332. {/* Screen blank timeout */}
  333. <div className="bg-zinc-800 rounded-lg p-4">
  334. <h3 className="text-sm font-semibold text-zinc-300 mb-1">
  335. {t('spoolbuddy.settings.screenBlank', 'Screen Blank Timeout')}
  336. </h3>
  337. <p className="text-xs text-zinc-500 mb-3">
  338. {t('spoolbuddy.settings.screenBlankDesc', 'Screen turns off after inactivity. Touch to wake.')}
  339. </p>
  340. <div className="grid grid-cols-3 gap-2">
  341. {BLANK_OPTIONS.map((opt) => (
  342. <button
  343. key={opt.value}
  344. onClick={() => handleBlankTimeoutChange(opt.value)}
  345. className={`px-3 py-2 rounded-lg text-sm font-medium transition-colors min-h-[40px] ${
  346. blankTimeout === opt.value
  347. ? 'bg-green-600 text-white'
  348. : 'bg-zinc-700 text-zinc-300 hover:bg-zinc-600'
  349. }`}
  350. >
  351. {opt.label}
  352. </button>
  353. ))}
  354. </div>
  355. </div>
  356. <p className="text-xs text-zinc-600 text-center">
  357. {t('spoolbuddy.settings.displayNote', 'Brightness is applied as a software filter.')}
  358. </p>
  359. </div>
  360. );
  361. }
  362. // --- Scale Tab ---
  363. function StepIndicator({ step, labels }: { step: 'tare' | 'weight'; labels: { tare: string; weight: string } }) {
  364. return (
  365. <div className="flex flex-col items-center w-16 shrink-0 pt-1">
  366. {/* Step 1 circle */}
  367. <div className={`flex items-center justify-center w-7 h-7 rounded-full text-xs font-bold ${
  368. step === 'tare'
  369. ? 'bg-green-600 text-white'
  370. : 'bg-green-600/20 text-green-400'
  371. }`}>
  372. {step === 'weight' ? (
  373. <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={3}>
  374. <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
  375. </svg>
  376. ) : '1'}
  377. </div>
  378. <span className={`text-[10px] mt-0.5 ${step === 'tare' ? 'text-green-400 font-medium' : 'text-green-400/60'}`}>
  379. {labels.tare}
  380. </span>
  381. {/* Connector line */}
  382. <div className={`w-px h-5 my-1 ${step === 'weight' ? 'bg-green-600/40' : 'bg-zinc-700'}`} />
  383. {/* Step 2 circle */}
  384. <div className={`flex items-center justify-center w-7 h-7 rounded-full text-xs font-bold ${
  385. step === 'weight'
  386. ? 'bg-green-600 text-white'
  387. : 'bg-zinc-700 text-zinc-500'
  388. }`}>
  389. 2
  390. </div>
  391. <span className={`text-[10px] mt-0.5 ${step === 'weight' ? 'text-green-400 font-medium' : 'text-zinc-600'}`}>
  392. {labels.weight}
  393. </span>
  394. </div>
  395. );
  396. }
  397. function ScaleTab({ device, weight, weightStable, rawAdc }: {
  398. device: SpoolBuddyDevice;
  399. weight: number | null;
  400. weightStable: boolean;
  401. rawAdc: number | null;
  402. }) {
  403. const { t } = useTranslation();
  404. const [calStep, setCalStep] = useState<'idle' | 'tare' | 'weight'>('idle');
  405. const [knownWeight, setKnownWeight] = useState('500');
  406. const [tareRawAdc, setTareRawAdc] = useState<number | null>(null);
  407. const [busy, setBusy] = useState(false);
  408. const [status, setStatus] = useState<{ type: 'ok' | 'error'; msg: string } | null>(null);
  409. const numpadPress = (key: string) => {
  410. if (key === 'backspace') {
  411. setKnownWeight((v) => v.slice(0, -1) || '');
  412. } else if (key === '.' && !knownWeight.includes('.')) {
  413. setKnownWeight((v) => v + '.');
  414. } else if (key >= '0' && key <= '9') {
  415. setKnownWeight((v) => (v === '0' ? key : v + key));
  416. }
  417. };
  418. const handleTare = async () => {
  419. setBusy(true);
  420. setStatus(null);
  421. try {
  422. await spoolbuddyApi.tare(device.device_id);
  423. setStatus({ type: 'ok', msg: t('spoolbuddy.settings.tareSet', 'Tare command sent. Waiting for device...') });
  424. } catch {
  425. setStatus({ type: 'error', msg: t('spoolbuddy.settings.tareFailed', 'Failed to send tare command') });
  426. } finally {
  427. setBusy(false);
  428. }
  429. };
  430. const handleCalStep = async () => {
  431. if (calStep === 'tare') {
  432. setBusy(true);
  433. setStatus(null);
  434. try {
  435. setTareRawAdc(rawAdc);
  436. await spoolbuddyApi.tare(device.device_id);
  437. setStatus({ type: 'ok', msg: t('spoolbuddy.settings.zeroSet', 'Zero point set. Place known weight on scale.') });
  438. setCalStep('weight');
  439. } catch {
  440. setStatus({ type: 'error', msg: t('spoolbuddy.settings.tareFailed', 'Failed to send tare command') });
  441. } finally {
  442. setBusy(false);
  443. }
  444. } else if (calStep === 'weight') {
  445. const weightNum = parseFloat(knownWeight);
  446. if (rawAdc === null || !weightNum || weightNum <= 0) return;
  447. setBusy(true);
  448. setStatus(null);
  449. try {
  450. await spoolbuddyApi.setCalibrationFactor(device.device_id, weightNum, rawAdc, tareRawAdc ?? undefined);
  451. setStatus({ type: 'ok', msg: t('spoolbuddy.settings.calibrationDone', 'Calibration complete!') });
  452. setCalStep('idle');
  453. } catch {
  454. setStatus({ type: 'error', msg: t('spoolbuddy.settings.calibrationFailed', 'Calibration failed') });
  455. } finally {
  456. setBusy(false);
  457. }
  458. }
  459. };
  460. // --- Idle state: weight card + buttons ---
  461. if (calStep === 'idle') {
  462. return (
  463. <div className="flex flex-col h-full">
  464. {/* Weight + info card */}
  465. <div className="bg-zinc-800 rounded-lg p-3 mb-3">
  466. <div className="flex items-center justify-between">
  467. <div className="flex items-center gap-2">
  468. <div className={`w-2 h-2 rounded-full ${weightStable ? 'bg-green-500' : 'bg-amber-500 animate-pulse'}`} />
  469. <span className="text-lg font-mono text-zinc-200">
  470. {weight !== null ? `${weight.toFixed(1)} g` : '-- g'}
  471. </span>
  472. </div>
  473. <div className="text-xs text-zinc-500 text-right">
  474. <span>{t('spoolbuddy.settings.tareOffset', 'Tare')}: {device.tare_offset}</span>
  475. <span className="mx-1.5">&middot;</span>
  476. <span>{t('spoolbuddy.settings.calFactor', 'Factor')}: {device.calibration_factor.toFixed(2)}</span>
  477. </div>
  478. </div>
  479. {device.last_calibrated_at && (
  480. <div className="text-xs text-zinc-600 mt-1">
  481. {t('spoolbuddy.settings.lastCalibrated', 'Last calibrated')}: {formatDateTime(device.last_calibrated_at)}
  482. </div>
  483. )}
  484. </div>
  485. {/* Status message */}
  486. {status && (
  487. <div className={`rounded-lg px-3 py-2 mb-3 text-sm ${
  488. status.type === 'ok' ? 'bg-green-900/30 text-green-300 border border-green-800' : 'bg-red-900/30 text-red-300 border border-red-800'
  489. }`}>
  490. {status.msg}
  491. </div>
  492. )}
  493. {/* Action buttons */}
  494. <div className="flex gap-2">
  495. <button
  496. onClick={handleTare}
  497. disabled={busy}
  498. className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-200 hover:bg-zinc-600 disabled:opacity-40 transition-colors min-h-[44px] flex items-center justify-center gap-2"
  499. >
  500. {busy && (
  501. <svg className="w-4 h-4 animate-spin" viewBox="0 0 24 24" fill="none">
  502. <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
  503. <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
  504. </svg>
  505. )}
  506. {t('spoolbuddy.weight.tare', 'Tare')}
  507. </button>
  508. <button
  509. onClick={() => { setCalStep('tare'); setStatus(null); }}
  510. className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 transition-colors min-h-[44px]"
  511. >
  512. {t('spoolbuddy.weight.calibrate', 'Calibrate')}
  513. </button>
  514. </div>
  515. </div>
  516. );
  517. }
  518. // --- Calibration wizard: step indicator left + content right ---
  519. return (
  520. <div className="flex gap-3">
  521. {/* Left: step indicator */}
  522. <StepIndicator step={calStep} labels={{ tare: t('spoolbuddy.weight.tare', 'Tare'), weight: t('spoolbuddy.settings.knownWeight', 'Known weight') }} />
  523. {/* Right: content */}
  524. <div className="flex-1 min-w-0">
  525. {/* Live weight bar */}
  526. <div className="flex items-center gap-2 bg-zinc-800 rounded-lg px-3 py-1.5 mb-1.5">
  527. <div className={`w-2 h-2 rounded-full shrink-0 ${weightStable ? 'bg-green-500' : 'bg-amber-500 animate-pulse'}`} />
  528. <span className="text-sm font-mono text-zinc-200">
  529. {weight !== null ? `${weight.toFixed(1)} g` : '-- g'}
  530. </span>
  531. <span className={`text-xs ml-auto ${weightStable ? 'text-green-400' : 'text-amber-400'}`}>
  532. {weightStable ? t('spoolbuddy.settings.stable', 'Stable') : t('spoolbuddy.settings.settling', 'Settling...')}
  533. </span>
  534. </div>
  535. {/* Status message */}
  536. {status && (
  537. <div className={`rounded-lg px-3 py-1.5 mb-1.5 text-xs ${
  538. status.type === 'ok' ? 'bg-green-900/30 text-green-300 border border-green-800' : 'bg-red-900/30 text-red-300 border border-red-800'
  539. }`}>
  540. {status.msg}
  541. </div>
  542. )}
  543. {/* Step content */}
  544. {calStep === 'tare' ? (
  545. <p className="text-sm text-zinc-300 mb-3">
  546. {t('spoolbuddy.settings.calStep1', 'Remove all items from the scale and press Set Zero.')}
  547. </p>
  548. ) : (
  549. <>
  550. <div className="flex items-center gap-2 mb-1.5">
  551. <span className="text-xs text-zinc-400 shrink-0">{t('spoolbuddy.settings.knownWeight', 'Known weight')}</span>
  552. <div className="flex-1 bg-zinc-900 border border-zinc-600 rounded px-3 py-1 text-right text-lg font-mono text-zinc-100">
  553. {knownWeight || '0'}<span className="text-zinc-500 ml-1">g</span>
  554. </div>
  555. </div>
  556. <div className="grid grid-cols-4 gap-1 mb-1.5">
  557. {['7','8','9','backspace','4','5','6','.','1','2','3','0'].map((key) => (
  558. <button
  559. key={key}
  560. onClick={() => numpadPress(key)}
  561. className={`rounded text-lg font-medium transition-colors h-[48px] active:scale-95 ${
  562. key === 'backspace'
  563. ? 'bg-zinc-700 text-zinc-300 hover:bg-zinc-600'
  564. : 'bg-zinc-800 text-zinc-100 hover:bg-zinc-700 border border-zinc-700'
  565. }`}
  566. >
  567. {key === 'backspace' ? '\u232B' : key}
  568. </button>
  569. ))}
  570. </div>
  571. </>
  572. )}
  573. {/* Action buttons */}
  574. <div className="flex gap-2">
  575. <button
  576. onClick={() => { setCalStep('idle'); setStatus(null); }}
  577. className="flex-1 px-4 py-2 rounded-lg text-sm bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors h-[40px]"
  578. >
  579. {t('common.cancel', 'Cancel')}
  580. </button>
  581. <button
  582. onClick={handleCalStep}
  583. disabled={busy}
  584. className="flex-1 px-4 py-2 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 transition-colors h-[40px] flex items-center justify-center gap-2"
  585. >
  586. {busy && (
  587. <svg className="w-4 h-4 animate-spin" viewBox="0 0 24 24" fill="none">
  588. <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
  589. <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
  590. </svg>
  591. )}
  592. {calStep === 'tare' ? t('spoolbuddy.settings.setZero', 'Set Zero') : t('spoolbuddy.settings.calibrateNow', 'Calibrate')}
  593. </button>
  594. </div>
  595. </div>
  596. </div>
  597. );
  598. }
  599. // --- Updates Tab ---
  600. function UpdatesTab({ device }: { device: SpoolBuddyDevice }) {
  601. const { t } = useTranslation();
  602. const [busy, setBusy] = useState<'checking' | 'applying' | null>(null);
  603. const [error, setError] = useState<string | null>(null);
  604. const [sshExpanded, setSSHExpanded] = useState(false);
  605. const [copied, setCopied] = useState(false);
  606. const isUpdating = device.update_status === 'pending' || device.update_status === 'updating';
  607. // When applying succeeds and device picks up the update, keep showing busy
  608. useEffect(() => {
  609. if (isUpdating && busy === 'applying') {
  610. setBusy(null); // device has picked it up, isUpdating takes over the UI
  611. }
  612. }, [isUpdating, busy]);
  613. // Reload the page when daemon comes back online after an update
  614. useEffect(() => {
  615. const handleOnline = () => {
  616. if (isUpdating) {
  617. // Daemon re-registered — reload to get fresh version + state
  618. setTimeout(() => window.location.reload(), 1000);
  619. }
  620. };
  621. window.addEventListener('spoolbuddy-online', handleOnline);
  622. return () => window.removeEventListener('spoolbuddy-online', handleOnline);
  623. }, [isUpdating]);
  624. const { data: updateResult, refetch } = useQuery({
  625. queryKey: ['spoolbuddy-update-check', device.device_id],
  626. queryFn: () => spoolbuddyApi.checkDaemonUpdate(device.device_id),
  627. staleTime: 0,
  628. });
  629. const { data: sshKeyData } = useQuery({
  630. queryKey: ['spoolbuddy-ssh-key'],
  631. queryFn: () => spoolbuddyApi.getSSHPublicKey(),
  632. enabled: sshExpanded,
  633. staleTime: Infinity,
  634. });
  635. const checkForUpdates = async () => {
  636. setBusy('checking');
  637. setError(null);
  638. try {
  639. await refetch();
  640. } finally {
  641. setBusy(null);
  642. }
  643. };
  644. const applyUpdate = async () => {
  645. setBusy('applying');
  646. setError(null);
  647. try {
  648. await spoolbuddyApi.triggerUpdate(device.device_id);
  649. // Don't clear busy — keep showing spinner until isUpdating takes over or timeout
  650. } catch (e) {
  651. setError(e instanceof Error ? e.message : 'Failed to trigger update');
  652. setBusy(null);
  653. }
  654. };
  655. const showSpinner = busy != null || isUpdating;
  656. const copyKey = () => {
  657. if (sshKeyData?.public_key) {
  658. navigator.clipboard.writeText(sshKeyData.public_key);
  659. setCopied(true);
  660. setTimeout(() => setCopied(false), 2000);
  661. }
  662. };
  663. const displayVersion = device.firmware_version
  664. || (updateResult?.current_version && updateResult.current_version !== '0.0.0' ? updateResult.current_version : null);
  665. return (
  666. <div className="space-y-3">
  667. {/* Version + Update status + Check — single card */}
  668. <div className="bg-zinc-800 rounded-lg p-3 space-y-3">
  669. {/* Version row */}
  670. <div className="flex justify-between items-center text-sm">
  671. <span className="text-zinc-500">{t('spoolbuddy.settings.currentVersion', 'Current Version')}</span>
  672. <span className="text-zinc-200 font-mono">
  673. {displayVersion || (
  674. <span className="text-zinc-500 italic text-xs">{t('spoolbuddy.settings.versionPending', 'Waiting for daemon...')}</span>
  675. )}
  676. </span>
  677. </div>
  678. {/* Status / progress row */}
  679. {showSpinner ? (
  680. <div className="flex items-center gap-2">
  681. <svg className="w-4 h-4 animate-spin text-green-400 flex-shrink-0" viewBox="0 0 24 24" fill="none">
  682. <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
  683. <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
  684. </svg>
  685. <span className="text-green-300 text-xs">
  686. {busy === 'checking' ? t('spoolbuddy.settings.checking', 'Checking for updates...')
  687. : device.update_message || t('spoolbuddy.settings.updateWaiting', 'Updating...')}
  688. </span>
  689. </div>
  690. ) : device.update_status === 'error' ? (
  691. <p className="text-xs text-red-300">{device.update_message || t('spoolbuddy.settings.updateFailed', 'Update failed')}</p>
  692. ) : error ? (
  693. <p className="text-xs text-red-300">{error}</p>
  694. ) : updateResult?.update_available ? (
  695. <p className="text-xs text-green-300">
  696. {t('spoolbuddy.settings.updateAvailable', 'Update available')}: {displayVersion} → {updateResult.latest_version}
  697. </p>
  698. ) : null}
  699. {/* Action buttons */}
  700. {!showSpinner && (
  701. updateResult?.update_available ? (
  702. <button
  703. onClick={applyUpdate}
  704. disabled={!device.online}
  705. className="w-full px-3 py-2 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 transition-colors"
  706. >
  707. {!device.online
  708. ? t('spoolbuddy.settings.deviceOffline', 'Device Offline')
  709. : t('spoolbuddy.settings.applyUpdate', 'Apply Update')}
  710. </button>
  711. ) : (
  712. <div className="flex gap-2">
  713. <button
  714. onClick={checkForUpdates}
  715. className="flex-1 px-3 py-2 rounded-lg text-xs font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors"
  716. >
  717. {t('spoolbuddy.settings.checkUpdates', 'Check for Updates')}
  718. </button>
  719. <button
  720. onClick={applyUpdate}
  721. disabled={!device.online}
  722. className="px-3 py-2 rounded-lg text-xs font-medium bg-zinc-700 text-zinc-400 hover:bg-zinc-600 hover:text-zinc-200 disabled:opacity-40 transition-colors"
  723. >
  724. {t('spoolbuddy.settings.forceUpdate', 'Force Update')}
  725. </button>
  726. </div>
  727. )
  728. )}
  729. </div>
  730. {/* SSH Setup — collapsible */}
  731. <div className="bg-zinc-800 rounded-lg p-3">
  732. <button
  733. onClick={() => setSSHExpanded(!sshExpanded)}
  734. className="w-full flex justify-between items-center text-xs"
  735. >
  736. <span className="font-medium text-zinc-400">
  737. {t('spoolbuddy.settings.sshSetup', 'SSH Setup')}
  738. </span>
  739. <svg
  740. className={`w-3 h-3 text-zinc-500 transition-transform ${sshExpanded ? 'rotate-180' : ''}`}
  741. fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}
  742. >
  743. <path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
  744. </svg>
  745. </button>
  746. {sshExpanded && (
  747. <div className="mt-2 space-y-2">
  748. <p className="text-xs text-zinc-500">
  749. {t('spoolbuddy.settings.sshDescription', 'SSH key is deployed automatically. For manual setup, add this key to ~/.ssh/authorized_keys on the device.')}
  750. </p>
  751. {sshKeyData?.public_key ? (
  752. <div className="relative">
  753. <pre className="bg-zinc-900 rounded p-2 text-[10px] text-zinc-400 font-mono break-all whitespace-pre-wrap">
  754. {sshKeyData.public_key}
  755. </pre>
  756. <button
  757. onClick={copyKey}
  758. className="absolute top-1 right-1 px-1.5 py-0.5 rounded text-[10px] bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors"
  759. >
  760. {copied ? t('common.copied', 'Copied!') : t('common.copy', 'Copy')}
  761. </button>
  762. </div>
  763. ) : (
  764. <span className="text-[10px] text-zinc-500 italic">
  765. {t('spoolbuddy.settings.sshKeyLoading', 'Loading...')}
  766. </span>
  767. )}
  768. </div>
  769. )}
  770. </div>
  771. </div>
  772. );
  773. }
  774. // --- Main Settings Page ---
  775. type SettingsTab = 'device' | 'display' | 'scale' | 'updates';
  776. export function SpoolBuddySettingsPage() {
  777. const { sbState, setDisplayBrightness, setDisplayBlankTimeout } = useOutletContext<SpoolBuddyOutletContext>();
  778. const { t } = useTranslation();
  779. const [activeTab, setActiveTab] = useState<SettingsTab>('device');
  780. const { data: devices = [] } = useQuery({
  781. queryKey: ['spoolbuddy-devices'],
  782. queryFn: () => spoolbuddyApi.getDevices(),
  783. refetchInterval: 10000,
  784. });
  785. // Use first device (most common setup) or find one matching current state
  786. const device = sbState.deviceId
  787. ? devices.find((d) => d.device_id === sbState.deviceId) ?? devices[0]
  788. : devices[0];
  789. const tabs: { id: SettingsTab; label: string }[] = [
  790. { id: 'device', label: t('spoolbuddy.settings.tabDevice', 'Device') },
  791. { id: 'display', label: t('spoolbuddy.settings.tabDisplay', 'Display') },
  792. { id: 'scale', label: t('spoolbuddy.settings.tabScale', 'Scale') },
  793. { id: 'updates', label: t('spoolbuddy.settings.tabUpdates', 'Updates') },
  794. ];
  795. return (
  796. <div className="h-full flex flex-col p-4">
  797. <h1 className="text-xl font-semibold text-zinc-100 mb-3">
  798. {t('spoolbuddy.nav.settings', 'Settings')}
  799. </h1>
  800. {/* Tab bar */}
  801. <div className="flex gap-1 bg-zinc-800/50 rounded-lg p-1 mb-4">
  802. {tabs.map((tab) => (
  803. <button
  804. key={tab.id}
  805. onClick={() => setActiveTab(tab.id)}
  806. className={`flex-1 px-2 py-2 rounded-md text-sm font-medium transition-colors min-h-[36px] ${
  807. activeTab === tab.id
  808. ? 'bg-zinc-700 text-zinc-100'
  809. : 'text-zinc-500 hover:text-zinc-300'
  810. }`}
  811. >
  812. {tab.label}
  813. </button>
  814. ))}
  815. </div>
  816. {/* Content */}
  817. <div className="flex-1 min-h-0 overflow-y-auto">
  818. {!device ? (
  819. <div className="flex items-center justify-center h-32">
  820. <div className="text-center text-zinc-500">
  821. <p className="text-sm">{t('spoolbuddy.settings.noDevice', 'No SpoolBuddy device found')}</p>
  822. </div>
  823. </div>
  824. ) : (
  825. <>
  826. {activeTab === 'device' && <DeviceTab device={device} />}
  827. {activeTab === 'display' && (
  828. <DisplayTab
  829. device={device}
  830. onBrightnessChange={setDisplayBrightness}
  831. onBlankTimeoutChange={setDisplayBlankTimeout}
  832. />
  833. )}
  834. {activeTab === 'scale' && (
  835. <ScaleTab
  836. device={device}
  837. weight={sbState.weight}
  838. weightStable={sbState.weightStable}
  839. rawAdc={sbState.rawAdc}
  840. />
  841. )}
  842. {activeTab === 'updates' && <UpdatesTab device={device} />}
  843. </>
  844. )}
  845. </div>
  846. </div>
  847. );
  848. }