|
| 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 | +} |
0 commit comments