Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 type VcsRefState,
4 type VcsRefTarget,
5 EMPTY_VCS_REF_ATOM,
6 EMPTY_VCS_REF_STATE,
7 createVcsRefManager,
8 getVcsRefTargetKey,
9 vcsRefStateAtom,
10} from "@t3tools/client-runtime";
11import { useEffect, useMemo } from "react";
12
13import {
14 readEnvironmentConnection,
15 subscribeEnvironmentConnections,
16} from "../environments/runtime";
17import { appAtomRegistry } from "../rpc/atomRegistry";
18
19const VCS_REF_LIST_LIMIT = 100;
20const VCS_REF_STALE_TIME_MS = 5_000;
21
22export const vcsRefManager = createVcsRefManager({
23 getRegistry: () => appAtomRegistry,
24 getClient: (environmentId) => readEnvironmentConnection(environmentId)?.client.vcs ?? null,
25 subscribeClientChanges: subscribeEnvironmentConnections,
26 watchLimit: VCS_REF_LIST_LIMIT,
27 staleTimeMs: VCS_REF_STALE_TIME_MS,
28 onBackgroundError: (error) => {
29 console.warn("[vcs-refs] background refresh failed", error);
30 },
31});
32
33export function useVcsRefs(target: VcsRefTarget): VcsRefState {
34 const stableTarget = useMemo(
35 () => ({
36 environmentId: target.environmentId,
37 cwd: target.cwd,
38 query: target.query ?? null,
39 }),
40 [target.cwd, target.environmentId, target.query],
41 );
42 const targetKey = getVcsRefTargetKey(stableTarget);
43
44 useEffect(() => vcsRefManager.watch(stableTarget), [stableTarget]);
45
46 const state = useAtomValue(targetKey !== null ? vcsRefStateAtom(targetKey) : EMPTY_VCS_REF_ATOM);
47 return targetKey === null ? EMPTY_VCS_REF_STATE : state;
48}