Open History to see the last commit
1import { useEffect } from "react";
2import { toast } from "sonner";
3
4import { Toaster } from "@/components/ui/sonner";
5import { exposeFakeApiOnWindow } from "@/api/dev-console";
6import { trackSectionProgress } from "@/guide/section-progress";
7import { useMediaQuery } from "@/hooks/use-media-query";
8import { subscribeToAppActions } from "@/lib/app-actions";
9import type { AppAction, AppActionEvent } from "@/lib/app-actions";
10import { SPLIT_LAYOUT_MIN_WIDTH } from "@/lib/layout";
11
12const FAILED: { [T in AppAction["type"]]?: (action: Extract<AppAction, { type: T }>) => string } = {
13 "note.created": () => "Couldn't create the note",
14 "note.edited": () => "Couldn't save the note",
15 "note.pinned": (action) => (action.pinned ? "Couldn't pin the note" : "Couldn't unpin the note"),
16 "note.moved": () => "Couldn't move the note",
17 "note.deleted": () => "Couldn't delete the note",
18 "note.restored": () => "Couldn't recover the note",
19 "folder.renamed": () => "Couldn't rename the folder",
20 "account.updated": () => "Couldn't save your settings",
21};
22
23export interface RollbackNotice {
24 /** Toasts with the same id replace each other. */
25 id: string;
26 title: string;
27 description: string;
28}
29
30/** What to tell the reader when the server rejected a change (TanStack DB has already rolled it back). */
31export function rollbackNotice(event: AppActionEvent): RollbackNotice | null {
32 if (event.status !== "rolled-back") return null;
33 const title =
34 (FAILED[event.type] as ((action: AppAction) => string) | undefined)?.(event) ??
35 "Couldn't save the change";
36 const description = `${event.error?.message ?? "The server rejected it."} The change was undone.`;
37 // One failed save also undoes the edits queued behind it: one toast for all of them.
38 return { id: `${title}|${description}`, title, description };
39}
40
41function showRollbackToast({ id, title, description }: RollbackNotice) {
42 toast.error(title, { id, description });
43}
44
45/**
46 * Listens to the app's actions for as long as the page is open: rollback
47 * toasts, and ticking off guide sections. Changes can settle after the
48 * reader has left the Notes app (for a deep dive, say), so this must not
49 * live inside it. Returns a stop function.
50 */
51export function startAppNotifications(
52 notify: (notice: RollbackNotice) => void = showRollbackToast,
53) {
54 const stopProgress = trackSectionProgress();
55 const stopToasts = subscribeToAppActions((event) => {
56 const notice = rollbackNotice(event);
57 if (notice) notify(notice);
58 });
59 return () => {
60 stopProgress();
61 stopToasts();
62 };
63}
64
65/** Clears the iPhone layout's bottom bar (`--ios-bottom-bar`: 4rem plus the home indicator), with a gap. */
66const SMALL_SCREEN_TOAST_OFFSET = { bottom: "calc(4.5rem + env(safe-area-inset-bottom))" };
67
68/** Mounted once, in the root route, so every page (workspace and deep dives) shares it. */
69export function AppNotifications() {
70 const isSplit = useMediaQuery(`(min-width: ${SPLIT_LAYOUT_MIN_WIDTH}px)`);
71 useEffect(() => startAppNotifications(), []);
72 useEffect(() => exposeFakeApiOnWindow(), []);
73 return isSplit ? (
74 <Toaster position="bottom-right" closeButton />
75 ) : (
76 // Small screens: at the bottom, just above the iPhone app's floating
77 // bars (search, the editor's toolbar). At the top they covered the app
78 // sheet's Close and Network buttons.
79 <Toaster
80 position="bottom-center"
81 offset={SMALL_SCREEN_TOAST_OFFSET}
82 mobileOffset={SMALL_SCREEN_TOAST_OFFSET}
83 closeButton
84 />
85 );
86}