|
| 1 | +import React, { createContext, useContext, useState, useCallback, useRef, useEffect } from "react"; |
| 2 | + |
| 3 | +export interface SmartPromptConfig { |
| 4 | + id: string; |
| 5 | + type: string; |
| 6 | + title: string; |
| 7 | + message: string; |
| 8 | + primaryAction?: { |
| 9 | + label: string; |
| 10 | + onClick: () => void; |
| 11 | + }; |
| 12 | + secondaryAction?: { |
| 13 | + label: string; |
| 14 | + onClick: () => void; |
| 15 | + }; |
| 16 | + dontAskAgainKey?: string; |
| 17 | + onDontAskAgain?: (key: string) => void; |
| 18 | + autoDismissMs?: number; |
| 19 | +} |
| 20 | + |
| 21 | +interface SmartPromptContextValue { |
| 22 | + showPrompt: (config: Omit<SmartPromptConfig, "id">) => void; |
| 23 | + dismissPrompt: (id: string) => void; |
| 24 | + activePrompt: SmartPromptConfig | null; |
| 25 | + hasDontAskAgain: (key: string) => boolean; |
| 26 | + setDontAskAgain: (key: string, value: boolean) => void; |
| 27 | +} |
| 28 | + |
| 29 | +const SmartPromptContext = createContext<SmartPromptContextValue | null>(null); |
| 30 | + |
| 31 | +const DONT_ASK_AGAIN_PREFIX = "vw_smart_prompt_dont_ask_"; |
| 32 | + |
| 33 | +export const SmartPromptProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { |
| 34 | + const [queue, setQueue] = useState<SmartPromptConfig[]>([]); |
| 35 | + const [activePrompt, setActivePrompt] = useState<SmartPromptConfig | null>(null); |
| 36 | + const idCounter = useRef(0); |
| 37 | + |
| 38 | + const hasDontAskAgain = useCallback((key: string) => { |
| 39 | + try { |
| 40 | + return localStorage.getItem(`${DONT_ASK_AGAIN_PREFIX}${key}`) === "true"; |
| 41 | + } catch { |
| 42 | + return false; |
| 43 | + } |
| 44 | + }, []); |
| 45 | + |
| 46 | + const setDontAskAgain = useCallback((key: string, value: boolean) => { |
| 47 | + try { |
| 48 | + if (value) { |
| 49 | + localStorage.setItem(`${DONT_ASK_AGAIN_PREFIX}${key}`, "true"); |
| 50 | + } else { |
| 51 | + localStorage.removeItem(`${DONT_ASK_AGAIN_PREFIX}${key}`); |
| 52 | + } |
| 53 | + } catch { |
| 54 | + // ignore localStorage errors |
| 55 | + } |
| 56 | + }, []); |
| 57 | + |
| 58 | + const showPrompt = useCallback( |
| 59 | + (config: Omit<SmartPromptConfig, "id">) => { |
| 60 | + if (config.dontAskAgainKey && hasDontAskAgain(config.dontAskAgainKey)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const newPrompt: SmartPromptConfig = { |
| 65 | + ...config, |
| 66 | + id: `prompt-${Date.now()}-${++idCounter.current}`, |
| 67 | + }; |
| 68 | + |
| 69 | + setQueue((prev) => { |
| 70 | + // dedupe by type |
| 71 | + if (prev.some((p) => p.type === newPrompt.type)) { |
| 72 | + return prev; |
| 73 | + } |
| 74 | + return [...prev, newPrompt]; |
| 75 | + }); |
| 76 | + }, |
| 77 | + [hasDontAskAgain], |
| 78 | + ); |
| 79 | + |
| 80 | + const dismissPrompt = useCallback((id: string) => { |
| 81 | + setQueue((prev) => prev.filter((p) => p.id !== id)); |
| 82 | + setActivePrompt((prev) => (prev?.id === id ? null : prev)); |
| 83 | + }, []); |
| 84 | + |
| 85 | + // Promote next prompt from queue when active is cleared |
| 86 | + useEffect(() => { |
| 87 | + if (!activePrompt && queue.length > 0) { |
| 88 | + const next = queue[0]; |
| 89 | + setActivePrompt(next); |
| 90 | + setQueue((prev) => prev.slice(1)); |
| 91 | + } |
| 92 | + }, [activePrompt, queue]); |
| 93 | + |
| 94 | + const value: SmartPromptContextValue = { |
| 95 | + showPrompt, |
| 96 | + dismissPrompt, |
| 97 | + activePrompt, |
| 98 | + hasDontAskAgain, |
| 99 | + setDontAskAgain, |
| 100 | + }; |
| 101 | + |
| 102 | + return <SmartPromptContext.Provider value={value}>{children}</SmartPromptContext.Provider>; |
| 103 | +}; |
| 104 | + |
| 105 | +export const useSmartPromptContext = () => { |
| 106 | + const ctx = useContext(SmartPromptContext); |
| 107 | + if (!ctx) { |
| 108 | + throw new Error("useSmartPromptContext must be used within SmartPromptProvider"); |
| 109 | + } |
| 110 | + return ctx; |
| 111 | +}; |
0 commit comments