This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscout-link.ts
More file actions
106 lines (91 loc) · 2.82 KB
/
Copy pathscout-link.ts
File metadata and controls
106 lines (91 loc) · 2.82 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
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 ScoutLinkEvent = {
OpenScout: "openScout",
} as const;
export interface ScoutLinkPayload {
/** Route slug for the scout (e.g. "error-tracking"). */
skillSlug: string;
/** Emission id to expand and scroll to, if the link carried one. */
findingId?: string;
}
export interface ScoutLinkEvents {
[ScoutLinkEvent.OpenScout]: ScoutLinkPayload;
}
@injectable()
export class ScoutLinkService extends TypedEventEmitter<ScoutLinkEvents> {
private pendingDeepLink: ScoutLinkPayload | 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("scout-link-service");
this.deepLinkService.registerHandler("scout", (path, searchParams) =>
this.handleScoutLink(path, searchParams),
);
}
private handleScoutLink(
path: string,
searchParams: URLSearchParams,
): boolean {
const skillSlug = decodeSegment(path.split("/")[0]);
if (!skillSlug) {
this.log.warn("Scout link missing skill slug");
return false;
}
const findingId = searchParams.get("finding") ?? undefined;
const payload: ScoutLinkPayload = { skillSlug, findingId };
const hasListeners = this.listenerCount(ScoutLinkEvent.OpenScout) > 0;
if (hasListeners) {
this.log.info(
`Emitting scout link event: skillSlug=${skillSlug} findingId=${findingId ?? "(none)"}`,
);
this.emit(ScoutLinkEvent.OpenScout, payload);
} else {
this.log.info(
`Queueing scout link (renderer not ready): skillSlug=${skillSlug}`,
);
this.pendingDeepLink = payload;
}
this.log.info("Deep link focusing window", { skillSlug });
if (this.mainWindow.isMinimized()) {
this.mainWindow.restore();
}
this.mainWindow.focus();
return true;
}
public consumePendingDeepLink(): ScoutLinkPayload | null {
const pending = this.pendingDeepLink;
this.pendingDeepLink = null;
if (pending) {
this.log.info(
`Consumed pending scout link: skillSlug=${pending.skillSlug}`,
);
}
return pending;
}
}
function decodeSegment(segment: string | undefined): string {
if (!segment) return "";
try {
return decodeURIComponent(segment);
} catch {
return segment;
}
}