mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { ReactNode, useState, useEffect } from 'react';
|
|
import { AISettingsContext, type AISettings, type AISettingsContextType } from '@/contexts/AISettingsContext';
|
|
|
|
interface AISettingsProviderProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
const DEFAULT_SETTINGS: AISettings = {
|
|
apiKey: '',
|
|
baseUrl: 'https://openrouter.ai/api/v1',
|
|
model: 'anthropic/claude-sonnet-4',
|
|
};
|
|
|
|
const STORAGE_KEY = 'ai-settings';
|
|
|
|
export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
|
const [settings, setSettings] = useState<AISettings>(() => {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored) {
|
|
try {
|
|
return { ...DEFAULT_SETTINGS, ...JSON.parse(stored) };
|
|
} catch {
|
|
return DEFAULT_SETTINGS;
|
|
}
|
|
}
|
|
return DEFAULT_SETTINGS;
|
|
});
|
|
|
|
useEffect(() => {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
|
}, [settings]);
|
|
|
|
const updateSettings = (newSettings: Partial<AISettings>) => {
|
|
setSettings(prev => ({ ...prev, ...newSettings }));
|
|
};
|
|
|
|
const isConfigured = Boolean(settings.apiKey);
|
|
|
|
const contextValue: AISettingsContextType = {
|
|
settings,
|
|
updateSettings,
|
|
isConfigured,
|
|
};
|
|
|
|
return (
|
|
<AISettingsContext.Provider value={contextValue}>
|
|
{children}
|
|
</AISettingsContext.Provider>
|
|
);
|
|
} |