Skip to content

Commit c7bb5d2

Browse files
fix(web): app-lifetime toasts reach a provider that outlives root branches
Toast providers move above every root branch, and the toast managers hold events until a provider subscribes, so toasts raised before the first render (the post-restart update notice) or during a branch switch are shown. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a317017 commit c7bb5d2

3 files changed

Lines changed: 105 additions & 50 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
import { page } from "vite-plus/test/browser";
3+
4+
import { render } from "../../../test/render";
5+
import { ToastProvider, toastManager } from "./toast";
6+
7+
describe("toastManager", () => {
8+
it("shows a toast added before any provider mounted", async () => {
9+
toastManager.add({ type: "info", title: "Raised before the first render" });
10+
11+
await using _screen = await render(<ToastProvider>{null}</ToastProvider>);
12+
13+
await expect.element(page.getByText("Raised before the first render")).toBeVisible();
14+
});
15+
});

‎apps/web/src/components/ui/toast.tsx‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,40 @@ export type ThreadToastData = {
6262
| "secondary";
6363
};
6464

65-
const toastManager = Toast.createToastManager<ThreadToastData>();
66-
const anchoredToastManager = Toast.createToastManager<ThreadToastData>();
65+
type BaseToastManager = ReturnType<typeof Toast.createToastManager<ThreadToastData>>;
66+
type ToastManagerListener = Parameters<BaseToastManager[" subscribe"]>[0];
67+
type ToastManagerEvent = Parameters<ToastManagerListener>[0];
68+
69+
/**
70+
* base-ui's manager drops events while no provider is subscribed. App services raise toasts
71+
* from before the first render (and while the root error view replaces the providers), so hold
72+
* those until a provider listens.
73+
*/
74+
function createBufferedToastManager(): BaseToastManager {
75+
const manager = Toast.createToastManager<ThreadToastData>();
76+
const listeners = new Set<ToastManagerListener>();
77+
const pending: ToastManagerEvent[] = [];
78+
manager[" subscribe"]((event) => {
79+
if (listeners.size === 0) {
80+
pending.push(event);
81+
return;
82+
}
83+
for (const listener of listeners) listener(event);
84+
});
85+
return {
86+
...manager,
87+
" subscribe"(listener) {
88+
listeners.add(listener);
89+
for (const event of pending.splice(0)) listener(event);
90+
return () => {
91+
listeners.delete(listener);
92+
};
93+
},
94+
};
95+
}
96+
97+
const toastManager = createBufferedToastManager();
98+
const anchoredToastManager = createBufferedToastManager();
6799
type ToastId = ReturnType<typeof toastManager.add>;
68100
const threadToastVisibleTimeoutRemainingMs = new Map<ToastId, number>();
69101

‎apps/web/src/routes/__root.tsx‎

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ function RootRouteNotFoundView() {
144144
}
145145

