import { Button } from "@components/UI/Button.tsx"; import { Checkbox } from "@components/UI/Checkbox/index.tsx"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@components/UI/Dialog.tsx"; import { Input } from "@components/UI/Input.tsx"; import { Label } from "@components/UI/Label.tsx"; import { Separator } from "@components/UI/Seperator.tsx"; import { useDevice } from "@core/stores"; import { ClockIcon, OctagonXIcon, RefreshCwIcon } from "lucide-react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; export interface RebootDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } const DEFAULT_REBOOT_DELAY = 5; // seconds export const RebootDialog = ({ open, onOpenChange }: RebootDialogProps) => { const { t } = useTranslation("dialog"); const { connection } = useDevice(); const [time, setTime] = useState(DEFAULT_REBOOT_DELAY); const [isScheduled, setIsScheduled] = useState(false); const [isOTA, setIsOTA] = useState(false); const [inputValue, setInputValue] = useState(DEFAULT_REBOOT_DELAY.toString()); const [timeoutId, setTimeoutId] = useState(); const handleReboot = (delay: number) => { if (!connection) { return; } if (isOTA) { connection.rebootOta(delay); } else { connection.reboot(delay); } }; 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 (!Number.isNaN(parsed) && parsed > 0) { setTime(parsed); } }; const handleRebootWithTimeout = async () => { setIsScheduled(true); const delay = time > 0 ? time : DEFAULT_REBOOT_DELAY; handleReboot(delay); const id = setTimeout(() => { setIsScheduled(false); onOpenChange(false); setInputValue(DEFAULT_REBOOT_DELAY.toString()); }, delay * 1000); setTimeoutId(id); }; const handleCancel = () => { clearTimeout(timeoutId); setIsScheduled(false); handleReboot(-1); }; const handleInstantReboot = async () => { handleReboot(0); onOpenChange(false); }; return ( {t("reboot.title")} {t("reboot.description")} {!isScheduled ? ( <> setIsOTA(checked)} className="px-2" > {t("reboot.ota")}
) : (
)}
); };