import type { ComputerStatus } from "@rakazo/contracts"; import { useState } from "react"; import { Alert, Pressable, Text, View } from "react-native"; import { rpc } from "../lib/api"; import { computerUpdates } from "../lib/computer-updates"; import { useI18n } from "../lib/i18n"; import { useMobileTokens } from "../lib/native"; type Action = "recover" | "reset" | "update"; export function ComputerMaintenanceActions({ botId, computer, onChanged, }: { botId: string; computer: ComputerStatus | null; onChanged: () => Promise; }) { const { t } = useI18n(); const tokens = useMobileTokens(); const [pending, setPending] = useState(null); const [error, setError] = useState(null); if (!computer) return null; const busy = Boolean(computer.busyBotName) || computer.state === "booting"; async function run(action: Action) { setPending(action); setError(null); try { if (action === "recover") await computerUpdates.start(botId, "recover"); else if (action === "reset") await rpc("computer/reset", { botId }); else await computerUpdates.start(botId); await onChanged(); } catch (err) { setError(err instanceof Error ? err.message : t("Could not update computer")); } finally { setPending(null); } } function confirmReset() { Alert.alert( t("Reset computer?"), t("Restore the last saved workspace. Unsaved work on the computer is lost."), [ { text: t("Cancel"), style: "cancel" }, { text: t("Reset"), style: "destructive", onPress: () => void run("reset") }, ], ); } return ( void run("recover")} style={{ opacity: busy || pending !== null ? 0.4 : 1 }} > {pending === "recover" ? t("Recovering…") : t("Recover computer")} {pending === "reset" ? t("Resetting…") : t("Reset computer")} {computer.canUpdate ? ( void run("update")} style={{ opacity: busy || pending !== null ? 0.4 : 1 }} > {pending === "update" ? t("Updating…") : t("Update computer")} ) : null} {error ? {error} : null} ); }