Skip to content

Commit 2b2c605

Browse files
authored
feat(app): show paced one-time tip toasts (#63664)
1 parent c4601e3 commit 2b2c605

7 files changed

Lines changed: 261 additions & 2 deletions

File tree

‎packages/app/src/runtime/i18n/en.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ export const dict = {
613613
"home.sessions.group.yesterday": "Yesterday",
614614
"home.sessions.group.older": "Older",
615615
"home.providerTip": "Connect to 75+ providers to use other models, including Claude, GPT, Gemini, etc",
616+
"tips.browse.title": "Control your browser",
617+
"tips.browse.description": "Let OpenCode work in your browser with the sites you’re already signed in to.",
618+
"tips.browse.install": "Install",
619+
"tips.search.title": "Find a past conversation",
620+
"tips.search.description": "Search your sessions to pick up where you left off.",
616621
"home.workspaceTip": "Start next session in a new workspace to keep changes isolated",
617622

618623
"session.tab.session": "Session",

‎packages/app/src/shell/notifications/coordinator.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onCleanup } from "solid-js"
1+
import { onCleanup, untrack } from "solid-js"
22
import { Predicate } from "effect"
33
import { INITIAL_FOCUS_LOCK_STATE, transitionFocusLock, type FocusLockEvent } from "./focus-lock-machine"
44

@@ -75,6 +75,44 @@ export function createNotificationCoordinator() {
7575
}
7676

7777
return {
78+
tip(show: () => () => void) {
79+
const lifetime = new AbortController()
80+
81+
const closed = new Promise<void>((resolve) =>
82+
lifetime.signal.addEventListener("abort", () => resolve(), { once: true }),
83+
)
84+
85+
let hide: VoidFunction | undefined
86+
87+
const open = () => {
88+
if (hide || lifetime.signal.aborted || !document.hasFocus() || document.hidden) return
89+
hide = untrack(show)
90+
91+
return closed
92+
}
93+
94+
const focus = () => {
95+
if (!locks) return void open()
96+
void locks
97+
.request("opencode:tip", { ifAvailable: true }, (lock) => (lock ? open() : undefined))
98+
.catch(() => undefined)
99+
}
100+
101+
// Plain HTTP has no Web Locks. Only its focused window keeps a toast mounted.
102+
const blur = () => {
103+
if (locks) return
104+
hide?.()
105+
hide = undefined
106+
}
107+
108+
window.addEventListener("focus", focus, { signal: lifetime.signal })
109+
window.addEventListener("blur", blur, { signal: lifetime.signal })
110+
onCleanup(() => {
111+
lifetime.abort()
112+
hide?.()
113+
})
114+
focus()
115+
},
78116
sound<T>(eventID: string, run: () => Promise<T> | void) {
79117
return once("sound", eventID, run)
80118
},

‎packages/app/src/shell/shell.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { prefersReducedMotion } from "@opencode/ui/motion"
77
import { Titlebar } from "@/shell/titlebar/titlebar"
88
import { usePlatform } from "@/runtime/platform/platform"
99
import { ToastRegion } from "@/shell/notifications/toast"
10+
import { createTipToasts } from "@/tips/model"
1011
import { UploadToastHost } from "@/composer/attachments/uploads"
1112
import { useSettingsSurface } from "@/settings/surface"
1213
import { useSettings } from "@/settings/model"
@@ -43,6 +44,8 @@ export default function Layout(props: ParentProps) {
4344
const tabs = useTabs()
4445
const mobile = createMediaQuery("(max-width: 767px)")
4546

47+
createTipToasts()
48+
4649
const [state, setState] = createStore<{
4750
tabsWidth: number
4851
tabsMount: HTMLElement | undefined

‎packages/app/src/tips/model.ts‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { showToast, toaster } from "@opencode/ui/toast"
2+
import { Schema } from "effect"
3+
import { createEffect, createMemo, createSignal, onCleanup } from "solid-js"
4+
import { useLanguage } from "@/runtime/i18n/language"
5+
import { Persistence } from "@/runtime/persistence/schema"
6+
import { Persist, persisted } from "@/runtime/persistence/storage"
7+
import { usePlatform } from "@/runtime/platform/platform"
8+
import { ServerConnection, useServers } from "@/runtime/server/registry"
9+
import { useGlobal } from "@/runtime/server/runtime"
10+
import { useCommand } from "@/shell/commands/command"
11+
import { selectTip, TIP_INTERVAL_MS, type Tip } from "./queue"
12+
13+
const STARTUP_DELAY = 2 * 60 * 1000
14+
15+
const History = Persistence.struct({
16+
current: Schema.String,
17+
shownAt: Schema.Finite,
18+
dismissed: Persistence.array(Schema.String),
19+
dismissedAt: Schema.Finite,
20+
})
21+
22+
export function createTipToasts() {
23+
const global = useGlobal()
24+
const servers = useServers()
25+
const language = useLanguage()
26+
const platform = usePlatform()
27+
const command = useCommand()
28+
29+
const server = createMemo(() => {
30+
const conn = servers.visible.find(servers.current) ?? servers.visible[0]
31+
32+
return conn && global.serverCtx(ServerConnection.key(conn))
33+
})
34+
35+
const location = () => server()?.data.location
36+
const [awake, setAwake] = createSignal(false)
37+
const startup = setTimeout(() => setAwake(true), STARTUP_DELAY)
38+
onCleanup(() => clearTimeout(startup))
39+
createEffect(() => {
40+
const ctx = server()
41+
42+
if (!awake() || ctx?.sdk.connection.status() !== "connected") return
43+
const location = ctx.data.location
44+
void location
45+
.syncInfo()
46+
.then(() => location.mcp.server.sync())
47+
.catch(() => undefined)
48+
})
49+
50+
const tips: Tip[] = [
51+
{
52+
id: "browse",
53+
title: "tips.browse.title",
54+
description: "tips.browse.description",
55+
when: () =>
56+
!location()
57+
?.mcp.server.list()
58+
?.some((item) => item.name === "browse"),
59+
action: {
60+
label: "tips.browse.install",
61+
run: () => platform.openExternal("https://chromewebstore.google.com/detail/mfnicocicmmlkpjnaffgihfjhdgjkdjg"),
62+
},
63+
},
64+
{
65+
id: "session-search",
66+
title: "tips.search.title",
67+
description: "tips.search.description",
68+
action: { label: "command.session.search", run: () => command.trigger("sessions.search") },
69+
},
70+
]
71+
72+
const [history, setHistory, , loaded] = persisted({ ...Persist.global("tips.queue"), sync: true }, History, {
73+
current: "",
74+
shownAt: 0,
75+
dismissed: [],
76+
dismissedAt: 0,
77+
})
78+
79+
const [tick, setTick] = createSignal(0)
80+
81+
const selected = createMemo(() => {
82+
if (!awake() || !loaded() || !location()?.mcp.server.list()) return
83+
tick()
84+
const now = Date.now()
85+
86+
const times = [
87+
Math.max(history.shownAt, history.dismissedAt) + TIP_INTERVAL_MS,
88+
...tips.flatMap((tip) => [tip.start ?? 0, tip.end ?? 0]),
89+
]
90+
91+
const next = Math.min(...times.filter((time) => time > now))
92+
93+
if (Number.isFinite(next)) {
94+
const timer = setTimeout(() => setTick((n) => n + 1), Math.min(next - now, 2 ** 31 - 1))
95+
onCleanup(() => clearTimeout(timer))
96+
}
97+
98+
return selectTip(tips, history, now)
99+
})
100+
101+
createEffect(() => {
102+
const tip = selected()
103+
104+
if (!tip) return
105+
106+
const handled = () =>
107+
setHistory({ current: "", dismissed: [...history.dismissed, tip.id], dismissedAt: Date.now() })
108+
109+
global.notifications.tip(() => {
110+
if (history.current !== tip.id) setHistory({ current: tip.id, shownAt: Date.now() })
111+
const action = tip.action
112+
113+
const id = showToast({
114+
persistent: true,
115+
title: language.t(tip.title),
116+
description: language.t(tip.description),
117+
onDismiss: handled,
118+
actions: action && [
119+
{
120+
label: language.t(action.label),
121+
onClick: () => {
122+
handled()
123+
action.run()
124+
},
125+
},
126+
],
127+
})
128+
129+
return () => toaster.dismiss(id)
130+
})
131+
})
132+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect, test } from "bun:test"
2+
import { selectTip, type Tip } from "./queue"
3+
4+
const fresh = { current: "", shownAt: 0, dismissed: [], dismissedAt: 0 }
5+
6+
const first: Tip = { id: "first", title: "tips.browse.title", description: "tips.browse.description" }
7+
8+
const second: Tip = { ...first, id: "second" }
9+
10+
const tips = [first, second]
11+
12+
const hours = 60 * 60 * 1000
13+
14+
test("first eligible tip wins until it is handled or stops being eligible", () => {
15+
expect(selectTip(tips, fresh, 1)).toBe(first)
16+
expect(selectTip([{ ...first, when: () => false }, second], fresh, 1)).toBe(second)
17+
expect(selectTip(tips, { ...fresh, current: second.id, shownAt: 1 }, 24 * hours)).toBe(second)
18+
})
19+
20+
test("handling starts a full day's wait and permanently skips that id", () => {
21+
const handled = { ...fresh, dismissedAt: hours, dismissed: [first.id] }
22+
expect(selectTip(tips, handled, 25 * hours - 1)).toBeUndefined()
23+
expect(selectTip(tips, handled, 25 * hours)).toBe(second)
24+
expect(selectTip([first], handled, 365 * 24 * hours)).toBeUndefined()
25+
})
26+
27+
test("promo starts inclusively, expires even while shown, and cannot immediately yield to another tip", () => {
28+
const promo = { ...first, start: hours, end: hours + 1 }
29+
expect(selectTip([promo], fresh, hours - 1)).toBeUndefined()
30+
expect(selectTip([promo], fresh, hours)).toBe(promo)
31+
const shown = { ...fresh, current: promo.id, shownAt: hours }
32+
expect(selectTip([promo, second], shown, hours + 1)).toBeUndefined()
33+
expect(selectTip([promo, second], shown, 25 * hours)).toBe(second)
34+
})

‎packages/app/src/tips/queue.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { useLanguage } from "@/runtime/i18n/language"
2+
3+
export const TIP_INTERVAL_MS = 24 * 60 * 60 * 1000
4+
5+
type Text = Parameters<ReturnType<typeof useLanguage>["t"]>[0]
6+
7+
export type Tip = {
8+
id: string
9+
title: Text
10+
description: Text
11+
action?: { label: Text; run: () => void }
12+
when?: () => boolean
13+
start?: number
14+
end?: number
15+
}
16+
17+
export type TipHistory = {
18+
current: string
19+
shownAt: number
20+
dismissed: readonly string[]
21+
dismissedAt: number
22+
}
23+
24+
/** Array order is priority. Dates are epoch milliseconds; the end is exclusive. */
25+
export function selectTip(tips: readonly Tip[], history: TipHistory, now: number) {
26+
const eligible = (tip: Tip) =>
27+
!history.dismissed.includes(tip.id) &&
28+
(tip.start === undefined || tip.start <= now) &&
29+
(tip.end === undefined || now < tip.end) &&
30+
(tip.when?.() ?? true)
31+
32+
const current = tips.find((tip) => tip.id === history.current && eligible(tip))
33+
34+
if (current) return current
35+
36+
const last = Math.max(history.shownAt, history.dismissedAt)
37+
38+
if (last && now < last + TIP_INTERVAL_MS) return
39+
40+
return tips.find(eligible)
41+
}

‎packages/ui/src/feedback/toast/toast.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ export interface ToastAction {
183183
}
184184

185185
export interface ToastOptions {
186+
/** Called when the user closes or swipes away the toast. */
187+
onDismiss?: () => void
186188
title?: string
187189
description?: string
188190
/** An element, or a render function the toast calls under its own owner, for icons created from imperative code. */
@@ -254,7 +256,11 @@ function publishToast(id: number, options: ToastOptions) {
254256
className: `toast-v2 toast-v2--${options.variant ?? "default"}`,
255257
testId: `toast-v2-${id}`,
256258
unstyled: true,
257-
onDismiss: release,
259+
onDismiss: () => {
260+
const current = selectToastOptions(registry, id)
261+
release()
262+
current?.onDismiss?.()
263+
},
258264
onAutoClose: release,
259265
})
260266
}

0 commit comments

Comments
 (0)