import { useQueryClient } from "@tanstack/react-query"; import { AnimatePresence, motion } from "motion/react"; import { useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { toast } from "sonner"; import { useUpdateSettingsMutation } from "@/app/api/mutations/useUpdateSettingsMutation"; import { useGetAnthropicModelsQuery } from "@/app/api/queries/useGetModelsQuery"; import type { ProviderHealthResponse } from "@/app/api/queries/useProviderHealthQuery"; import AnthropicLogo from "@/components/icons/anthropic-logo"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { AnthropicSettingsForm, type AnthropicSettingsFormData, } from "./anthropic-settings-form"; import { useRouter } from "next/navigation"; const AnthropicSettingsDialog = ({ open, setOpen, }: { open: boolean; setOpen: (open: boolean) => void; }) => { const queryClient = useQueryClient(); const [isValidating, setIsValidating] = useState(false); const [validationError, setValidationError] = useState(null); const router = useRouter(); const methods = useForm({ mode: "onSubmit", defaultValues: { apiKey: "", }, }); const { handleSubmit, watch } = methods; const apiKey = watch("apiKey"); const { refetch: validateCredentials } = useGetAnthropicModelsQuery( { apiKey: apiKey, }, { enabled: false, }, ); const settingsMutation = useUpdateSettingsMutation({ onSuccess: () => { // Update provider health cache to healthy since backend validated the setup const healthData: ProviderHealthResponse = { status: "healthy", message: "Provider is configured and working correctly", provider: "anthropic", }; queryClient.setQueryData(["provider", "health"], healthData); toast.message("Anthropic successfully configured", { description: "You can now access the provided language models.", duration: Infinity, closeButton: true, icon: , action: { label: "Settings", onClick: () => { router.push("/settings?focusLlmModel=true"); }, }, }); setOpen(false); }, }); const onSubmit = async (data: AnthropicSettingsFormData) => { // Clear any previous validation errors setValidationError(null); // Only validate if a new API key was entered if (data.apiKey) { setIsValidating(true); const result = await validateCredentials(); setIsValidating(false); if (result.isError) { setValidationError(result.error); return; } } const payload: { anthropic_api_key?: string; } = {}; // Only include api_key if a value was entered if (data.apiKey) { payload.anthropic_api_key = data.apiKey; } // Submit the update settingsMutation.mutate(payload); }; return (
Anthropic Setup
{settingsMutation.isError && (

{settingsMutation.error?.message}

)}
); }; export default AnthropicSettingsDialog;