({ config, children }: ProviderPropsType)
| 58 | * An interface for working with our config. |
| 59 | */ |
| 60 | export function SettingsProvider({ config, children }: ProviderPropsType) { |
| 61 | const revalidator = useRevalidator(); |
| 62 | const [openRouterModels, setOpenRouterModels] = useState<GroupedOpenRouterModels>({}); |
| 63 | const [isLoadingOpenRouterModels, setIsLoadingOpenRouterModels] = useState(false); |
| 64 | |
| 65 | const updateConfig = async (newConfig: Partial<SettingsType>) => { |
| 66 | // Filter out null values and convert back to an object |
| 67 | const changeSet = Object.fromEntries( |
| 68 | Object.entries(newConfig).filter(([_, value]) => value !== null), |
| 69 | ); |
| 70 | |
| 71 | await updateConfigServer(changeSet); |
| 72 | revalidator.revalidate(); |
| 73 | }; |
| 74 | |
| 75 | const refreshOpenRouterModels = async () => { |
| 76 | setIsLoadingOpenRouterModels(true); |
| 77 | try { |
| 78 | const models = await fetchOpenRouterModels(); |
| 79 | const grouped = groupModelsByProvider(models); |
| 80 | setOpenRouterModels(grouped); |
| 81 | } finally { |
| 82 | setIsLoadingOpenRouterModels(false); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | useEffect(() => { |
| 87 | if (config.aiProvider === 'openrouter') { |
| 88 | refreshOpenRouterModels(); |
| 89 | } |
| 90 | }, [config.aiProvider]); |
| 91 | |
| 92 | const aiEnabled = |
| 93 | (config.openaiKey && config.aiProvider === 'openai') || |
| 94 | (config.anthropicKey && config.aiProvider === 'anthropic') || |
| 95 | (config.xaiKey && config.aiProvider === 'Xai') || |
| 96 | (config.geminiKey && config.aiProvider === 'Gemini') || |
| 97 | (config.openrouterKey && config.aiProvider === 'openrouter') || |
| 98 | (config.aiProvider === 'custom' && !!config.aiBaseUrl) || |
| 99 | false; |
| 100 | |
| 101 | const context: SettingsContextValue = { |
| 102 | ...config, |
| 103 | aiEnabled, |
| 104 | updateConfig, |
| 105 | openRouterModels, |
| 106 | isLoadingOpenRouterModels, |
| 107 | refreshOpenRouterModels, |
| 108 | }; |
| 109 | |
| 110 | return <SettingsContext.Provider value={context}>{children}</SettingsContext.Provider>; |
| 111 | } |
| 112 | |
| 113 | export function useSettings() { |
| 114 | const context = useContext(SettingsContext); |
nothing calls this directly
no test coverage detected