Skip to content

Commit fe60590

Browse files
committed
improvement(files): derive routed-file presence from existing selectedFile memo
Local review follow-ups: reuse the memoized selectedFile/selectedFileRef instead of a second O(n) scan per render, and collapse the applied-mode ref to string | null — initializing at null (the list view) preserves the pre-existing fresh-mount behavior while still deferring deep-link mode application until the file record loads.
1 parent df46f60 commit fe60590

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/files

apps/sim/app/workspace/[workspaceId]/files/files.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,14 +1294,14 @@ export function Files() {
12941294
}, [canEdit, uploading, closeListContextMenu])
12951295

12961296
/**
1297-
* Tracks the route target whose preview mode has been applied. Starts unset
1298-
* (rather than at the initial route id) because on a hard load the files
1299-
* list may not have arrived when the mode initializer ran — a deep-linked
1300-
* previewable file would otherwise be locked into the code editor.
1297+
* Tracks the route target whose preview mode has been applied. Starts at
1298+
* null (the list view) rather than the initial route id because on a hard
1299+
* load the files list may not have arrived when the mode initializer ran —
1300+
* a deep-linked previewable file would otherwise be locked into the code
1301+
* editor. The effect therefore defers until the routed file record exists.
13011302
*/
1302-
const appliedModeFileIdRef = useRef<string | null | undefined>(undefined)
1303-
const routedFileLoaded =
1304-
fileIdFromRoute != null && files.some((file) => file.id === fileIdFromRoute)
1303+
const appliedModeFileIdRef = useRef<string | null>(null)
1304+
const routedFileLoaded = selectedFile != null
13051305
useEffect(() => {
13061306
if (fileIdFromRoute === appliedModeFileIdRef.current) return
13071307
const isJustCreated =
@@ -1311,14 +1311,9 @@ export function Files() {
13111311
}
13121312
if (fileIdFromRoute != null && !routedFileLoaded && !isJustCreated) return
13131313
appliedModeFileIdRef.current = fileIdFromRoute
1314-
const nextMode: PreviewMode = isJustCreated
1315-
? 'editor'
1316-
: (() => {
1317-
const file = fileIdFromRoute
1318-
? filesRef.current.find((f) => f.id === fileIdFromRoute)
1319-
: null
1320-
return file && isPreviewable(file) ? 'preview' : 'editor'
1321-
})()
1314+
const file = fileIdFromRoute ? selectedFileRef.current : null
1315+
const nextMode: PreviewMode =
1316+
!isJustCreated && file && isPreviewable(file) ? 'preview' : 'editor'
13221317
setPreviewMode((current) => (nextMode === current ? current : nextMode))
13231318
}, [fileIdFromRoute, isNewFile, routedFileLoaded])
13241319

0 commit comments

Comments
 (0)