Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 type VcsStatusClient,
4 type VcsStatusState,
5 type VcsStatusTarget,
6 EMPTY_VCS_STATUS_ATOM,
7 EMPTY_VCS_STATUS_STATE,
8 createVcsStatusManager,
9 getVcsStatusTargetKey,
10 vcsStatusStateAtom,
11} from "@t3tools/client-runtime";
12import type { EnvironmentId } from "@t3tools/contracts";
13import { useEffect } from "react";
14
15import {
16 readEnvironmentConnection,
17 subscribeEnvironmentConnections,
18} from "../environments/runtime";
19import { appAtomRegistry } from "../rpc/atomRegistry";
20
21export type { VcsStatusState, VcsStatusTarget };
22
23const manager = createVcsStatusManager({
24 getRegistry: () => appAtomRegistry,
25 getClient: (environmentId) => {
26 const connection = readEnvironmentConnection(environmentId as EnvironmentId);
27 return connection ? connection.client.vcs : null;
28 },
29 getClientIdentity: (environmentId) => {
30 const connection = readEnvironmentConnection(environmentId as EnvironmentId);
31 return connection ? connection.environmentId : null;
32 },
33 subscribeClientChanges: subscribeEnvironmentConnections,
34});
35
36export function getVcsStatusSnapshot(target: VcsStatusTarget): VcsStatusState {
37 return manager.getSnapshot(target);
38}
39
40export function watchVcsStatus(target: VcsStatusTarget, client?: VcsStatusClient): () => void {
41 return manager.watch(target, client);
42}
43
44export function refreshVcsStatus(target: VcsStatusTarget, client?: VcsStatusClient) {
45 return manager.refresh(target, client);
46}
47
48export function resetVcsStatusStateForTests(): void {
49 manager.reset();
50}
51
52export function useVcsStatus(target: VcsStatusTarget): VcsStatusState {
53 const targetKey = getVcsStatusTargetKey(target);
54 useEffect(
55 () => manager.watch({ environmentId: target.environmentId, cwd: target.cwd }),
56 [target.environmentId, target.cwd],
57 );
58
59 const state = useAtomValue(
60 targetKey !== null ? vcsStatusStateAtom(targetKey) : EMPTY_VCS_STATUS_ATOM,
61 );
62 return targetKey === null ? EMPTY_VCS_STATUS_STATE : state;
63}