File size: 3,244 Bytes
8145bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
8145bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
8145bee
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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>;
}