Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/connectors/LoopableApiTabs.tsx from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/connectors/LoopableApiTabs.tsx
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/connectors/LoopableApiTabs.tsx
-
curl -L -o LoopableApiTabs.tsx https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/connectors/LoopableApiTabs.tsx
3.24 kB
| import { useCallback, useEffect, useId, useRef, useState } from "react"; | |
| import ExternalAccessPane from "../settings/ExternalAccessPane"; | |
| import ApiExplorer from "./ApiExplorer"; | |
| import ApiActivity, { ACTIVITY_ERROR, readApiActivity } from "./ApiActivity"; | |
| import "./apiExplorer.css"; | |
| type Tab = "Keys" | "Explorer" | "Activity"; | |
| export default function LoopableApiTabs({ onChanged }: { onChanged: () => void }) { | |
| const [tab, setTab] = useState<Tab>("Keys"); | |
| const [access, setAccess] = useState<"loading" | "admin" | "denied" | "error">("loading"); | |
| const [revision, setRevision] = useState(0); | |
| const id = useId(); | |
| const buttons = useRef<(HTMLButtonElement | null)[]>([]); | |
| const activityRequest = useRef<AbortController | null>(null); | |
| const activityAuthorized = access === "admin"; | |
| const tabs: Tab[] = activityAuthorized ? ["Keys", "Explorer", "Activity"] : ["Keys", "Explorer"]; | |
| const onForbidden = useCallback(() => { | |
| setAccess("denied"); | |
| setTab(current => current === "Activity" ? "Keys" : current); | |
| buttons.current[0]?.focus(); | |
| }, []); | |
| useEffect(() => { | |
| const controller = new AbortController(); | |
| activityRequest.current = controller; | |
| setAccess("loading"); | |
| void readApiActivity({ limit: 1 }, controller.signal).then(result => { | |
| if (controller.signal.aborted) return; | |
| setAccess(result.status === 200 && result.data ? "admin" : result.status === 403 ? "denied" : "error"); | |
| }).catch(() => { if (!controller.signal.aborted) setAccess("error"); }); | |
| return () => controller.abort(); | |
| }, [revision]); | |
| function select(index: number) { | |
| setTab(tabs[index]); | |
| buttons.current[index]?.focus(); | |
| } | |
| return <section className="conn-api-tabs"> | |
| <div className="conn-modal-tabs" role="tablist" aria-label="Loopable API sections"> | |
| {tabs.map((label, index) => <button key={label} type="button" role="tab" | |
| ref={(element) => { buttons.current[index] = element; }} | |
| id={`${id}-${label}`} className="conn-modal-tab" aria-selected={tab === label} | |
| aria-controls={`${id}-panel`} tabIndex={tab === label ? 0 : -1} | |
| onClick={() => setTab(label)} onKeyDown={(event) => { | |
| const next = event.key === "Home" ? 0 : event.key === "End" ? tabs.length - 1 | |
| : event.key === "ArrowRight" ? (index + 1) % tabs.length | |
| : event.key === "ArrowLeft" ? (index + tabs.length - 1) % tabs.length : null; | |
| if (next !== null) { event.preventDefault(); select(next); } | |
| }}>{label}</button>)} | |
| </div> | |
| {access === "error" ? <div className="conn-kc set-actions conn-api-access-error"> | |
| <p className="set-help" role="alert">{ACTIVITY_ERROR}</p> | |
| <button type="button" className="set-secondary" onClick={() => { | |
| activityRequest.current?.abort(); setAccess("loading"); setRevision(value => value + 1); | |
| }}>Retry</button> | |
| </div> : null} | |
| <div id={`${id}-panel`} role="tabpanel" aria-labelledby={`${id}-${tab}`} tabIndex={0}> | |
| {tab === "Keys" ? <ExternalAccessPane view="api" onChanged={onChanged} /> | |
| : tab === "Explorer" ? <ApiExplorer onKeys={() => select(0)} /> | |
| : activityAuthorized ? <ApiActivity onForbidden={onForbidden} /> : null} | |
| </div> | |
| </section>; | |
| } | |