-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcanvas-link.ts
More file actions
114 lines (99 loc) · 3.36 KB
/
Copy pathcanvas-link.ts
File metadata and controls
114 lines (99 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { ROOT_LOGGER, type RootLogger } from "@posthog/di/logger";
import {
DEEP_LINK_SERVICE,
type IDeepLinkRegistry,
} from "@posthog/platform/deep-link";
import {
type IMainWindow,
MAIN_WINDOW_SERVICE,
} from "@posthog/platform/main-window";
import { TypedEventEmitter } from "@posthog/shared";
import { inject, injectable } from "inversify";
import type { LinkLogger } from "./identifiers";
export const CanvasLinkEvent = {
OpenCanvas: "openCanvas",
} as const;
export interface CanvasLinkPayload {
/** Channel (folder) row id the canvas lives under. */
channelId: string;
/** Dashboard row id of the canvas. */
dashboardId: string;
}
export interface CanvasLinkEvents {
[CanvasLinkEvent.OpenCanvas]: CanvasLinkPayload;
}
/**
* Handles canvas deep links (`<scheme>://canvas/{channelId}/{dashboardId}`, e.g.
* `posthog-code://…` in production and `posthog-code-dev://…` in local dev).
* Shareable canvas links resolve to a web interstitial that fires this scheme,
* so a teammate can open a canvas straight in the desktop app. Both ids are
* stable, rename-proof desktop file-system row ids.
*
* Mirrors `InboxLinkService`: queues a link that arrived before the renderer was
* ready, and emits for links delivered while the app is already running.
*/
@injectable()
export class CanvasLinkService extends TypedEventEmitter<CanvasLinkEvents> {
private pendingDeepLink: CanvasLinkPayload | null = null;
private readonly log: LinkLogger;
constructor(
@inject(DEEP_LINK_SERVICE)
private readonly deepLinkService: IDeepLinkRegistry,
@inject(MAIN_WINDOW_SERVICE)
private readonly mainWindow: IMainWindow,
@inject(ROOT_LOGGER)
rootLogger: RootLogger,
) {
super();
this.log = rootLogger.scope("canvas-link-service");
this.deepLinkService.registerHandler("canvas", (path) =>
this.handleCanvasLink(path),
);
}
private handleCanvasLink(path: string): boolean {
const [channelId, dashboardId] = path
.split("/")
.map((segment) => decodeSegment(segment));
if (!channelId || !dashboardId) {
this.log.warn("Canvas link missing channel or dashboard id");
return false;
}
const payload: CanvasLinkPayload = { channelId, dashboardId };
const hasListeners = this.listenerCount(CanvasLinkEvent.OpenCanvas) > 0;
if (hasListeners) {
this.log.info(
`Emitting canvas link event: channelId=${channelId} dashboardId=${dashboardId}`,
);
this.emit(CanvasLinkEvent.OpenCanvas, payload);
} else {
this.log.info(
`Queueing canvas link (renderer not ready): channelId=${channelId} dashboardId=${dashboardId}`,
);
this.pendingDeepLink = payload;
}
this.log.info("Deep link focusing window", { channelId, dashboardId });
if (this.mainWindow.isMinimized()) {
this.mainWindow.restore();
}
this.mainWindow.focus();
return true;
}
public consumePendingDeepLink(): CanvasLinkPayload | null {
const pending = this.pendingDeepLink;
this.pendingDeepLink = null;
if (pending) {
this.log.info(
`Consumed pending canvas link: channelId=${pending.channelId} dashboardId=${pending.dashboardId}`,
);
}
return pending;
}
}
function decodeSegment(segment: string | undefined): string {
if (!segment) return "";
try {
return decodeURIComponent(segment);
} catch {
return segment;
}
}