: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
feat(desktop): explain a canvas that will not open (#112083)
posthog[bot]2fbe3c4c3bc3
diff --git a/products/desktop/packages/core/src/canvas/dashboardSchemas.ts b/products/desktop/packages/core/src/canvas/dashboardSchemas.tsindex 6f038aa0b701..4245ff439ca5 100644--- a/products/desktop/packages/core/src/canvas/dashboardSchemas.ts+++ b/products/desktop/packages/core/src/canvas/dashboardSchemas.ts@@ -136,6 +136,13 @@ export const createDashboardInput = z.object({export const dashboardIdInput = z.object({ id: z.string().min(1) });+// Why a canvas did not open, when it did not. `missing` covers a deleted+// canvas, another project's canvas, and a bad id alike — the API cannot tell+// them apart and neither can we. `no_access` is the one case it can name: the+// canvas is in this project, in a space nobody shared with the caller.+export const canvasAvailabilitySchema = z.enum(["ok", "missing", "no_access"]);+export type CanvasAvailability = z.infer<typeof canvasAvailabilitySchema>;+export const canvasBuildsInput = z.object({id: z.string().min(1),versionId: z.string().min(1).optional(),diff --git a/products/desktop/packages/core/src/canvas/dashboardsService.ts b/products/desktop/packages/core/src/canvas/dashboardsService.tsindex bfd30e4a8c95..e88a571abc06 100644--- a/products/desktop/packages/core/src/canvas/dashboardsService.ts+++ b/products/desktop/packages/core/src/canvas/dashboardsService.ts@@ -8,6 +8,7 @@ import {import {type CanvasActionDefinition,type CanvasActionResult,+ type CanvasAvailability,type CanvasConnectorCallResult,type CanvasCreator,type CanvasDraft,@@ -189,13 +190,26 @@ export class DashboardsService {return rows.map(toRecord);}+ // Null for both "no such canvas here" (404) and "its space is not shared with+ // you" (403): neither is an error the caller can retry, and `availability`+ // names which one it was for the surface that has to explain it.async get(id: string): Promise<DashboardRecord | null> {const res = await this.api.fetch(`canvases/${encodeURIComponent(id)}/`);- if (res.status === 404) return null;+ if (res.status === 404 || res.status === 403) return null;if (!res.ok) throw new Error(`Failed to load canvas (${res.status})`);return toRecord((await res.json()) as ApiCanvas);}+ // Why a canvas would not open. Read on the dead-end path only, so it costs a+ // request nobody makes while canvases are opening normally.+ async availability(id: string): Promise<CanvasAvailability> {+ const res = await this.api.fetch(`canvases/${encodeURIComponent(id)}/`);+ if (res.ok) return "ok";+ if (res.status === 403) return "no_access";+ if (res.status === 404) return "missing";+ throw new Error(`Failed to load canvas (${res.status})`);+ }+// Everything needed to open a canvas, in one round trip: the record, the// live build (signed artifact URL included), and, when there is nothing// built to render, the head source (freeform/component) or layout (grid).diff --git a/products/desktop/packages/core/src/canvas/services.ts b/products/desktop/packages/core/src/canvas/services.tsindex e16930d4e06e..8fdadbcc1250 100644--- a/products/desktop/packages/core/src/canvas/services.ts+++ b/products/desktop/packages/core/src/canvas/services.ts@@ -7,6 +7,7 @@ import type { ChannelTaskRecord } from "./channelTaskSchemas";import type {CanvasActionDefinition,CanvasActionResult,+ CanvasAvailability,CanvasConnectorCallResult,CanvasDraft,CanvasSource,@@ -50,6 +51,8 @@ export interface IDashboardsService {listAll(): Promise<DashboardRecord[]>;get(id: string): Promise<DashboardRecord | null>;publishProject(input: PublishProjectInput): Promise<PublishProjectResult>;+ // Why a canvas would not open, for the surface that has to explain it.+ availability(id: string): Promise<CanvasAvailability>;// Everything needed to open a canvas, in one round trip.view(id: string): Promise<CanvasView>;create(input: {diff --git a/products/desktop/packages/host-router/src/routers/dashboards.router.ts b/products/desktop/packages/host-router/src/routers/dashboards.router.tsindex c97ce94ba5d3..b0910045caba 100644--- a/products/desktop/packages/host-router/src/routers/dashboards.router.ts+++ b/products/desktop/packages/host-router/src/routers/dashboards.router.ts@@ -7,6 +7,7 @@ import {canvasActionDefinitionSchema,canvasActionInvokeInput,canvasActionResultSchema,+ canvasAvailabilitySchema,canvasBuildsInput,canvasConnectorCallResultSchema,canvasConnectorCallServiceInput,@@ -73,6 +74,15 @@ export const dashboardsRouter = router({.query(({ ctx, input }) =>ctx.container.get<IDashboardsService>(DASHBOARDS_SERVICE).get(input.id),),+ // Why a canvas would not open. Only the dead-end surface asks.+ availability: publicProcedure+ .input(dashboardIdInput)+ .output(canvasAvailabilitySchema)+ .query(({ ctx, input }) =>+ ctx.container+ .get<IDashboardsService>(DASHBOARDS_SERVICE)+ .availability(input.id),+ ),// Everything needed to open a canvas, in one round trip.view: publicProcedure.input(dashboardIdInput)diff --git a/products/desktop/packages/shared/src/analytics-events.ts b/products/desktop/packages/shared/src/analytics-events.tsindex 7dd039227e46..615891f49fa2 100644--- a/products/desktop/packages/shared/src/analytics-events.ts+++ b/products/desktop/packages/shared/src/analytics-events.ts@@ -1352,6 +1352,12 @@ export interface CanvasViewedProperties {template_id: string;}+export interface CanvasUnavailableProperties {+ dashboard_id: string;+ /** `no_access`: the canvas is in a space not shared with the viewer. `missing`: any other miss. */+ reason: "no_access" | "missing";+}+export interface CanvasRuntimeErrorProperties {channel_id?: string;dashboard_id?: string;@@ -1852,6 +1858,7 @@ export const ANALYTICS_EVENTS = {DASHBOARD_ACTION: "Dashboard action",CANVAS_PROMPT_SENT: "Canvas prompt sent",CANVAS_VIEWED: "Canvas viewed",+ CANVAS_UNAVAILABLE: "Canvas unavailable",CANVAS_RENDERED: "Canvas rendered",CANVAS_RUNTIME_ERROR: "Canvas runtime error",CANVAS_DATA_REQUEST_REJECTED: "Canvas data request rejected",@@ -2076,6 +2083,7 @@ export type EventPropertyMap = {[ANALYTICS_EVENTS.DASHBOARD_ACTION]: DashboardActionProperties;[ANALYTICS_EVENTS.CANVAS_PROMPT_SENT]: CanvasPromptSentProperties;[ANALYTICS_EVENTS.CANVAS_VIEWED]: CanvasViewedProperties;+ [ANALYTICS_EVENTS.CANVAS_UNAVAILABLE]: CanvasUnavailableProperties;[ANALYTICS_EVENTS.CANVAS_RENDERED]: CanvasRenderedProperties;[ANALYTICS_EVENTS.CANVAS_RUNTIME_ERROR]: CanvasRuntimeErrorProperties;[ANALYTICS_EVENTS.CANVAS_DATA_REQUEST_REJECTED]: CanvasDataRequestRejectedProperties;diff --git a/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.test.tsxnew file mode 100644index 000000000000..9da372762855--- /dev/null+++ b/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.test.tsx@@ -0,0 +1,70 @@+import { render, screen } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import { CanvasUnavailable } from "./CanvasUnavailable";++const availabilityState = vi.hoisted(() => ({+ current: {+ availability: undefined as string | undefined,+ isLoading: false,+ },+}));++vi.mock("@posthog/ui/features/canvas/hooks/useDashboards", () => ({+ useCanvasAvailability: () => availabilityState.current,+}));++const track = vi.hoisted(() => vi.fn());+vi.mock("@posthog/ui/shell/analytics", () => ({ track }));++function renderWith(current: {+ availability?: string;+ isLoading: boolean;+}): void {+ track.mockClear();+ availabilityState.current = {+ availability: current.availability,+ isLoading: current.isLoading,+ };+ render(<CanvasUnavailable canvasId="canvas-1" />);+}++describe("CanvasUnavailable", () => {+ it("waits for the reason before naming one", () => {+ renderWith({ isLoading: true });++ expect(screen.queryByText("Canvas not found")).toBeNull();+ expect(+ screen.queryByText("You don't have access to this canvas"),+ ).toBeNull();+ expect(track).not.toHaveBeenCalled();+ });++ it("names the space when the canvas is real but not shared", () => {+ renderWith({ availability: "no_access", isLoading: false });++ expect(+ screen.getByText("You don't have access to this canvas"),+ ).toBeVisible();+ expect(screen.getByText(/has not been shared with you/)).toBeVisible();+ expect(track).toHaveBeenCalledWith("Canvas unavailable", {+ dashboard_id: "canvas-1",+ reason: "no_access",+ });+ });++ it.each(["missing", undefined])(+ "falls back to not-found for %s",+ (availability) => {+ renderWith({ availability, isLoading: false });++ expect(screen.getByText("Canvas not found")).toBeVisible();+ expect(+ screen.getByText(/different organization or project/),+ ).toBeVisible();+ expect(track).toHaveBeenCalledWith("Canvas unavailable", {+ dashboard_id: "canvas-1",+ reason: "missing",+ });+ },+ );+});diff --git a/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.tsx b/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.tsxnew file mode 100644index 000000000000..610d29164acd--- /dev/null+++ b/products/desktop/packages/ui/src/features/canvas/components/CanvasUnavailable.tsx@@ -0,0 +1,55 @@+import { LockKeyIcon, ShapesIcon } from "@phosphor-icons/react";+import {+ Empty,+ EmptyDescription,+ EmptyHeader,+ EmptyMedia,+ EmptyTitle,+} from "@posthog/quill";+import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";+import { useCanvasAvailability } from "@posthog/ui/features/canvas/hooks/useDashboards";+import { LoadingState } from "@posthog/ui/primitives/LoadingState";+import { track } from "@posthog/ui/shell/analytics";+import { useEffect } from "react";++// Shown in place of a canvas that would not open. A canvas link carries no+// project, so it resolves against whichever project the reader has open. The+// two dead ends look identical from the link alone, so the reader has to be+// told which one they hit before they can do anything about it.+export function CanvasUnavailable({ canvasId }: { canvasId: string }) {+ const { availability, isLoading } = useCanvasAvailability(canvasId);+ const reason = availability === "no_access" ? "no_access" : "missing";++ useEffect(() => {+ if (isLoading) return;+ track(ANALYTICS_EVENTS.CANVAS_UNAVAILABLE, {+ dashboard_id: canvasId,+ reason,+ });+ }, [canvasId, isLoading, reason]);++ if (isLoading) return <LoadingState label="Loading canvas" />;++ const noAccess = reason === "no_access";+ return (+ <div className="flex h-full min-h-0 w-full items-center justify-center p-8">+ <Empty className="border-0">+ <EmptyHeader>+ <EmptyMedia variant="icon">+ {noAccess ? <LockKeyIcon /> : <ShapesIcon />}+ </EmptyMedia>+ <EmptyTitle>+ {noAccess+ ? "You don't have access to this canvas"+ : "Canvas not found"}+ </EmptyTitle>+ <EmptyDescription>+ {noAccess+ ? "It is in a space that has not been shared with you. Ask whoever sent the link to add you to the space, or to move the canvas to a public one."+ : "It may have been deleted, or the link may belong to a different organization or project than the one you have open."}+ </EmptyDescription>+ </EmptyHeader>+ </Empty>+ </div>+ );+}diff --git a/products/desktop/packages/ui/src/features/canvas/components/WebsiteDashboard.tsx b/products/desktop/packages/ui/src/features/canvas/components/WebsiteDashboard.tsxindex eb6380a9a9d7..a2c88bd386f7 100644--- a/products/desktop/packages/ui/src/features/canvas/components/WebsiteDashboard.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/WebsiteDashboard.tsx@@ -1,4 +1,5 @@import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";+import { CanvasUnavailable } from "@posthog/ui/features/canvas/components/CanvasUnavailable";import { FreeformCanvasView } from "@posthog/ui/features/canvas/freeform/FreeformCanvasView";import { GridCanvasView } from "@posthog/ui/features/canvas/grid/GridCanvasView";import { useDashboard } from "@posthog/ui/features/canvas/hooks/useDashboards";@@ -27,6 +28,14 @@ export function WebsiteDashboard({ dashboardId }: { dashboardId: string }) {});}, [dashboard]);+ // `null` (not `undefined`) is the settled answer that the canvas will not+ // open — the service returns it for both a miss and a space the reader is not+ // in. Without this the canvas surface renders its own empty frame, which+ // reads as a canvas that never finished loading.+ if (dashboard === null) {+ return <CanvasUnavailable canvasId={dashboardId} />;+ }+if (dashboard?.kind === "grid") {return <GridCanvasView canvasId={dashboardId} interactive={editing} />;}diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useDashboards.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useDashboards.tsindex 151a6e8bc21c..0d1daf160d1b 100644--- a/products/desktop/packages/ui/src/features/canvas/hooks/useDashboards.ts+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useDashboards.ts@@ -1,5 +1,6 @@import { UNTITLED_CANVAS_NAME } from "@posthog/core/canvas/canvasNaming";import type {+ CanvasAvailability,CanvasDraft,CanvasSource,CanvasVersion,@@ -158,6 +159,24 @@ export function useDashboard(id: string | undefined): {return { dashboard: data, isLoading, isFetching };}+/**+ * Why a canvas would not open. Asked only once `useDashboard` has answered+ * `null`, so a canvas that opens normally never pays for it.+ */+export function useCanvasAvailability(id: string | undefined): {+ availability: CanvasAvailability | undefined;+ isLoading: boolean;+} {+ const trpc = useHostTRPC();+ const { data, isLoading } = useQuery(+ trpc.dashboards.availability.queryOptions(+ { id: id ?? "" },+ { enabled: !!id, meta: AUTH_SCOPED_QUERY_META, staleTime: 5_000 },+ ),+ );+ return { availability: data, isLoading };+}+/** A canvas's source project — the head, or a historical version. */export function useCanvasSource(input: {id: string | undefined;