Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Open md in split view
  • Loading branch information
Theodore Li committed Mar 17, 2026
commit 464c0ec36ca8864cbecc75f5c2803be99536ac79
10 changes: 8 additions & 2 deletions apps/sim/app/workspace/[workspaceId]/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,14 @@ export function Files() {
if (justCreatedFileIdRef.current && !isJustCreated) {
justCreatedFileIdRef.current = null
}
setPreviewMode(isJustCreated ? 'editor' : 'preview')
}, [selectedFileId])
if (isJustCreated) {
setPreviewMode('editor')
} else {
const file = selectedFileId ? files.find((f) => f.id === selectedFileId) : null
const isMd = file ? getFileExtension(file.name) === 'md' : false
setPreviewMode(isMd ? 'split' : 'preview')
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
}
}, [selectedFileId, files])
Comment thread
TheodoreSpeaks marked this conversation as resolved.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 previewMode resets on every background files refetch

Adding files to this effect's dependency array means that whenever the useWorkspaceFiles query refetches (e.g., on window focus, network reconnect, or after any mutation like a rename/upload/delete), the effect re-runs and resets previewMode to the file's default. A user who manually switches from preview to editor mode will silently have their preference discarded on the next background data refresh.

The original code only had [selectedFileId] as a dependency, which is the correct intent — the mode should only reset when the user navigates to a different file.

The simplest fix is to exclude files from the dependency array with a suppression comment explaining the intent:

Suggested change
}, [selectedFileId, files])
}, [selectedFileId]) // intentionally excluding `files` — previewMode should only reset when the selected file changes, not on every refetch

If the lint warning is a concern, you can capture files in a ref that does not trigger the effect:

const filesRef = useRef(files)
useEffect(() => { filesRef.current = files }, [files])

useEffect(() => {
  // ...
  const file = selectedFileId ? filesRef.current.find((f) => f.id === selectedFileId) : null
  // ...
}, [selectedFileId])


useEffect(() => {
if (!selectedFile) return
Expand Down
Loading