Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Task } from "@posthog/shared/domain-types";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import type { PropsWithChildren } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useRefreshedTask } from "./useRefreshedTask";

const mocks = vi.hoisted(() => ({ getTask: vi.fn() }));

vi.mock("@posthog/ui/features/auth/authClientImperative", () => ({
getAuthenticatedClient: vi.fn(async () => ({ getTask: mocks.getTask })),
}));

function task(runId: string, status: "failed" | "in_progress"): Task {
return {
id: "task-123",
title: "Cloud task",
description: "Keep working",
repository: null,
latest_run: {
id: runId,
task: "task-123",
environment: "cloud",
status,
state: {},
},
} as Task;
}

describe("useRefreshedTask", () => {
beforeEach(() => {
mocks.getTask.mockReset();
});

it("replaces a cached failed run with the authoritative resumed run", async () => {
const failedParent = task("run-parent", "failed");
const resumedChild = task("run-child", "in_progress");
mocks.getTask.mockResolvedValue(resumedChild);
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
const wrapper = ({ children }: PropsWithChildren) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

const { result } = renderHook(
() => useRefreshedTask("task-123", failedParent),
{ wrapper },
);

expect(result.current.latest_run?.id).toBe("run-parent");
await waitFor(() => {
expect(result.current.latest_run?.id).toBe("run-child");
});
expect(mocks.getTask).toHaveBeenCalledWith("task-123");
});
});
13 changes: 13 additions & 0 deletions packages/ui/src/features/task-detail/hooks/useRefreshedTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Task } from "@posthog/shared/domain-types";
import { useQuery } from "@tanstack/react-query";
import { taskDetailQuery } from "../../tasks/queries";

export function useRefreshedTask(taskId: string, initialTask: Task): Task {
const { data } = useQuery({
...taskDetailQuery(taskId),
initialData: initialTask,
refetchOnMount: "always",
});

return data;
}
10 changes: 2 additions & 8 deletions packages/ui/src/features/task-detail/hooks/useTaskData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { getTaskRepository } from "@posthog/shared";
import type { Task } from "@posthog/shared/domain-types";
import { useWorkspaceTRPC } from "@posthog/workspace-client/trpc";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { cloneStore } from "../../clone/cloneStore";
import { useTasks } from "../../tasks/useTasks";
import { useWorkspace } from "../../workspace/useWorkspace";
import { useRefreshedTask } from "./useRefreshedTask";

interface UseTaskDataParams {
taskId: string;
Expand All @@ -19,12 +18,7 @@ interface UseTaskDataParams {

export function useTaskData({ taskId, initialTask }: UseTaskDataParams) {
const trpcReact = useWorkspaceTRPC();
const { data: tasks = [] } = useTasks();

const task = useMemo(
() => tasks.find((t) => t.id === taskId) || initialTask,
[tasks, taskId, initialTask],
);
const task = useRefreshedTask(taskId, initialTask);

const workspace = useWorkspace(taskId);
const repoPath = workspace?.folderPath ?? null;
Expand Down
Loading