/** * API Settings Dialog Component */ "use client"; import { useState, useEffect } from "react"; import { Settings } from "lucide-react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { getApiSettings, saveApiSettings, clearApiSettings, type ApiSettings, DEFAULT_API_SETTINGS, } from "@/lib/storage"; const formSchema = z.object({ // Required openai_api_key: z.string().min(1, "OpenAI API Key 為必填"), alpha_vantage_api_key: z.string().min(1, "Alpha Vantage API Key 為必填"), // Optional anthropic_api_key: z.string().optional().or(z.literal("")), google_api_key: z.string().optional().or(z.literal("")), grok_api_key: z.string().optional().or(z.literal("")), deepseek_api_key: z.string().optional().or(z.literal("")), qwen_api_key: z.string().optional().or(z.literal("")), }); type FormValues = z.infer; export function ApiSettingsDialog() { const [open, setOpen] = useState(false); const [saveSuccess, setSaveSuccess] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: getApiSettings(), }); // Load settings when dialog opens useEffect(() => { if (open) { const settings = getApiSettings(); form.reset(settings); setSaveSuccess(false); } }, [open, form]); const onSubmit = (values: FormValues) => { try { // Type assertion since our form values match ApiSettings structure saveApiSettings(values as ApiSettings); setSaveSuccess(true); setTimeout(() => { setSaveSuccess(false); setOpen(false); }, 1500); } catch (error) { console.error("Failed to save settings:", error); } }; const handleClear = () => { clearApiSettings(); form.reset(DEFAULT_API_SETTINGS); }; return ( API 配置 設定您的 API 金鑰。這些資訊會儲存在瀏覽器的本機儲存空間中。
{/* Required Section */}

必填項目

{/* OpenAI API Key */} ( OpenAI API Key * 用於 OpenAI 模型(GPT-4, GPT-5, o4 等) )} /> {/* Alpha Vantage API Key */} ( Alpha Vantage API Key * 用於獲取市場基本面數據(必填) )} />
{/* Optional Section */}

選填項目(依需求填寫)

{/* Anthropic API Key */} ( Anthropic API Key 用於 Claude 模型 )} /> {/* Google API Key */} ( Google API Key 用於 Gemini 模型 )} /> {/* Grok API Key */} ( Grok (xAI) API Key 用於 Grok 模型 )} /> {/* DeepSeek API Key */} ( DeepSeek API Key 用於 DeepSeek 模型 )} /> {/* Qwen API Key */} ( Qwen (Alibaba) API Key 用於 Qwen 模型 )} />
{saveSuccess && (
✓ 設定已成功儲存
)}
); }