Skip to content

improvement(chat): code-split resource preview panel out of initial /chat bundle#5331

Merged
waleedlatif1 merged 2 commits into
stagingfrom
worktree-chat-react-doctor
Jul 1, 2026
Merged

improvement(chat): code-split resource preview panel out of initial /chat bundle#5331
waleedlatif1 merged 2 commits into
stagingfrom
worktree-chat-react-doctor

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Code-split the MothershipView resource-preview panel (file-viewer, rich-markdown, CSV/PDF stack) out of the initial /chat bundle via React.lazy + a local <Suspense>. It only renders once a chat has messages, so it no longer ships on every navigation to /chat.
  • Removed the now-dead barrel re-export of MothershipView so the split actually takes effect — this app has no "sideEffects": false, so a leftover barrel edge would drag the heavy module back into the initial chunk.
  • Small behavior-preserving perf pass on the input surface: toSorted (non-mutating) instead of spread+sort in use-skill-auto-mention; a Map first-match lookup instead of .find()-in-loop in prompt-editor.
  • Documented the code-split/barrel gotcha in .claude/rules/sim-imports.md.

Type of Change

  • Improvement (performance)

Testing

  • tsc 0 errors, biome clean
  • user-input tests 14/14 pass
  • Two independent adversarial audits confirmed zero regression: ref forwarding through lazy(memo(forwardRef)) reaches the DOM node (resize keeps working), the fallback window is null-guarded in use-mothership-resize, Suspense is scoped locally (no page-level fallback flash), and the two micro-fixes are behaviorally identical (first-match preserved, non-mutating sort)

Checklist

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

…chat bundle

Lazy-load the MothershipView resource-preview panel (file-viewer, rich-markdown,
CSV/PDF stack) via React.lazy + local Suspense so it is not in the initial /chat
bundle; it only renders once a chat has messages. Remove its now-dead barrel
re-export so the split takes effect (this app has no sideEffects:false, so a
leftover barrel edge drags the heavy module back into the initial chunk).

Also a small behavior-preserving perf pass on the input surface:
- toSorted (non-mutating) instead of spread+sort in use-skill-auto-mention
- Map first-match lookup instead of .find()-in-loop in prompt-editor

Document the code-split/barrel gotcha in .claude/rules/sim-imports.md.
@vercel

vercel Bot commented Jul 1, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Jul 1, 2026 8:11pm

Request Review

@cursor

cursor Bot commented Jul 1, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Performance and import-structure changes with behavior-preserving mention rendering; main risk is verifying the bundle split and that resize/ref handling tolerates the lazy Suspense window (already guarded per PR notes).

Overview
Code-splits the resource preview panel (MothershipView — file viewer, rich markdown, CSV/PDF) off the initial /chat bundle by loading it with React.lazy from the deep ./components/mothership-view/mothership-view path and wrapping it in a local <Suspense fallback={null}> on home.tsx, so the heavy stack only loads when the chat-with-messages layout mounts.

Removes MothershipView from components/index.ts so the barrel no longer keeps a static edge to that module (important without "sideEffects": false in the app package). Documents this lazy + barrel pattern in .claude/rules/sim-imports.md, including local Suspense and ref null-guards during the fallback window.

Micro perf in prompt-editor: builds a label→context Map for mention overlay rendering instead of contexts.find() inside the highlight loop (first-match semantics unchanged).

Reviewed by Cursor Bugbot for commit 5c0f786. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR code-splits the MothershipView resource-preview panel (file-viewer, rich-markdown, CSV/PDF stack) out of the initial /chat route bundle using React.lazy + a local <Suspense>, and removes the now-dead barrel re-export that would have silently re-pulled the heavy module back into the initial chunk.

  • Lazy-load + barrel removal: home.tsx now imports MothershipView via a deep lazy(() => import('./components/mothership-view/mothership-view')) and the corresponding export { MothershipView } line is deleted from components/index.ts, ensuring no bundler edge reintroduces the chunk.
  • prompt-editor.tsx micro-optimization: a Map<label, context> is built once before the range loop, replacing an O(n) .find() call per mention range with an O(1) lookup; first-match semantics are preserved.
  • Documentation: .claude/rules/sim-imports.md documents the code-split/barrel interaction so the pattern is not accidentally regressed.

