-
Notifications
You must be signed in to change notification settings - Fork 66
feat(archive): load archived tasks as users scroll #3819
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
4 commits
Select commit
Hold shift + click to select a range
a8f4d87
Add infinite loading for archived tasks
richardsolomou 16789a9
Address archive pagination review feedback
richardsolomou 16dcd3d
Fix archived task filter pagination
richardsolomou 58fe370
chore(visual): update storybook baselines
posthog[bot] 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
75 changes: 75 additions & 0 deletions
75
packages/ui/src/features/archive/archiveListPagination.test.ts
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,75 @@ | ||
| import type { ArchivedTaskWithRepo } from "@posthog/core/archive/archiveListView"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| getVisibleArchivedTasks, | ||
| shouldLoadMoreArchivedTasks, | ||
| } from "./archiveListPagination"; | ||
|
|
||
| function item( | ||
| id: string, | ||
| title: string, | ||
| archivedAt: string, | ||
| ): ArchivedTaskWithRepo { | ||
| return { | ||
| archived: { | ||
| taskId: id, | ||
| archivedAt, | ||
| folderId: "", | ||
| mode: "cloud", | ||
| worktreeName: null, | ||
| branchName: null, | ||
| checkpointId: null, | ||
| }, | ||
| task: { | ||
| id, | ||
| title, | ||
| created_at: archivedAt, | ||
| repository: "posthog/code", | ||
| }, | ||
| repoName: "code", | ||
| }; | ||
| } | ||
|
|
||
| const defaultSort = { column: "archived", direction: "desc" } as const; | ||
|
|
||
| describe("getVisibleArchivedTasks", () => { | ||
| it("finds matches outside the unfiltered page prefix", () => { | ||
| const items = [ | ||
| item("first", "First task", "2026-01-03T00:00:00Z"), | ||
| item("second", "Matching task", "2026-01-02T00:00:00Z"), | ||
| ]; | ||
|
|
||
| expect( | ||
| getVisibleArchivedTasks( | ||
| items, | ||
| { searchQuery: "matching", repoFilter: null, sort: defaultSort }, | ||
| 1, | ||
| ).map((entry) => entry.archived.taskId), | ||
| ).toEqual(["second"]); | ||
| }); | ||
|
|
||
| it("sorts the complete archive before limiting rows", () => { | ||
| const items = [ | ||
| item("older", "Older task", "2026-01-01T00:00:00Z"), | ||
| item("newer", "Newer task", "2026-01-03T00:00:00Z"), | ||
| ]; | ||
|
|
||
| expect( | ||
| getVisibleArchivedTasks( | ||
| items, | ||
| { searchQuery: "", repoFilter: null, sort: defaultSort }, | ||
| 1, | ||
| ).map((entry) => entry.archived.taskId), | ||
| ).toEqual(["newer"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("shouldLoadMoreArchivedTasks", () => { | ||
| it("loads near the unfiltered boundary", () => { | ||
| expect(shouldLoadMoreArchivedTasks(40, 50, false)).toBe(true); | ||
| }); | ||
|
|
||
| it("does not load from the end of filtered results", () => { | ||
| expect(shouldLoadMoreArchivedTasks(0, 1, true)).toBe(false); | ||
| }); | ||
| }); |
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,25 @@ | ||
| import { | ||
| type ArchivedTaskWithRepo, | ||
| type ArchiveFilterSortInput, | ||
| filterAndSortArchivedTasks, | ||
| } from "@posthog/core/archive/archiveListView"; | ||
|
|
||
| export function getVisibleArchivedTasks( | ||
| items: ArchivedTaskWithRepo[], | ||
| filters: ArchiveFilterSortInput, | ||
| loadedCount: number, | ||
| ): ArchivedTaskWithRepo[] { | ||
| return filterAndSortArchivedTasks(items, filters).slice(0, loadedCount); | ||
| } | ||
|
|
||
| export function shouldLoadMoreArchivedTasks( | ||
| lastVirtualRowIndex: number | undefined, | ||
| visibleItemCount: number, | ||
| hasActiveFilter: boolean, | ||
| ): boolean { | ||
| return ( | ||
| !hasActiveFilter && | ||
| lastVirtualRowIndex !== undefined && | ||
| lastVirtualRowIndex >= visibleItemCount - 10 | ||
| ); | ||
| } |
28 changes: 28 additions & 0 deletions
28
packages/ui/src/features/archive/useArchivedTaskSummaries.test.ts
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,28 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getNextArchivedTaskPage } from "./useArchivedTaskSummaries"; | ||
|
|
||
| describe("getNextArchivedTaskPage", () => { | ||
| it("returns the number of requested tasks as the next offset", () => { | ||
| expect( | ||
| getNextArchivedTaskPage( | ||
| [ | ||
| { results: [], requested: 50 }, | ||
| { results: [], requested: 50 }, | ||
| ], | ||
| 125, | ||
| ), | ||
| ).toBe(100); | ||
| }); | ||
|
|
||
| it("stops after every archived task has been requested", () => { | ||
| expect( | ||
| getNextArchivedTaskPage( | ||
| [ | ||
| { results: [], requested: 50 }, | ||
| { results: [], requested: 25 }, | ||
| ], | ||
| 75, | ||
| ), | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); |
46 changes: 46 additions & 0 deletions
46
packages/ui/src/features/archive/useArchivedTaskSummaries.ts
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,46 @@ | ||
| import type { Schemas } from "@posthog/api-client"; | ||
| import { useAuthenticatedInfiniteQuery } from "@posthog/ui/hooks/useAuthenticatedInfiniteQuery"; | ||
| import { useMemo } from "react"; | ||
|
|
||
| export const ARCHIVED_TASKS_PAGE_SIZE = 50; | ||
|
|
||
| export interface ArchivedTaskSummaryPage { | ||
| results: Schemas.TaskSummary[]; | ||
| requested: number; | ||
| } | ||
|
|
||
| export function getNextArchivedTaskPage( | ||
| allPages: ArchivedTaskSummaryPage[], | ||
| taskCount: number, | ||
| ): number | undefined { | ||
| const loaded = allPages.reduce((total, page) => total + page.requested, 0); | ||
| return loaded < taskCount ? loaded : undefined; | ||
| } | ||
|
|
||
| export function useArchivedTaskSummaries(ids: string[]) { | ||
| const query = useAuthenticatedInfiniteQuery<ArchivedTaskSummaryPage, number>( | ||
| ["tasks", "archived-summaries", ids], | ||
| async (client, offset) => { | ||
| const pageIds = ids.slice(offset, offset + ARCHIVED_TASKS_PAGE_SIZE); | ||
| return { | ||
| results: await client.getTaskSummaries(pageIds), | ||
| requested: pageIds.length, | ||
| }; | ||
| }, | ||
| { | ||
| enabled: ids.length > 0, | ||
| initialPageParam: 0, | ||
| getNextPageParam: (_lastPage, allPages) => | ||
| getNextArchivedTaskPage(allPages, ids.length), | ||
| }, | ||
| ); | ||
|
|
||
| const summaries = useMemo( | ||
| () => query.data?.pages.flatMap((page) => page.results) ?? [], | ||
| [query.data?.pages], | ||
| ); | ||
| const loadedCount = | ||
| query.data?.pages.reduce((total, page) => total + page.requested, 0) ?? 0; | ||
|
|
||
| return { ...query, summaries, loadedCount }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.