File size: 899 Bytes
85a4cbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useCallback } from "preact/hooks";
import type { TestConnectionResult } from "../types.js";

export function useTestConnection() {
  const [testing, setTesting] = useState(false);
  const [result, setResult] = useState<TestConnectionResult | null>(null);
  const [error, setError] = useState<string | null>(null);

  const runTest = useCallback(async () => {
    setTesting(true);
    setError(null);
    setResult(null);
    try {
      const resp = await fetch("/admin/test-connection", { method: "POST" });
      if (!resp.ok) {
        setError(`HTTP ${resp.status}`);
        return;
      }
      const data = (await resp.json()) as TestConnectionResult;
      setResult(data);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Network error");
    } finally {
      setTesting(false);
    }
  }, []);

  return { testing, result, error, runTest };
}