Confidence Score: 5/5

Safe to merge — the change is purely additive in bundle terms, all DOM ref usages remain guarded, and the barrel removal has no other consumers.

The lazy split is implemented correctly: the deep import path bypasses the barrel, the Suspense boundary is scoped locally so there is no page-level flash, and every access to mothershipRef.current in use-mothership-resize.ts already carries a null guard (if (!el) return / optional chaining). The barrel re-export removal was verified to have no remaining consumers. The Map optimization in prompt-editor preserves first-match semantics. No regressions were introduced.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/home/home.tsx Introduces React.lazy + local Suspense for MothershipView; all mothershipRef.current usages in use-mothership-resize.ts are null-guarded, so the fallback window is safe.
apps/sim/app/workspace/[workspaceId]/home/components/index.ts Dead barrel re-export of MothershipView removed; no other file imported MothershipView from this barrel, so the change is safe.
apps/sim/app/workspace/[workspaceId]/home/components/user-input/components/prompt-editor/prompt-editor.tsx Map built outside the range loop for O(1) mention lookups; first-match semantics preserved with has() guard before set(); only constructed when ranges exist (after early-return guards).
.claude/rules/sim-imports.md Documents the code-split/barrel interaction, Suspense scoping, and ref null-guard requirement — accurate and helpful for preventing future regression.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Browser
    participant Next.js
    participant InitialChunk as /chat initial chunk
    participant LazyChunk as MothershipView chunk (lazy)

    Browser->>Next.js: navigate to /chat
    Next.js->>InitialChunk: load (home.tsx, MothershipChat, UserInput, etc.)
    Note over InitialChunk: MothershipView NOT included
    InitialChunk-->>Browser: render landing view (no messages)

    Browser->>Next.js: user sends first message
    Next.js-->>Browser: "hasMessages = true, non-early-return branch"
    Browser->>LazyChunk: React.lazy triggers fetch
    LazyChunk-->>Browser: MothershipView chunk loaded
    Note over Browser: Suspense fallback=null during load (no flash)
    Browser-->>Browser: MothershipView mounts with ref forwarded
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Browser
    participant Next.js
    participant InitialChunk as /chat initial chunk
    participant LazyChunk as MothershipView chunk (lazy)

    Browser->>Next.js: navigate to /chat
    Next.js->>InitialChunk: load (home.tsx, MothershipChat, UserInput, etc.)
    Note over InitialChunk: MothershipView NOT included
    InitialChunk-->>Browser: render landing view (no messages)

    Browser->>Next.js: user sends first message
    Next.js-->>Browser: "hasMessages = true, non-early-return branch"
    Browser->>LazyChunk: React.lazy triggers fetch
    LazyChunk-->>Browser: MothershipView chunk loaded
    Note over Browser: Suspense fallback=null during load (no flash)
    Browser-->>Browser: MothershipView mounts with ref forwarded
Loading

Reviews (2): Last reviewed commit: "fix(chat): drop unsafe toSorted, keep co..." | Re-trigger Greptile

toSorted (ES2023) is not polyfilled by SWC and crashes iOS15/Safari<16 in
client bundles, so revert use-skill-auto-mention to the non-mutating
[...arr].sort() it had. The code-split of MothershipView and the first-match
Map lookup in prompt-editor are unaffected. Tighten the sim-imports code-split
note to state webpack *can* retain the barrel edge (removing the dead re-export
is the guaranteed fix).
@waleedlatif1 waleedlatif1 merged commit 8cebeb6 into staging Jul 1, 2026
9 of 10 checks passed
@waleedlatif1 waleedlatif1 deleted the worktree-chat-react-doctor branch July 1, 2026 20:08
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5c0f786. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant