import { useSyncStore } from "../../store/useSyncStore";
import { Button, DropdownMenu, Tooltip } from "@cloudflare/kumo";
import { BookOpenTextIcon, CaretDownIcon, CaretRightIcon, CreditCardIcon, KeyboardIcon, MoonIcon, QuestionIcon, RocketLaunchIcon, SignInIcon, SignOutIcon, SunIcon, UserCircleIcon, UserIcon } from "@phosphor-icons/react";
import hfLogo from "../../assets/hf-logo.svg";
import { useEffect, useState } from "react";
import { useAuthStore } from "../../store/useAuthStore";
import { useCanvasStore } from "../../store/useCanvasStore";
import { useQuotaStore } from "../../store/useQuotaStore";
import type { InferenceUsage } from "../../lib/hub";
/** GPU-seconds as the Hub shows them: minutes with one decimal ("11.2m"), hours above that. */
const fmtGpu = (sec: number) => {
const s = Math.max(0, sec);
if (s < 60) return `${Math.round(s)}s`;
const m = s / 60;
if (m < 100) return `${(Math.round(m * 10) / 10).toString().replace(/\.0$/, "")}m`;
const h = Math.floor(m / 60);
const rm = Math.round(m % 60);
return rm ? `${h}h ${rm}m` : `${h}h`;
};
const fmtUsd = (cents: number) => `$${(cents / 100).toFixed(2)}`;
const fmtNanoUsd = (n: number) => fmtUsd(n / 1e7);
const fmtIn = (iso: string | null, now: number) => {
if (!iso) return null;
const ms = new Date(iso).getTime() - now;
return ms <= 0 ? "any moment" : `in ${fmtGpu(ms / 1000)}`;
};
/** Remaining ZeroGPU allowance as a fraction plus a traffic-light level. */
function useQuotaLevel() {
const quota = useQuotaStore((s) => s.quota);
if (!quota) return null;
const frac = quota.base > 0 ? Math.min(1, Math.max(0, quota.current / quota.base)) : 0;
const level = quota.current <= 0 ? "empty" : frac < 0.2 ? "low" : "ok";
return { quota, frac, level };
}
/** Full quota breakdown shown inside the user menu. */
function QuotaSection({ canPay }: { canPay: boolean }) {
const q = useQuotaLevel();
const loading = useQuotaStore((s) => s.loading);
const error = useQuotaStore((s) => s.error);
const refresh = useQuotaStore((s) => s.refresh);
const [now, setNow] = useState(Date.now());
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 30_000);
return () => clearInterval(id);
}, []);
if (!q) {
return (
ZeroGPU quota
{error ? `Unavailable: ${error}` : "Not loaded yet."}
{error?.startsWith("403") && " Your sign-in predates the billing permission: sign out and back in to grant it."}
Billed this period
{fmtUsd(quota.overage.totalCents)} · {fmtGpu(quota.overage.billedSeconds)}
)}
{quota.runs && (
Runs left
{quota.runs.remaining} / {quota.runs.limit}
)}
{quota.inference && }
{level === "empty" && (
{canPay ? "Free quota is used up. ZeroGPU runs are now billed to your account." : "Free quota is used up. ZeroGPU Spaces will refuse runs until it resets."}
);
}
/** On phones the avatar is all that fits, so the quota becomes a ring around it. */
function QuotaRing({ children }: { children: React.ReactNode }) {
const q = useQuotaLevel();
return (
{children}
);
}
/** Thin quota bar along the bottom of the user button. */
function QuotaStrip() {
const q = useQuotaLevel();
if (!q) return null;
return (
);
}
const HF_LOGO = ;
export function AuthButton() {
const user = useAuthStore((s) => s.user);
const theme = useCanvasStore((s) => s.theme);
const toggleTheme = useCanvasStore((s) => s.toggleTheme);
const setDialog = useCanvasStore((s) => s.setDialog);
const busy = useAuthStore((s) => s.busy);
const configured = useAuthStore((s) => s.configured);
const login = useAuthStore((s) => s.login);
const logout = useAuthStore((s) => s.logout);
const error = useAuthStore((s) => s.error);
const pending = useAuthStore((s) => s.pending);
const finishLogin = useAuthStore((s) => s.finishLogin);
const cancelLogin = useAuthStore((s) => s.cancelLogin);
if (!configured) {
return (
Sign-in is not configured. Create a public OAuth app at huggingface.co/settings/applications and set VITE_HF_CLIENT_ID (see .env.example). Until then Spaces run on the anonymous quota.
}
render={
}
/>
);
}
if (!user && pending) {
return (
{error}
) : (
Finish signing in in the pop-up. Flow picks it up automatically; press Finish if it doesn't.
)
}
render={
}
/>
);
}
if (!user) {
return (
{error} : "Runs Spaces on your own Hugging Face quota (ZeroGPU, PRO)"}
render={
}
/>
);
}
return (
{user.avatar ? : }{user.username || user.name}
{user.isPro && PRO}
}
/>
{user.name || user.username}
{user.isPro && PRO}
window.open(`https://huggingface.co/${user.username}`, "_blank")}>
Profile
window.open("https://huggingface.co/settings/billing", "_blank")}>
Usage & billing
{theme === "dark" ? "Light theme" : "Dark theme"}
Help setDialog("welcome")}>
Quick start
setDialog("shortcuts")}>
Keyboard shortcuts
? window.open("/llms.txt", "_blank", "noopener")}>
Guide for LLM agents
{
if (useSyncStore.getState().hasUnsaved() && !window.confirm("This workflow has changes that aren't saved to your storage yet. Sign out anyway and lose them?")) return;
logout();
}}
>
Sign out
);
}