import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { useModelPricing, useDeleteModelPricing } from "@/lib/query/usage"; import { PricingEditModal } from "./PricingEditModal"; import type { ModelPricing } from "@/types/usage"; import { Plus, Pencil, Trash2, ChevronDown, ChevronRight } from "lucide-react"; export function PricingConfigPanel() { const { t } = useTranslation(); const { data: pricing, isLoading, error } = useModelPricing(); const deleteMutation = useDeleteModelPricing(); const [editingModel, setEditingModel] = useState(null); const [isAddingNew, setIsAddingNew] = useState(false); const [deleteConfirm, setDeleteConfirm] = useState(null); const [isExpanded, setIsExpanded] = useState(false); const handleDelete = (modelId: string) => { deleteMutation.mutate(modelId, { onSuccess: () => { setDeleteConfirm(null); }, }); }; const handleAddNew = () => { setIsAddingNew(true); setEditingModel({ modelId: "", displayName: "", inputCostPerMillion: "0", outputCostPerMillion: "0", cacheReadCostPerMillion: "0", cacheCreationCostPerMillion: "0", }); }; if (isLoading) { return ( setIsExpanded(!isExpanded)} >
{t("usage.modelPricing", "模型定价")}
); } if (error) { return ( setIsExpanded(!isExpanded)} >
{isExpanded ? ( ) : ( )} {t("usage.modelPricing", "模型定价")}
{isExpanded && ( {t("usage.loadPricingError", "加载定价数据失败")}:{" "} {String(error)} )}
); } return ( setIsExpanded(!isExpanded)} >
{isExpanded ? ( ) : ( )}
{t("usage.modelPricing", "模型定价")} {pricing && pricing.length > 0 && ( ({pricing.length}) )} {!isExpanded && ( {t( "usage.modelPricingDesc", "配置各模型的 Token 成本(每百万 tokens 的 USD 价格,支持 * 与 ? 通配)", )} )}
{isExpanded && ( {!pricing || pricing.length === 0 ? ( {t( "usage.noPricingData", '暂无定价数据。点击"新增"添加模型定价配置。', )} ) : (
{t("usage.model", "模型")} {t("usage.displayName", "显示名称")} {t("usage.inputCost", "输入成本")} {t("usage.outputCost", "输出成本")} {t("usage.cacheReadCost", "缓存读取")} {t("usage.cacheWriteCost", "缓存写入")} {t("common.actions", "操作")} {pricing.map((model) => ( {model.modelId} {model.displayName} ${model.inputCostPerMillion} ${model.outputCostPerMillion} ${model.cacheReadCostPerMillion} ${model.cacheCreationCostPerMillion}
))}
)}
)} {editingModel && ( { setEditingModel(null); setIsAddingNew(false); }} /> )} setDeleteConfirm(null)} > {t("usage.deleteConfirmTitle", "确认删除")} {t( "usage.deleteConfirmDesc", "确定要删除此模型定价配置吗?此操作无法撤销。", )}
); }