feat: split out components from ChatView.tsx #860
main fromrefs/pull/860/head·merged by juliusmarminge View on GitHubWhat Changed#
Split out all of the components from ChatView.tsx into their own files, and pulled the functions into a ChatView.logic.ts. I didn't change the actual content of those components, just moved them to their own files.
Why#
In order to tame the massive ChatView.tsx file (and ChatView component!) it is basically required that we move out some of this functionality, otherwise any attempts to refactor will be met with merge conflict hell. It's fair that some of these components I broke out might belong together, however, it is much easier to break them all out and later put them back together in smaller PRs. This way we can independently work to refactor individual aspects (e.g. message timeline component).
I considered making this multiple PRs due to volume of code changes, however i choose not two because:
- AI makes it easier to validate that content didn't change, just organization.
- I didn't want to spam 10 PRs just to get a number lower arbitrarily.
This is associated with the effort of #830, i don't mean to step on toes if others are working on this effort, hopefully this split will make it much easier for many to work on individual parts of this refactor in a more coordinated matter!
UI Changes#
No UI changes, pure refactor.
Checklist#
- This PR is small and focused
- I explained what changed and why
- I included before/after screenshots for any UI changes
- I included a video for animation/interaction changes
(technically this is not a small PR, but it is very very focused.)
Future Work#
Rather than having a single super ChatView component, we should lower some of the responsibilities to more appropriate locations in the component tree. Ideally, in my opinion, ChatView itself should be quite small and only handle orchestrating the various major components it mounts (e.g. the header, timeline, composer, etc...).
Note
Split ChatView.tsx into modular chat sub-components#
Extracts a large number of components and utilities previously inlined in ChatView.tsx into separate files under apps/web/src/components/chat/.
- New components include
MessagesTimeline,ChatHeader,ProposedPlanCard,ProviderModelPicker,ProviderHealthBanner,ThreadErrorBanner,ComposerCommandMenu,ComposerPendingUserInputPanel,ComposerPendingApprovalPanel,ChangedFilesTree,OpenInPicker, and several smaller UI pieces. - Logic helpers (blob URL management, draft thread construction, model resolution, localStorage utilities) are extracted into ChatView.logic.ts.
ChatView.tsxis refactored to import from these new modules, with no intended change to runtime behavior.
📊 Macroscope summarized 24b93b3. 20 files reviewed, 4 issues evaluated, 2 issues filtered, 1 comment posted
🗂️ Filtered Issues#
apps/web/src/components/chat/MessageCopyButton.tsx — 1 comment posted, 2 evaluated, 1 filtered
- line 11: The
setTimeoutat line 11 is not cleaned up when the component unmounts. If the user navigates away or the message is removed before 2 seconds elapse, React will warn about setting state on an unmounted component. The equivalent code inChatMarkdown.tsxstores the timer ref and clears it in auseEffectcleanup. [ Failed validation ]
apps/web/src/components/chat/ProviderModelPicker.tsx — 0 comments posted, 1 evaluated, 1 filtered
- line 28: The type guard
isAvailableProviderOptionasserts the return type narrowsvaluetoProviderKind, but the runtime check only excludes"claudeCode"viaoption.value !== "claudeCode". Looking atProviderPickerKind = ProviderKind | "claudeCode" | "cursor", the guard should also exclude"cursor"to properly narrow toProviderKind. Ifcursorever becomes available (available: true), the function would returntruebut the type annotation would incorrectly claimoption.valueisProviderKindwhen it's actually"cursor", causing type unsoundness. [ Out of scope (triage) ]
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
I'm not opposed to this change, I am opposed to how the files are structured. Potentially we could put them in a folder or something?
Thoughts on “/chat” in the components folder for the organization? @binbandit
Thoughts on “/chat” in the components folder for the organization? @binbandit
Ye. That works nicely. It explains it well 😂
Very cool, I’ll push a new commit with the change, thanks for the feedback!
updated to move the files into /chat. Also rebased to get new changes from https://github.com/pingdotgg/t3code/pull/488 in ChatView.tsx
how much of
ChatViewis now invoking hooks and just passing down to child components? Can we have the child components own their state more? e.g. composer can probably just fetch the state from store directly, instead of the chatview pulling it just to pass it down as props?Most of ChatView right now is as you describe, @juliusmarminge. Unfortunately, a lot of the useEffects and various callbacks are fairly intertwined 😅. I'm working on pulling out the composer pieces right now and already reduced ChatView significantly, so seems very promising. I think a couple of focused stacked PRs will be able to split it apart nicely. The tough part is balancing splitting it up and actually improving the architecture without accidentally introducing subtle bugs.
Excited to see work on this. Out of curiosity, how much does this refactor out Plan mode? I see some components split out (as was the original ask), but I feel like the business logic of Plan mode should be kept logically separate as well. Not sure if that's scope creep, though.
This first pass is just moving stuff. I’m working on a second pass to actually lower hooks down out of chatview into respective subcomponents. I haven’t explicitly touched moving out plan mode but i imagine that would naturally happen as this line of work progresses.
@juliusmarminge , just curious, does this PR block any
ChatView.tsx-related work in other PR's? I would suspect there would be a lot of merge conflicts to deal with if it didn't. But perhaps we could make it work.@juliusmarminge , just curious, does this PR block any
ChatView.tsx-related work in other PR's? I would suspect there would be a lot of merge conflicts to deal with if it didn't. But perhaps we could make it work.it's always gonna conflict. will pull and test it now and merge so others can rebase their stuff
- @@ -0,0 +1,10 @@1+import { memo, useCallback, useState } from "react";2+import { CopyIcon, CheckIcon } from "lucide-react";3+import { Button } from "../ui/button";4+5+export const MessageCopyButton = memo(function MessageCopyButton({ text }: { text: string }) {6+ const [copied, setCopied] = useState(false);7+8+ const handleCopy = useCallback(() => {9+ void navigator.clipboard.writeText(text);10+ setCopied(true);
🟢 Low
chat/MessageCopyButton.tsx:8navigator.clipboardisundefinedin non-secure contexts (HTTP), sonavigator.clipboard.writeText(text)throwsTypeError: Cannot read property 'writeText' of undefined. Consider adding a null check before callingwriteText, similar to the guard inChatMarkdown.tsx.- const handleCopy = useCallback(() => { - void navigator.clipboard.writeText(text); + const handleCopy = useCallback(() => { + if (typeof navigator === "undefined" || navigator.clipboard == null) { + return; + } + void navigator.clipboard.writeText(text);🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file apps/web/src/components/chat/MessageCopyButton.tsx around lines 8-10: `navigator.clipboard` is `undefined` in non-secure contexts (HTTP), so `navigator.clipboard.writeText(text)` throws `TypeError: Cannot read property 'writeText' of undefined`. Consider adding a null check before calling `writeText`, similar to the guard in `ChatMarkdown.tsx`. Evidence trail: - `apps/web/src/components/chat/MessageCopyButton.tsx` line 9: `void navigator.clipboard.writeText(text);` (no null check) - `apps/web/src/components/ChatMarkdown.tsx` lines 140-142: shows the null guard pattern `if (typeof navigator === "undefined" || navigator.clipboard == null) { return; }` - `apps/web/src/components/Sidebar.tsx` lines 92-95: another guard example `if (typeof navigator === "undefined" || navigator.clipboard?.writeText === undefined)` - Microsoft documentation confirms Clipboard API is secure-context only: https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/working-with-local-content lgtm as a first pass. feel free to do follow ups breaking out the logic into relevant components too :)
juliusmarminge merged thise3d46b6
juliusmarminge closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
e3cb2de5-f507-45b1-a7db-3ccdd94c3458You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share