Files
t3code/apps/web/src/lib/vcsRefState.ts
T
b3e8c0334b T3 Code Mobile [WIP] (#2013)
Co-authored-by: codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
2026-05-29 17:00:54 -07:00

49 lines
1.5 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import {
type VcsRefState,
type VcsRefTarget,
EMPTY_VCS_REF_ATOM,
EMPTY_VCS_REF_STATE,
createVcsRefManager,
getVcsRefTargetKey,
vcsRefStateAtom,
} from "@t3tools/client-runtime";
import { useEffect, useMemo } from "react";
import {
readEnvironmentConnection,
subscribeEnvironmentConnections,
} from "../environments/runtime";
import { appAtomRegistry } from "../rpc/atomRegistry";
const VCS_REF_LIST_LIMIT = 100;
const VCS_REF_STALE_TIME_MS = 5_000;
export const vcsRefManager = createVcsRefManager({
getRegistry: () => appAtomRegistry,
getClient: (environmentId) => readEnvironmentConnection(environmentId)?.client.vcs ?? null,
subscribeClientChanges: subscribeEnvironmentConnections,
watchLimit: VCS_REF_LIST_LIMIT,
staleTimeMs: VCS_REF_STALE_TIME_MS,
onBackgroundError: (error) => {
console.warn("[vcs-refs] background refresh failed", error);
},
});
export function useVcsRefs(target: VcsRefTarget): VcsRefState {
const stableTarget = useMemo(
() => ({
environmentId: target.environmentId,
cwd: target.cwd,
query: target.query ?? null,
}),
[target.cwd, target.environmentId, target.query],
);
const targetKey = getVcsRefTargetKey(stableTarget);
useEffect(() => vcsRefManager.watch(stableTarget), [stableTarget]);
const state = useAtomValue(targetKey !== null ? vcsRefStateAtom(targetKey) : EMPTY_VCS_REF_ATOM);
return targetKey === null ? EMPTY_VCS_REF_STATE : state;
}