import { useCallback, useEffect, useRef, useState } from "react"; import { Cloud, LogIn, LogOut, User } from "lucide-react"; import { api } from "../lib/api"; import { useApp } from "../lib/store"; import { bumpPluginsRev, bumpSessionRev } from "../lib/userId"; import { useDismissOn } from "../lib/useDismissOn"; /** * Compact user-state control in the TopBar: * * Not logged in → "Sign in" button that kicks off the OAuth dance * (full-page navigation, since OAuth needs a top-level redirect). * * Logged in → avatar / name pill that toggles a dropdown with * a logout button and a persistence-status hint. * * Hidden entirely when the server reports oauth_enabled=false (local * dev without HF metadata). */ export function AuthButton({ mobile = false }: { mobile?: boolean } = {}) { const session = useApp((s) => s.session); const authStatus = useApp((s) => s.authStatus); const setSession = useApp((s) => s.setSession); const setPlugins = useApp((s) => s.setPlugins); const [menuOpen, setMenuOpen] = useState(false); const wrapRef = useRef(null); const triggerRef = useRef(null); // When the dropdown closes (via Escape, click-outside, or explicit // close), return focus to the trigger so keyboard users can keep // navigating from where they were. useEffect(() => { if (!menuOpen && triggerRef.current && document.activeElement) { // Only restore focus when something inside us had focus — // otherwise we'd steal focus from elsewhere on a programmatic // close. if (wrapRef.current?.contains(document.activeElement)) { triggerRef.current.focus(); } } }, [menuOpen]); useDismissOn(menuOpen, wrapRef, useCallback(() => setMenuOpen(false), [])); // While we haven't probed /api/auth/status yet, reserve the slot // with an invisible placeholder so the TopBar's right cluster // doesn't shift when the button materialises. Once the probe // resolves we either render the real button or vanish for good // (oauth_enabled=false in dev). if (authStatus === null) { return ; } if (!authStatus.oauth_enabled) return null; if (!session) { return ( { // Show a brief 'redirecting' UI by setting a session-storage // flag the next page render reads. Without this, slow HF // redirects (cold pods can take 1-3s) leave the user // wondering if their click registered. try { sessionStorage.setItem("quda.loggingIn", "1"); } catch { /* storage blocked is fine */ } }} > Sign in to keep plugins Sign in ); } const handleLogout = async () => { try { await api.authLogout(); } catch { /* network errors are fine — we still clear local state */ } setSession(null); setPlugins([]); // Visible plugins were tied to the session; clear. setMenuOpen(false); bumpPluginsRev(); bumpSessionRev(); }; return (
{menuOpen && ( // Mobile (
Signed in as
{session.username}
{session.persistence_enabled ? ( <> Your plugins are saved under{" "} {session.user_data_repo}/users/{session.username}/ {" "}— a private folder only you and the QuDA server can read. They survive container restarts and follow you to any device you sign in on. ) : ( Persistence is not configured on this deployment, so your plugins will still vanish on container restart. )}
Plugins you uploaded before signing in remain under your anonymous browser id — sign out to access them.
)} ); }