Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2
3import type { EnvironmentId, ReviewDiffPreviewSource, ThreadId } from "@t3tools/contracts";
4import { Atom } from "effect/unstable/reactivity";
5
6import { scopedThreadKey } from "../../lib/scopedEntities";
7import { appAtomRegistry } from "../../state/atom-registry";
8import { buildReviewParsedDiff, type ReviewParsedDiff } from "./reviewModel";
9
10const EMPTY_GIT_REVIEW_SECTIONS = Object.freeze<ReadonlyArray<ReviewDiffPreviewSource>>([]);
11const EMPTY_REVIEW_TURN_DIFFS = Object.freeze<Readonly<Record<string, string>>>({});
12const EMPTY_REVIEW_LOADING_TURN_IDS = Object.freeze<Readonly<Record<string, boolean>>>({});
13const EMPTY_REVIEW_ASYNC_STATE = Object.freeze<ReviewAsyncState>({
14 loadingTurnIds: EMPTY_REVIEW_LOADING_TURN_IDS,
15 error: null,
16});
17const EMPTY_REVIEW_SECTION_FILE_IDS = Object.freeze<
18 Readonly<Record<string, ReadonlyArray<string> | undefined>>
19>({});
20const EMPTY_REVIEW_GIT_SECTIONS_ATOM = Atom.make(EMPTY_GIT_REVIEW_SECTIONS).pipe(
21 Atom.keepAlive,
22 Atom.withLabel("mobile:review:git-sections:null"),
23);
24const EMPTY_REVIEW_TURN_DIFFS_ATOM = Atom.make(EMPTY_REVIEW_TURN_DIFFS).pipe(
25 Atom.keepAlive,
26 Atom.withLabel("mobile:review:turn-diffs:null"),
27);
28const EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM = Atom.make<string | null>(null).pipe(
29 Atom.keepAlive,
30 Atom.withLabel("mobile:review:selected-section-id:null"),
31);
32const EMPTY_REVIEW_ASYNC_STATE_ATOM = Atom.make(EMPTY_REVIEW_ASYNC_STATE).pipe(
33 Atom.keepAlive,
34 Atom.withLabel("mobile:review:async-state:null"),
35);
36const EMPTY_REVIEW_SECTION_FILE_IDS_ATOM = Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
37 Atom.keepAlive,
38 Atom.withLabel("mobile:review:section-file-ids:null"),
39);
40
41const reviewGitSectionsByThreadKeyAtom = Atom.family((threadKey: string) =>
42 Atom.make(EMPTY_GIT_REVIEW_SECTIONS).pipe(
43 Atom.keepAlive,
44 Atom.withLabel(`mobile:review:git-sections:${threadKey}`),
45 ),
46);
47
48const reviewTurnDiffByThreadKeyAtom = Atom.family((threadKey: string) =>
49 Atom.make(EMPTY_REVIEW_TURN_DIFFS).pipe(
50 Atom.keepAlive,
51 Atom.withLabel(`mobile:review:turn-diffs:${threadKey}`),
52 ),
53);
54
55const reviewSelectedSectionIdByThreadKeyAtom = Atom.family((threadKey: string) =>
56 Atom.make<string | null>(null).pipe(
57 Atom.keepAlive,
58 Atom.withLabel(`mobile:review:selected-section-id:${threadKey}`),
59 ),
60);
61
62const reviewAsyncStateByThreadKeyAtom = Atom.family((threadKey: string) =>
63 Atom.make(EMPTY_REVIEW_ASYNC_STATE).pipe(
64 Atom.keepAlive,
65 Atom.withLabel(`mobile:review:async-state:${threadKey}`),
66 ),
67);
68
69const reviewExpandedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
70 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
71 Atom.keepAlive,
72 Atom.withLabel(`mobile:review:expanded-file-ids:${threadKey}`),
73 ),
74);
75
76const reviewRevealedLargeFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
77 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
78 Atom.keepAlive,
79 Atom.withLabel(`mobile:review:revealed-large-file-ids:${threadKey}`),
80 ),
81);
82
83const reviewViewedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
84 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
85 Atom.keepAlive,
86 Atom.withLabel(`mobile:review:viewed-file-ids:${threadKey}`),
87 ),
88);
89
90const reviewParsedDiffBySectionCacheKeyAtom = Atom.family((cacheKey: string) =>
91 Atom.make<{ readonly diff: string | null; readonly parsed: ReviewParsedDiff } | null>(null).pipe(
92 Atom.keepAlive,
93 Atom.withLabel(`mobile:review:parsed-diffs:${cacheKey}`),
94 ),
95);
96
97export interface ReviewCacheForThread {
98 readonly threadKey: string | null;
99 readonly gitSections: ReadonlyArray<ReviewDiffPreviewSource>;
100 readonly turnDiffById: Readonly<Record<string, string>>;
101 readonly selectedSectionId: string | null;
102 readonly asyncState: ReviewAsyncState;
103 readonly expandedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
104 readonly revealedLargeFileIdsBySection: Readonly<
105 Record<string, ReadonlyArray<string> | undefined>
106 >;
107 readonly viewedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
108}
109
110export interface ReviewAsyncState {
111 readonly loadingTurnIds: Readonly<Record<string, boolean>>;
112 readonly error: string | null;
113}
114
115function buildThreadKey(input: {
116 readonly environmentId?: EnvironmentId;
117 readonly threadId?: ThreadId;
118}): string | null {
119 return input.environmentId && input.threadId
120 ? scopedThreadKey(input.environmentId, input.threadId)
121 : null;
122}
123
124function buildSectionCacheKey(threadKey: string, sectionId: string): string {
125 return `${threadKey}:${sectionId}`;
126}
127
128export function useReviewCacheForThread(input: {
129 readonly environmentId?: EnvironmentId;
130 readonly threadId?: ThreadId;
131}): ReviewCacheForThread {
132 const threadKey = buildThreadKey(input);
133 const gitSections = useAtomValue(
134 threadKey ? reviewGitSectionsByThreadKeyAtom(threadKey) : EMPTY_REVIEW_GIT_SECTIONS_ATOM,
135 );
136 const turnDiffById = useAtomValue(
137 threadKey ? reviewTurnDiffByThreadKeyAtom(threadKey) : EMPTY_REVIEW_TURN_DIFFS_ATOM,
138 );
139 const selectedSectionId = useAtomValue(
140 threadKey
141 ? reviewSelectedSectionIdByThreadKeyAtom(threadKey)
142 : EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM,
143 );
144 const asyncState = useAtomValue(
145 threadKey ? reviewAsyncStateByThreadKeyAtom(threadKey) : EMPTY_REVIEW_ASYNC_STATE_ATOM,
146 );
147 const expandedFileIdsBySection = useAtomValue(
148 threadKey
149 ? reviewExpandedFileIdsByThreadKeyAtom(threadKey)
150 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
151 );
152 const revealedLargeFileIdsBySection = useAtomValue(
153 threadKey
154 ? reviewRevealedLargeFileIdsByThreadKeyAtom(threadKey)
155 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
156 );
157 const viewedFileIdsBySection = useAtomValue(
158 threadKey ? reviewViewedFileIdsByThreadKeyAtom(threadKey) : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
159 );
160
161 return {
162 threadKey,
163 gitSections,
164 turnDiffById,
165 selectedSectionId,
166 asyncState,
167 expandedFileIdsBySection,
168 revealedLargeFileIdsBySection,
169 viewedFileIdsBySection,
170 };
171}
172
173export function setReviewGitSections(
174 threadKey: string,
175 sections: ReadonlyArray<ReviewDiffPreviewSource>,
176): void {
177 appAtomRegistry.set(reviewGitSectionsByThreadKeyAtom(threadKey), sections);
178}
179
180export function setReviewTurnDiff(threadKey: string, sectionId: string, diff: string): void {
181 const atom = reviewTurnDiffByThreadKeyAtom(threadKey);
182 const current = appAtomRegistry.get(atom);
183 appAtomRegistry.set(atom, {
184 ...current,
185 [sectionId]: diff,
186 });
187}
188
189export function setReviewSelectedSectionId(threadKey: string, sectionId: string | null): void {
190 appAtomRegistry.set(reviewSelectedSectionIdByThreadKeyAtom(threadKey), sectionId);
191}
192
193function updateReviewAsyncState(
194 threadKey: string,
195 update: (current: ReviewAsyncState) => ReviewAsyncState,
196): void {
197 const atom = reviewAsyncStateByThreadKeyAtom(threadKey);
198 appAtomRegistry.set(atom, update(appAtomRegistry.get(atom)));
199}
200
201export function setReviewTurnDiffLoading(
202 threadKey: string,
203 sectionId: string,
204 isLoading: boolean,
205): void {
206 updateReviewAsyncState(threadKey, (current) => {
207 const loadingTurnIds = { ...current.loadingTurnIds };
208 if (isLoading) {
209 loadingTurnIds[sectionId] = true;
210 } else {
211 delete loadingTurnIds[sectionId];
212 }
213 return {
214 ...current,
215 loadingTurnIds,
216 };
217 });
218}
219
220export function setReviewAsyncError(threadKey: string, error: string | null): void {
221 updateReviewAsyncState(threadKey, (current) => ({
222 ...current,
223 error,
224 }));
225}
226
227export function getReviewAsyncStateSnapshot(threadKey: string): ReviewAsyncState {
228 return appAtomRegistry.get(reviewAsyncStateByThreadKeyAtom(threadKey));
229}
230
231export function updateReviewExpandedFileIds(
232 threadKey: string,
233 sectionId: string,
234 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
235): void {
236 const atom = reviewExpandedFileIdsByThreadKeyAtom(threadKey);
237 const current = appAtomRegistry.get(atom);
238 const nextValue = update(current[sectionId]);
239 appAtomRegistry.set(atom, {
240 ...current,
241 [sectionId]: nextValue,
242 });
243}
244
245export function updateReviewViewedFileIds(
246 threadKey: string,
247 sectionId: string,
248 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
249): void {
250 const atom = reviewViewedFileIdsByThreadKeyAtom(threadKey);
251 const current = appAtomRegistry.get(atom);
252 const nextValue = update(current[sectionId]);
253 appAtomRegistry.set(atom, {
254 ...current,
255 [sectionId]: nextValue,
256 });
257}
258
259export function getCachedReviewParsedDiff(input: {
260 readonly threadKey: string | null;
261 readonly sectionId: string | null;
262 readonly diff: string | null | undefined;
263}): ReviewParsedDiff {
264 if (!input.threadKey || !input.sectionId) {
265 return buildReviewParsedDiff(input.diff, input.sectionId ?? "mobile-review");
266 }
267
268 const cacheKey = buildSectionCacheKey(input.threadKey, input.sectionId);
269 const normalizedDiff = input.diff?.trim() ?? null;
270 const atom = reviewParsedDiffBySectionCacheKeyAtom(cacheKey);
271 const cached = appAtomRegistry.get(atom);
272 if (cached && cached.diff === normalizedDiff) {
273 return cached.parsed;
274 }
275
276 const parsed = buildReviewParsedDiff(input.diff, input.sectionId);
277 appAtomRegistry.set(atom, {
278 diff: normalizedDiff,
279 parsed,
280 });
281 return parsed;
282}