Reality123b commited on
Commit
b1712cf
·
verified ·
1 Parent(s): ddb09f1

Update Home.tsx

Browse files
Files changed (1) hide show
  1. Home.tsx +27 -7
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
- const API_KEY = process.env.API_KEY;
11
- let ai: GoogleGenAI | null = null;
12
- if (API_KEY) {
13
- ai = new GoogleGenAI({apiKey: API_KEY});
14
- } else {
15
- console.error("API_KEY is not set. Please ensure it's configured in your environment.");
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 {