146146
function RootRouteView() {
147+
// Above every branch: app services raise toasts for the whole page, so the surface they write to
148+
// must not unmount when the root switches branches (for example /welcome to the app shell).
149+
return (
150+
<ToastProvider>
151+
<AnchoredToastProvider>
152+
<RootRouteBranch />
153+
</AnchoredToastProvider>
154+
</ToastProvider>
155+
);
156+
}
157+
158+
function RootRouteBranch() {
147159
useEffect(() => installDesktopPasteAsText(window.desktopBridge, window), []);
148160
const pathname = useLocation({ select: (location) => location.pathname });
149161
const { authGateState } = Route.useRouteContext();
@@ -178,19 +190,17 @@ function RootRouteView() {
178190

179191
if (pathname === "/welcome") {
180192
return (
181-
<ToastProvider>
182-
<AnchoredToastProvider>
183-
<BrowserChromeThemeSync key={pathname} />
184-
<EnvironmentThemeSync />
185-
<CustomSnoozeDialogHost />
186-
<GuestShareDialogHost />
187-
<CommandPalette>
188-
<AppSidebarLayout>
189-
<Outlet />
190-
</AppSidebarLayout>
191-
</CommandPalette>
192-
</AnchoredToastProvider>
193-
</ToastProvider>
193+
<>
194+
<BrowserChromeThemeSync key={pathname} />
195+
<EnvironmentThemeSync />
196+
<CustomSnoozeDialogHost />
197+
<GuestShareDialogHost />
198+
<CommandPalette>
199+
<AppSidebarLayout>
200+
<Outlet />
201+
</AppSidebarLayout>
202+
</CommandPalette>
203+
</>
194204
);
195205
}
196206

@@ -212,41 +222,39 @@ function RootRouteView() {
212222
);
213223

214224
return (
215-
<ToastProvider>
216-
<AnchoredToastProvider>
217-
<BrowserChromeThemeSync key={pathname} />
218-
<EnvironmentThemeSync />
219-
<ChatGptWelcomeCoordinator />
220-
<FirstRunGate
221-
enabled={primaryEnvironmentAuthenticated}
222-
hostedStatic={authGateState.status === "hosted-static"}
223-
>
224-
{primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}
225-
{primaryEnvironmentAuthenticated ? <DesktopAppActivationCoordinator /> : null}
226-
{primaryEnvironmentAuthenticated ? <DesktopWebLinkCoordinator /> : null}
227-
{isElectron ? <RunningThreadKeepAlive /> : null}
228-
<RelayClientInstallDialog />
229-
<ConnectOnboardingDialog />
230-
<SshPasswordPromptDialog />
231-
<SnapShotCoordinator />
232-
<ReopenClosedViewShortcut />
233-
<ConfirmDialogHost />
234-
<CustomSnoozeDialogHost />
235-
<GuestShareDialogHost />
236-
<SlowRpcRequestToastCoordinator />
237-
<ProjectCloneToastCoordinator />
238-
<AddProjectToMachineDialogHost />
239-
<PullRequestGuideRunCoordinator />
240-
<HostedStaticEnvironmentBootstrap />
241-
{primaryEnvironmentAuthenticated ? (
242-
<EventRouter skipInitialBootstrapNavigation={returningFromWelcome} />
243-
) : null}
244-
{appShell}
245-
{/* Keep the theme editor above the router so drafts survive navigation. */}
246-
<ThemeEditorHost />
247-
</FirstRunGate>
248-
</AnchoredToastProvider>
249-
</ToastProvider>
225+
<>
226+
<BrowserChromeThemeSync key={pathname} />
227+
<EnvironmentThemeSync />
228+
<ChatGptWelcomeCoordinator />
229+
<FirstRunGate
230+
enabled={primaryEnvironmentAuthenticated}
231+
hostedStatic={authGateState.status === "hosted-static"}
232+
>
233+
{primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}
234+
{primaryEnvironmentAuthenticated ? <DesktopAppActivationCoordinator /> : null}
235+
{primaryEnvironmentAuthenticated ? <DesktopWebLinkCoordinator /> : null}
236+
{isElectron ? <RunningThreadKeepAlive /> : null}
237+
<RelayClientInstallDialog />
238+
<ConnectOnboardingDialog />
239+
<SshPasswordPromptDialog />
240+
<SnapShotCoordinator />
241+
<ReopenClosedViewShortcut />
242+
<ConfirmDialogHost />
243+
<CustomSnoozeDialogHost />
244+
<GuestShareDialogHost />
245+
<SlowRpcRequestToastCoordinator />
246+
<ProjectCloneToastCoordinator />
247+
<AddProjectToMachineDialogHost />
248+
<PullRequestGuideRunCoordinator />
249+
<HostedStaticEnvironmentBootstrap />
250+
{primaryEnvironmentAuthenticated ? (
251+
<EventRouter skipInitialBootstrapNavigation={returningFromWelcome} />
252+
) : null}
253+
{appShell}
254+
{/* Keep the theme editor above the router so drafts survive navigation. */}
255+
<ThemeEditorHost />
256+
</FirstRunGate>
257+
</>
250258
);
251259
}
252260

0 commit comments

Comments
 (0)