Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2
3import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import { Atom } from "effect/reactivity";
5
6import { scopedThreadKey } from "../../lib/scopedEntities";
7import { appAtomRegistry } from "../../state/atom-registry";
8import { clearReviewCommentTarget } from "./reviewCommentSelection";
9import { buildReviewParsedDiff, type ReviewParsedDiff } from "./reviewModel";
10
11const EMPTY_REVIEW_SECTION_FILE_IDS = Object.freeze<
12 Readonly<Record<string, ReadonlyArray<string> | undefined>>
13>({});
14const EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM = Atom.make<string | null>(null).pipe(
15 Atom.keepAlive,
16 Atom.withLabel("mobile:review:selected-section-id:null"),
17);
18const EMPTY_REVIEW_SECTION_FILE_IDS_ATOM = Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
19 Atom.keepAlive,
20 Atom.withLabel("mobile:review:section-file-ids:null"),
21);
22
23const reviewSelectedSectionIdByThreadKeyAtom = Atom.family((threadKey: string) =>
24 Atom.make<string | null>(null).pipe(
25 Atom.keepAlive,
26 Atom.withLabel(`mobile:review:selected-section-id:${threadKey}`),
27 ),
28);
29
30const reviewExpandedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
31 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
32 Atom.keepAlive,
33 Atom.withLabel(`mobile:review:expanded-file-ids:${threadKey}`),
34 ),
35);
36
37const reviewRevealedLargeFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
38 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
39 Atom.keepAlive,
40 Atom.withLabel(`mobile:review:revealed-large-file-ids:${threadKey}`),
41 ),
42);
43
44const reviewViewedFileIdsByThreadKeyAtom = Atom.family((threadKey: string) =>
45 Atom.make(EMPTY_REVIEW_SECTION_FILE_IDS).pipe(
46 Atom.keepAlive,
47 Atom.withLabel(`mobile:review:viewed-file-ids:${threadKey}`),
48 ),
49);
50
51export const MAX_CACHED_REVIEW_DIFFS = 8;
52// This bounds source string length, not the parsed or native heap size.
53export const MAX_CACHED_REVIEW_SOURCE_CHARACTERS = 4 * 1024 * 1024;
54
55interface CachedReviewParsedDiff {
56 readonly diff: string | null;
57 readonly parsed: ReviewParsedDiff;
58 readonly sourceCharacterCount: number;
59}
60
61const reviewParsedDiffCacheAtom = Atom.make(() => ({
62 entries: new Map<string, CachedReviewParsedDiff>(),
63 sourceCharacterCount: 0,
64})).pipe(Atom.keepAlive, Atom.withLabel("mobile:review:parsed-diffs"));
65
66export interface ReviewCacheForThread {
67 readonly threadKey: string | null;
68 readonly selectedSectionId: string | null;
69 readonly expandedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
70 readonly revealedLargeFileIdsBySection: Readonly<
71 Record<string, ReadonlyArray<string> | undefined>
72 >;
73 readonly viewedFileIdsBySection: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
74}
75
76function buildThreadKey(input: {
77 readonly environmentId?: EnvironmentId;
78 readonly threadId?: ThreadId;
79}): string | null {
80 return input.environmentId && input.threadId
81 ? scopedThreadKey(input.environmentId, input.threadId)
82 : null;
83}
84
85function buildSectionCacheKey(threadKey: string, sectionId: string): string {
86 return `${threadKey}:${sectionId}`;
87}
88
89export function useReviewCacheForThread(input: {
90 readonly environmentId?: EnvironmentId;
91 readonly threadId?: ThreadId;
92}): ReviewCacheForThread {
93 const threadKey = buildThreadKey(input);
94 const selectedSectionId = useAtomValue(
95 threadKey
96 ? reviewSelectedSectionIdByThreadKeyAtom(threadKey)
97 : EMPTY_REVIEW_SELECTED_SECTION_ID_ATOM,
98 );
99 const expandedFileIdsBySection = useAtomValue(
100 threadKey
101 ? reviewExpandedFileIdsByThreadKeyAtom(threadKey)
102 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
103 );
104 const revealedLargeFileIdsBySection = useAtomValue(
105 threadKey
106 ? reviewRevealedLargeFileIdsByThreadKeyAtom(threadKey)
107 : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
108 );
109 const viewedFileIdsBySection = useAtomValue(
110 threadKey ? reviewViewedFileIdsByThreadKeyAtom(threadKey) : EMPTY_REVIEW_SECTION_FILE_IDS_ATOM,
111 );
112
113 return {
114 threadKey,
115 selectedSectionId,
116 expandedFileIdsBySection,
117 revealedLargeFileIdsBySection,
118 viewedFileIdsBySection,
119 };
120}
121
122export function setReviewSelectedSectionId(threadKey: string, sectionId: string | null): void {
123 // A line selection belongs to the section it was made in; switching away drops it, so
124 // switching back does not bring back a range over lines that may have changed.
125 clearReviewCommentTarget();
126 appAtomRegistry.set(reviewSelectedSectionIdByThreadKeyAtom(threadKey), sectionId);
127}
128
129export function updateReviewExpandedFileIds(
130 threadKey: string,
131 sectionId: string,
132 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
133): void {
134 const atom = reviewExpandedFileIdsByThreadKeyAtom(threadKey);
135 const current = appAtomRegistry.get(atom);
136 const nextValue = update(current[sectionId]);
137 appAtomRegistry.set(atom, {
138 ...current,
139 [sectionId]: nextValue,
140 });
141}
142
143export function updateReviewViewedFileIds(
144 threadKey: string,
145 sectionId: string,
146 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
147): void {
148 const atom = reviewViewedFileIdsByThreadKeyAtom(threadKey);
149 const current = appAtomRegistry.get(atom);
150 const nextValue = update(current[sectionId]);
151 appAtomRegistry.set(atom, {
152 ...current,
153 [sectionId]: nextValue,
154 });
155}
156
157export function getReviewParsedDiffSourceCharacterCount(input: {
158 readonly threadKey: string;
159 readonly sectionId: string;
160 readonly diff: string | null;
161}): number {
162 const sourceCharacterCount = input.diff?.length ?? 0;
163 const cache = appAtomRegistry.get(reviewParsedDiffCacheAtom);
164 const cached = cache.entries.get(buildSectionCacheKey(input.threadKey, input.sectionId));
165 return cached && cached.diff === (input.diff?.trim() ?? null)
166 ? Math.max(sourceCharacterCount, cached.sourceCharacterCount)
167 : sourceCharacterCount;
168}
169
170export function getCachedReviewParsedDiff(input: {
171 readonly threadKey: string | null;
172 readonly sectionId: string | null;
173 readonly diff: string | null | undefined;
174}): ReviewParsedDiff {
175 if (!input.threadKey || !input.sectionId) {
176 return buildReviewParsedDiff(input.diff, input.sectionId ?? "mobile-review");
177 }
178
179 const cacheKey = buildSectionCacheKey(input.threadKey, input.sectionId);
180 const normalizedDiff = input.diff?.trim() ?? null;
181 const cache = appAtomRegistry.get(reviewParsedDiffCacheAtom);
182 const cached = cache.entries.get(cacheKey);
183 if (cached && cached.diff === normalizedDiff) {
184 cache.entries.delete(cacheKey);
185 cache.entries.set(cacheKey, cached);
186 return cached.parsed;
187 }
188
189 const parsed = buildReviewParsedDiff(input.diff, input.sectionId);
190 if (cached) {
191 cache.entries.delete(cacheKey);
192 cache.sourceCharacterCount -= cached.sourceCharacterCount;
193 }
194 const sourceCharacterCount = input.diff?.length ?? 0;
195 if (sourceCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
196 return parsed;
197 }
198
199 for (const [oldestKey, oldest] of cache.entries) {
200 if (
201 cache.entries.size < MAX_CACHED_REVIEW_DIFFS &&
202 cache.sourceCharacterCount + sourceCharacterCount <= MAX_CACHED_REVIEW_SOURCE_CHARACTERS
203 ) {
204 break;
205 }
206 cache.entries.delete(oldestKey);
207 cache.sourceCharacterCount -= oldest.sourceCharacterCount;
208 }
209 cache.entries.set(cacheKey, {
210 diff: normalizedDiff,
211 parsed,
212 sourceCharacterCount,
213 });
214 cache.sourceCharacterCount += sourceCharacterCount;
215 return parsed;
216}