@@ -52,7 +52,9 @@ export function useReviewFilePatches({
5252 scope,
5353 requested,
5454 } ) ;
55- const queriedPatchesAtom = patchesAtom ( queries , theme ) ;
55+ const queriedPatchesAtom = reviewPatchesAtom (
56+ JSON . stringify ( [ theme , queries . map ( ( { index, target } ) => [ index , target ] ) ] ) ,
57+ ) ;
5658 const previousPreview = useRef ( { scope, revision, queries : [ ] as typeof queries } ) ;
5759 useEffect ( ( ) => {
5860 const previous = previousPreview . current ;
@@ -157,7 +159,7 @@ export function useReviewFilePatches({
157159 } ;
158160}
159161
160- // The refresh effect and the patches atom key on the identity of these queries.
162+ // The refresh effect keys on the identity of these queries.
161163function reviewFileQueries ( {
162164 environmentId,
163165 cwd,
@@ -184,25 +186,23 @@ function reviewFileQueries({
184186 . filter ( ( index ) => index < files . length )
185187 . map ( ( index ) => {
186188 const file = files [ index ] ! ;
187- return {
188- index,
189- query : reviewEnvironment . diffFilePatch ( {
190- environmentId,
191- input : {
192- cacheKey : scope ,
193- request : {
194- cwd,
195- ...( baseRef ? { baseRef } : { } ) ,
196- ignoreWhitespace,
197- file : {
198- path : file . path ,
199- previousPath : file . previousPath ,
200- sourceKind : source . kind ,
201- } ,
189+ const target : ReviewPatchTarget = {
190+ environmentId,
191+ input : {
192+ cacheKey : scope ,
193+ request : {
194+ cwd,
195+ ...( baseRef ? { baseRef } : { } ) ,
196+ ignoreWhitespace,
197+ file : {
198+ path : file . path ,
199+ previousPath : file . previousPath ,
200+ sourceKind : source . kind ,
202201 } ,
203202 } ,
204- } ) ,
203+ } ,
205204 } ;
205+ return { index, target, query : reviewEnvironment . diffFilePatch ( target ) } ;
206206 } ) ;
207207 return queries ;
208208}
@@ -211,32 +211,36 @@ function sortedReviewFiles(source: ReviewDiffPreviewSource | null) {
211211 return source ?. files ?. toSorted ( ( a , b ) => filePathCollator . compare ( a . path , b . path ) ) ?? [ ] ;
212212}
213213
214- function patchesAtom (
215- queries : ReadonlyArray < {
216- index : number ;
217- query : ReturnType < typeof reviewEnvironment . diffFilePatch > ;
218- } > ,
219- theme : "light" | "dark" ,
220- ) {
221- // Derived atoms parse each query result once, even when another file finishes loading.
222- const parsedQuery = Atom . family ( ( query : ReturnType < typeof reviewEnvironment . diffFilePatch > ) =>
223- Atom . map ( query , ( result ) =>
224- AsyncResult . map ( result , ( source ) => {
225- let patch = getRenderablePatch ( source . diff , `diff-panel:${ theme } ` , {
226- compactPartialHunkOffsets : true ,
227- } ) ;
228- if ( patch ?. kind === "files" && patch . files . length === 1 && source . files ?. length === 1 ) {
229- const stat = source . files [ 0 ] ! ;
230- const file = { ...patch . files [ 0 ] ! , name : stat . path } ;
231- if ( stat . previousPath !== null ) file . prevName = stat . previousPath ;
232- else delete file . prevName ;
233- patch = { ...patch , files : [ file ] } ;
234- }
235- return { source, patch } ;
236- } ) ,
237- ) ,
214+ type ReviewPatchTarget = Parameters < typeof reviewEnvironment . diffFilePatch > [ 0 ] ;
215+
216+ // Derived atoms parse each query result once, even when another file finishes loading.
217+ const parsedPatchAtom = Atom . family ( ( key : string ) => {
218+ const [ theme , target ] = JSON . parse ( key ) as [ "light" | "dark" , ReviewPatchTarget ] ;
219+ return Atom . map ( reviewEnvironment . diffFilePatch ( target ) , ( result ) =>
220+ AsyncResult . map ( result , ( source ) => {
221+ let patch = getRenderablePatch ( source . diff , `diff-panel:${ theme } ` , {
222+ compactPartialHunkOffsets : true ,
223+ } ) ;
224+ if ( patch ?. kind === "files" && patch . files . length === 1 && source . files ?. length === 1 ) {
225+ const stat = source . files [ 0 ] ! ;
226+ const file = { ...patch . files [ 0 ] ! , name : stat . path } ;
227+ if ( stat . previousPath !== null ) file . prevName = stat . previousPath ;
228+ else delete file . prevName ;
229+ patch = { ...patch , files : [ file ] } ;
230+ }
231+ return { source, patch } ;
232+ } ) ,
238233 ) ;
239- return Atom . make (
240- ( get ) => new Map ( queries . map ( ( { index, query } ) => [ index , get ( parsedQuery ( query ) ) ] ) ) ,
234+ } ) ;
235+
236+ // Keyed by theme and query targets so every render with the same files reads the same atom.
237+ const reviewPatchesAtom = Atom . family ( ( key : string ) => {
238+ const [ theme , entries ] = JSON . parse ( key ) as [
239+ "light" | "dark" ,
240+ ReadonlyArray < readonly [ number , ReviewPatchTarget ] > ,
241+ ] ;
242+ const parsed = entries . map (
243+ ( [ index , target ] ) => [ index , parsedPatchAtom ( JSON . stringify ( [ theme , target ] ) ) ] as const ,
241244 ) ;
242- }
245+ return Atom . make ( ( get ) => new Map ( parsed . map ( ( [ index , atom ] ) => [ index , get ( atom ) ] ) ) ) ;
246+ } ) ;
0 commit comments