import type { ProviderInfo } from '~/types/model'; import type { ModelInfo } from '~/utils/types'; import { useEffect, useState } from 'react'; import Cookies from 'js-cookie'; interface ModelSelectorProps { model?: string; setModel?: (model: string) => void; provider?: ProviderInfo; setProvider?: (provider: ProviderInfo) => void; modelList: ModelInfo[]; providerList: ProviderInfo[]; apiKeys: Record; } export const ModelSelector = ({ model, setModel, provider, setProvider, modelList, providerList, }: ModelSelectorProps) => { // Load enabled providers from cookies const [enabledProviders, setEnabledProviders] = useState(() => { const savedProviders = Cookies.get('providers'); if (savedProviders) { try { const parsedProviders = JSON.parse(savedProviders); return providerList.filter((p) => parsedProviders[p.name]); } catch (error) { console.error('Failed to parse providers from cookies:', error); return providerList; } } return providerList; }); // Update enabled providers when cookies change useEffect(() => { // Function to update providers from cookies const updateProvidersFromCookies = () => { const savedProviders = Cookies.get('providers'); if (savedProviders) { try { const parsedProviders = JSON.parse(savedProviders); const newEnabledProviders = providerList.filter((p) => parsedProviders[p.name]); setEnabledProviders(newEnabledProviders); // If current provider is disabled, switch to first enabled provider if (provider && !parsedProviders[provider.name] && newEnabledProviders.length > 0) { const firstEnabledProvider = newEnabledProviders[0]; setProvider?.(firstEnabledProvider); // Also update the model to the first available one for the new provider const firstModel = modelList.find((m) => m.provider === firstEnabledProvider.name); if (firstModel) { setModel?.(firstModel.name); } } } catch (error) { console.error('Failed to parse providers from cookies:', error); } } }; // Initial update updateProvidersFromCookies(); // Set up an interval to check for cookie changes const interval = setInterval(updateProvidersFromCookies, 1000); return () => clearInterval(interval); }, [providerList, provider, setProvider, modelList, setModel]); if (enabledProviders.length === 0) { return (

No providers are currently enabled. Please enable at least one provider in the settings to start using the chat.

); } return (
); };