"use client"; import { useEffect, useState } from "react"; import { Check, ChevronsUpDown, Loader2 } from "lucide-react"; import { Button } from "./ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "./ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; import { cn } from "@/lib/utils"; import { toast } from "sonner"; import { Switch } from "./ui/switch"; interface GoogleConfigProps { googleApiKey: string; googleModel: string; webGrounding?: boolean; onInputChange: (value: string | boolean, field: string) => void; } export default function GoogleConfig({ googleApiKey, googleModel, webGrounding, onInputChange }: GoogleConfigProps) { const [openModelSelect, setOpenModelSelect] = useState(false); const [availableModels, setAvailableModels] = useState([]); const [modelsLoading, setModelsLoading] = useState(false); const [modelsChecked, setModelsChecked] = useState(false); const [apiKey, setApiKey] = useState(googleApiKey); useEffect(() => { setAvailableModels([]); setModelsChecked(false); onInputChange("", "google_model"); }, [apiKey]); const onApiKeyChange = (value: string) => { setApiKey(value); onInputChange(value, "google_api_key"); }; const fetchAvailableModels = async () => { if (!googleApiKey) return; setModelsLoading(true); try { const response = await fetch('/api/v1/ppt/google/models/available', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ api_key: googleApiKey }), }); if (response.ok) { const data = await response.json(); setAvailableModels(data); setModelsChecked(true); onInputChange("models/gemini-2.5-flash", "google_model"); } else { console.error('Failed to fetch models'); setAvailableModels([]); setModelsChecked(true); } } catch (error) { console.error('Error fetching models:', error); toast.error('Error fetching models'); setAvailableModels([]); setModelsChecked(true); } finally { setModelsLoading(false); } }; return (
{/* API Key Input */}
onApiKeyChange(e.target.value)} className="w-full px-4 py-2.5 outline-none border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-colors" placeholder="Enter your API key" />

Your API key will be stored locally and never shared

{/* Check for available models button - show when no models checked or no models found */} {(!modelsChecked || (modelsChecked && availableModels.length === 0)) && (
)} {/* Show message if no models found */} {modelsChecked && availableModels.length === 0 && (

No models found. Please make sure your API key is valid and has access to Google models.

)} {/* Model Selection - only show if models are available */} {modelsChecked && availableModels.length > 0 ? (
No model found. {availableModels.map((model, index) => ( { onInputChange(value, "google_model"); setOpenModelSelect(false); }} >
{model}
))}
) : null} {/* Web Grounding Toggle - at the end, below models dropdown */}
onInputChange(checked, "web_grounding")} />

If enabled, the model can use web search grounding when available.

); }