import { useEffect, useState } from "react"; import { BacklightSettings, useSettingsStore } from "@hooks/stores"; import { JsonRpcResponse, useJsonRpc } from "@hooks/useJsonRpc"; import { Checkbox } from "@components/Checkbox"; import { FeatureFlag } from "@components/FeatureFlag"; import { SelectMenuBasic } from "@components/SelectMenuBasic"; import { SettingsItem } from "@components/SettingsItem"; import { SettingsPageHeader } from "@components/SettingsPageheader"; import { SettingsSectionHeader } from "@components/SettingsSectionHeader"; import { UsbDeviceSetting } from "@components/UsbDeviceSetting"; import { UsbInfoSetting } from "@components/UsbInfoSetting"; import notifications from "@/notifications"; import { m } from "@localizations/messages.js"; export default function SettingsHardwareRoute() { const { send } = useJsonRpc(); const settings = useSettingsStore(); const { displayRotation, setDisplayRotation } = useSettingsStore(); const [powerSavingEnabled, setPowerSavingEnabled] = useState(false); const handleDisplayRotationChange = (rotation: string) => { setDisplayRotation(rotation); handleDisplayRotationSave(); }; const handleDisplayRotationSave = () => { send("setDisplayRotation", { params: { rotation: displayRotation } }, (resp: JsonRpcResponse) => { if ("error" in resp) { notifications.error( m.hardware_display_orientation_error({ error: resp.error.data || m.unknown_error() }), ); return; } notifications.success(m.hardware_display_orientation_success()); }); }; const { backlightSettings, setBacklightSettings } = useSettingsStore(); const handleBacklightSettingsChange = (settings: BacklightSettings) => { // If the user has set the display to dim after it turns off, set the dim_after // value to never. if (settings.dim_after > settings.off_after && settings.off_after != 0) { settings.dim_after = 0; } setBacklightSettings(settings); handleBacklightSettingsSave(settings); }; const handleBacklightSettingsSave = (backlightSettings: BacklightSettings) => { send("setBacklightSettings", { params: backlightSettings }, (resp: JsonRpcResponse) => { if ("error" in resp) { notifications.error( m.hardware_backlight_settings_error({ error: resp.error.data || m.unknown_error() }), ); return; } notifications.success(m.hardware_backlight_settings_success()); }); }; const handleBacklightMaxBrightnessChange = (max_brightness: number) => { const settings = { ...backlightSettings, max_brightness }; handleBacklightSettingsChange(settings); }; const handleBacklightDimAfterChange = (dim_after: number) => { const settings = { ...backlightSettings, dim_after }; handleBacklightSettingsChange(settings); }; const handleBacklightOffAfterChange = (off_after: number) => { const settings = { ...backlightSettings, off_after }; handleBacklightSettingsChange(settings); }; const handlePowerSavingChange = (enabled: boolean) => { setPowerSavingEnabled(enabled); const duration = enabled ? 90 : -1; send("setVideoSleepMode", { duration }, (resp: JsonRpcResponse) => { if ("error" in resp) { notifications.error(m.hardware_power_saving_failed_error({ error: resp.error.data || m.unknown_error() })); setPowerSavingEnabled(!enabled); // Attempt to revert on error return; } notifications.success(enabled ? m.hardware_power_saving_enabled() : m.hardware_power_saving_disabled()); }); }; useEffect(() => { send("getBacklightSettings", {}, (resp: JsonRpcResponse) => { if ("error" in resp) { return notifications.error( m.hardware_backlight_settings_get_error({ error: resp.error.data || m.unknown_error() }), ); } const result = resp.result as BacklightSettings; setBacklightSettings(result); }); }, [send, setBacklightSettings]); useEffect(() => { send("getVideoSleepMode", {}, (resp: JsonRpcResponse) => { if ("error" in resp) { console.error("Failed to get power saving mode:", resp.error); return; } const result = resp.result as { enabled: boolean; duration: number }; setPowerSavingEnabled(result.duration >= 0); }); }, [send]); return (
{m.hardware_display_wake_up_note()}