Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(frontend): show static-preview notice once per session (#9796)

Kiran Gadhave895576548df1
diff --git a/frontend/src/components/ui/__tests__/use-toast.test.ts b/frontend/src/components/ui/__tests__/use-toast.test.ts
new file mode 100644
index 0000000000..e233d04193
--- /dev/null
+++ b/frontend/src/components/ui/__tests__/use-toast.test.ts
@@ -0,0 +1,75 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import { act, renderHook } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { toast, useToast } from "@/components/ui/use-toast";
+
+describe("toast once", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ const { result } = renderHook(() => useToast());
+ act(() => {
+ result.current.dismiss();
+ vi.runAllTimers();
+ });
+ vi.useRealTimers();
+ });
+
+ it("shows a once-toast a single time per session, even after removal", () => {
+ const { result } = renderHook(() => useToast());
+
+ act(() => {
+ toast({ id: "static-notebook", once: true, title: "Static notebook" });
+ });
+ expect(result.current.toasts).toHaveLength(1);
+
+ act(() => {
+ result.current.dismiss("static-notebook");
+ vi.advanceTimersByTime(10_000);
+ });
+ expect(result.current.toasts).toHaveLength(0);
+
+ act(() => {
+ toast({ id: "static-notebook", once: true, title: "Static notebook" });
+ });
+ expect(result.current.toasts).toHaveLength(0);
+ });
+
+ it("does not suppress normal (non-once) toasts", () => {
+ const { result } = renderHook(() => useToast());
+
+ act(() => {
+ toast({ id: "normal", title: "First" });
+ });
+ act(() => {
+ result.current.dismiss("normal");
+ vi.advanceTimersByTime(10_000);
+ });
+ expect(result.current.toasts).toHaveLength(0);
+
+ act(() => {
+ toast({ id: "normal", title: "First" });
+ });
+ expect(result.current.toasts).toHaveLength(1);
+ });
+
+ it("does not dedupe a once-toast without a stable id", () => {
+ const { result } = renderHook(() => useToast());
+
+ act(() => {
+ toast({ once: true, title: "No id" });
+ });
+ act(() => {
+ result.current.dismiss();
+ vi.advanceTimersByTime(10_000);
+ });
+ expect(result.current.toasts).toHaveLength(0);
+
+ act(() => {
+ toast({ once: true, title: "No id" });
+ });
+ expect(result.current.toasts).toHaveLength(1);
+ });
+});
diff --git a/frontend/src/components/ui/use-toast.ts b/frontend/src/components/ui/use-toast.ts
index a52b6dbe2c..1b33c62da9 100644
--- a/frontend/src/components/ui/use-toast.ts
+++ b/frontend/src/components/ui/use-toast.ts
@@ -58,6 +58,10 @@ interface State {
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
+// Keys of toasts requested with `once: true`, suppressed after first show
+// for the lifetime of the page (i.e. one static-preview session).
+const shownOnceKeys = new Set<string>();
+
const addToRemoveQueue = (toastId: string) => {
if (toastTimeouts.has(toastId)) {
return;
@@ -159,9 +163,18 @@ function dispatch(action: Action) {
type Toast = Omit<ToasterToast, "id">;
-function toast({ id, ...props }: Toast & { id?: string }) {
+function toast({
+ id,
+ once,
+ ...props
+}: Toast & { id?: string; once?: boolean }) {
const toastId = id || genId();
+ // `once` dedupe requires a caller-provided stable `id`. A generated id is
+ // unique per call, so it could never match (and would grow the set without
+ // bound); gate on `id` so `once` without one is simply a no-op.
+ const dedupeOnce = once === true && id !== undefined;
+
const update = (props: Toast) =>
dispatch({
type: "UPDATE_TOAST",
@@ -183,19 +196,26 @@ function toast({ id, ...props }: Toast & { id?: string }) {
},
});
- dispatch({
- type: "ADD_TOAST",
- toast: {
- ...props,
- id: toastId,
- open: true,
- onOpenChange: (open) => {
- if (!open) {
- dismiss();
- }
+ const suppressed = dedupeOnce && shownOnceKeys.has(toastId);
+ if (dedupeOnce) {
+ shownOnceKeys.add(toastId);
+ }
+
+ if (!suppressed) {
+ dispatch({
+ type: "ADD_TOAST",
+ toast: {
+ ...props,
+ id: toastId,
+ open: true,
+ onOpenChange: (open) => {
+ if (!open) {
+ dismiss();
+ }
+ },
},
- },
- });
+ });
+ }
return {
id: toastId,
diff --git a/frontend/src/core/network/__tests__/requests-static.test.ts b/frontend/src/core/network/__tests__/requests-static.test.ts
new file mode 100644
index 0000000000..f7da8cdaeb
--- /dev/null
+++ b/frontend/src/core/network/__tests__/requests-static.test.ts
@@ -0,0 +1,30 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { toast } from "@/components/ui/use-toast";
+import { createStaticRequests } from "../requests-static";
+
+vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));
+
+describe("createStaticRequests static-notebook toast", () => {
+ beforeEach(() => {
+ vi.mocked(toast).mockClear();
+ });
+
+ it("requests a once-toast with a stable id on component value updates", async () => {
+ const requests = createStaticRequests();
+ await requests.sendComponentValues({} as never);
+
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({ id: "static-notebook", once: true }),
+ );
+ });
+
+ it("uses the same once-toast for function requests", async () => {
+ const requests = createStaticRequests();
+ await requests.sendFunctionRequest({} as never);
+
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({ id: "static-notebook", once: true }),
+ );
+ });
+});
diff --git a/frontend/src/core/network/requests-static.ts b/frontend/src/core/network/requests-static.ts
index 285d117b65..1b7aaba017 100644
--- a/frontend/src/core/network/requests-static.ts
+++ b/frontend/src/core/network/requests-static.ts
@@ -3,6 +3,18 @@ import { toast } from "@/components/ui/use-toast";
import { Logger } from "@/utils/Logger";
import type { EditRequests, RunRequests } from "./types";
+const STATIC_NOTEBOOK_TOAST_ID = "static-notebook";
+
+function showStaticNotebookToast() {
+ toast({
+ id: STATIC_NOTEBOOK_TOAST_ID,
+ once: true,
+ title: "Static notebook",
+ description:
+ "This notebook is not connected to a kernel. Any interactive elements will not work.",
+ });
+}
+
export function createStaticRequests(): EditRequests & RunRequests {
const throwNotInEditMode = () => {
throw new Error("Unreachable. Expected to be in run mode");
@@ -10,11 +22,7 @@ export function createStaticRequests(): EditRequests & RunRequests {
return {
sendComponentValues: async () => {
- toast({
- title: "Static notebook",
- description:
- "This notebook is not connected to a kernel. Any interactive elements will not work.",
- });
+ showStaticNotebookToast();
Logger.log("Updating UI elements is not supported in static mode");
return null;
},
@@ -27,11 +35,7 @@ export function createStaticRequests(): EditRequests & RunRequests {
return null;
},
sendFunctionRequest: async () => {
- toast({
- title: "Static notebook",
- description:
- "This notebook is not connected to a kernel. Any interactive elements will not work.",
- });
+ showStaticNotebookToast();
Logger.log("Function requests are not supported in static mode");
return null;
},