Files
t3code/apps/web/src/lib/vcsStatusState.ts
T
Julius Marminge 4ec9647ce7 Rename git state to VCS and add diff preview caching
- Replace git-scoped client/runtime state with VCS-scoped APIs
- Add checkpoint diff preview state and reuse cached diffs
- Update mobile and web review flows to use the new shared state
2026-05-08 20:41:39 -07:00

64 lines
2.0 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import {
type VcsStatusClient,
type VcsStatusState,
type VcsStatusTarget,
EMPTY_VCS_STATUS_ATOM,
EMPTY_VCS_STATUS_STATE,
createVcsStatusManager,
getVcsStatusTargetKey,
vcsStatusStateAtom,
} from "@t3tools/client-runtime";
import type { EnvironmentId } from "@t3tools/contracts";
import { useEffect } from "react";
import {
readEnvironmentConnection,
subscribeEnvironmentConnections,
} from "../environments/runtime";
import { appAtomRegistry } from "../rpc/atomRegistry";
export type { VcsStatusState, VcsStatusTarget };
const manager = createVcsStatusManager({
getRegistry: () => appAtomRegistry,
getClient: (environmentId) => {
const connection = readEnvironmentConnection(environmentId as EnvironmentId);
return connection ? connection.client.vcs : null;
},
getClientIdentity: (environmentId) => {
const connection = readEnvironmentConnection(environmentId as EnvironmentId);
return connection ? connection.environmentId : null;
},
subscribeClientChanges: subscribeEnvironmentConnections,
});
export function getVcsStatusSnapshot(target: VcsStatusTarget): VcsStatusState {
return manager.getSnapshot(target);
}
export function watchVcsStatus(target: VcsStatusTarget, client?: VcsStatusClient): () => void {
return manager.watch(target, client);
}
export function refreshVcsStatus(target: VcsStatusTarget, client?: VcsStatusClient) {
return manager.refresh(target, client);
}
export function resetVcsStatusStateForTests(): void {
manager.reset();
}
export function useVcsStatus(target: VcsStatusTarget): VcsStatusState {
const targetKey = getVcsStatusTargetKey(target);
useEffect(
() => manager.watch({ environmentId: target.environmentId, cwd: target.cwd }),
[target.environmentId, target.cwd],
);
const state = useAtomValue(
targetKey !== null ? vcsStatusStateAtom(targetKey) : EMPTY_VCS_STATUS_ATOM,
);
return targetKey === null ? EMPTY_VCS_STATUS_STATE : state;
}