Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useReviewSections.ts

164 lines6.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem";
3import { Atom } from "effect/reactivity";
4import { msg, useMessages } from "gt-react-native";
5​
6import { deriveThreadCheckpointSummaries } from "@t3tools/client-runtime/state/thread-checkpoints";
7import type { EnvironmentId, ReviewDiffPreviewSource, ThreadId } from "@t3tools/contracts";
8​
9import { useCheckpointDiff } from "../../state/queries";
10import { useEnvironmentQuery } from "../../state/query";
11import { useEnvironmentPresentation } from "../../state/presentation";
12import { lastReviewGitSectionsAtom, reviewEnvironment } from "../../state/review";
13import { lastReviewTurnDiffsAtom } from "../../state/reviewTurnDiffs";
14import { environmentSession } from "../../state/session";
15import { useSelectedThreadProjection } from "../../state/use-thread-detail";
16import { useSelectedThreadWorktree } from "../../state/use-selected-thread-worktree";
17import {
18 buildReviewSectionItems,
19 getReadyReviewCheckpoints,
20 getReviewSectionIdForCheckpoint,
21 resolveReviewSelectedSectionId,
22 reviewTurnDiffsBySectionId,
23 type ReviewSectionItem,
24} from "./reviewModel";
25import { setReviewSelectedSectionId, type ReviewCacheForThread } from "./reviewState";
26​
27const NO_GIT_SECTIONS_ATOM = Atom.make<ReadonlyArray<ReviewDiffPreviewSource>>([]).pipe(
28 Atom.keepAlive,
29 Atom.withLabel("mobile:review:git-sections:null"),
30);
31const NO_TURN_DIFFS_ATOM = Atom.make<Readonly<Record<number, string>>>({}).pipe(
32 Atom.keepAlive,
33 Atom.withLabel("mobile:review:turn-diffs:null"),
34);
35​
36export function useReviewSections(input: {
37 readonly enabled?: boolean;
38 readonly environmentId?: EnvironmentId;
39 readonly threadId?: ThreadId;
40 readonly reviewCache: ReviewCacheForThread;
41}) {
42 const { environmentId, reviewCache, threadId } = input;
43 const messages = useMessages();
44 const enabled = input.enabled ?? true;
45 const fileAccessSession = useEnvironmentQuery(
46 environmentId === undefined ? null : environmentSession.sessionStateAtom(environmentId),
47 );
48 const fileEnvironment = useEnvironmentPresentation(environmentId ?? null);
49 const fileAccess = resolveFilesystemReadAccess(
50 {
51 isCatalogReady: fileEnvironment.isReady,
52 connection: fileEnvironment.presentation?.connection ?? null,
53 session: fileAccessSession.data,
54 sessionError: fileAccessSession.error,
55 },
56 messages,
57 );
58 const { canReadFiles } = fileAccess;
59 const selectedThread = useSelectedThreadProjection();
60 const { selectedThreadCwd } = useSelectedThreadWorktree();
61 const diffPreview = useEnvironmentQuery(
62 canReadFiles && enabled && environmentId !== undefined && selectedThreadCwd !== null
63 ? reviewEnvironment.diffPreview({
64 environmentId,
65 input: { cwd: selectedThreadCwd },
66 })
67 : null,
68 );
69 const lastGitSections = useAtomValue(
70 environmentId !== undefined && selectedThreadCwd !== null
71 ? lastReviewGitSectionsAtom(environmentId, selectedThreadCwd)
72 : NO_GIT_SECTIONS_ATOM,
73 );
74​
75 const lastTurnDiffs = useAtomValue(
76 environmentId !== undefined && threadId !== undefined
77 ? lastReviewTurnDiffsAtom(environmentId, threadId)
78 : NO_TURN_DIFFS_ATOM,
79 );
80​
81 const readyCheckpoints = getReadyReviewCheckpoints(
82 selectedThread === null ? [] : deriveThreadCheckpointSummaries(selectedThread.projection),
83 );
84 const checkpointBySectionId = new Map(
85 readyCheckpoints.map((checkpoint) => [getReviewSectionIdForCheckpoint(checkpoint), checkpoint]),
86 );
87 // Turns show their last answered diff; the active turn's live query below overrides it.
88 // Section ids and the selection do not depend on the live query, so they are resolved first.
89 const cachedSections = buildReviewSectionItems({
90 checkpoints: readyCheckpoints,
91 gitSections:
92 canReadFiles || fileAccess.isPending ? (diffPreview.data?.sources ?? lastGitSections) : [],
93 turnDiffById: reviewTurnDiffsBySectionId(readyCheckpoints, lastTurnDiffs),
94 loadingGitSections: fileAccess.isPending || diffPreview.isPending,
95 resolve: messages,
96 });
97 const selectedSectionId = resolveReviewSelectedSectionId(
98 cachedSections,
99 reviewCache.selectedSectionId,
100 );
101​
102 const selectedCheckpoint =
103 selectedSectionId === null ? undefined : checkpointBySectionId.get(selectedSectionId);
104 const activeCheckpoint = selectedCheckpoint ?? readyCheckpoints[0] ?? null;
105 const activeSectionId = activeCheckpoint
106 ? getReviewSectionIdForCheckpoint(activeCheckpoint)
107 : null;
108 const activeTurnDiff = useCheckpointDiff({
109 environmentId: enabled ? (environmentId ?? null) : null,
110 threadId: enabled ? (threadId ?? null) : null,
111 fromTurnCount:
112 enabled && activeCheckpoint ? Math.max(0, activeCheckpoint.checkpointTurnCount - 1) : null,
113 toTurnCount: enabled ? (activeCheckpoint?.checkpointTurnCount ?? null) : null,
114 ignoreWhitespace: false,
115 });
116​
117 const reviewSections = cachedSections.map((section): ReviewSectionItem => {
118 if (section.kind === "turn") {
119 return section.id === activeSectionId
120 ? {
121 ...section,
122 diff: activeTurnDiff.data?.diff ?? section.diff,
123 isLoading: activeTurnDiff.isPending,
124 }
125 : section;
126 }
127 // Keep the selected section while its grant loads, without displaying cached host files.
128 return fileAccess.isPending ? { ...section, diff: null, isLoading: true } : section;
129 });
130 const selectedSection =
131 reviewSections.find((section) => section.id === selectedSectionId) ?? null;
132​
133 const refreshSelectedSection = async () => {
134 if (!enabled) {
135 return;
136 }
137 if (selectedSection?.kind === "turn") {
138 activeTurnDiff.refresh();
139 return;
140 }
141 diffPreview.refresh();
142 };
143​
144 const selectSection = (sectionId: string) => {
145 if (reviewCache.threadKey) {
146 setReviewSelectedSectionId(reviewCache.threadKey, sectionId);
147 }
148 };
149​
150 return {
151 error:
152 diffPreview.error ??
153 activeTurnDiff.error ??
154 (selectedSection === null && !fileAccess.isPending && !canReadFiles
155 ? (fileAccess.error ?? messages(msg("This connection cannot read local diffs.")))
156 : null),
157 isSelectedSectionPending:
158 selectedSection?.kind === "turn" ? activeTurnDiff.isPending : diffPreview.isPending,
159 reviewSections,
160 selectedSection,
161 refreshSelectedSection,
162 selectSection,
163 };