Spaces:
Running
Running
Download src/components/panels/AuthButton.tsx from mrfakename/flow: direct link, hf CLI and curl.
- Browser
- Download file 13 kB
-
https://huggingface.co/spaces/mrfakename/flow/resolve/main/src/components/panels/AuthButton.tsx
- Command line
-
hf download hf://spaces/mrfakename/flow/src/components/panels/AuthButton.tsx
-
curl -L -o AuthButton.tsx https://huggingface.co/spaces/mrfakename/flow/resolve/main/src/components/panels/AuthButton.tsx
13 kB
| 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 ( | |
| <div className="quota" data-level="none"> | |
| <div className="quota__head"> | |
| <span>ZeroGPU quota</span> | |
| <button type="button" className="quota__refresh" onClick={() => void refresh()} disabled={loading}> | |
| {loading ? "Loading…" : "Refresh"} | |
| </button> | |
| </div> | |
| <div className="quota__note"> | |
| {error ? `Unavailable: ${error}` : "Not loaded yet."} | |
| {error?.startsWith("403") && " Your sign-in predates the billing permission: sign out and back in to grant it."} | |
| </div> | |
| </div> | |
| ); | |
| } | |
| const { quota, frac, level } = q; | |
| const overage = quota.overquotaUsed ?? 0; | |
| const reset = fmtIn(quota.resetsAt, now); | |
| return ( | |
| <div className="quota" data-level={level} data-loading={loading || undefined}> | |
| <div className="quota__head"> | |
| <span>ZeroGPU quota</span> | |
| <b className="tabular" title={`${fmtGpu(Math.max(0, quota.base - quota.current))} used`}> | |
| {fmtGpu(quota.current)} <span className="text-kumo-subtle">/ {fmtGpu(quota.base)} left</span> | |
| </b> | |
| </div> | |
| <div className="quota__bar" role="meter" aria-valuenow={Math.round(frac * 100)} aria-valuemin={0} aria-valuemax={100} aria-label="ZeroGPU quota left"> | |
| <i style={{ transform: `scaleX(${frac})` }} /> | |
| </div> | |
| <div className="quota__meta"> | |
| <span>{reset ? `Resets ${reset}` : "Unused this period"}</span> | |
| <button type="button" className="quota__refresh" onClick={() => void refresh()} disabled={loading}> | |
| Refresh | |
| </button> | |
| </div> | |
| {overage > 0 && ( | |
| <div className="quota__meta quota__meta--warn"> | |
| <span>Overage today</span> | |
| <b className="tabular">{fmtGpu(overage)}</b> | |
| </div> | |
| )} | |
| {quota.overage && quota.overage.totalCents > 0 && ( | |
| <div className="quota__meta quota__meta--warn"> | |
| <span>Billed this period</span> | |
| <b className="tabular"> | |
| {fmtUsd(quota.overage.totalCents)} <span className="font-normal opacity-75">· {fmtGpu(quota.overage.billedSeconds)}</span> | |
| </b> | |
| </div> | |
| )} | |
| {quota.runs && ( | |
| <div className="quota__meta"> | |
| <span>Runs left</span> | |
| <b className="tabular"> | |
| {quota.runs.remaining} / {quota.runs.limit} | |
| </b> | |
| </div> | |
| )} | |
| {quota.inference && <InferenceSection usage={quota.inference} />} | |
| {level === "empty" && ( | |
| <div className="quota__note"> | |
| {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."} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |
| /** Inference-provider spend this month, folded away until opened. */ | |
| function InferenceSection({ usage }: { usage: InferenceUsage }) { | |
| const [open, setOpen] = useState(false); | |
| const frac = usage.limitNanoUsd > 0 ? Math.min(1, usage.usedNanoUsd / usage.limitNanoUsd) : 0; | |
| return ( | |
| <div className="quota__sub" data-open={open || undefined}> | |
| <button type="button" className="quota__meta quota__toggle" onClick={() => setOpen((o) => !o)} aria-expanded={open}> | |
| <span className="flex items-center gap-1"> | |
| {open ? <CaretDownIcon size={9} weight="bold" /> : <CaretRightIcon size={9} weight="bold" />} | |
| Inference providers | |
| </span> | |
| <b className="tabular">{fmtNanoUsd(usage.usedNanoUsd)}</b> | |
| </button> | |
| {open && ( | |
| <div className="quota__sub-body"> | |
| {usage.limitNanoUsd > 0 && ( | |
| <> | |
| <div className="quota__bar quota__bar--neutral" role="meter" aria-valuenow={Math.round(frac * 100)} aria-valuemin={0} aria-valuemax={100} aria-label="Inference spend"> | |
| <i style={{ transform: `scaleX(${frac})` }} /> | |
| </div> | |
| <div className="quota__meta"> | |
| <span>Monthly limit</span> | |
| <b className="tabular">{fmtNanoUsd(usage.limitNanoUsd)}</b> | |
| </div> | |
| </> | |
| )} | |
| {usage.providers.map((p) => ( | |
| <div className="quota__meta" key={p.provider}> | |
| <span> | |
| {p.provider} <span className="opacity-60">· {p.numRequests.toLocaleString()} req</span> | |
| </span> | |
| <b className="tabular">{fmtNanoUsd(p.totalCostNanoUsd)}</b> | |
| </div> | |
| ))} | |
| {usage.providers.length === 0 && <div className="quota__meta">No requests this month.</div>} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |
| /** 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 ( | |
| <span className="quota-ring" data-level={q?.level} style={{ "--frac": q?.frac ?? 0 } as React.CSSProperties}> | |
| {children} | |
| </span> | |
| ); | |
| } | |
| /** Thin quota bar along the bottom of the user button. */ | |
| function QuotaStrip() { | |
| const q = useQuotaLevel(); | |
| if (!q) return null; | |
| return ( | |
| <span className="quota-strip" data-level={q.level} aria-hidden> | |
| <i style={{ transform: `scaleX(${q.frac})` }} /> | |
| </span> | |
| ); | |
| } | |
| const HF_LOGO = <img src={hfLogo} alt="" aria-hidden className="h-[15px] w-[15px]" draggable={false} />; | |
| 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 ( | |
| <Tooltip | |
| content={ | |
| <span className="block max-w-64 text-xs leading-snug"> | |
| 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. | |
| </span> | |
| } | |
| render={ | |
| <Button size="sm" variant="secondary" icon={SignInIcon} disabled> | |
| Sign in | |
| </Button> | |
| } | |
| /> | |
| ); | |
| } | |
| if (!user && pending) { | |
| return ( | |
| <span className="flex items-center gap-1"> | |
| <Tooltip | |
| content={ | |
| error ? ( | |
| <span className="block max-w-64 text-xs leading-snug text-kumo-danger">{error}</span> | |
| ) : ( | |
| <span className="block max-w-64 text-xs leading-snug">Finish signing in in the pop-up. Flow picks it up automatically; press Finish if it doesn't.</span> | |
| ) | |
| } | |
| render={ | |
| <Button size="sm" variant="secondary" onClick={() => void finishLogin()} icon={HF_LOGO}> | |
| Finish sign-in | |
| </Button> | |
| } | |
| /> | |
| <Button size="sm" variant="ghost" onClick={cancelLogin} aria-label="Cancel sign-in"> | |
| Cancel | |
| </Button> | |
| </span> | |
| ); | |
| } | |
| if (!user) { | |
| return ( | |
| <Tooltip | |
| content={error ? <span className="text-kumo-danger">{error}</span> : "Runs Spaces on your own Hugging Face quota (ZeroGPU, PRO)"} | |
| render={ | |
| <Button size="sm" variant="secondary" loading={busy} onClick={login} icon={HF_LOGO}> | |
| Sign in | |
| </Button> | |
| } | |
| /> | |
| ); | |
| } | |
| return ( | |
| <DropdownMenu> | |
| <DropdownMenu.Trigger | |
| render={ | |
| <Button size="sm" variant="ghost" className="relative gap-1.5 overflow-hidden pr-1.5 pl-1.5 max-lg:overflow-visible max-lg:px-1"> | |
| <QuotaRing>{user.avatar ? <img className="avatar" src={user.avatar} alt="" /> : <UserIcon size={16} />}</QuotaRing> | |
| <span className="flex items-center gap-1.5 max-lg:hidden"> | |
| <span className="max-w-32 truncate text-[12.5px] font-medium">{user.username || user.name}</span> | |
| {user.isPro && <span className="hw-tag" data-zero="true">PRO</span>} | |
| <CaretDownIcon size={11} weight="bold" className="text-kumo-subtle" /> | |
| </span> | |
| <QuotaStrip /> | |
| </Button> | |
| } | |
| /> | |
| <DropdownMenu.Content sideOffset={6} align="end" className="min-w-52"> | |
| <div className="flex items-center gap-1.5 px-2.5 py-2 text-xs"> | |
| <span className="truncate font-medium text-kumo-strong">{user.name || user.username}</span> | |
| {user.isPro && <span className="hw-tag" data-zero="true">PRO</span>} | |
| </div> | |
| <QuotaSection canPay={!!user.canPay} /> | |
| <DropdownMenu.Separator /> | |
| <DropdownMenu.Item icon={UserCircleIcon} onClick={() => window.open(`https://huggingface.co/${user.username}`, "_blank")}> | |
| Profile | |
| </DropdownMenu.Item> | |
| <DropdownMenu.Item icon={CreditCardIcon} onClick={() => window.open("https://huggingface.co/settings/billing", "_blank")}> | |
| Usage & billing | |
| </DropdownMenu.Item> | |
| <DropdownMenu.Separator /> | |
| <DropdownMenu.Item icon={theme === "dark" ? SunIcon : MoonIcon} onClick={toggleTheme}> | |
| {theme === "dark" ? "Light theme" : "Dark theme"} | |
| </DropdownMenu.Item> | |
| <DropdownMenu.Sub> | |
| <DropdownMenu.SubTrigger icon={QuestionIcon}>Help</DropdownMenu.SubTrigger> | |
| <DropdownMenu.SubContent> | |
| <DropdownMenu.Item icon={RocketLaunchIcon} onClick={() => setDialog("welcome")}> | |
| Quick start | |
| </DropdownMenu.Item> | |
| <DropdownMenu.Item icon={KeyboardIcon} onClick={() => setDialog("shortcuts")}> | |
| Keyboard shortcuts | |
| <DropdownMenu.Shortcut>?</DropdownMenu.Shortcut> | |
| </DropdownMenu.Item> | |
| <DropdownMenu.Separator /> | |
| <DropdownMenu.Item icon={BookOpenTextIcon} onClick={() => window.open("/llms.txt", "_blank", "noopener")}> | |
| Guide for LLM agents | |
| </DropdownMenu.Item> | |
| </DropdownMenu.SubContent> | |
| </DropdownMenu.Sub> | |
| <DropdownMenu.Separator /> | |
| <DropdownMenu.Item | |
| icon={SignOutIcon} | |
| onClick={() => { | |
| 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 | |
| </DropdownMenu.Item> | |
| </DropdownMenu.Content> | |
| </DropdownMenu> | |
| ); | |
| } | |