This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinbox-link.ts
More file actions
86 lines (73 loc) · 2.26 KB
/
Copy pathinbox-link.ts
File metadata and controls
86 lines (73 loc) · 2.26 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
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 InboxLinkEvent = {
OpenReport: "openReport",
} as const;
export interface InboxLinkEvents {
[InboxLinkEvent.OpenReport]: { reportId: string };
}
export interface PendingInboxDeepLink {
reportId: string;
}
@injectable()
export class InboxLinkService extends TypedEventEmitter<InboxLinkEvents> {
private pendingDeepLink: PendingInboxDeepLink | 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("inbox-link-service");
this.deepLinkService.registerHandler("inbox", (path) =>
this.handleInboxLink(path),
);
}
private handleInboxLink(path: string): boolean {
const reportId = path.split("/")[0];
if (!reportId) {
this.log.warn("Inbox link missing report ID");
return false;
}
const hasListeners = this.listenerCount(InboxLinkEvent.OpenReport) > 0;
if (hasListeners) {
this.log.info(`Emitting inbox link event: reportId=${reportId}`);
this.emit(InboxLinkEvent.OpenReport, { reportId });
} else {
this.log.info(
`Queueing inbox link (renderer not ready): reportId=${reportId}`,
);
this.pendingDeepLink = { reportId };
}
this.log.info("Deep link focusing window", { reportId });
if (this.mainWindow.isMinimized()) {
this.mainWindow.restore();
}
this.mainWindow.focus();
return true;
}
public consumePendingDeepLink(): PendingInboxDeepLink | null {
const pending = this.pendingDeepLink;
this.pendingDeepLink = null;
if (pending) {
this.log.info(
`Consumed pending inbox link: reportId=${pending.reportId}`,
);
}
return pending;
}
}