import { useCallback } from "react"; import { useEffect, useState } from "react"; import { useJsonRpc } from "../hooks/useJsonRpc"; import notifications from "../notifications"; import { SettingsItem } from "../routes/devices.$id.settings"; import Checkbox from "./Checkbox"; export interface USBConfig { vendor_id: string; product_id: string; serial_number: string; manufacturer: string; product: string; } export interface UsbDeviceConfig { keyboard: boolean; absolute_mouse: boolean; relative_mouse: boolean; mass_storage: boolean; } const defaultUsbDeviceConfig: UsbDeviceConfig = { keyboard: true, absolute_mouse: true, relative_mouse: true, mass_storage: true, } export function UsbDeviceSetting() { const [send] = useJsonRpc(); const [usbDeviceConfig, setUsbDeviceConfig] = useState(defaultUsbDeviceConfig); const syncUsbDeviceConfig = useCallback(() => { send("getUsbDevices", {}, resp => { if ("error" in resp) { console.error("Failed to load USB devices:", resp.error); notifications.error( `Failed to load USB devices: ${resp.error.data || "Unknown error"}`, ); } else { console.log("syncUsbDeviceConfig#getUsbDevices result:", resp.result); const usbConfigState = resp.result as UsbDeviceConfig; setUsbDeviceConfig(usbConfigState); } }); }, [send]); const handleUsbConfigChange = useCallback( (devices: UsbDeviceConfig) => { send("setUsbDevices", { devices }, resp => { if ("error" in resp) { notifications.error( `Failed to set usb devices: ${resp.error.data || "Unknown error"}`, ); return; } notifications.success( `USB Devices updated` ); syncUsbDeviceConfig(); }); }, [send, syncUsbDeviceConfig], ); const onUsbConfigItemChange = useCallback((key: keyof UsbDeviceConfig) => (e: React.ChangeEvent) => { setUsbDeviceConfig((val) => { val[key] = e.target.checked; handleUsbConfigChange(val); return val; }); }, [handleUsbConfigChange]); useEffect(() => { syncUsbDeviceConfig(); }, [syncUsbDeviceConfig]); return ( <>
); }