fix(desktop): forward second-instance deep links to the running app #7180
Closedmaslinedwin(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromrefs/pull/7180/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/desktop/src/app/DesktopClerk.ts line 213Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -139,9 +155,15 @@155+ yield* Effect.sync(() => {156+ void Promise.resolve(mainWindow.value.loadURL(url)).catch(() => undefined);157+ });158+ });159+160+ yield* electronApp.on("second-instance", (_event, argv) => {139161 void runPromise(140- Effect.gen(function* () {141- const mainWindow = yield* electronWindow.currentMainOrFirst;142- if (Option.isSome(mainWindow)) {143- yield* electronWindow.reveal(mainWindow.value);144- }145- }),162+ revealAndDispatch(163+ extractDesktopProtocolUrl(Array.isArray(argv) ? argv.filter(isStringArg) : [], scheme),164+ ),146165 );147166 });167+168+ if (environment.platform === "darwin") {169+ yield* electronApp.on("open-url", (event, url) => {
- apps/desktop/src/app/DesktopClerk.ts line 173Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -135,4 +138,10 @@135138 return yield* Effect.interrupt;136139 }137140138- yield* electronApp.on("second-instance", () => {141+ const scheme = ElectronProtocol.getDesktopScheme(environment.isDevelopment);142+ const revealAndDispatch = Effect.fn("desktop.clerk.revealAndDispatchProtocolUrl")(function* (143+ url: string | null,144+ ) {145+ const mainWindow = yield* electronWindow.currentMainOrFirst;146+ if (Option.isNone(mainWindow)) {147+ return;apps/desktop/src/app/DesktopClerk.ts line 156OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -135,4 +138,19 @@135138 return yield* Effect.interrupt;136139 }137140138- yield* electronApp.on("second-instance", () => {141+ const scheme = ElectronProtocol.getDesktopScheme(environment.isDevelopment);142+ const revealAndDispatch = Effect.fn("desktop.clerk.revealAndDispatchProtocolUrl")(function* (143+ url: string | null,144+ ) {145+ const mainWindow = yield* electronWindow.currentMainOrFirst;146+ if (Option.isNone(mainWindow)) {147+ return;148+ }149+ yield* electronWindow.reveal(mainWindow.value);150+ if (url === null) {151+ return;152+ }153+ // Same path as first-launch renderer loads: the custom protocol serves154+ // t3code:// (and t3code-dev://) on the existing window.155+ yield* Effect.sync(() => {156+ void Promise.resolve(mainWindow.value.loadURL(url)).catch(() => undefined);
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +7,21 @@7+export function extractDesktopProtocolUrl(argv: readonly string[], scheme: string): string | null {8+ let found: string | null = null;9+ for (const arg of argv) {10+ if (arg.length === 0 || arg.startsWith("-") || !isDesktopProtocolUrl(arg, scheme)) {11+ continue;12+ }13+ found = arg;14+ }15+ return found;16+}17+18+export function loadDesktopProtocolUrl(19+ window: { readonly loadURL: (url: string) => unknown },20+ url: string,21+): void {22+ void Promise.resolve(window.loadURL(url)).catch(() => undefined);23+}24+25+// Latest protocol URL received before a real main window exists (cold launch26+// or WSL connecting splash). DesktopWindow.createMain applies it after setMain.27+let pendingDesktopProtocolUrl: string | null = null;
- apps/desktop/src/app/DesktopClerk.ts line 174OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -138,0 +154,21 @@154+ const scheme = ElectronProtocol.getDesktopScheme(environment.isDevelopment);155+ const revealAndDispatch = Effect.fn("desktop.clerk.revealAndDispatchProtocolUrl")(function* (156+ url: string | null,157+ ) {158+ // Registered main only. currentMainOrFirst can be the WSL splash.159+ const mainWindow = yield* electronWindow.main;160+ if (Option.isSome(mainWindow)) {161+ yield* electronWindow.reveal(mainWindow.value);162+ if (url !== null) {163+ yield* Effect.sync(() => {164+ loadDesktopProtocolUrl(mainWindow.value, url);165+ });166+ }167+ return;168+ }169+170+ if (url !== null) {171+ queuePendingDesktopProtocolUrl(url);172+ // Opens the real main when the backend is already ready (macOS with173+ // no windows). Otherwise createMain applies the queued URL later.174+ yield* desktopWindow.createMainIfBackendReady.pipe(Effect.ignore);
- apps/desktop/src/app/DesktopClerk.ts line 174OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -138,0 +154,21 @@154+ const scheme = ElectronProtocol.getDesktopScheme(environment.isDevelopment);155+ const revealAndDispatch = Effect.fn("desktop.clerk.revealAndDispatchProtocolUrl")(function* (156+ url: string | null,157+ ) {158+ // Registered main only. currentMainOrFirst can be the WSL splash.159+ const mainWindow = yield* electronWindow.main;160+ if (Option.isSome(mainWindow)) {161+ yield* electronWindow.reveal(mainWindow.value);162+ if (url !== null) {163+ yield* Effect.sync(() => {164+ loadDesktopProtocolUrl(mainWindow.value, url);165+ });166+ }167+ return;168+ }169+170+ if (url !== null) {171+ queuePendingDesktopProtocolUrl(url);172+ // Opens the real main when the backend is already ready (macOS with173+ // no windows). Otherwise createMain applies the queued URL later.174+ yield* desktopWindow.createMainIfBackendReady.pipe(Effect.ignore);apps/desktop/src/window/DesktopWindow.ts line 781Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -752,3 +753,6 @@752753 const window = yield* createWindow();753754 yield* electronWindow.setMain(window);754755 yield* logWindowInfo("main window created");756+ yield* Effect.sync(() => {757+ applyPendingDesktopProtocolUrl(window);758+ });
- apps/desktop/src/app/DesktopClerk.ts line 187Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -138,0 +163,21 @@163+ yield* electronWindow.reveal(mainWindow.value);164+ if (url !== null) {165+ yield* Effect.sync(() => {166+ loadDesktopProtocolUrl(mainWindow.value, url);167+ });168+ }169+ return;170+ }171+172+ if (url !== null) {173+ queuePendingDesktopProtocolUrl(url);174+ // Serialize so concurrent open-url / second-instance events cannot175+ // each pass the empty-main check and create two untracked windows.176+ yield* createMainGate.withPermits(1)(177+ Effect.gen(function* () {178+ const existing = yield* electronWindow.main;179+ if (Option.isSome(existing)) {180+ yield* electronWindow.reveal(existing.value);181+ yield* Effect.sync(() => {182+ loadDesktopProtocolUrl(existing.value, url);183+ });
- apps/desktop/src/window/DesktopWindow.ts line 636Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -611,9 +616,15 @@611616 developmentLoadRetryFiber = undefined;612617 runFork(Fiber.interrupt(retryFiber));613618 };619+ let currentLoadUrl = applicationUrl;614620 const loadApplication = () => {615621 if (window.isDestroyed()) {616622 return;617623 }618- void window.loadURL(applicationUrl).catch(() => undefined);624+ void window.loadURL(currentLoadUrl).catch(() => undefined);619625 };626+ registerDesktopProtocolWindowLoader(window, (url) => {627+ currentLoadUrl = url;628+ clearDevelopmentLoadRetry();629+ loadApplication();630+ });
- @@ -0,0 +10,21 @@10+ if (arg.length === 0 || arg.startsWith("-") || !isDesktopProtocolUrl(arg, scheme)) {11+ continue;12+ }13+ found = arg;14+ }15+ return found;16+}17+18+type DesktopProtocolWindowLoader = (url: string) => void;19+20+// DesktopWindow registers the loader that also updates the window's intended21+// URL, so development did-fail-load retries cannot replace a deep link with22+// the default home URL.23+const desktopProtocolWindowLoaders = new WeakMap<object, DesktopProtocolWindowLoader>();24+25+export function registerDesktopProtocolWindowLoader(26+ window: object,27+ load: DesktopProtocolWindowLoader,28+): void {29+ desktopProtocolWindowLoaders.set(window, load);30+}
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
9f3a274a-0470-459d-acd4-3c93b3bc33a5You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share