Spaces:
Runtime error
Runtime error
Update Home.tsx
Browse files
Home.tsx
CHANGED
|
@@ -7,13 +7,33 @@ import {ContentUnion, GoogleGenAI, Modality} from '@google/genai';
|
|
| 7 |
import {LoaderCircle, SendHorizontal, Trash2, X, Eraser} from 'lucide-react'; // Added Eraser
|
| 8 |
import {useEffect, useRef, useState} from 'react';
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
function parseError(error: string) {
|
| 19 |
try {
|
|
|
|
| 7 |
import {LoaderCircle, SendHorizontal, Trash2, X, Eraser} from 'lucide-react'; // Added Eraser
|
| 8 |
import {useEffect, useRef, useState} from 'react';
|
| 9 |
|
| 10 |
+
function Home() {
|
| 11 |
+
const [apiKey, setApiKey] = useState<string | null>(null);
|
| 12 |
+
const [ai, setAi] = useState<GoogleGenAI | null>(null);
|
| 13 |
+
const [keyError, setKeyError] = useState<string | null>(null);
|
| 14 |
+
|
| 15 |
+
useEffect(() => {
|
| 16 |
+
async function fetchKey() {
|
| 17 |
+
try {
|
| 18 |
+
const response = await fetch('/api/get-api-key'); // Assuming Nginx proxies this
|
| 19 |
+
if (!response.ok) {
|
| 20 |
+
throw new Error(`Failed to fetch API key: ${response.statusText}`);
|
| 21 |
+
}
|
| 22 |
+
const data = await response.json();
|
| 23 |
+
if (data.apiKey) {
|
| 24 |
+
setApiKey(data.apiKey);
|
| 25 |
+
setAi(new GoogleGenAI({ apiKey: data.apiKey }));
|
| 26 |
+
} else {
|
| 27 |
+
throw new Error(data.error || "API Key not returned from backend");
|
| 28 |
+
}
|
| 29 |
+
} catch (error: any) {
|
| 30 |
+
console.error("Error fetching API key:", error);
|
| 31 |
+
setKeyError(error.message);
|
| 32 |
+
// Show error to user
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
fetchKey();
|
| 36 |
+
}, []);
|
| 37 |
|
| 38 |
function parseError(error: string) {
|
| 39 |
try {
|