Skip to content

Commit f35e44b

Browse files
committed
fix(desktop): bring the app forward when a link is rejected
1 parent 1958e98 commit f35e44b

3 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎packages/desktop/src/main/lifecycle/deep-link.test.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2-
import { formatDeepLink, parseDeepLink, pickSessionWindow, tabsHoldSession } from "./deep-link"
2+
import { formatDeepLink, linkBatchWindow, parseDeepLink, pickLinkWindow, tabsHoldSession } from "./deep-link"
33

44
const session = "ses_efcd8972fffeT5zZbj9YtIl3LU"
55

@@ -108,11 +108,11 @@ describe("tabsHoldSession", () => {
108108
})
109109
})
110110

111-
describe("pickSessionWindow", () => {
111+
describe("pickLinkWindow", () => {
112112
const pick = (input: { open: string[]; persisted?: string[]; focused?: string; holder?: string }) => {
113113
const calls: string[] = []
114114

115-
const win = pickSessionWindow({
115+
const win = pickLinkWindow({
116116
open: input.open,
117117
restore: () => {
118118
calls.push("restore")
@@ -146,3 +146,21 @@ describe("pickSessionWindow", () => {
146146
expect(pick({ open: [], persisted: [] })).toEqual({ win: "new", calls: ["restore", "create"] })
147147
})
148148
})
149+
150+
describe("linkBatchWindow", () => {
151+
test("brings forward the window the last accepted link opened in", () => {
152+
expect(linkBatchWindow(["a", undefined, "b", null], () => "fallback")).toBe("b")
153+
})
154+
155+
test("still brings the app forward when every link was rejected", () => {
156+
// A rejected link opens no window; with every window closed the fallback restores them.
157+
const fallback = () => pickLinkWindow({ open: [], restore: () => ["restored"], create: () => "new", focused: null })
158+
159+
expect(linkBatchWindow([undefined, undefined], fallback)).toBe("restored")
160+
expect(
161+
linkBatchWindow([undefined], () =>
162+
pickLinkWindow({ open: ["a", "b"], restore: () => [], create: () => "new", focused: "b" }),
163+
),
164+
).toBe("b")
165+
})
166+
})

‎packages/desktop/src/main/lifecycle/deep-link.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,29 @@ export function tabsHoldSession(value: string | null, sessionID: string) {
4747
}
4848

4949
/**
50-
* The window a session link opens in: the one already holding the session, then the window the user
51-
* was last in, then any. With every window closed (macOS) the persisted windows are restored first, so
52-
* the link can land in the one that held the session and restoration isn't lost to a blank window.
50+
* The window a link opens in: the one already holding its session, then the window the user was last
51+
* in, then any. With every window closed (macOS) the persisted windows are restored first, so the link
52+
* can land in the one that held the session and restoration isn't lost to a blank window.
5353
*/
54-
export function pickSessionWindow<W>(input: {
54+
export function pickLinkWindow<W>(input: {
5555
open: W[]
5656
restore: () => W[]
5757
create: () => W
5858
focused: W | null
59-
holds: (win: W) => boolean
59+
holds?: (win: W) => boolean
6060
}) {
6161
const open = input.open.length ? input.open : input.restore()
6262
const focused = input.focused && open.includes(input.focused) ? input.focused : undefined
6363

64-
return open.find(input.holds) ?? focused ?? open[0] ?? input.create()
64+
return (input.holds && open.find(input.holds)) || focused || open[0] || input.create()
65+
}
66+
67+
/**
68+
* The window a batch of links brings forward: the last one a link opened in. When every link was
69+
* rejected (an old v1 link, a typo) the app still comes forward, as it did before links were validated.
70+
*/
71+
export function linkBatchWindow<W>(opened: (W | null | undefined)[], fallback: () => W) {
72+
return opened.findLast((win) => !!win) ?? fallback()
6573
}
6674

6775
function sessionLink(url: URL): DeepLink | undefined {

‎packages/desktop/src/main/lifecycle/index.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import { marks } from "./marks"
2525
import { initializeFirstLaunchOnboarding } from "./onboarding"
2626
import { Shutdown } from "./shutdown"
27-
import { formatDeepLink, parseDeepLink, pickSessionWindow, tabsHoldSession } from "./deep-link"
27+
import { formatDeepLink, linkBatchWindow, parseDeepLink, pickLinkWindow, tabsHoldSession } from "./deep-link"
2828
import { linkArguments, receiveLinks } from "./link-intake"
2929
import { createLinkQueue } from "./link-queue"
3030

@@ -60,17 +60,19 @@ const runtime = Layer.effect(
6060
}
6161

6262
// Restored windows exist before `activate` runs, so it finds windows and doesn't restore them again.
63-
const sessionWindow = (sessionID: string) =>
64-
pickSessionWindow({
63+
const linkWindow = (sessionID?: string) =>
64+
pickLinkWindow({
6565
open: getMainWindows(),
6666
restore: restoreWindows,
6767
create: createWindow,
6868
focused: getLastFocusedWindow(),
69-
holds: (win) => {
70-
const id = getWindowID(win)
69+
holds: sessionID
70+
? (win) => {
71+
const id = getWindowID(win)
7172

72-
return !!id && tabsHoldSession(storage.state.get(windowDataFile(id), "tabs"), sessionID)
73-
},
73+
return !!id && tabsHoldSession(storage.state.get(windowDataFile(id), "tabs"), sessionID)
74+
}
75+
: undefined,
7476
})
7577

7678
const openLink = (url: string) => {
@@ -83,7 +85,7 @@ const runtime = Layer.effect(
8385
}
8486

8587
if (link.type === "console") return getWindowByID(link.windowID) ?? getLastFocusedWindow()
86-
const win = sessionWindow(link.sessionID)
88+
const win = linkWindow(link.sessionID)
8789
const canonical = formatDeepLink(link)
8890
runFork(
8991
Effect.logInfo("deep link opened", { type: link.type, sessionID: link.sessionID, window: getWindowID(win) }),
@@ -95,7 +97,7 @@ const runtime = Layer.effect(
9597
return win
9698
}
9799

98-
const openLinks = (urls: string[]) => focusWindow(urls.map(openLink).findLast((win) => !!win) ?? null)
100+
const openLinks = (urls: string[]) => focusWindow(linkBatchWindow(urls.map(openLink), () => linkWindow()))
99101

100102
const wireLinks = (win: BrowserWindow) => {
101103
const contents = win.webContents.id

0 commit comments

Comments
 (0)