|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createContext, useContext, useEffect, useMemo, useState } from "react"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | + |
| 6 | +type Provider = "openai" | "gemini"; |
| 7 | + |
| 8 | +interface AssistantSettingsState { |
| 9 | + provider: Provider; |
| 10 | + openaiApiKey: string; |
| 11 | + geminiApiKey: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface AssistantSettingsContextValue extends AssistantSettingsState { |
| 15 | + setProvider: (provider: Provider) => void; |
| 16 | + setOpenaiApiKey: (key: string) => void; |
| 17 | + setGeminiApiKey: (key: string) => void; |
| 18 | +} |
| 19 | + |
| 20 | +const SETTINGS_KEY = "assistant-settings-storage"; |
| 21 | + |
| 22 | +const defaultSettings: AssistantSettingsState = { |
| 23 | + provider: "openai", |
| 24 | + openaiApiKey: "", |
| 25 | + geminiApiKey: "", |
| 26 | +}; |
| 27 | + |
| 28 | +const AssistantSettingsContext = createContext< |
| 29 | + AssistantSettingsContextValue | undefined |
| 30 | +>(undefined); |
| 31 | + |
| 32 | +const parseStoredSettings = (raw: string | null): AssistantSettingsState => { |
| 33 | + if (!raw) { |
| 34 | + return { ...defaultSettings }; |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + const parsed = JSON.parse(raw) as Partial<AssistantSettingsState>; |
| 39 | + return { |
| 40 | + provider: parsed.provider === "gemini" ? "gemini" : "openai", |
| 41 | + openaiApiKey: |
| 42 | + typeof parsed.openaiApiKey === "string" ? parsed.openaiApiKey : "", |
| 43 | + geminiApiKey: |
| 44 | + typeof parsed.geminiApiKey === "string" ? parsed.geminiApiKey : "", |
| 45 | + }; |
| 46 | + } catch (error) { |
| 47 | + console.error( |
| 48 | + "Failed to parse assistant settings from localStorage", |
| 49 | + error, |
| 50 | + ); |
| 51 | + return { ...defaultSettings }; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +const readStoredSettings = (): AssistantSettingsState => { |
| 56 | + if (typeof window === "undefined") { |
| 57 | + return { ...defaultSettings }; |
| 58 | + } |
| 59 | + |
| 60 | + const raw = window.localStorage.getItem(SETTINGS_KEY); |
| 61 | + return parseStoredSettings(raw); |
| 62 | +}; |
| 63 | + |
| 64 | +export const AssistantSettingsProvider = ({ |
| 65 | + children, |
| 66 | +}: { |
| 67 | + children: ReactNode; |
| 68 | +}) => { |
| 69 | + const [settings, setSettings] = useState<AssistantSettingsState>(() => |
| 70 | + readStoredSettings(), |
| 71 | + ); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + if (typeof window === "undefined") { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + window.localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings)); |
| 80 | + } catch (error) { |
| 81 | + console.error("Failed to save assistant settings to localStorage", error); |
| 82 | + } |
| 83 | + }, [settings]); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + if (typeof window === "undefined") { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const handleStorage = (event: StorageEvent) => { |
| 91 | + if (event.key !== SETTINGS_KEY) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + setSettings(parseStoredSettings(event.newValue)); |
| 96 | + }; |
| 97 | + |
| 98 | + window.addEventListener("storage", handleStorage); |
| 99 | + return () => window.removeEventListener("storage", handleStorage); |
| 100 | + }, []); |
| 101 | + |
| 102 | + const value = useMemo( |
| 103 | + (): AssistantSettingsContextValue => ({ |
| 104 | + ...settings, |
| 105 | + setProvider: (provider: Provider) => { |
| 106 | + setSettings((prev) => ({ ...prev, provider })); |
| 107 | + }, |
| 108 | + setOpenaiApiKey: (key: string) => { |
| 109 | + setSettings((prev) => ({ ...prev, openaiApiKey: key })); |
| 110 | + }, |
| 111 | + setGeminiApiKey: (key: string) => { |
| 112 | + setSettings((prev) => ({ ...prev, geminiApiKey: key })); |
| 113 | + }, |
| 114 | + }), |
| 115 | + [settings], |
| 116 | + ); |
| 117 | + |
| 118 | + return ( |
| 119 | + <AssistantSettingsContext.Provider value={value}> |
| 120 | + {children} |
| 121 | + </AssistantSettingsContext.Provider> |
| 122 | + ); |
| 123 | +}; |
| 124 | + |
| 125 | +export const useAssistantSettings = () => { |
| 126 | + const context = useContext(AssistantSettingsContext); |
| 127 | + |
| 128 | + if (!context) { |
| 129 | + throw new Error( |
| 130 | + "useAssistantSettings must be used within an AssistantSettingsProvider", |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + return context; |
| 135 | +}; |
0 commit comments