import { useState } from "react"; import { ClockIcon, RefreshCwIcon } from "lucide-react"; import { Button } from "@components/UI/Button.tsx"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@components/UI/Dialog.tsx"; import { Input } from "@components/UI/Input.tsx"; import { useDevice } from "@core/stores/deviceStore.ts"; export interface RebootOTADialogProps { open: boolean; onOpenChange: (open: boolean) => void; } const DEFAULT_REBOOT_DELAY = 5; // seconds export const RebootOTADialog = ({ open, onOpenChange }: RebootOTADialogProps) => { const { connection } = useDevice(); const [time, setTime] = useState(DEFAULT_REBOOT_DELAY); const [isScheduled, setIsScheduled] = useState(false); const [inputValue, setInputValue] = useState(DEFAULT_REBOOT_DELAY.toString()); const handleSetTime = (e: React.ChangeEvent) => { if (!e.target.validity.valid) { e.preventDefault(); return }; const val = e.target.value; setInputValue(val); const parsed = Number(val); if (!isNaN(parsed) && parsed > 0) { setTime(parsed); } }; const handleRebootWithTimeout = async () => { if (!connection) return; setIsScheduled(true); const delay = time > 0 ? time : DEFAULT_REBOOT_DELAY; await new Promise((resolve) => { setTimeout(() => { console.log("Rebooting..."); resolve(); }, delay * 1000); }).finally(() => { setIsScheduled(false); onOpenChange(false); setInputValue(DEFAULT_REBOOT_DELAY.toString()); }); connection.rebootOta(0); }; const handleInstantReboot = async () => { if (!connection) return; await connection.rebootOta(DEFAULT_REBOOT_DELAY); onOpenChange(false); }; return ( Reboot to OTA Mode Reboot the connected node after a delay into OTA (Over-the-Air) mode.
); };