: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop/perf): remove per-channel task lookup fan-out (#84986)
Adam Leith9087b9554234
diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useTaskChannelMap.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useTaskChannelMap.tsdeleted file mode 100644index 10cf6c6873e3..000000000000--- a/products/desktop/packages/ui/src/features/canvas/hooks/useTaskChannelMap.ts+++ /dev/null@@ -1,41 +0,0 @@-import { useHostTRPC } from "@posthog/host-router/react";-import type { Channel } from "@posthog/ui/features/canvas/hooks/useChannels";-import { useQueries } from "@tanstack/react-query";-import { useMemo } from "react";--/**- * Map of taskId → the channel it's filed to. A task is filed to at most one- * channel (ChannelTasksService moves the row rather than duplicating it), so- * the mapping is unambiguous. Fans out one `channelTasks.list` query per- * channel; results are shared with the channel sidebar's per-section queries- * through the react-query cache.- *- * Takes the already-fetched `channels` rather than subscribing to `useChannels`- * itself, so the caller owns the single subscription and a stable reference.- */-export function useTaskChannelMap(- channels: Channel[],- options?: { enabled?: boolean },-): Map<string, Channel> {- const enabled = options?.enabled ?? true;- const trpc = useHostTRPC();- const results = useQueries({- queries: channels.map((channel) =>- trpc.channelTasks.list.queryOptions(- { channelId: channel.id },- { enabled, staleTime: 5_000 },- ),- ),- });- return useMemo(() => {- const map = new Map<string, Channel>();- results.forEach((res, i) => {- const channel = channels[i];- if (!channel) return;- for (const record of res.data ?? []) {- if (record.taskId) map.set(record.taskId, channel);- }- });- return map;- }, [results, channels]);-}diff --git a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx b/products/desktop/packages/ui/src/features/command/CommandMenu.tsxindex 5160303cd600..db269216149d 100644--- a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx+++ b/products/desktop/packages/ui/src/features/command/CommandMenu.tsx@@ -36,7 +36,6 @@ import { useArchivedTaskIds } from "@posthog/ui/features/archive/useArchivedTaskimport { channelGlyph } from "@posthog/ui/features/canvas/components/channelGlyph";import { useChannels } from "@posthog/ui/features/canvas/hooks/useChannels";import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";-import { useTaskChannelMap } from "@posthog/ui/features/canvas/hooks/useTaskChannelMap";import { getDefaultReviewMode } from "@posthog/ui/features/code-review/getDefaultReviewMode";import { useReviewNavigationStore } from "@posthog/ui/features/code-review/reviewNavigationStore";import { CommandKeyHints } from "@posthog/ui/features/command/CommandKeyHints";@@ -137,17 +136,12 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {const openSettingsDialog = openSettings;const closeSettingsDialog = closeSettings;const { folders } = useFolders();- // Channels (and the task→channel detail) are a Project Bluebird feature. Gate- // the channel fetches behind the flag so they never reach ungated users.const bluebirdEnabled = useFeatureFlag(PROJECT_BLUEBIRD_FLAG,import.meta.env.DEV,);const loopsEnabled = useFeatureFlag(LOOPS_FLAG, import.meta.env.DEV);const { channels } = useChannels({ enabled: bluebirdEnabled });- const taskChannelMap = useTaskChannelMap(channels, {- enabled: open && bluebirdEnabled,- });const { theme, setTheme } = useThemeStore();const toggleLeftSidebar = useSidebarStore((state) => state.toggle);const view = useAppView();@@ -503,11 +497,16 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {provisioningTaskIds.has(task.id)),);if (visibleTasks.length === 0) return [];+ const channelsById = new Map(+ channels.map((channel) => [channel.id, channel] as const),+ );return [{label: "Tasks",items: visibleTasks.map((task) => {- const channel = taskChannelMap.get(task.id);+ const channel = task.channel+ ? channelsById.get(task.channel)+ : undefined;return {id: `task-${task.id}`,label: task.title,@@ -538,7 +537,7 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {workspaces,workspacesFetched,provisioningTaskIds,- taskChannelMap,+ channels,bluebirdEnabled,closeSettingsDialog,]);diff --git a/products/desktop/packages/ui/src/features/deep-links/useHandleOpenTask.ts b/products/desktop/packages/ui/src/features/deep-links/useHandleOpenTask.tsindex 513b1618da93..b2238ffc5759 100644--- a/products/desktop/packages/ui/src/features/deep-links/useHandleOpenTask.ts+++ b/products/desktop/packages/ui/src/features/deep-links/useHandleOpenTask.ts@@ -7,8 +7,6 @@ import {import { useService } from "@posthog/di/react";import { PROJECT_BLUEBIRD_FLAG } from "@posthog/shared";import type { Task } from "@posthog/shared/domain-types";-import { useChannels } from "@posthog/ui/features/canvas/hooks/useChannels";-import { useTaskChannelMap } from "@posthog/ui/features/canvas/hooks/useTaskChannelMap";import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";import { useCommentNavigationStore } from "@posthog/ui/features/sessions/commentNavigationStore";import { useTaskViewed } from "@posthog/ui/features/sidebar/useTaskViewed";@@ -17,7 +15,7 @@ import { toast } from "@posthog/ui/primitives/toast";import { openTask as openTaskHelper } from "@posthog/ui/router/useOpenTask";import { logger } from "@posthog/ui/shell/logger";import { useQueryClient } from "@tanstack/react-query";-import { useCallback, useEffect, useRef } from "react";+import { useCallback } from "react";const log = logger.scope("open-task");@@ -50,20 +48,10 @@ export function useHandleOpenTask(): (const { markAsViewed } = useTaskViewed();const queryClient = useQueryClient();- // A task filed to a Project Bluebird channel opens in the channel-organized- // view under /website. Gate the channel fetches behind the flag.const bluebirdEnabled = useFeatureFlag(PROJECT_BLUEBIRD_FLAG,import.meta.env.DEV,);- const { channels } = useChannels({ enabled: bluebirdEnabled });- const channelMap = useTaskChannelMap(channels, { enabled: bluebirdEnabled });- // Mirror the latest map into a ref so the stable callback can read it without- // listing the map in its deps — otherwise it'd be recreated on every poll.- const channelMapRef = useRef(channelMap);- useEffect(() => {- channelMapRef.current = channelMap;- }, [channelMap]);return useCallback(async (@@ -101,13 +89,11 @@ export function useHandleOpenTask(): (queryClient.invalidateQueries({ queryKey: taskKeys.lists() });markAsViewed(taskId);- const channel = bluebirdEnabled- ? channelMapRef.current.get(task.id)- : undefined;- void openTaskHelper(- task,- channel ? { channelId: channel.id } : undefined,- );+ const channelTarget =+ bluebirdEnabled && task.channel+ ? { channelId: task.channel }+ : undefined;+ void openTaskHelper(task, channelTarget);if (comment) {useCommentNavigationStore.getState()diff --git a/products/desktop/packages/ui/src/features/deep-links/useOpenTargetDeepLink.test.tsx b/products/desktop/packages/ui/src/features/deep-links/useOpenTargetDeepLink.test.tsxindex e649a87a5538..0b0352f1fadc 100644--- a/products/desktop/packages/ui/src/features/deep-links/useOpenTargetDeepLink.test.tsx+++ b/products/desktop/packages/ui/src/features/deep-links/useOpenTargetDeepLink.test.tsx@@ -47,12 +47,6 @@ vi.mock("@posthog/ui/primitives/toast", () => ({ toast: { error: vi.fn() } }));vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({useFeatureFlag: () => false,}));-vi.mock("@posthog/ui/features/canvas/hooks/useChannels", () => ({- useChannels: () => ({ channels: [], isLoading: false }),-}));-vi.mock("@posthog/ui/features/canvas/hooks/useTaskChannelMap", () => ({- useTaskChannelMap: () => new Map(),-}));import { useOpenTargetDeepLink } from "./useOpenTargetDeepLink";diff --git a/products/desktop/packages/ui/src/features/deep-links/useTaskDeepLink.test.tsx b/products/desktop/packages/ui/src/features/deep-links/useTaskDeepLink.test.tsxindex f8987ebaa620..0f1b8ea4ae2e 100644--- a/products/desktop/packages/ui/src/features/deep-links/useTaskDeepLink.test.tsx+++ b/products/desktop/packages/ui/src/features/deep-links/useTaskDeepLink.test.tsx@@ -3,11 +3,17 @@ import { renderHook, waitFor } from "@testing-library/react";import type { ReactNode } from "react";import { beforeEach, describe, expect, it, vi } from "vitest";+const taskState = vi.hoisted(() => ({ channel: null as string | null }));const openTask = vi.hoisted(() =>- vi.fn().mockResolvedValue({- success: true,- data: { task: { id: "t1" }, workspace: null },- }),+ vi.fn().mockImplementation(() =>+ Promise.resolve({+ success: true,+ data: {+ task: { id: "t1", channel: taskState.channel },+ workspace: null,+ },+ }),+ ),);const getPendingDeepLink = vi.hoisted(() => vi.fn().mockResolvedValue(null));const onOpenTask = vi.hoisted(() =>@@ -21,9 +27,6 @@ const onOpenTask = vi.hoisted(() =>const routerOpenTask = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));const markAsViewed = vi.hoisted(() => vi.fn());const bluebirdState = vi.hoisted(() => ({ enabled: true }));-const channelMapState = vi.hoisted(() => ({- map: new Map<string, { id: string; name: string; path: string }>(),-}));vi.mock("@posthog/host-router/react", () => ({useHostTRPCClient: () => ({@@ -55,12 +58,6 @@ vi.mock("@posthog/ui/primitives/toast", () => ({vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({useFeatureFlag: () => bluebirdState.enabled,}));-vi.mock("@posthog/ui/features/canvas/hooks/useChannels", () => ({- useChannels: () => ({ channels: [], isLoading: false }),-}));-vi.mock("@posthog/ui/features/canvas/hooks/useTaskChannelMap", () => ({- useTaskChannelMap: () => channelMapState.map,-}));import { useTaskDeepLink } from "./useTaskDeepLink";@@ -78,11 +75,9 @@ describe("useTaskDeepLink", () => {vi.clearAllMocks();getPendingDeepLink.mockResolvedValue(null);bluebirdState.enabled = true;- channelMapState.map = new Map();+ taskState.channel = null;});- const marketing = { id: "chan-1", name: "marketing", path: "/marketing" };-// Both entry points (cold-start pending link, warm-start subscription event)// run the same routing dispatch: a channel-filed task opens in its /website// channel view, otherwise it falls back to /code — and only when the bluebird@@ -99,26 +94,26 @@ describe("useTaskDeepLink", () => {name: "cold-start channel-filed task → its channel view",trigger: "pending" as const,enabled: true,- channel: marketing,+ channel: "chan-1",expected: { channelId: "chan-1" },},{name: "cold-start filed task with flag off → /code",trigger: "pending" as const,enabled: false,- channel: marketing,+ channel: "chan-1",expected: undefined,},{name: "warm-start channel-filed task → its channel view",trigger: "warm" as const,enabled: true,- channel: marketing,+ channel: "chan-1",expected: { channelId: "chan-1" },},])("$name", async ({ trigger, enabled, channel, expected }) => {bluebirdState.enabled = enabled;- if (channel) channelMapState.map = new Map([["t1", channel]]);+ taskState.channel = channel;if (trigger === "pending") {getPendingDeepLink.mockResolvedValue({ taskId: "t1" });@@ -130,7 +125,10 @@ describe("useTaskDeepLink", () => {}await waitFor(() =>- expect(routerOpenTask).toHaveBeenCalledWith({ id: "t1" }, expected),+ expect(routerOpenTask).toHaveBeenCalledWith(+ { id: "t1", channel },+ expected,+ ),);expect(openTask).toHaveBeenCalledWith("t1", undefined);expect(markAsViewed).toHaveBeenCalledWith("t1");