From a6da22f86a01ca30df8da2616062813f553f09c5 Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:41:06 +0000 Subject: [PATCH] fix(app): keep a toast that replaces one timing out (#10183) When a toast replaced another just as the other's timer expired, the old timer fired before the viewport rescheduled it and dismissed the new toast. The new toast showed for one frame and faded out. The timer now dismisses only the toast it was set for. This made "Reloaded npm-review" vanish in the plugin-management e2e journey when the reload finished about 2200 ms after the install toast. Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../components/toast-host.browser.test.tsx | 69 +++++++++++++++++++ packages/app/src/components/toast-host.tsx | 39 +++++++---- packages/app/src/panels/agent-panel.tsx | 9 ++- packages/app/src/timeline/sync-status.tsx | 2 +- 4 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 packages/app/src/components/toast-host.browser.test.tsx diff --git a/packages/app/src/components/toast-host.browser.test.tsx b/packages/app/src/components/toast-host.browser.test.tsx new file mode 100644 index 000000000..842478354 --- /dev/null +++ b/packages/app/src/components/toast-host.browser.test.tsx @@ -0,0 +1,69 @@ +import React, { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { SafeAreaProvider } from "react-native-safe-area-context"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { ToastViewport, useToastHost, type ToastApi } from "@/components/toast-host"; + +Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); + +let root: Root | null = null; +let container: HTMLDivElement | null = null; + +afterEach(() => { + act(() => root?.unmount()); + container?.remove(); + root = null; + container = null; + vi.useRealTimers(); +}); + +function mountToastHost(): ToastApi { + let api: ToastApi | null = null; + function Host() { + const host = useToastHost(); + api = host.api; + return ; + } + container = document.createElement("div"); + document.body.appendChild(container); + root = createRoot(container); + act(() => + root?.render( + + + , + ), + ); + if (!api) throw new Error("The toast host did not render"); + return api; +} + +function shownMessage(): string | null { + return document.querySelector('[data-testid="app-toast-message"]')?.textContent ?? null; +} + +describe("ToastViewport", () => { + it("keeps a new toast when the toast it replaced times out before the next render", () => { + vi.useFakeTimers(); + const toast = mountToastHost(); + act(() => { + toast.show("Installed review"); + }); + act(() => { + vi.advanceTimersByTime(2199); + }); + + // The new toast is in state, but React has not rendered it, so the old toast's timer is + // still scheduled when it expires. + act(() => { + toast.show("Reloaded review"); + vi.advanceTimersByTime(1); + }); + + expect(shownMessage()).toBe("Reloaded review"); + act(() => { + vi.advanceTimersByTime(2199); + }); + expect(shownMessage()).toBe("Reloaded review"); + }); +}); diff --git a/packages/app/src/components/toast-host.tsx b/packages/app/src/components/toast-host.tsx index 6c907e396..a5ec252e5 100644 --- a/packages/app/src/components/toast-host.tsx +++ b/packages/app/src/components/toast-host.tsx @@ -53,6 +53,12 @@ export interface ToastState { /** Dismisses the toast it was returned for; a no-op once another toast replaced it. */ export type DismissToast = () => void; +/** + * Dismisses the toast with this ID, and does nothing once another toast replaced it. Without an + * ID, it dismisses whichever toast is shown. + */ +export type DismissToastById = (id?: number) => void; + export interface ToastApi { show: (content: ReactNode, options?: ToastShowOptions) => DismissToast; copied: (label?: string) => void; @@ -81,7 +87,7 @@ const errorIcon = (theme: Theme) => ({ color: theme.colors.destructive }); export function useToastHost(): { api: ToastApi; toast: ToastState | null; - dismiss: () => void; + dismiss: DismissToastById; } { const { t } = useTranslation(); const [toast, setToastState] = useState(null); @@ -96,6 +102,13 @@ export function useToastHost(): { previous?.onDismiss?.(); }, []); + const dismiss = useCallback( + (id) => { + if (id === undefined || toastRef.current?.id === id) setToast(null); + }, + [setToast], + ); + const show = useCallback( (content: ReactNode, options?: ToastShowOptions): DismissToast => { const nativeMessage = typeof content === "string" ? content.trim() : null; @@ -127,11 +140,9 @@ export function useToastHost(): { testID: options?.testID, onDismiss: options?.onDismiss, }); - return () => { - if (toastRef.current?.id === id) setToast(null); - }; + return () => dismiss(id); }, - [setToast], + [dismiss, setToast], ); const api = useMemo( @@ -147,10 +158,6 @@ export function useToastHost(): { [show, t], ); - const dismiss = useCallback(() => { - setToast(null); - }, [setToast]); - return { api, toast, dismiss }; } @@ -161,7 +168,8 @@ export function ToastViewport({ onBottomCoverageChange, }: { toast: ToastState | null; - onDismiss: () => void; + /** Called with the ID of the toast that timed out. */ + onDismiss: (id: number) => void; placement?: ToastViewportPlacement; /** Sidebar placement: reports the distance from the region's bottom edge to the toast's top. */ onBottomCoverageChange?: (coverage: number) => void; @@ -180,7 +188,7 @@ export function ToastViewport({ }, []); const scheduleDismiss = useCallback( - (durationMs: number | null) => { + (id: number, durationMs: number | null) => { clearTimer(); if (durationMs === null) { remainingDurationRef.current = 0; @@ -190,8 +198,10 @@ export function ToastViewport({ const nextDurationMs = Math.max(0, durationMs); remainingDurationRef.current = nextDurationMs; dismissDeadlineRef.current = Date.now() + nextDurationMs; + // A replacing toast is in the host before this viewport renders it and reschedules, so the + // timer names its own toast rather than dismissing whichever one is current. timeoutRef.current = setTimeout(() => { - onDismiss(); + onDismiss(id); }, nextDurationMs); }, [onDismiss, clearTimer], @@ -209,13 +219,14 @@ export function ToastViewport({ if (!toast || toast.durationMs === null) { return; } - scheduleDismiss(remainingDurationRef.current || toast.durationMs); + scheduleDismiss(toast.id, remainingDurationRef.current || toast.durationMs); }, [scheduleDismiss, toast]); const toastId = toast?.id; const durationMs = toast?.durationMs ?? null; useEffect(() => { - scheduleDismiss(durationMs); + if (toastId === undefined) return; + scheduleDismiss(toastId, durationMs); return clearTimer; }, [clearTimer, durationMs, scheduleDismiss, toastId]); diff --git a/packages/app/src/panels/agent-panel.tsx b/packages/app/src/panels/agent-panel.tsx index 46096dd6b..d41452935 100644 --- a/packages/app/src/panels/agent-panel.tsx +++ b/packages/app/src/panels/agent-panel.tsx @@ -44,7 +44,12 @@ import { RewindComposerRestoreProvider, } from "@/components/rewind"; import { useAgentDescriptor } from "@/agent-presentation"; -import { useToastHost, type ToastApi, type ToastState } from "@/components/toast-host"; +import { + useToastHost, + type DismissToastById, + type ToastApi, + type ToastState, +} from "@/components/toast-host"; import type { WorkspaceComposerAttachment } from "@/attachments/types"; import { useWorkspaceAttachmentScopeKey } from "@/attachments/workspace-attachments-store"; import { COMPACT_CONTENT_WIDTH, useIsCompactFormFactor } from "@/constants/layout"; @@ -1077,7 +1082,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({ hasCachedHistory: boolean; toastApi: ToastApi; toast: ToastState | null; - dismiss: () => void; + dismiss: DismissToastById; streamViewRef: React.RefObject; handleComposerHeightChange: (height: number) => void; handleMessageSent: () => void; diff --git a/packages/app/src/timeline/sync-status.tsx b/packages/app/src/timeline/sync-status.tsx index c4d74e400..6bcd06b9e 100644 --- a/packages/app/src/timeline/sync-status.tsx +++ b/packages/app/src/timeline/sync-status.tsx @@ -17,7 +17,7 @@ export function TimelineSyncStatus({ }: { sync: AgentScreenReadySyncState | null; toast: ToastState | null; - onDismiss: () => void; + onDismiss: (id: number) => void; }) { const { t } = useTranslation(); let state: "reconnecting" | "updating" | null = null;