Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): keep Recent filled when pins are newest and bound the pin poll (#118014)

Paul D'Ambra688b71a23aa2
diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts
index 01640aba2fc3..d5d91db989f9 100644
--- a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts
+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts
@@ -2,7 +2,11 @@ import type { ChannelSessionFacts } from "@posthog/core/canvas/channelItems";
import type { DashboardRecord } from "@posthog/core/canvas/dashboardSchemas";
import type { Task } from "@posthog/shared/domain-types";
import { describe, expect, it } from "vitest";
-import { RECENT_WORK_CAP, selectRecentWorkItems } from "./useRecentWorkItems";
+import {
+ RECENT_WORK_CAP,
+ recentWorkPageSize,
+ selectRecentWorkItems,
+} from "./useRecentWorkItems";
const ME = "me-uuid";
const OLD = 1_000;
@@ -156,3 +160,16 @@ describe("selectRecentWorkItems", () => {
]);
});
});
+
+describe("recentWorkPageSize", () => {
+ it.each([
+ { pins: 0, expected: RECENT_WORK_CAP },
+ { pins: 7, expected: RECENT_WORK_CAP + 7 },
+ { pins: 500, expected: 100 },
+ ])(
+ "asks for room past $pins pins, so pins cannot fill Recent's page",
+ ({ pins, expected }) => {
+ expect(recentWorkPageSize(pins)).toBe(expected);
+ },
+ );
+});
diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts
index 97b550893c76..c4040f704ad4 100644
--- a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts
+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts
@@ -13,6 +13,7 @@ import { useAllCanvases } from "@posthog/ui/features/canvas/hooks/useDashboards"
import { useCanvasViewedStore } from "@posthog/ui/features/canvas/stores/canvasViewedStore";
import { usePinnedTasks } from "@posthog/ui/features/sidebar/usePinnedTasks";
import {
+ TASK_LIST_MAX_PAGE_SIZE,
usePinnedTaskRecords,
useTasks,
} from "@posthog/ui/features/tasks/useTasks";
@@ -37,6 +38,14 @@ function keepPinsAndCapRecent(
);
}
+/**
+ * The server cuts the page before pins are set apart, so the page leaves a row for each pin.
+ * Otherwise the newest tasks could all be pins and leave Recent empty.
+ */
+export function recentWorkPageSize(pinCount: number): number {
+ return Math.min(RECENT_WORK_CAP + pinCount, TASK_LIST_MAX_PAGE_SIZE);
+}
+
export function selectRecentWorkItems({
dashboards,
tasks,
@@ -91,14 +100,17 @@ export function useRecentWorkItems({
items: RecentWorkItem[];
isLoading: boolean;
} {
- // Nothing past the cap shows, so the page asks for no more than that. The server cuts the page
- // from the newest activity: by creation date, a session started last week and still in use is
- // already off the page when scheduled runs create many tasks.
- const { data: tasks = [], isLoading: tasksLoading } = useTasks({
- sources,
- limit: RECENT_WORK_CAP,
- ordering: "-last_activity_at",
- });
+ const { pinnedTaskIds, isLoading: pinIdsLoading } = usePinnedTasks();
+ // The server cuts the page from the newest activity: by creation date, a session started last
+ // week and still in use is already off the page when scheduled runs create many tasks.
+ const { data: tasks = [], isLoading: tasksLoading } = useTasks(
+ {
+ sources,
+ limit: recentWorkPageSize(pinnedTaskIds.size),
+ ordering: "-last_activity_at",
+ },
+ { keepPreviousPage: true },
+ );
const { data: pinnedTasks = [], isLoading: pinnedLoading } =
usePinnedTaskRecords();
const { dashboards, isLoading: canvasesLoading } = useAllCanvases();
@@ -106,7 +118,6 @@ export function useRecentWorkItems({
(state) => state.lastViewedAtByCanvasId,
);
const archivedTaskIds = useArchivedTaskIds();
- const { pinnedTaskIds, isLoading: pinIdsLoading } = usePinnedTasks();
const sessionFacts = useChannelSessionFacts();
const client = useOptionalAuthenticatedClient();
const { data: currentUser } = useCurrentUser({ client });
diff --git a/products/desktop/packages/ui/src/features/tasks/useTasks.test.tsx b/products/desktop/packages/ui/src/features/tasks/useTasks.test.tsx
index e87dda5caace..c74f5342d1e1 100644
--- a/products/desktop/packages/ui/src/features/tasks/useTasks.test.tsx
+++ b/products/desktop/packages/ui/src/features/tasks/useTasks.test.tsx
@@ -1,12 +1,14 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
-import { act, renderHook } from "@testing-library/react";
+import { act, renderHook, waitFor } from "@testing-library/react";
import type { PropsWithChildren } from "react";
import { describe, expect, it, vi } from "vitest";
import { taskKeys } from "./taskKeys";
-import { useTasks } from "./useTasks";
+import { pinnedRecordsPollInterval, useTasks } from "./useTasks";
+
+const mocks = vi.hoisted(() => ({ getTasks: vi.fn() }));
vi.mock("@posthog/ui/features/auth/authClient", () => ({
- useOptionalAuthenticatedClient: () => ({ getTasks: vi.fn() }),
+ useOptionalAuthenticatedClient: () => ({ getTasks: mocks.getTasks }),
}));
vi.mock("@posthog/ui/features/auth/useMeQuery", () => ({
@@ -46,4 +48,43 @@ describe("useTasks", () => {
expect(renderCount).toBe(1);
expect(result.current.data).toBeUndefined();
});
+
+ it("keeps the previous page while a new page size loads", async () => {
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const wrapper = ({ children }: PropsWithChildren) => (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ );
+ const firstPage = [{ id: "task-1" }];
+ mocks.getTasks
+ .mockResolvedValueOnce(firstPage)
+ .mockReturnValueOnce(new Promise(() => {}));
+ const { result, rerender } = renderHook(
+ ({ limit }) => useTasks({ limit }, { keepPreviousPage: true }),
+ { wrapper, initialProps: { limit: 50 } },
+ );
+ await waitFor(() => expect(result.current.data).toEqual(firstPage));
+
+ rerender({ limit: 51 });
+
+ expect(result.current.data).toEqual(firstPage);
+ expect(result.current.isLoading).toBe(false);
+ });
+});
+
+describe("pinnedRecordsPollInterval", () => {
+ it.each([
+ { pins: undefined, pages: 1 },
+ { pins: 100, pages: 1 },
+ { pins: 101, pages: 2 },
+ { pins: 1000, pages: 10 },
+ ])(
+ "waits one poll interval per page for $pins pins ($pages pages)",
+ ({ pins, pages }) => {
+ expect(pinnedRecordsPollInterval(pins)).toBe(
+ pages * pinnedRecordsPollInterval(0),
+ );
+ },
+ );
});
diff --git a/products/desktop/packages/ui/src/features/tasks/useTasks.ts b/products/desktop/packages/ui/src/features/tasks/useTasks.ts
index 149745d9c83a..dbd0dce60bf1 100644
--- a/products/desktop/packages/ui/src/features/tasks/useTasks.ts
+++ b/products/desktop/packages/ui/src/features/tasks/useTasks.ts
@@ -28,7 +28,11 @@ export function useTasks(
limit?: number;
ordering?: "-last_activity_at" | "-created_at";
},
- options?: { enabled?: boolean; subscribed?: boolean },
+ options?: {
+ enabled?: boolean;
+ subscribed?: boolean;
+ keepPreviousPage?: boolean;
+ },
) {
const { data: currentUser } = useMeQuery();
const createdBy = filters?.showAllUsers ? undefined : currentUser?.id;
@@ -62,10 +66,27 @@ export function useTasks(
enabled: (options?.enabled ?? true) && !!currentUser?.id,
subscribed: options?.subscribed,
refetchInterval: TASK_LIST_POLL_INTERVAL_MS,
+ placeholderData: options?.keepPreviousPage ? keepPreviousData : undefined,
},
);
}
+export const TASK_LIST_MAX_PAGE_SIZE = 100;
+
+/**
+ * Pinned records read every page on each poll, so the poll waits one interval per page. Any pin
+ * history then costs about one page request a minute and still refreshes.
+ */
+export function pinnedRecordsPollInterval(
+ pinCount: number | undefined,
+): number {
+ const pages = Math.max(
+ 1,
+ Math.ceil((pinCount ?? 0) / TASK_LIST_MAX_PAGE_SIZE),
+ );
+ return pages * TASK_LIST_POLL_INTERVAL_MS;
+}
+
// A pin can be on any task the user can see, also one older than the recent list reaches. The
// server cuts each page at 100 rows, so this reads every page to keep every pin. It polls like the
// other task lists: a pin on another user's task, or from a source the work list hides, has no
@@ -81,7 +102,11 @@ export function usePinnedTaskRecords() {
{ fetchAll: true },
)
).tasks as unknown as Task[],
- { staleTime: 30_000, refetchInterval: TASK_LIST_POLL_INTERVAL_MS },
+ {
+ staleTime: 30_000,
+ refetchInterval: (query) =>
+ pinnedRecordsPollInterval(query.state.data?.length),
+ },
);
}