import { useState } from "react"; import { AlertTriangle, PlusIcon, TrashIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { CreateCustomModelDialog } from "@/components/CreateCustomModelDialog"; import { useLanguageModelsForProvider } from "@/hooks/useLanguageModelsForProvider"; // Use the hook directly here import { useDeleteCustomModel } from "@/hooks/useDeleteCustomModel"; // Import the new hook import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; interface ModelsSectionProps { providerId: string; } export function ModelsSection({ providerId }: ModelsSectionProps) { const [isCustomModelDialogOpen, setIsCustomModelDialogOpen] = useState(false); const [isConfirmDeleteDialogOpen, setIsConfirmDeleteDialogOpen] = useState(false); const [modelToDelete, setModelToDelete] = useState(null); // Fetch custom models within this component now const { data: models, isLoading: modelsLoading, error: modelsError, refetch: refetchModels, } = useLanguageModelsForProvider(providerId); const { mutate: deleteModel, isPending: isDeleting } = useDeleteCustomModel({ onSuccess: () => { refetchModels(); // Refetch models list after successful deletion // Optionally show a success toast here }, onError: (error: Error) => { // Optionally show an error toast here console.error("Failed to delete model:", error); }, }); const handleDeleteClick = (modelApiName: string) => { setModelToDelete(modelApiName); setIsConfirmDeleteDialogOpen(true); }; const handleConfirmDelete = () => { if (modelToDelete) { deleteModel({ providerId, modelApiName: modelToDelete }); setModelToDelete(null); } setIsConfirmDeleteDialogOpen(false); }; return (

Models

Manage specific models available through this provider.

{/* Custom Models List Area */} {modelsLoading && (
)} {modelsError && ( Error Loading Models {modelsError.message} )} {!modelsLoading && !modelsError && models && models.length > 0 && (
{models.map((model) => (

{model.displayName}

{model.type === "custom" && ( )}

{model.apiName}

{model.description && (

{model.description}

)}
{model.contextWindow && ( Context: {model.contextWindow.toLocaleString()} tokens )} {model.maxOutputTokens && ( Max Output: {model.maxOutputTokens.toLocaleString()} tokens )}
{model.type === "cloud" ? "Built-in" : "Custom"} {model.tag && ( {model.tag} )}
))}
)} {!modelsLoading && !modelsError && (!models || models.length === 0) && (

No custom models have been added for this provider yet.

)} {/* End Custom Models List Area */} {providerId !== "auto" && ( )} {/* Render the dialog */} setIsCustomModelDialogOpen(false)} onSuccess={() => { setIsCustomModelDialogOpen(false); refetchModels(); // Refetch models on success }} providerId={providerId} /> Are you sure you want to delete this model? This action cannot be undone. This will permanently delete the custom model " {modelToDelete ? models?.find((m) => m.apiName === modelToDelete) ?.displayName || modelToDelete : ""} " (API Name: {modelToDelete}). setModelToDelete(null)}> Cancel {isDeleting ? "Deleting..." : "Yes, delete it"}
); }