import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { Loader2, Trash2, Cpu, HardDrive, Thermometer, Wifi, WifiOff, AlertTriangle, Info, CheckCircle2, XCircle, Clock, } from 'lucide-react'; import { spoolbuddyApi, type SpoolBuddyDevice } from '../api/client'; import { Card, CardContent, CardHeader } from './Card'; import { Button } from './Button'; import { ConfirmModal } from './ConfirmModal'; import { useToast } from '../contexts/ToastContext'; import { formatRelativeTime } from '../utils/date'; function formatUptime(seconds: number): string { if (seconds < 60) return `${seconds}s`; const m = Math.floor(seconds / 60); if (m < 60) return `${m}m`; const h = Math.floor(m / 60); const remM = m % 60; if (h < 24) return remM ? `${h}h ${remM}m` : `${h}h`; const d = Math.floor(h / 24); const remH = h % 24; return remH ? `${d}d ${remH}h` : `${d}d`; } function formatMB(mb?: number): string { if (mb === undefined || mb === null) return '—'; if (mb >= 1024) return `${(mb / 1024).toFixed(1)} GB`; return `${Math.round(mb)} MB`; } interface DeviceCardProps { device: SpoolBuddyDevice; onUnregister: (device: SpoolBuddyDevice) => void; isDeleting: boolean; } function DeviceCard({ device, onUnregister, isDeleting }: DeviceCardProps) { const { t } = useTranslation(); const stats = device.system_stats; const mem = stats?.memory; const disk = stats?.disk; const online = device.online; return (

{device.hostname}

{online ? : } {online ? t('settings.spoolbuddy.online') : t('settings.spoolbuddy.offline')}

{device.device_id}

{/* Connection */}
{t('settings.spoolbuddy.ipAddress')}
{device.ip_address}
{t('settings.spoolbuddy.firmware')}
{device.firmware_version ?? '—'}
{t('settings.spoolbuddy.lastSeen')}
{device.last_seen ? formatRelativeTime(device.last_seen) : t('settings.spoolbuddy.never')}
{t('settings.spoolbuddy.daemonUptime')}
{formatUptime(device.uptime_s)}
{/* Hardware flags */}
{device.nfc_ok ? ( ) : ( )} {t('settings.spoolbuddy.nfc')} {device.nfc_reader_type && ({device.nfc_reader_type})} {device.scale_ok ? ( ) : ( )} {t('settings.spoolbuddy.scale')}
{/* System stats */} {stats && (
{stats.cpu_temp_c !== undefined && (
{t('settings.spoolbuddy.cpuTemp')}
{stats.cpu_temp_c.toFixed(1)}°C
)} {mem && mem.percent !== undefined && (
{t('settings.spoolbuddy.memory')}
{mem.percent.toFixed(0)}% ({formatMB(mem.used_mb)} / {formatMB(mem.total_mb)})
)} {disk && disk.percent !== undefined && (
{t('settings.spoolbuddy.disk')}
{disk.percent.toFixed(0)}% ({disk.used_gb?.toFixed(1)} / {disk.total_gb?.toFixed(1)} GB)
)} {stats.system_uptime_s !== undefined && (
{t('settings.spoolbuddy.systemUptime')}
{formatUptime(stats.system_uptime_s)}
)}
{stats.os && (
{[stats.os.os, stats.os.kernel, stats.os.arch, stats.os.python && `Python ${stats.os.python}`] .filter(Boolean) .join(' · ')}
)}
)}
); } export function SpoolBuddySettings() { const { t } = useTranslation(); const queryClient = useQueryClient(); const { showToast } = useToast(); const [pendingDelete, setPendingDelete] = useState(null); const { data: devices = [], isLoading } = useQuery({ queryKey: ['spoolbuddy-devices'], queryFn: () => spoolbuddyApi.getDevices(), refetchInterval: 15000, }); const deleteMutation = useMutation({ mutationFn: (deviceId: string) => spoolbuddyApi.deleteDevice(deviceId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['spoolbuddy-devices'] }); showToast(t('settings.spoolbuddy.unregisterSuccess'), 'success'); setPendingDelete(null); }, onError: (err: Error) => { showToast(err.message || t('settings.spoolbuddy.unregisterError'), 'error'); }, }); if (isLoading) { return ( ); } const hasDuplicates = devices.length > 1; return (

{t('settings.spoolbuddy.infoTitle')}

{t('settings.spoolbuddy.infoBody')}

{hasDuplicates && (

{t('settings.spoolbuddy.duplicatesTitle', { count: devices.length })}

{t('settings.spoolbuddy.duplicatesBody')}

)} {devices.length === 0 ? ( {t('settings.spoolbuddy.empty')} ) : (
{devices.map((device) => ( ))}
)} {pendingDelete && ( deleteMutation.mutate(pendingDelete.device_id)} onCancel={() => setPendingDelete(null)} /> )}
); }