-
Notifications
You must be signed in to change notification settings - Fork 52.5k
fix(desktop): memoize sidebar flatRows and row renderers to prevent s… #75714
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
StanleyStetson
wants to merge
2
commits into
NousResearch:main
from
StanleyStetson:fix/desktop-sessions-scroll-flicker
Closed
Changes from all commits
Commits
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
182 changes: 182 additions & 0 deletions
182
apps/desktop/src/app/chat/sidebar/sessions-section.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,182 @@ | ||
| import { cleanup, render } from '@testing-library/react' | ||
| import type * as React from 'react' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { SessionInfo } from '@/hermes' | ||
| import { SidebarSessionsSection, VIRTUALIZE_THRESHOLD } from './sessions-section' | ||
| import type { VirtualSessionListProps } from './virtual-session-list' | ||
|
|
||
| afterEach(cleanup) | ||
|
|
||
| vi.mock('@/i18n', () => ({ | ||
| useI18n: () => ({ | ||
| t: { | ||
| sidebar: { | ||
| dateDivider: { | ||
| earlierThisMonth: 'Earlier this month', | ||
| lastMonth: 'Last month', | ||
| lastWeek: 'Last week', | ||
| older: 'Older', | ||
| today: 'Today', | ||
| yesterday: 'Yesterday' | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| })) | ||
|
|
||
| const mockVirtualListPropsHistory: VirtualSessionListProps[] = [] | ||
|
|
||
| vi.mock('./virtual-session-list', () => ({ | ||
| VirtualSessionList: (props: VirtualSessionListProps) => { | ||
| mockVirtualListPropsHistory.push(props) | ||
| return <div data-testid="virtual-session-list">Virtual List ({props.rows.length} rows)</div> | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('./session-row', () => ({ | ||
| SidebarSessionRow: ({ session }: { session: SessionInfo }) => ( | ||
| <div data-testid={`session-row-${session.id}`}>{session.id}</div> | ||
| ) | ||
| })) | ||
|
|
||
| function makeSession(id: string, startedAt = 1000): SessionInfo { | ||
| return { | ||
| handoff_platform: null, | ||
| handoff_state: null, | ||
| id, | ||
| last_active: startedAt, | ||
| profile: 'default', | ||
| started_at: startedAt | ||
| } as unknown as SessionInfo | ||
| } | ||
|
|
||
| function generateSessions(count: number): SessionInfo[] { | ||
| return Array.from({ length: count }, (_, i) => makeSession(`session-${i + 1}`, 10000 - i * 100)) | ||
| } | ||
|
|
||
| const noop = () => {} | ||
|
|
||
| describe('SidebarSessionsSection memoization & virtualizer stability', () => { | ||
| it('memoizes flatRows and passes the exact same rows array reference across parent re-renders', () => { | ||
| mockVirtualListPropsHistory.length = 0 | ||
|
|
||
| const sessions = generateSessions(VIRTUALIZE_THRESHOLD + 5) | ||
|
|
||
| const { rerender } = render( | ||
| <SidebarSessionsSection | ||
| activeSessionId={null} | ||
| emptyState={<div>Empty</div>} | ||
| label="Sessions" | ||
| onArchiveSession={noop} | ||
| onDeleteSession={noop} | ||
| onResumeSession={noop} | ||
| onToggle={noop} | ||
| onTogglePin={noop} | ||
| open={true} | ||
| pinned={false} | ||
| sessions={sessions} | ||
| workingSessionIdSet={new Set()} | ||
| /> | ||
| ) | ||
|
|
||
| expect(mockVirtualListPropsHistory.length).toBe(1) | ||
| const initialRowsRef = mockVirtualListPropsHistory[0].rows | ||
| expect(initialRowsRef.length).toBeGreaterThan(VIRTUALIZE_THRESHOLD) | ||
|
|
||
| // Re-render parent with the exact same sessions array and props | ||
| rerender( | ||
| <SidebarSessionsSection | ||
| activeSessionId={null} | ||
| emptyState={<div>Empty</div>} | ||
| label="Sessions" | ||
| onArchiveSession={noop} | ||
| onDeleteSession={noop} | ||
| onResumeSession={noop} | ||
| onToggle={noop} | ||
| onTogglePin={noop} | ||
| open={true} | ||
| pinned={false} | ||
| sessions={sessions} | ||
| workingSessionIdSet={new Set()} | ||
| /> | ||
| ) | ||
|
|
||
| expect(mockVirtualListPropsHistory.length).toBe(2) | ||
| const nextRowsRef = mockVirtualListPropsHistory[1].rows | ||
|
|
||
| // Confirm that the flatRows array reference remains strictly identical across renders (useMemo proof) | ||
| expect(nextRowsRef).toBe(initialRowsRef) | ||
| }) | ||
|
|
||
| it('re-computes flatRows reference when dateGrouped or sessions change', () => { | ||
| mockVirtualListPropsHistory.length = 0 | ||
|
|
||
| const initialSessions = generateSessions(VIRTUALIZE_THRESHOLD + 2) | ||
|
|
||
| const { rerender } = render( | ||
| <SidebarSessionsSection | ||
| activeSessionId={null} | ||
| dateGrouped={false} | ||
| emptyState={<div>Empty</div>} | ||
| label="Sessions" | ||
| onArchiveSession={noop} | ||
| onDeleteSession={noop} | ||
| onResumeSession={noop} | ||
| onToggle={noop} | ||
| onTogglePin={noop} | ||
| open={true} | ||
| pinned={false} | ||
| sessions={initialSessions} | ||
| workingSessionIdSet={new Set()} | ||
| /> | ||
| ) | ||
|
|
||
| const firstRowsRef = mockVirtualListPropsHistory[0].rows | ||
|
|
||
| // Change dateGrouped to true | ||
| rerender( | ||
| <SidebarSessionsSection | ||
| activeSessionId={null} | ||
| dateGrouped={true} | ||
| emptyState={<div>Empty</div>} | ||
| label="Sessions" | ||
| onArchiveSession={noop} | ||
| onDeleteSession={noop} | ||
| onResumeSession={noop} | ||
| onToggle={noop} | ||
| onTogglePin={noop} | ||
| open={true} | ||
| pinned={false} | ||
| sessions={initialSessions} | ||
| workingSessionIdSet={new Set()} | ||
| /> | ||
| ) | ||
|
|
||
| const secondRowsRef = mockVirtualListPropsHistory[1].rows | ||
| expect(secondRowsRef).not.toBe(firstRowsRef) | ||
|
|
||
| // Change sessions array identity | ||
| const updatedSessions = generateSessions(VIRTUALIZE_THRESHOLD + 4) | ||
| rerender( | ||
| <SidebarSessionsSection | ||
| activeSessionId={null} | ||
| dateGrouped={true} | ||
| emptyState={<div>Empty</div>} | ||
| label="Sessions" | ||
| onArchiveSession={noop} | ||
| onDeleteSession={noop} | ||
| onResumeSession={noop} | ||
| onToggle={noop} | ||
| onTogglePin={noop} | ||
| open={true} | ||
| pinned={false} | ||
| sessions={updatedSessions} | ||
| workingSessionIdSet={new Set()} | ||
| /> | ||
| ) | ||
|
|
||
| const thirdRowsRef = mockVirtualListPropsHistory[2].rows | ||
| expect(thirdRowsRef).not.toBe(secondRowsRef) | ||
| }) | ||
| }) | ||
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
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.
VirtualSessionListPropsis not exported byvirtual-session-list.tsx(it is a module-private interface at line 30), so this import fails TypeScript checking. Please use a local minimal mock-prop shape containingrows, or deliberately export the interface.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.
Thanks for the detailed review @teknium1!
I've addressed both feedback points in the latest commit:
VirtualSessionListPropsinterface invirtual-session-list.tsxso the import passes TypeScript checks cleanly.sessions-section.test.tsxto explicitly test changing thesessionsarray identity in addition todateGrouped.