import { useState } from "react"; import { api, BASE_URL } from "./api/client"; import "./CreateProfile.css"; import type { AuthResponse } from "./types/api"; // Shown after CreateProfile (or on any later login, until it succeeds -- // see LoginGate) since connecting Garmin is mandatory: the account exists // but this is the last gate before the main app. Deliberately not a reuse // of GarminConnection.tsx (the Profile page's version), which also renders // Sync now/Disconnect/Reset all -- none of which make sense here. export function ConnectGarmin({ onConnected }: { onConnected: () => void }) { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [auth, setAuth] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function connect(e: React.FormEvent) { e.preventDefault(); if (!email.trim() || !password) { setError("Please enter your Garmin email and password."); return; } setBusy(true); setError(null); try { const profile = await api.getProfile(); await api.updateProfile({ ...profile, GarminEmail: email.trim(), GarminPassword: password }); setAuth(await api.login()); } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong, please try again."); } finally { setBusy(false); } } async function submitMFA() { if (!code.trim()) return; setBusy(true); setError(null); try { setAuth(await api.submitMFA(code.trim())); setCode(""); } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong, please try again."); } finally { setBusy(false); } } const status = auth?.status; return (

🧞‍♀️ Connect your Garmin account

geniusrun needs your Garmin credentials to sync your activities.

{status !== "authenticated" && status !== "mfa_required" && (
setEmail(e.target.value)} disabled={busy} autoFocus /> setPassword(e.target.value)} disabled={busy} />
)} {status === "mfa_required" && (
setCode(e.target.value)} onKeyDown={(e) => e.key === "Enter" && submitMFA()} disabled={busy} autoFocus />
)} {status === "authenticated" && ( <>

Connected to Garmin.

)} {auth?.message && status !== "authenticated" &&

{auth.message}

} {error &&

{error}

}
); }