"use client"; import { Select, SelectItem } from "@heroui/select"; import { zodResolver } from "@hookform/resolvers/zod"; import { SaveIcon } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; import * as z from "zod"; import { createLighthouseConfig, updateLighthouseConfig, } from "@/actions/lighthouse"; import { useToast } from "@/components/ui"; import { CustomButton, CustomInput, CustomTextarea, } from "@/components/ui/custom"; import { Form } from "@/components/ui/form"; const chatbotConfigSchema = z.object({ model: z.string().min(1, "Model selection is required"), apiKey: z.string().min(1, "API Key is required"), businessContext: z .string() .max(1000, "Business context cannot exceed 1000 characters") .optional(), }); type FormValues = z.infer; interface ChatbotConfigClientProps { initialValues: FormValues; configExists: boolean; } export const ChatbotConfig = ({ initialValues, configExists: initialConfigExists, }: ChatbotConfigClientProps) => { const router = useRouter(); const { toast } = useToast(); const [isLoading, setIsLoading] = useState(false); const [configExists, setConfigExists] = useState(initialConfigExists); const form = useForm({ resolver: zodResolver(chatbotConfigSchema), defaultValues: initialValues, mode: "onChange", }); const onSubmit = async (data: FormValues) => { if (isLoading) return; // Validate API key: required for new config, or if changing an existing masked key if (!configExists && (!data.apiKey || data.apiKey.trim().length === 0)) { form.setError("apiKey", { type: "manual", message: "API Key is required", }); return; } setIsLoading(true); try { const configData: any = { model: data.model, businessContext: data.businessContext || "", }; if (data.apiKey && !data.apiKey.includes("*")) { configData.apiKey = data.apiKey; } const result = configExists ? await updateLighthouseConfig(configData) : await createLighthouseConfig(configData); if (result) { setConfigExists(true); toast({ title: "Success", description: `Lighthouse AI configuration ${ configExists ? "updated" : "created" } successfully`, }); // Navigate to lighthouse chat page after successful save router.push("/lighthouse"); } else { throw new Error("Failed to save configuration"); } } catch (error) { toast({ title: "Error", description: "Failed to save Lighthouse AI configuration: " + String(error), variant: "destructive", }); } finally { setIsLoading(false); } }; return (

Chatbot Settings

Configure your chatbot model and API settings.

( )} />
} > {isLoading ? "Saving..." : "Save"}
); };