: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): open canvas links in the app, not the browser (#83161)
Thomas Obermüllerf1cd15764b72
diff --git a/products/desktop/packages/ui/src/features/canvas/components/MentionText.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/MentionText.test.tsxindex f2cb18e513af..77c6d0877e10 100644--- a/products/desktop/packages/ui/src/features/canvas/components/MentionText.test.tsx+++ b/products/desktop/packages/ui/src/features/canvas/components/MentionText.test.tsx@@ -11,6 +11,10 @@ vi.mock("@posthog/ui/router/navigationBridge", () => ({navigateToChannelTask: vi.fn(),}));+vi.mock("@posthog/ui/utils/urls", () => ({+ getPostHogUrl: (path: string) => `https://us.posthog.com${path}`,+}));+beforeEach(() => {vi.clearAllMocks();});diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useShareLinkInterceptor.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useShareLinkInterceptor.tsnew file mode 100644index 000000000000..b815602d8afc--- /dev/null+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useShareLinkInterceptor.ts@@ -0,0 +1,10 @@+import { interceptShareLinkClicks } from "@posthog/ui/utils/shareLinks";+import { useEffect } from "react";++/**+ * App-wide fallback so a canvas or channel link opens in the app rather than the+ * browser, whichever surface rendered it. See {@link interceptShareLinkClicks}.+ */+export function useShareLinkInterceptor(): void {+ useEffect(() => interceptShareLinkClicks(document), []);+}diff --git a/products/desktop/packages/ui/src/router/routes/__root.tsx b/products/desktop/packages/ui/src/router/routes/__root.tsxindex c2c514cb3d11..ebf6061d7b12 100644--- a/products/desktop/packages/ui/src/router/routes/__root.tsx+++ b/products/desktop/packages/ui/src/router/routes/__root.tsx@@ -30,6 +30,7 @@ import {import { useCanvasDeepLink } from "@posthog/ui/features/canvas/hooks/useCanvasDeepLink";import { useChannelDeepLink } from "@posthog/ui/features/canvas/hooks/useChannelDeepLink";import { useChannelsLayout } from "@posthog/ui/features/canvas/hooks/useChannelsLayout";+import { useShareLinkInterceptor } from "@posthog/ui/features/canvas/hooks/useShareLinkInterceptor";import { usePostHogWebFeedbackStore } from "@posthog/ui/features/canvas/stores/posthogWebFeedbackStore";import { CommandMenu } from "@posthog/ui/features/command/CommandMenu";import { CommandSearchBar } from "@posthog/ui/features/command/CommandSearchBar";@@ -236,6 +237,7 @@ function RootLayout() {useCanvasDeepLink();useChannelDeepLink();useLoopDeepLink();+ useShareLinkInterceptor();const approvalDeepLink = useApprovalDeepLink();useSetupDiscovery();useNewTaskDeepLink();diff --git a/products/desktop/packages/ui/src/utils/shareLinks.test.ts b/products/desktop/packages/ui/src/utils/shareLinks.test.tsindex 7184867d6ac8..17045ee0651a 100644--- a/products/desktop/packages/ui/src/utils/shareLinks.test.ts+++ b/products/desktop/packages/ui/src/utils/shareLinks.test.ts@@ -1,5 +1,8 @@-import { handleShareLinkClick } from "@posthog/ui/utils/shareLinks";-import { beforeEach, describe, expect, it, vi } from "vitest";+import {+ handleShareLinkClick,+ interceptShareLinkClicks,+} from "@posthog/ui/utils/shareLinks";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const navigateToChannel = vi.fn();const navigateToChannelDashboard = vi.fn();@@ -12,6 +15,10 @@ vi.mock("@posthog/ui/router/navigationBridge", () => ({navigateToChannelTask: (...args: unknown[]) => navigateToChannelTask(...args),}));+vi.mock("@posthog/ui/utils/urls", () => ({+ getPostHogUrl: (path: string) => `https://us.posthog.com${path}`,+}));+beforeEach(() => {vi.clearAllMocks();});@@ -62,6 +69,31 @@ describe("handleShareLinkClick", () => {},);+ it("leaves a canvas on another PostHog instance to the browser", () => {+ const event = { preventDefault: vi.fn() };++ const handled = handleShareLinkClick(+ "https://eu.posthog.com/code/canvas/chan1/dash1",+ event,+ );++ expect(handled).toBe(false);+ expect(event.preventDefault).not.toHaveBeenCalled();+ expect(navigateToChannelDashboard).not.toHaveBeenCalled();+ });++ it("stands down when something already handled the click", () => {+ const event = { preventDefault: vi.fn(), defaultPrevented: true };++ const handled = handleShareLinkClick(+ "https://us.posthog.com/code/canvas/chan1/dash1",+ event,+ );++ expect(handled).toBe(false);+ expect(navigateToChannelDashboard).not.toHaveBeenCalled();+ });+it("leaves an external link alone", () => {const event = { preventDefault: vi.fn() };@@ -81,3 +113,58 @@ describe("handleShareLinkClick", () => {expect(event.preventDefault).not.toHaveBeenCalled();});});++describe("interceptShareLinkClicks", () => {+ let uninstall: () => void;++ function clickAnchor(href: string): boolean {+ const anchor = document.createElement("a");+ anchor.href = href;+ anchor.target = "_blank";+ const label = document.createElement("span");+ label.textContent = "link";+ anchor.append(label);+ document.body.append(anchor);+ // Clicking the inner span, not the anchor, also covers the `closest` walk+ // from whatever element the user actually hit.+ return label.dispatchEvent(+ new MouseEvent("click", { bubbles: true, cancelable: true }),+ );+ }++ beforeEach(() => {+ uninstall = interceptShareLinkClicks(document);+ });++ afterEach(() => {+ uninstall();+ document.body.replaceChildren();+ });++ it("opens a canvas link the app rendered without asking the browser", () => {+ const notCancelled = clickAnchor(+ "https://us.posthog.com/code/canvas/chan1/dash1",+ );++ expect(notCancelled).toBe(false);+ expect(navigateToChannelDashboard).toHaveBeenCalledWith("chan1", "dash1");+ });++ it("lets an external link through to the browser", () => {+ const notCancelled = clickAnchor("https://example.com/docs");++ expect(notCancelled).toBe(true);+ expect(navigateToChannelDashboard).not.toHaveBeenCalled();+ });++ it("stops intercepting once uninstalled", () => {+ uninstall();++ const notCancelled = clickAnchor(+ "https://us.posthog.com/code/canvas/chan1/dash1",+ );++ expect(notCancelled).toBe(true);+ expect(navigateToChannelDashboard).not.toHaveBeenCalled();+ });+});diff --git a/products/desktop/packages/ui/src/utils/shareLinks.ts b/products/desktop/packages/ui/src/utils/shareLinks.tsindex 62f0b4b17c49..f37e2b6e97b0 100644--- a/products/desktop/packages/ui/src/utils/shareLinks.ts+++ b/products/desktop/packages/ui/src/utils/shareLinks.ts@@ -7,6 +7,7 @@ import {parseShareLink,type ShareLinkTarget,} from "@posthog/ui/utils/posthogLinks";+import { getPostHogUrl } from "@posthog/ui/utils/urls";export function navigateToShareTarget(target: ShareLinkTarget): void {switch (target.kind) {@@ -23,8 +24,29 @@ export function navigateToShareTarget(target: ShareLinkTarget): void {}}+/**+ * The canvas or channel an href points at, but only when the link belongs to the+ * PostHog instance the user is signed in to. Canvas and channel ids are+ * per-instance, so a link from another region names rows that don't exist here —+ * opening it in-app would land on an empty canvas. Those stay browser links.+ */+export function parseLocalShareLink(+ href: string | undefined | null,+): ShareLinkTarget | null {+ if (!href) return null;+ const base = getPostHogUrl("/");+ if (!base) return null;+ try {+ if (new URL(href).origin !== new URL(base).origin) return null;+ } catch {+ return null;+ }+ return parseShareLink(href);+}+interface ShareLinkClickEvent {preventDefault: () => void;+ defaultPrevented?: boolean;metaKey?: boolean;ctrlKey?: boolean;shiftKey?: boolean;@@ -46,10 +68,34 @@ export function handleShareLinkClick(href: string | undefined,event: ShareLinkClickEvent,): boolean {- if (!href || isModifiedClick(event)) return false;- const target = parseShareLink(href);+ if (event.defaultPrevented || isModifiedClick(event)) return false;+ const target = parseLocalShareLink(href);if (!target) return false;event.preventDefault();navigateToShareTarget(target);return true;}++/**+ * Open canvas and channel links in the app instead of the browser, wherever the+ * link is rendered — agent prose, thread messages, an inbox report, a canvas's+ * own HTML. Electron hands any anchor it doesn't recognise to `shell.openExternal`,+ * so a surface that doesn't call {@link handleShareLinkClick} itself sends the+ * user out to the web page for a canvas the app can show directly.+ *+ * Capture phase on the document, so it runs before React's own handlers; those+ * then see `defaultPrevented` and stand down rather than navigating twice.+ * Links to another instance and modified clicks fall through untouched, keeping+ * "open in a new window" working.+ */+export function interceptShareLinkClicks(root: Document): () => void {+ const onClick = (event: MouseEvent): void => {+ const node = event.target;+ if (!(node instanceof Element)) return;+ const anchor = node.closest("a[href]");+ if (!(anchor instanceof HTMLAnchorElement)) return;+ handleShareLinkClick(anchor.href, event);+ };+ root.addEventListener("click", onClick, true);+ return () => root.removeEventListener("click", onClick, true);+}