Repository navigation
fix(web): the chat view no longer keeps every thread copy it has shown #31464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+151
−18
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
apps/web/src/components/chat/useComposerFocus.browser.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { RefObject } from "react"; | ||
| import { expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import type { ChatComposerHandle } from "./ChatComposer"; | ||
| import { registerModelPickerContent } from "../../modelPickerVisibility"; | ||
| import { render } from "../../../test/render"; | ||
| import { useComposerFocus } from "./useComposerFocus"; | ||
|
|
||
| type Focus = ReturnType<typeof useComposerFocus>; | ||
|
|
||
| function Probe(props: { | ||
| readonly composerRef: RefObject<ChatComposerHandle | null>; | ||
| readonly onRender: (focus: Focus) => void; | ||
| }) { | ||
| props.onRender(useComposerFocus(props.composerRef)); | ||
| return null; | ||
| } | ||
|
|
||
| const handle = (focusAtEnd: () => void) => ({ focusAtEnd }) as unknown as ChatComposerHandle; | ||
|
|
||
| it("focuses the composer's current handle without changing identity", async () => { | ||
| const composerRef: RefObject<ChatComposerHandle | null> = { current: null }; | ||
| const renders: Focus[] = []; | ||
| const onRender = (focus: Focus) => renders.push(focus); | ||
| await using screen = await render(<Probe composerRef={composerRef} onRender={onRender} />); | ||
|
|
||
| const replaced = vi.fn(); | ||
| const current = vi.fn(); | ||
| composerRef.current = handle(replaced); | ||
| // The composer replaces its handle as the thread changes, with no render here in between. | ||
| composerRef.current = handle(current); | ||
| renders.at(-1)!.focusComposer(); | ||
| expect(current).toHaveBeenCalledTimes(1); | ||
|
|
||
| await screen.rerender(<Probe composerRef={composerRef} onRender={onRender} />); | ||
| expect(renders.at(-1)!.focusComposer).toBe(renders[0]!.focusComposer); | ||
| expect(renders.at(-1)!.scheduleComposerFocus).toBe(renders[0]!.scheduleComposerFocus); | ||
| expect(replaced).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("schedules focus for the next frame unless the model picker is open by then", async () => { | ||
| const focusAtEnd = vi.fn(); | ||
| const composerRef: RefObject<ChatComposerHandle | null> = { current: handle(focusAtEnd) }; | ||
| const renders: Focus[] = []; | ||
| await using _screen = await render( | ||
| <Probe composerRef={composerRef} onRender={(focus) => renders.push(focus)} />, | ||
| ); | ||
| const { scheduleComposerFocus } = renders.at(-1)!; | ||
| const frames: FrameRequestCallback[] = []; | ||
| using _frames = vi.spyOn(window, "requestAnimationFrame").mockImplementation((frame) => { | ||
| frames.push(frame); | ||
| return frames.length; | ||
| }); | ||
|
|
||
| scheduleComposerFocus(); | ||
| expect(focusAtEnd).not.toHaveBeenCalled(); | ||
| frames.shift()!(0); | ||
| expect(focusAtEnd).toHaveBeenCalledTimes(1); | ||
|
|
||
| scheduleComposerFocus(); | ||
| const closeModelPicker = registerModelPickerContent(); | ||
| frames.shift()!(0); | ||
| closeModelPicker(); | ||
| expect(focusAtEnd).toHaveBeenCalledTimes(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { ComposerHandleRef } from "../../composerHandleContext"; | ||
| import { useStableCallback } from "../../hooks/useStableCallback"; | ||
| import { isModelPickerOpen } from "../../modelPickerVisibility"; | ||
|
|
||
| /** | ||
| * Made outside ChatView's render: a memoized closure there keeps that render's scope, thread | ||
| * projection included, and the scope keeps the previous render's, so every thread copy | ||
| * ChatView showed stayed in memory until it unmounted. | ||
| */ | ||
| export function useComposerFocus(composerRef: ComposerHandleRef) { | ||
| const focusComposer = useStableCallback(() => { | ||
| composerRef.current?.focusAtEnd(); | ||
| }); | ||
| const scheduleComposerFocus = useStableCallback(() => { | ||
| window.requestAnimationFrame(() => { | ||
| // The model picker stays open through its own changes, so it keeps focus. | ||
| if (!isModelPickerOpen()) focusComposer(); | ||
| }); | ||
| }); | ||
| return { focusComposer, scheduleComposerFocus }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the required one-line comment for
router: false.Line 112 passes
{ router: false }without a reason. The browser-test rules allowrouter: falseonly with a one-line comment that says why. The reason here is thatAppRootrenders its ownRouterProviderfor theroutercreated at Line 110, so the harness router is not needed.📝 Proposed fix
As per coding guidelines,
**/*.browser.test.{tsx,ts}: "routeris on by default. Passrouter: falseonly with a one-line comment saying why."Committable suggestion
Source: Coding guidelines