import React, { createContext, useContext, useState, useEffect } from 'react'; import { Config } from '../api/config'; interface ConfigContextType { config: Record; upsert: (key: string, value: any, isSecret?: boolean) => Promise; read: (key: string) => Promise; remove: (key: string) => Promise; addExtension: (name: string, config: any) => Promise; removeExtension: (name: string) => Promise; } interface ConfigProviderProps { children: React.ReactNode; } const ConfigContext = createContext(undefined); export const ConfigProvider: React.FC = ({ children }) => { const [config, setConfig] = useState>({}); useEffect(() => { // Load all configuration data on mount (async () => { const initialConfig = await Config.readAll(); setConfig(initialConfig || {}); })(); }, []); const reloadConfig = async () => { const newConfig = await Config.readAll(); setConfig(newConfig || {}); }; const upsert = async (key: string, value: any, isSecret?: boolean) => { await Config.upsert(key, value, isSecret); await reloadConfig(); }; const read = async (key: string) => { return Config.read(key); }; const remove = async (key: string) => { await Config.remove(key); await reloadConfig(); }; const addExtension = async (name: string, config: any) => { await Config.addExtension(name, config); await reloadConfig(); }; const removeExtension = async (name: string) => { await Config.removeExtension(name); await reloadConfig(); }; return ( {children} ); }; export const useConfig = () => { const context = useContext(ConfigContext); if (context === undefined) { throw new Error('useConfig must be used within a ConfigProvider'); } return context; };