The best way to control Claude Code, Codex and more from any computer or phone

main
ts 57 lines 2.03 KB
Raw
1 import { useMessages } from "gt-react";
2 import type { EnvironmentId, PullRequestRef } from "@t3tools/contracts";
3 import { useSyncExternalStore } from "react";
4
5 import { pullRequestEnvironment } from "~/state/pullRequests";
6 import { useEnvironmentQuery } from "~/state/query";
7
8 import { filesViewedScope } from "./filesViewedStore";
9 import {
10 isFileViewed,
11 isStaleViewedState,
12 toFileViewedStates,
13 } from "./pullRequestFilesViewed.logic";
14
15 export interface PullRequestFilesViewedView {
16 readonly enabled: boolean;
17 readonly isViewed: (path: string) => boolean;
18 readonly isStale: (path: string) => boolean;
19 readonly setViewed: (path: string, viewed: boolean, revision?: string) => void;
20 readonly truncated: boolean;
21 readonly error: string | null;
22 readonly refresh: () => void;
23 }
24
25 const NO_OVERLAY = new Map<string, boolean>();
26 // Without this a change request that cannot record views would still be handed a scope handle.
27 const DISABLED_SCOPE = {
28 subscribe: () => () => {},
29 getSnapshot: () => NO_OVERLAY,
30 setViewed: () => {},
31 };
32
33 export function usePullRequestFilesViewed(options: {
34 readonly environmentId: EnvironmentId;
35 readonly reference: PullRequestRef;
36 readonly enabled: boolean;
37 }): PullRequestFilesViewedView {
38 const { environmentId, reference, enabled } = options;
39 const query = useEnvironmentQuery(
40 enabled ? pullRequestEnvironment.filesViewed({ environmentId, input: reference }) : null,
41 useMessages(),
42 );
43 const states = toFileViewedStates(query.data);
44 // Stable per scope: `setViewed` is handed to every file header the viewer draws.
45 const scope = enabled ? filesViewedScope({ environmentId, reference }) : DISABLED_SCOPE;
46 const overlay = useSyncExternalStore(scope.subscribe, scope.getSnapshot);
47
48 return {
49 enabled,
50 isViewed: (path: string) => isFileViewed(path, states, overlay),
51 isStale: (path: string) => !overlay.has(path) && isStaleViewedState(states?.get(path)),
52 setViewed: scope.setViewed,
53 truncated: query.data?.truncated === true,
54 error: query.error,
55 refresh: query.refresh,
56 };
57 }