Files
route-commerce/src/components/notifications/toast-store.ts
T
Nora 29d9d23a26 fix: react-doctor → 64/100 (Bugs 122, Perf 286, A11y 613, Maint 436)
- CartContext: lazy initializers replace mount-only useEffect
  hydration; remove 8 no-initialize-state warnings
- Toast/AdminSearchInput: React 19 useContext/use + drop
  forwardRef (3 no-react19-deprecated-apis)
- ProductFormModal: lazy initializers + useSyncExternalStore
  for mount; parent adds key=editingProduct.id
- InstallPrompt: useReducer for prompt state (no-cascading-set-state)
- QRScanModal: ref-based latest-callback pattern replaces
  useEffectEvent deps mistake
- OnboardingFlow: functional setState (rerender-functional-setstate)
- UsersPage/StopsCalendar/FeaturesAndStats: lazy initializers
  (rerender-lazy-state-init)
- FAQClientPage: server-side brand settings fetch via getBrandSettingsPublic
  in layout; remove supabase import
- LandingPageWrapper: href='#' → href='#top' (anchor-is-valid)
- TuxedoVideoHero: replace animate-bounce with ease-out-expo
  (no-inline-bounce-easing)
- ProductTableClient: useCallback for handleDeleted
  (jsx-no-new-function-as-prop)
- excel-parser: pre-compile delimiter regexes (js-hoist-regexp)
- water-log/settings: Promise.all for parallel DB calls
  (async-parallel)
- ToastNotification: extract toast store to separate file
  (only-export-components)
- WholesaleClient: inline <WholesaleIcon/> instead of hoisting to
  const (rendering-hoist-jsx)
2026-06-26 02:41:56 -06:00

37 lines
1.3 KiB
TypeScript

// Toast notification store — extracted to its own module so the
// `ToastNotification` component file can stay focused on rendering and
// keep React Fast Refresh's "component-only" boundary intact.
export type ToastType = "success" | "error" | "warning" | "info";
export interface Toast {
id: string;
type: ToastType;
title: string;
message?: string;
duration?: number;
}
let toastCounter = 0;
const toastListeners: Set<(toast: Toast) => void> = new Set();
function emit(type: ToastType, title: string, message?: string) {
const newToast: Toast = { id: `toast-${++toastCounter}`, type, title, message };
toastListeners.forEach((listener) => listener(newToast));
}
export const toast = {
success: (title: string, message?: string) => emit("success", title, message),
error: (title: string, message?: string) => emit("error", title, message),
warning: (title: string, message?: string) => emit("warning", title, message),
info: (title: string, message?: string) => emit("info", title, message),
};
/** Internal subscription helper used by `ToastNotification` to receive
* newly-fired toasts. Returns an unsubscribe function. */
export function subscribeToToasts(listener: (toast: Toast) => void): () => void {
toastListeners.add(listener);
return () => {
toastListeners.delete(listener);
};
}