"use client"; import { useMemo } from "react"; import useSWR from "swr"; import { Check, ImageIcon, Loader2, UserRound } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn, fetcher } from "@/lib/utils"; import { normalizeAgentWorkflowBrandModelConfig } from "@/lib/agents/workflow-brand-model-config"; import type { AgentWorkflowBrandModelConfig, AgentWorkflowSelectedBrandModel, } from "@/types/agent"; interface AgentWorkflowBrandModelPickerProps { brand: string; selectedBrandModel?: AgentWorkflowSelectedBrandModel | null; onSelectBrandModel: (value: AgentWorkflowSelectedBrandModel | null) => void; theme?: "light" | "dark"; } export function AgentWorkflowBrandModelPicker({ brand, selectedBrandModel = null, onSelectBrandModel, theme = "light", }: AgentWorkflowBrandModelPickerProps) { const normalizedBrand = brand.trim(); const brandModelSettingsKey = normalizedBrand ? `/api/site-settings?scope=agent-brand-model&brand=${encodeURIComponent( normalizedBrand )}` : null; const { data, error } = useSWR<{ agentWorkflowBrandModelConfig?: AgentWorkflowBrandModelConfig; }>(brandModelSettingsKey, fetcher, { errorRetryCount: 1, revalidateOnFocus: false, }); const config = useMemo( () => normalizeAgentWorkflowBrandModelConfig(data?.agentWorkflowBrandModelConfig), [data?.agentWorkflowBrandModelConfig] ); const matchedEntry = useMemo( () => config.entries.find( (entry) => entry.brandName.toLowerCase() === normalizedBrand.toLowerCase() ) || null, [config.entries, normalizedBrand] ); const images = matchedEntry?.images || []; const isDark = theme === "dark"; const isLoading = Boolean(normalizedBrand) && !data && !error; const renderPlaceholderGrid = (message: string, iconMode: "loading" | "empty" = "empty") => (
{Array.from({ length: 4 }).map((_, index) => (
{iconMode === "loading" ? ( ) : ( )}
{iconMode === "loading" ? "加载中" : "模特预览"}
))}
{message}
); return (
品牌模特选择
根据第一步选择的品牌,展示管理员预先上传的模特图。选中后会自动带入右侧“模特图”槽位;如果当前模式只是开启了模特槽位,这里也可以不选,后续直接继续生成。
{isLoading ? ( renderPlaceholderGrid("正在加载品牌模特图库,请稍候...", "loading") ) : error ? ( renderPlaceholderGrid("品牌模特图库暂时加载失败,不影响继续生成;稍后可刷新重试。") ) : !normalizedBrand ? ( renderPlaceholderGrid("先回到第一步选择品牌,第二步才会按品牌展示可选模特图。") ) : images.length > 0 ? (
品牌:{normalizedBrand} 共 {images.length} 张模特图 {selectedBrandModel ? ( 已选 1 张 ) : null} 可不选
{images.map((image) => { const selected = selectedBrandModel?.brandName === normalizedBrand && selectedBrandModel?.id === image.id; return ( ); })}
) : renderPlaceholderGrid(`品牌“${normalizedBrand}”暂未配置模特图库,请到系统设置上传对应模特图。`)}
); }