Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/ui/src/features/tasks/taskKeys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
export interface TaskListFilters {
repository?: string;
createdBy?: number;
originProduct?: string;
internal?: boolean;
}

export const taskKeys = {
all: ["tasks"] as const,
lists: () => [...taskKeys.all, "list"] as const,
list: (filters?: {
repository?: string;
createdBy?: number;
originProduct?: string;
internal?: boolean;
}) => [...taskKeys.lists(), filters] as const,
list: (filters?: TaskListFilters) => [...taskKeys.lists(), filters] as const,
// Extract the filters object from a `list` query key. Keeps knowledge of the
// key's shape (filters live in the last slot) here, next to `list`, instead
// of letting consumers reach in by positional index.
filtersOf: (queryKey: readonly unknown[]): TaskListFilters | undefined =>
queryKey[queryKey.length - 1] as TaskListFilters | undefined,
allSummaries: () => [...taskKeys.all, "summaries"] as const,
summaries: (ids: string[]) =>
[...taskKeys.allSummaries(), [...ids].sort()] as const,
Expand Down
81 changes: 80 additions & 1 deletion packages/ui/src/features/tasks/useTaskCrudMutations.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Task } from "@posthog/shared/domain-types";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
Expand Down Expand Up @@ -27,7 +28,8 @@ vi.mock("@posthog/di/react", () => ({
useService: () => deletionService,
}));

import { useDeleteTask } from "./useTaskCrudMutations";
import { taskKeys } from "./taskKeys";
import { useCreateTask, useDeleteTask } from "./useTaskCrudMutations";

function wrapper({ children }: { children: ReactNode }) {
const queryClient = new QueryClient();
Expand All @@ -36,6 +38,20 @@ function wrapper({ children }: { children: ReactNode }) {
);
}

function createTask(overrides: Partial<Task> = {}): Task {
return {
id: "new-task",
task_number: 1,
slug: "new-task",
title: "New task",
description: "New task",
created_at: "2026-06-15T00:00:00.000Z",
updated_at: "2026-06-15T00:00:00.000Z",
origin_product: "user_created",
...overrides,
};
}

describe("useDeleteTask.deleteWithConfirm", () => {
beforeEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -71,3 +87,66 @@ describe("useDeleteTask.deleteWithConfirm", () => {
expect(ok).toBe(false);
});
});

describe("useCreateTask.invalidateTasks", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it.each([
{ name: "plain list", filters: undefined, expectedLength: 1 },
{
name: "repository-scoped list",
filters: { repository: "owner/repo" },
expectedLength: 1,
},
{
name: "slack-origin list",
filters: { originProduct: "slack" },
expectedLength: 0,
},
])(
"seeds the $name with the new task ($expectedLength entr(y/ies))",
({ filters, expectedLength }) => {
const queryClient = new QueryClient();
const key = taskKeys.list(filters);
queryClient.setQueryData<Task[]>(key, []);

const localWrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);
const { result } = renderHook(() => useCreateTask(), {
wrapper: localWrapper,
});

result.current.invalidateTasks(createTask());

// Origin-less lists mirror the new task; origin-scoped lists (read by the
// sidebar to brand icons by id membership) must not be seeded.
expect(queryClient.getQueryData<Task[]>(key)).toHaveLength(
expectedLength,
);
},
);

it("still invalidates every list, including the slack-origin one", () => {
const queryClient = new QueryClient();
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");

const localWrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
const { result } = renderHook(() => useCreateTask(), {
wrapper: localWrapper,
});

result.current.invalidateTasks(createTask());

// Seeding is scoped, but the refetch is not: all lists (slack included) are
// invalidated so they reconcile with the server. A future refactor must not
// "fix" the no-seed expectation above by dropping a list from refetch.
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: taskKeys.lists() });
});
});
16 changes: 15 additions & 1 deletion packages/ui/src/features/tasks/useTaskCrudMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ export function useCreateTask() {

const invalidateTasks = (newTask?: Task) => {
if (newTask) {
// Only seed list caches that aren't scoped to a specific origin_product.
// An origin-scoped list (e.g. the slack-origin list behind useSlackTasks)
// is read by the sidebar to brand a task's icon by id membership, so
// seeding a freshly created, non-slack task into it would make that task
// briefly render as a Slack task until the list refetches. Origin-less
// lists, by contrast, should mirror every new task.
queryClient.setQueriesData<Task[]>(
{ queryKey: taskKeys.lists() },
{
queryKey: taskKeys.lists(),
predicate: (query) => {
const isOriginScopedList = Boolean(
taskKeys.filtersOf(query.queryKey)?.originProduct,
);
return !isOriginScopedList;
},
},
(old) => insertTaskDedup(old, newTask),
);
}
Expand Down
Loading