import React, { useState, useEffect } from 'react'; import { formatGeminiResponseToExcel } from '../utils/excelFormatter'; interface GeminiChatProps { apiKey: string; initialInput?: string; } const GeminiChat: React.FC = ({ apiKey, initialInput = '' }) => { const [input, setInput] = useState(''); const [response, setResponse] = useState(''); const [loading, setLoading] = useState(false); const fetchGeminiResponse = async (text: string) => { setLoading(true); try { const response = await fetch( `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${apiKey}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ contents: [{ parts: [{ text: text }] }] }) } ); const data = await response.json(); if (data.error) { throw new Error(data.error.message); } setResponse(data.candidates[0].content.parts[0].text); } catch (error) { console.error('Error:', error); setResponse('Error getting response from Gemini: ' + (error as Error).message); } finally { setLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (input.trim()) { await fetchGeminiResponse(input); } }; useEffect(() => { if (initialInput) { fetchGeminiResponse(initialInput); } }, [initialInput]); const handleExportExcel = () => { try { formatGeminiResponseToExcel(response); } catch (error) { console.error('Export error:', error); alert('Failed to export to Excel. Please check the response format.'); } }; return (

Ask Anything with Gemini