import { Button } from "@components/Button"; import { LuHardDrive, LuPower, LuRotateCcw } from "react-icons/lu"; import Card from "@components/Card"; import { SettingsPageHeader } from "@components/SettingsPageheader"; import { useEffect, useState } from "react"; import notifications from "@/notifications"; import { useJsonRpc } from "../../hooks/useJsonRpc"; import LoadingSpinner from "../LoadingSpinner"; const LONG_PRESS_DURATION = 3000; // 3 seconds for long press interface ATXState { power: boolean; hdd: boolean; } export function ATXPowerControl() { const [isPowerPressed, setIsPowerPressed] = useState(false); const [powerPressTimer, setPowerPressTimer] = useState | null>(null); const [atxState, setAtxState] = useState(null); const [send] = useJsonRpc(function onRequest(resp) { if (resp.method === "atxState") { setAtxState(resp.params as ATXState); } }); // Request initial state useEffect(() => { send("getATXState", {}, resp => { if ("error" in resp) { notifications.error( `Failed to get ATX state: ${resp.error.data || "Unknown error"}`, ); return; } setAtxState(resp.result as ATXState); }); }, [send]); const handlePowerPress = (pressed: boolean) => { // Prevent phantom releases if (!pressed && !isPowerPressed) return; setIsPowerPressed(pressed); // Handle button press if (pressed) { // Start long press timer const timer = setTimeout(() => { // Send long press action console.log("Sending long press ATX power action"); send("setATXPowerAction", { action: "power-long" }, resp => { if ("error" in resp) { notifications.error( `Failed to send ATX power action: ${resp.error.data || "Unknown error"}`, ); } setIsPowerPressed(false); }); }, LONG_PRESS_DURATION); setPowerPressTimer(timer); } // Handle button release else { // If timer exists, was a short press if (powerPressTimer) { clearTimeout(powerPressTimer); setPowerPressTimer(null); // Send short press action console.log("Sending short press ATX power action"); send("setATXPowerAction", { action: "power-short" }, resp => { if ("error" in resp) { notifications.error( `Failed to send ATX power action: ${resp.error.data || "Unknown error"}`, ); } }); } } }; // Cleanup timer on unmount useEffect(() => { return () => { if (powerPressTimer) { clearTimeout(powerPressTimer); } }; }, [powerPressTimer]); return (
{atxState === null ? ( ) : (
{/* Control Buttons */}

{/* Status Indicators */}
Power LED
HDD LED
)}
); }