1import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts";
2import { useEffect, useState } from "react";
3
4import { readDesktopSecondaryBootstraps } from "./desktopLocal";
5
6const DESKTOP_LOCAL_BOOTSTRAP_POLL_MS = 2_000;
7
8/**
9 * Reactively track the desktop's secondary local backends (e.g. a parallel WSL
10 * backend). The bridge exposes no change event, so we re-read on an interval;
11 * failed reads retain the latest successful snapshot, while a successful empty
12 * read clears it. Use this instead of polling the bridge ad hoc so every
13 * renderer consumer reads the same topology.
14 */
15export function useDesktopLocalBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
16 const [bootstraps, setBootstraps] = useState<ReadonlyArray<DesktopEnvironmentBootstrap>>(
17 readDesktopSecondaryBootstraps,
18 );
19
20 useEffect(() => {
21 const read = () => setBootstraps(readDesktopSecondaryBootstraps());
22 read();
23 const interval = setInterval(read, DESKTOP_LOCAL_BOOTSTRAP_POLL_MS);
24 return () => clearInterval(interval);
25 }, []);
26
27 return bootstraps;
28}