Skip to content

Commit 23f0333

Browse files
authored
fix: validate and diagnose recents writes
Generated-By: PostHog Code Task-Id: f68869f0-4c94-4017-add1-ce18b6a323b3
1 parent 89ec095 commit 23f0333

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/core/src/recents/recentsService.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,20 @@ describe("RecentsService", () => {
7575
}),
7676
);
7777
});
78+
79+
it("does not rewrite a non-canvas row supplied as a canvas id", async () => {
80+
const { service, getEntry, fetch } = serviceWith([]);
81+
getEntry.mockResolvedValue({
82+
id: "task-row",
83+
path: "Tasks/task-row",
84+
type: "task",
85+
ref: "task-row",
86+
} as never);
87+
88+
await expect(
89+
service.record({ kind: "canvas", id: "task-row" }),
90+
).rejects.toThrow("Canvas not found");
91+
92+
expect(fetch).not.toHaveBeenCalled();
93+
});
7894
});

packages/core/src/recents/recentsService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export class RecentsService {
5454

5555
private async ensureCanvasReference(id: string): Promise<void> {
5656
const entry = await this.fs.getEntry<RecentFsEntry>(id, "canvas");
57-
if (!entry) throw new Error("Canvas not found");
57+
if (!entry || entry.type !== "dashboard")
58+
throw new Error("Canvas not found");
5859
if (entry.ref === id) return;
5960
const response = await this.fs.fetch(`${encodeURIComponent(id)}/`, {
6061
method: "PATCH",

packages/ui/src/features/recents/useRecents.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { RecentEngagementInput } from "@posthog/core/recents/schemas";
22
import { useHostTRPC, useHostTRPCClient } from "@posthog/host-router/react";
33
import { AUTH_SCOPED_QUERY_META } from "@posthog/ui/features/auth/useCurrentUser";
4+
import { logger } from "@posthog/ui/shell/logger";
45
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
56
import { useCallback } from "react";
67

8+
const log = logger.scope("recents");
9+
710
export function useRecents() {
811
const trpc = useHostTRPC();
912
return useQuery(
@@ -25,6 +28,9 @@ export function useRecordRecentEngagement(): (
2528
client.recents.record.mutate(input),
2629
onSuccess: () =>
2730
queryClient.invalidateQueries(trpc.recents.list.pathFilter()),
31+
onError: (error, input) => {
32+
log.warn("Failed to record recent engagement", { error, input });
33+
},
2834
});
2935
return useCallback((input: RecentEngagementInput) => mutate(input), [mutate]);
3036
}

0 commit comments

Comments
 (0)