-
Notifications
You must be signed in to change notification settings - Fork 408
feat(activity): virtualize the feed and page on scroll #6238
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { decodeActivityEvent } from '../queries/decode'; | ||
| import { createdEvent, editedEvent } from '../queries/fixtures'; | ||
| import { flattenFeed, reuseRows, shouldFetchMore } from './feed-rows'; | ||
|
|
||
| const created = decodeActivityEvent(createdEvent); | ||
| const edited = decodeActivityEvent(editedEvent); | ||
|
|
||
| describe('flattenFeed', () => { | ||
| it('interleaves day headers and events in order and ends with a tail when more pages exist', () => { | ||
| const rows = flattenFeed( | ||
| [ | ||
| { key: 'today', label: 'Today', events: [created] }, | ||
| { key: 'yesterday', label: 'Yesterday', events: [edited] }, | ||
| ], | ||
| { hasMore: true } | ||
| ); | ||
| expect(rows.map((row) => row.kind)).toEqual([ | ||
| 'day', | ||
| 'event', | ||
| 'day', | ||
| 'event', | ||
| 'tail', | ||
| ]); | ||
| expect(rows[0]).toEqual({ kind: 'day', key: 'today', label: 'Today' }); | ||
| expect(rows[1]).toEqual({ kind: 'event', event: created }); | ||
| }); | ||
|
|
||
| it('omits the tail on the last page', () => { | ||
| const rows = flattenFeed( | ||
| [{ key: 'today', label: 'Today', events: [created] }], | ||
| { hasMore: false } | ||
| ); | ||
| expect(rows.map((row) => row.kind)).toEqual(['day', 'event']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reuseRows', () => { | ||
| it('keeps previous row objects for matching keys and adds the rest', () => { | ||
| const previous = flattenFeed( | ||
| [{ key: 'today', label: 'Today', events: [created] }], | ||
| { hasMore: true } | ||
| ); | ||
| const next = flattenFeed( | ||
| [ | ||
| { key: 'today', label: 'Today', events: [{ ...created }] }, | ||
| { key: 'yesterday', label: 'Yesterday', events: [edited] }, | ||
| ], | ||
| { hasMore: false } | ||
| ); | ||
| const reused = reuseRows(previous, next); | ||
| expect(reused[0]).toBe(previous[0]); | ||
| expect(reused[1]).toBe(previous[1]); | ||
| expect(reused[2]).toBe(next[2]); | ||
| expect(reused[3]).toBe(next[3]); | ||
| expect(reused.map((row) => row.kind)).toEqual([ | ||
| 'day', | ||
| 'event', | ||
| 'day', | ||
| 'event', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldFetchMore', () => { | ||
| it.each([ | ||
| // [scrollSize, viewportSize, offset, expected] | ||
| [3000, 800, 0, false], | ||
| [3000, 800, 1399, false], | ||
| [3000, 800, 1400, true], | ||
| [3000, 800, 2200, true], | ||
| [500, 800, 0, true], | ||
| [1000, 50, 800, false], | ||
| [1000, 50, 850, true], | ||
| ])( | ||
| 'scrollSize %i viewport %i offset %i -> %s', | ||
| (scrollSize, viewportSize, offset, expected) => { | ||
| expect(shouldFetchMore({ scrollSize, viewportSize, offset })).toBe( | ||
| expected | ||
| ); | ||
| } | ||
| ); | ||
| }); |
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,72 @@ | ||
| import type { ActivityEvent } from './event'; | ||
| import type { FeedGroup } from './group-events'; | ||
|
|
||
| /** | ||
| * One virtualized row of the Activity screen. The overview card is row zero | ||
| * so it scrolls with the feed and the virtualizer needs no start margin. | ||
| */ | ||
| export type FeedRow = | ||
| | { kind: 'overview' } | ||
| | { kind: 'day'; key: string; label: string } | ||
| | { kind: 'event'; event: ActivityEvent } | ||
| | { kind: 'status'; status: 'loading' | 'error' | 'empty' } | ||
| | { kind: 'tail' }; | ||
|
|
||
| /** Day headers and their events in order, plus a tail row while more pages exist. */ | ||
| export function flattenFeed( | ||
| groups: FeedGroup[], | ||
| options: { hasMore: boolean } | ||
| ): FeedRow[] { | ||
| const rows: FeedRow[] = []; | ||
| for (const group of groups) { | ||
| rows.push({ kind: 'day', key: group.key, label: group.label }); | ||
| for (const event of group.events) rows.push({ kind: 'event', event }); | ||
| } | ||
| if (options.hasMore) rows.push({ kind: 'tail' }); | ||
| return rows; | ||
| } | ||
|
|
||
| function rowKey(row: FeedRow): string { | ||
| switch (row.kind) { | ||
| case 'overview': | ||
| case 'tail': | ||
| return row.kind; | ||
| case 'day': | ||
| return `day:${row.key}`; | ||
| case 'event': | ||
| return `event:${row.event.id}`; | ||
| case 'status': | ||
| return `status:${row.status}`; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Carry previous row objects forward where the key matches, so the list | ||
| * keys rows by reference and a refetch or a paging flag flip does not | ||
| * remount every mounted row. Events are immutable once recorded, so a | ||
| * matching id is a matching row. | ||
| */ | ||
| export function reuseRows(previous: FeedRow[], next: FeedRow[]): FeedRow[] { | ||
| if (previous.length === 0) return next; | ||
| const byKey = new Map(previous.map((row) => [rowKey(row), row])); | ||
| return next.map((row) => byKey.get(rowKey(row)) ?? row); | ||
| } | ||
|
|
||
| /** Floor for the near-bottom threshold so tiny viewports still page. */ | ||
| const MIN_FETCH_THRESHOLD = 100; | ||
|
|
||
| /** | ||
| * Whether the scroller is within one viewport of its end, the point at which | ||
| * the next page should start loading so it usually lands before the user | ||
| * reaches the bottom. | ||
| */ | ||
| export function shouldFetchMore(metrics: { | ||
| scrollSize: number; | ||
| viewportSize: number; | ||
| offset: number; | ||
| }): boolean { | ||
| const threshold = Math.max(MIN_FETCH_THRESHOLD, metrics.viewportSize); | ||
| return ( | ||
| metrics.scrollSize - metrics.viewportSize - metrics.offset <= threshold | ||
| ); | ||
| } | ||
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
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Do not reuse a day row when its label changed.
rowKeyexcludeslabel, so this returns the old day row for the samegroup.key. The view then renders the stale label after a refetch changes a relative or localized day label. Reuse the row only when its displayed label also matches, and add that case to the test.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents