Open History to see the last commit
1import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
2import {
3 isLocalEditorFileUrl,
4 REMOTE_CAPABLE_EDITOR_IDS,
5 remoteSchemeForEditor,
6 type SystemSettingsPane,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12
13import * as Electron from "electron";
14import { parseObsidianNavigationHref } from "@t3tools/shared/markdownLinks";
15
16// Remote editor deep links and local editor file links must reach the OS handler.
17const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
18const REMOTE_EDITOR_PROTOCOLS = new Set(
19 REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
20 const scheme = remoteSchemeForEditor(id);
21 return scheme === undefined ? [] : [`${scheme}:`];
22 }),
23);
24
25// "Use T3 Code in another app" opens these apps (apps/web/src/components/mcp/mcpApps.ts). Match
26// each exact link shape the wizard sends, never a whole scheme.
27const isAppLaunchUrl = (url: URL) =>
28 url.protocol === "grokbot:" &&
29 url.host === "app" &&
30 url.pathname === "/v1/prompt" &&
31 url.username.length === 0 &&
32 url.password.length === 0 &&
33 url.hash.length === 0 &&
34 [...url.searchParams.keys()].join() === "text";
35
36// Zed embeds [user@]host in the path; reject passwords and ports there.
37const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
38
39const isRemoteEditorUrl = (url: URL) =>
40 REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&
41 url.username.length === 0 &&
42 url.password.length === 0 &&
43 (url.protocol === "zed:"
44 ? url.host === "ssh" && ZED_SSH_PATHNAME.test(url.pathname)
45 : url.protocol === "jetbrains:"
46 ? url.host === "gateway" &&
47 url.pathname === "/ssh/environment" &&
48 (url.searchParams.get("h") ?? "").length > 0
49 : url.host === "vscode-remote" &&
50 url.pathname.startsWith("/ssh-remote+") &&
51 url.pathname.length > "/ssh-remote+".length);
52
53export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
54 if (typeof rawUrl !== "string") {
55 return Option.none();
56 }
57
58 try {
59 const url = new URL(rawUrl);
60 return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
61 isAppLaunchUrl(url) ||
62 isRemoteEditorUrl(url) ||
63 isLocalEditorFileUrl(url) ||
64 parseObsidianNavigationHref(rawUrl) !== null
65 ? Option.some(url.href)
66 : Option.none();
67 } catch {
68 return Option.none();
69 }
70}
71
72/** Links that open in the browser, for callers such as popups that carry no identity. */
73export const parseWebExternalUrl = (rawUrl: unknown): Option.Option<string> =>
74 Option.filter(parseSafeExternalUrl(rawUrl), (url) =>
75 SAFE_WEB_PROTOCOLS.has(new URL(url).protocol),
76 );
77
78export class ElectronShell extends Context.Service<
79 ElectronShell,
80 {
81 readonly openExternal: (rawUrl: unknown) => Effect.Effect<boolean>;
82 readonly openSystemSettings: (pane: SystemSettingsPane) => Effect.Effect<boolean>;
83 readonly copyText: (text: string) => Effect.Effect<void>;
84 }
85>()("@t3tools/desktop/electron/ElectronShell") {}
86
87/** @public */
88export const make = ElectronShell.of({
89 openExternal: Effect.fn("ElectronShell.openExternal")(function (rawUrl) {
90 return Option.match(parseSafeExternalUrl(rawUrl), {
91 onNone: () => Effect.succeed(false),
92 onSome: (externalUrl) =>
93 Effect.promise(() =>
94 Electron.shell.openExternal(externalUrl).then(
95 () => true,
96 () => false,
97 ),
98 ),
99 });
100 }),
101 openSystemSettings: Effect.fn("ElectronShell.openSystemSettings")(function (pane) {
102 return Effect.promise(() =>
103 Electron.shell.openExternal(MAC_PERMISSION_SETTINGS_URLS[pane]).then(
104 () => true,
105 () => false,
106 ),
107 );
108 }),
109 copyText: Effect.fn("ElectronShell.copyText")(function (text) {
110 return Effect.promise(() => Electron.clipboard.writeText(text).catch(() => undefined));
111 }),
112});
113
114export const layer = Layer.succeed(ElectronShell, make);