SpoolBuddySettings.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  4. import {
  5. Loader2,
  6. Trash2,
  7. Cpu,
  8. HardDrive,
  9. Thermometer,
  10. Wifi,
  11. WifiOff,
  12. AlertTriangle,
  13. Info,
  14. CheckCircle2,
  15. XCircle,
  16. Clock,
  17. } from 'lucide-react';
  18. import { spoolbuddyApi, type SpoolBuddyDevice } from '../api/client';
  19. import { Card, CardContent, CardHeader } from './Card';
  20. import { Button } from './Button';
  21. import { ConfirmModal } from './ConfirmModal';
  22. import { useToast } from '../contexts/ToastContext';
  23. import { formatRelativeTime } from '../utils/date';
  24. function formatUptime(seconds: number): string {
  25. if (seconds < 60) return `${seconds}s`;
  26. const m = Math.floor(seconds / 60);
  27. if (m < 60) return `${m}m`;
  28. const h = Math.floor(m / 60);
  29. const remM = m % 60;
  30. if (h < 24) return remM ? `${h}h ${remM}m` : `${h}h`;
  31. const d = Math.floor(h / 24);
  32. const remH = h % 24;
  33. return remH ? `${d}d ${remH}h` : `${d}d`;
  34. }
  35. function formatMB(mb?: number): string {
  36. if (mb === undefined || mb === null) return '—';
  37. if (mb >= 1024) return `${(mb / 1024).toFixed(1)} GB`;
  38. return `${Math.round(mb)} MB`;
  39. }
  40. interface DeviceCardProps {
  41. device: SpoolBuddyDevice;
  42. onUnregister: (device: SpoolBuddyDevice) => void;
  43. isDeleting: boolean;
  44. }
  45. function DeviceCard({ device, onUnregister, isDeleting }: DeviceCardProps) {
  46. const { t } = useTranslation();
  47. const stats = device.system_stats;
  48. const mem = stats?.memory;
  49. const disk = stats?.disk;
  50. const online = device.online;
  51. return (
  52. <Card>
  53. <CardHeader>
  54. <div className="flex items-start justify-between gap-3 flex-wrap">
  55. <div className="min-w-0">
  56. <div className="flex items-center gap-2">
  57. <h3 className="text-base font-semibold text-white truncate">{device.hostname}</h3>
  58. <span
  59. className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${
  60. online
  61. ? 'bg-green-500/15 text-green-400 border border-green-500/40'
  62. : 'bg-gray-500/15 text-gray-400 border border-gray-500/40'
  63. }`}
  64. >
  65. {online ? <Wifi className="w-3 h-3" /> : <WifiOff className="w-3 h-3" />}
  66. {online ? t('settings.spoolbuddy.online') : t('settings.spoolbuddy.offline')}
  67. </span>
  68. </div>
  69. <p className="text-xs text-bambu-gray font-mono mt-1 truncate">{device.device_id}</p>
  70. </div>
  71. <Button
  72. variant="danger"
  73. size="sm"
  74. onClick={() => onUnregister(device)}
  75. disabled={isDeleting}
  76. aria-label={t('settings.spoolbuddy.unregister')}
  77. >
  78. <Trash2 className="w-3.5 h-3.5" />
  79. <span className="hidden sm:inline">{t('settings.spoolbuddy.unregister')}</span>
  80. </Button>
  81. </div>
  82. </CardHeader>
  83. <CardContent className="space-y-3">
  84. {/* Connection */}
  85. <div className="grid grid-cols-2 sm:grid-cols-4 gap-3 text-xs">
  86. <div>
  87. <div className="text-bambu-gray">{t('settings.spoolbuddy.ipAddress')}</div>
  88. <div className="text-white font-mono">{device.ip_address}</div>
  89. </div>
  90. <div>
  91. <div className="text-bambu-gray">{t('settings.spoolbuddy.firmware')}</div>
  92. <div className="text-white">{device.firmware_version ?? '—'}</div>
  93. </div>
  94. <div>
  95. <div className="text-bambu-gray flex items-center gap-1">
  96. <Clock className="w-3 h-3" />
  97. {t('settings.spoolbuddy.lastSeen')}
  98. </div>
  99. <div className="text-white">
  100. {device.last_seen ? formatRelativeTime(device.last_seen) : t('settings.spoolbuddy.never')}
  101. </div>
  102. </div>
  103. <div>
  104. <div className="text-bambu-gray">{t('settings.spoolbuddy.daemonUptime')}</div>
  105. <div className="text-white">{formatUptime(device.uptime_s)}</div>
  106. </div>
  107. </div>
  108. {/* Hardware flags */}
  109. <div className="flex items-center gap-3 text-xs flex-wrap">
  110. <span className="flex items-center gap-1 text-bambu-gray">
  111. {device.nfc_ok ? (
  112. <CheckCircle2 className="w-3.5 h-3.5 text-green-400" />
  113. ) : (
  114. <XCircle className="w-3.5 h-3.5 text-red-400" />
  115. )}
  116. {t('settings.spoolbuddy.nfc')}
  117. {device.nfc_reader_type && <span className="text-bambu-gray/70">({device.nfc_reader_type})</span>}
  118. </span>
  119. <span className="flex items-center gap-1 text-bambu-gray">
  120. {device.scale_ok ? (
  121. <CheckCircle2 className="w-3.5 h-3.5 text-green-400" />
  122. ) : (
  123. <XCircle className="w-3.5 h-3.5 text-red-400" />
  124. )}
  125. {t('settings.spoolbuddy.scale')}
  126. </span>
  127. </div>
  128. {/* System stats */}
  129. {stats && (
  130. <div className="pt-2 border-t border-bambu-dark-tertiary">
  131. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3 text-xs">
  132. {stats.cpu_temp_c !== undefined && (
  133. <div className="flex items-center gap-2">
  134. <Thermometer className="w-3.5 h-3.5 text-bambu-gray" />
  135. <div>
  136. <div className="text-bambu-gray">{t('settings.spoolbuddy.cpuTemp')}</div>
  137. <div className="text-white">{stats.cpu_temp_c.toFixed(1)}°C</div>
  138. </div>
  139. </div>
  140. )}
  141. {mem && mem.percent !== undefined && (
  142. <div className="flex items-center gap-2">
  143. <Cpu className="w-3.5 h-3.5 text-bambu-gray" />
  144. <div>
  145. <div className="text-bambu-gray">{t('settings.spoolbuddy.memory')}</div>
  146. <div className="text-white">
  147. {mem.percent.toFixed(0)}% ({formatMB(mem.used_mb)} / {formatMB(mem.total_mb)})
  148. </div>
  149. </div>
  150. </div>
  151. )}
  152. {disk && disk.percent !== undefined && (
  153. <div className="flex items-center gap-2">
  154. <HardDrive className="w-3.5 h-3.5 text-bambu-gray" />
  155. <div>
  156. <div className="text-bambu-gray">{t('settings.spoolbuddy.disk')}</div>
  157. <div className="text-white">
  158. {disk.percent.toFixed(0)}% ({disk.used_gb?.toFixed(1)} / {disk.total_gb?.toFixed(1)} GB)
  159. </div>
  160. </div>
  161. </div>
  162. )}
  163. {stats.system_uptime_s !== undefined && (
  164. <div className="flex items-center gap-2">
  165. <Clock className="w-3.5 h-3.5 text-bambu-gray" />
  166. <div>
  167. <div className="text-bambu-gray">{t('settings.spoolbuddy.systemUptime')}</div>
  168. <div className="text-white">{formatUptime(stats.system_uptime_s)}</div>
  169. </div>
  170. </div>
  171. )}
  172. </div>
  173. {stats.os && (
  174. <div className="mt-3 text-xs text-bambu-gray font-mono truncate">
  175. {[stats.os.os, stats.os.kernel, stats.os.arch, stats.os.python && `Python ${stats.os.python}`]
  176. .filter(Boolean)
  177. .join(' · ')}
  178. </div>
  179. )}
  180. </div>
  181. )}
  182. </CardContent>
  183. </Card>
  184. );
  185. }
  186. export function SpoolBuddySettings() {
  187. const { t } = useTranslation();
  188. const queryClient = useQueryClient();
  189. const { showToast } = useToast();
  190. const [pendingDelete, setPendingDelete] = useState<SpoolBuddyDevice | null>(null);
  191. const { data: devices = [], isLoading } = useQuery({
  192. queryKey: ['spoolbuddy-devices'],
  193. queryFn: () => spoolbuddyApi.getDevices(),
  194. refetchInterval: 15000,
  195. });
  196. const deleteMutation = useMutation({
  197. mutationFn: (deviceId: string) => spoolbuddyApi.deleteDevice(deviceId),
  198. onSuccess: () => {
  199. queryClient.invalidateQueries({ queryKey: ['spoolbuddy-devices'] });
  200. showToast(t('settings.spoolbuddy.unregisterSuccess'), 'success');
  201. setPendingDelete(null);
  202. },
  203. onError: (err: Error) => {
  204. showToast(err.message || t('settings.spoolbuddy.unregisterError'), 'error');
  205. },
  206. });
  207. if (isLoading) {
  208. return (
  209. <Card>
  210. <CardContent className="py-8 flex justify-center">
  211. <Loader2 className="w-6 h-6 animate-spin text-bambu-green" />
  212. </CardContent>
  213. </Card>
  214. );
  215. }
  216. const hasDuplicates = devices.length > 1;
  217. return (
  218. <div className="space-y-4">
  219. <Card>
  220. <CardContent className="py-3 px-4">
  221. <div className="flex items-start gap-2 text-xs">
  222. <Info className="w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5" />
  223. <div className="text-bambu-gray">
  224. <p className="text-white font-medium mb-1">{t('settings.spoolbuddy.infoTitle')}</p>
  225. <p>{t('settings.spoolbuddy.infoBody')}</p>
  226. </div>
  227. </div>
  228. </CardContent>
  229. </Card>
  230. {hasDuplicates && (
  231. <Card className="border-l-4 border-l-yellow-500">
  232. <CardContent className="py-3 px-4">
  233. <div className="flex items-start gap-2 text-xs">
  234. <AlertTriangle className="w-4 h-4 text-yellow-500 flex-shrink-0 mt-0.5" />
  235. <div className="text-bambu-gray">
  236. <p className="text-white font-medium mb-1">
  237. {t('settings.spoolbuddy.duplicatesTitle', { count: devices.length })}
  238. </p>
  239. <p>{t('settings.spoolbuddy.duplicatesBody')}</p>
  240. </div>
  241. </div>
  242. </CardContent>
  243. </Card>
  244. )}
  245. {devices.length === 0 ? (
  246. <Card>
  247. <CardContent className="py-8 text-center text-bambu-gray text-sm">
  248. {t('settings.spoolbuddy.empty')}
  249. </CardContent>
  250. </Card>
  251. ) : (
  252. <div className="space-y-3">
  253. {devices.map((device) => (
  254. <DeviceCard
  255. key={device.id}
  256. device={device}
  257. onUnregister={setPendingDelete}
  258. isDeleting={deleteMutation.isPending && deleteMutation.variables === device.device_id}
  259. />
  260. ))}
  261. </div>
  262. )}
  263. {pendingDelete && (
  264. <ConfirmModal
  265. variant="danger"
  266. title={t('settings.spoolbuddy.confirmTitle')}
  267. message={t('settings.spoolbuddy.confirmBody', {
  268. hostname: pendingDelete.hostname,
  269. deviceId: pendingDelete.device_id,
  270. })}
  271. confirmText={t('settings.spoolbuddy.unregister')}
  272. isLoading={deleteMutation.isPending}
  273. onConfirm={() => deleteMutation.mutate(pendingDelete.device_id)}
  274. onCancel={() => setPendingDelete(null)}
  275. />
  276. )}
  277. </div>
  278. );
  279. }