import React from "react"; import { toast } from "sonner"; import Container from "src/components/Container"; import PasswordInput from "src/components/password/PasswordInput"; import TwoColumnLayoutHeader from "src/components/TwoColumnLayoutHeader"; import { LoadingButton } from "src/components/ui/custom/loading-button"; import { Label } from "src/components/ui/label"; import { useInfo } from "src/hooks/useInfo"; import { saveAuthToken } from "src/lib/auth"; import { AuthTokenResponse } from "src/types"; import { handleRequestError } from "src/utils/handleRequestError"; import { request } from "src/utils/request"; export default function Start() { const [unlockPassword, setUnlockPassword] = React.useState(""); const [loading, setLoading] = React.useState(false); const [buttonText, setButtonText] = React.useState("Start"); const { data: info } = useInfo(true); // poll the info endpoint to auto-redirect when app is running const startupState = info?.startupState; const startupError = info?.startupError; const startupErrorTime = info?.startupErrorTime; React.useEffect(() => { if (startupState) { setButtonText(startupState); } }, [startupState]); React.useEffect(() => { if (startupError && startupErrorTime) { toast.error("Failed to start", { description: startupError, }); setLoading(false); setButtonText("Start"); setUnlockPassword(""); } }, [startupError, startupErrorTime]); async function onSubmit(e: React.FormEvent) { e.preventDefault(); try { setLoading(true); setButtonText("Please wait..."); const authTokenResponse = await request("/api/start", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ unlockPassword, permission: "full", }), }); if (authTokenResponse) { saveAuthToken(authTokenResponse.token); } } catch (error) { handleRequestError("Failed to connect", error); setLoading(false); setButtonText("Start"); setUnlockPassword(""); } } return (
{buttonText} {loading && (

Starting Alby Hub may take a few minutes.

)}
); }