Skip to content

chore: fix rerenders on files#3805

Merged
icecrasher321 merged 2 commits intosimstudioai:stagingfrom
adithyaakrishna:feat/files-v2
Mar 27, 2026
Merged

chore: fix rerenders on files#3805
icecrasher321 merged 2 commits intosimstudioai:stagingfrom
adithyaakrishna:feat/files-v2

Conversation

@adithyaakrishna
Copy link
Copy Markdown
Contributor

Summary

  • Fix multiple renderers on the files page

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: Fix multiple rerenders

Testing

  • Go to the files page and do alll actions however you used to do on Prod

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@cursor
Copy link
Copy Markdown

cursor bot commented Mar 27, 2026

PR Summary

Medium Risk
Primarily a performance refactor (memoization, debounced search, stable refs) but it touches many callbacks/state transitions around save/delete/navigation, so regressions would show up as stale actions or incorrect preview mode changes.

Overview
Reduces unnecessary rerenders on the workspace Files page by memoizing preview/context-menu components and stabilizing many handlers with useRef-backed values (e.g., save/delete/download/rename/context-menu actions).

Adds a 200ms debounced search input and introduces lightweight row caching so ResourceRow objects aren’t recreated on every render, while preserving inline-rename behavior.

Refactors preview-mode initialization and route-change handling to avoid effect churn, and extracts the file row context menu into a memoized component; FileViewer save now uses a ref to avoid re-creating the autosave onSave callback.

Written by Cursor Bugbot for commit 3750f6e. Configure here.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 27, 2026

@adithyaakrishna is attempting to deploy a commit to the Sim Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 27, 2026

Greptile Summary

This PR applies a broad set of memoization and ref-stabilization optimizations to the files page to reduce unnecessary re-renders. The changes include debounced search with immediate UI feedback, row caching via rowCacheRef, stable callbacks via the ref pattern, memo wrapping for all leaf preview components, extraction of a new FileRowContextMenu component, merged keydown/beforeunload effects, and a lazy previewMode initializer. Two issues found: headerRename callbacks missing from fileDetailBreadcrumbs deps (latent maintenance risk), and a render-time prevFileIdRef.current mutation that can leave previewMode stuck in React 18 concurrent mode.

Confidence Score: 4/5

Safe to merge after addressing the concurrent-mode ref-mutation issue in previewMode derivation; the missing headerRename deps are a low-risk maintenance concern.

The PR delivers a large, well-structured rerender optimization. The two remaining findings are both in files.tsx: one is a latent (not currently broken) dep-array inconsistency, and the other is a genuine correctness risk under React 18 concurrent rendering where previewMode can get stuck after a route change.

apps/sim/app/workspace/[workspaceId]/files/files.tsx — specifically the render-time prevFileIdRef mutation (lines 567–590) and the fileDetailBreadcrumbs missing deps (lines 425–465).

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/files/files.tsx Major rerender-reduction refactor: debounced search, row caching, ref-based stable callbacks, extracted memoized sub-components. Two issues: headerRename callbacks missing from fileDetailBreadcrumbs deps, and render-time ref mutation that can get previewMode stuck in React 18 concurrent mode.
apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx Wraps IframePreview, ImagePreview, and UnsupportedPreview in memo; uses updateContentRef pattern to remove updateContent from saveContent callback deps — clean and correct.
apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx Wraps PreviewPanel, HtmlPreview, SvgPreview, and CsvPreview in memo — straightforward and correct.
apps/sim/app/workspace/[workspaceId]/files/components/files-list-context-menu/files-list-context-menu.tsx Wraps FilesListContextMenu in memo — simple, no issues.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[User types in search box] --> B[setInputValue - immediate]
    B --> C[Debounce timer 200ms]
    C --> D[setDebouncedSearchTerm]
    D --> E[filteredFiles useMemo recalculates]
    E --> F{Row in rowCacheRef?}
    F -- Yes, same file+members --> G[Return cached ResourceRow]
    F -- No --> H[Build new ResourceRow]
    G & H --> I[baseRows array]
    I --> J{listRename.editingId set?}
    J -- No --> K[rows = baseRows]
    J -- Yes --> L[Inject InlineRenameInput into matching row]
    L --> K
    K --> M[Resource table renders]

    N[fileIdFromRoute changes] --> O{prevFileIdRef check during render}
    O -- Different --> P[Mutate prevFileIdRef.current]
    P --> Q[Compute nextMode via filesRef.current]
    Q --> R[setPreviewMode if different]
    O -- Same --> S[No-op]

    style P fill:#f9c,stroke:#c00
    style R fill:#ffc,stroke:#990
Loading

Reviews (2): Last reviewed commit: "chore: fix review changes" | Re-trigger Greptile

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Mar 27, 2026 0:59am

Request Review

@waleedlatif1
Copy link
Copy Markdown
Collaborator

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator

@cursor review