All files / src/components/spe use-spe-status.ts

92.59% Statements 50/54
88.46% Branches 23/26
87.5% Functions 14/16
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 905x             5x   5x   5x                                     74x 74x 74x 74x 74x 74x   74x 47x 47x   44x     44x 28x   16x     16x 16x 16x     29x 25x 25x     45x   47x 47x       74x 74x 72x 29x 6x 6x 3x 3x   6x       74x 74x 1x 1x 1x     74x    
"use client";
 
import { useCallback, useEffect, useRef, useState } from "react";
import { fetchApi } from "@/lib/api";
import type { SpeStatusResponse } from "@/lib/spe";
 
/** How often a page asks again while a workspace starts (ADR-072). */
export const SPE_POLL_MS = 10_000;
/** How long a page keeps asking after a Start or Stop, at most. */
const SPE_WATCH_MS = 10 * 60_000;
/** Polls after a Start or Stop even before ARM reports the change. */
const SPE_WATCH_MIN_POLLS = 3;
 
/**
 * Reads `/api/admin/spe`. Every admin page that shows an SPE uses this,
 * so there is one source for what the SPEs are (#774).
 *
 * While a workspace is starting it asks again every 10 s until it runs
 * (ADR-072), for at most ten minutes after the page opened or the last
 * `watch()`. `watch()` is what a Start or Stop button calls: it asks at once
 * and keeps asking, so the page follows the workspace from the button to
 * running or stopped.
 */
export function useSpeStatus(): {
  data: SpeStatusResponse | null;
  loading: boolean;
  error: string | null;
  reload: () => void;
  watch: () => void;
} {
  const [data, setData] = useState<SpeStatusResponse | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [tick, setTick] = useState(0);
  const watchUntil = useRef(Date.now() + SPE_WATCH_MS);
  const watchPolls = useRef(0);
 
  useEffect(() => {
    let cancelled = false;
    fetchApi("/api/admin/spe")
      .then(async (r) => {
        const body = (await r.json()) as
          | (SpeStatusResponse & { error?: string })
          | null;
        if (!r.ok || !body || body.error || !Array.isArray(body.spes)) {
          throw new Error(body?.error || `HTTP ${r.status}`);
        }
        return body;
      })
      .then((body) => {
        Iif (cancelled) return;
        setData(body);
        setError(null);
      })
      .catch((e: unknown) => {
        if (cancelled) return;
        setData(null);
        setError(e instanceof Error ? e.message : String(e));
      })
      .finally(() => {
        if (!cancelled) setLoading(false);
      });
    return () => {
      cancelled = true;
    };
  }, [tick]);
 
  const starting = Boolean(data?.spes.some((s) => s.powerState === "starting"));
  useEffect(() => {
    if (loading) return;
    if (!starting && watchPolls.current === 0) return;
    Iif (Date.now() > watchUntil.current) return;
    const timer = setTimeout(() => {
      if (watchPolls.current > 0) watchPolls.current -= 1;
      setTick((n) => n + 1);
    }, SPE_POLL_MS);
    return () => clearTimeout(timer);
    // tick: a failed read leaves data as it was, and must not end the polling.
  }, [data, loading, starting, tick]);
 
  const reload = useCallback(() => setTick((n) => n + 1), []);
  const watch = useCallback(() => {
    watchUntil.current = Date.now() + SPE_WATCH_MS;
    watchPolls.current = SPE_WATCH_MIN_POLLS;
    setTick((n) => n + 1);
  }, []);
 
  return { data, loading, error, reload, watch };
}