244 lines
8.7 KiB
TypeScript
244 lines
8.7 KiB
TypeScript
"use client";
|
||
|
||
import { useMemo } from "react";
|
||
import { Check, ImageIcon, Loader2, RefreshCw, Search } from "lucide-react";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Input } from "@/components/ui/input";
|
||
import { cn } from "@/lib/utils";
|
||
import type { AgentWorkflowMaterialSearchResult } from "@/types/agent";
|
||
|
||
interface AgentWorkflowMaterialPickerProps {
|
||
title: string;
|
||
description: string;
|
||
value: string;
|
||
placeholder: string;
|
||
buttonLabel: string;
|
||
searchNote?: string;
|
||
disabled?: boolean;
|
||
disabledHint?: string;
|
||
isLoading?: boolean;
|
||
errorText?: string | null;
|
||
result?: AgentWorkflowMaterialSearchResult | null;
|
||
selectedImageIds: string[];
|
||
onValueChange: (value: string) => void;
|
||
onSearch: () => void;
|
||
onSelectedImageIdsChange: (next: string[]) => void;
|
||
}
|
||
|
||
export function AgentWorkflowMaterialPicker({
|
||
title,
|
||
description,
|
||
value,
|
||
placeholder,
|
||
buttonLabel,
|
||
searchNote,
|
||
disabled = false,
|
||
disabledHint,
|
||
isLoading = false,
|
||
errorText,
|
||
result,
|
||
selectedImageIds,
|
||
onValueChange,
|
||
onSearch,
|
||
onSelectedImageIdsChange,
|
||
}: AgentWorkflowMaterialPickerProps) {
|
||
const selectedSet = useMemo(
|
||
() => new Set((selectedImageIds || []).map((item) => String(item || "").trim()).filter(Boolean)),
|
||
[selectedImageIds]
|
||
);
|
||
const selectedCount = result?.images
|
||
? result.images.filter((item) => selectedSet.has(item.id)).length
|
||
: 0;
|
||
|
||
const handleToggleImage = (imageId: string) => {
|
||
const next = new Set(selectedImageIds);
|
||
if (next.has(imageId)) {
|
||
next.delete(imageId);
|
||
} else {
|
||
next.add(imageId);
|
||
}
|
||
onSelectedImageIdsChange(Array.from(next));
|
||
};
|
||
|
||
const handleSelectAll = () => {
|
||
onSelectedImageIdsChange(result?.images?.map((item) => item.id) || []);
|
||
};
|
||
|
||
return (
|
||
<section className="space-y-3 rounded-2xl border bg-muted/20 p-4">
|
||
<div className="space-y-1">
|
||
<div className="text-sm font-medium">{title}</div>
|
||
<div className="text-xs text-muted-foreground">{description}</div>
|
||
</div>
|
||
|
||
<div className="flex flex-col gap-2 md:flex-row">
|
||
<Input
|
||
value={value}
|
||
onChange={(event) => onValueChange(event.target.value)}
|
||
placeholder={placeholder}
|
||
disabled={disabled}
|
||
onKeyDown={(event) => {
|
||
if (event.key === "Enter") {
|
||
event.preventDefault();
|
||
if (disabled) return;
|
||
onSearch();
|
||
}
|
||
}}
|
||
/>
|
||
<Button
|
||
type="button"
|
||
onClick={onSearch}
|
||
disabled={disabled || isLoading || !value.trim()}
|
||
>
|
||
{isLoading ? (
|
||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||
) : (
|
||
<Search className="mr-2 h-4 w-4" />
|
||
)}
|
||
{buttonLabel}
|
||
</Button>
|
||
</div>
|
||
|
||
{searchNote ? (
|
||
<div className="text-xs text-muted-foreground">{searchNote}</div>
|
||
) : null}
|
||
{disabled && disabledHint ? (
|
||
<div className="rounded-xl border border-amber-200 bg-amber-50 px-3 py-2 text-xs text-amber-800">
|
||
{disabledHint}
|
||
</div>
|
||
) : null}
|
||
|
||
{!disabled && errorText ? (
|
||
<div className="rounded-xl border border-destructive/20 bg-destructive/5 px-3 py-2 text-xs text-destructive">
|
||
{errorText}
|
||
</div>
|
||
) : null}
|
||
|
||
{!disabled && result ? (
|
||
<div className="space-y-3">
|
||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||
<div className="flex flex-wrap gap-2 text-xs text-muted-foreground">
|
||
<span className="rounded-full bg-background px-2 py-1">
|
||
命中:{result.matchedSku || result.query}
|
||
</span>
|
||
{result.productModel ? (
|
||
<span className="rounded-full bg-background px-2 py-1">
|
||
型号:{result.productModel}
|
||
</span>
|
||
) : null}
|
||
<span className="rounded-full bg-background px-2 py-1">
|
||
共 {result.totalCount} 张
|
||
</span>
|
||
<span className="rounded-full bg-background px-2 py-1">
|
||
已选 {selectedCount} 张
|
||
</span>
|
||
</div>
|
||
<div className="flex items-center gap-2">
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
size="sm"
|
||
onClick={handleSelectAll}
|
||
disabled={!result.images?.length}
|
||
>
|
||
全选
|
||
</Button>
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
size="sm"
|
||
onClick={() => onSelectedImageIdsChange([])}
|
||
disabled={selectedCount === 0}
|
||
>
|
||
清空已选
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-2 gap-3 xl:grid-cols-3">
|
||
{result.images.map((image) => {
|
||
const selected = selectedSet.has(image.id);
|
||
return (
|
||
<button
|
||
key={image.id}
|
||
type="button"
|
||
onClick={() => handleToggleImage(image.id)}
|
||
className={cn(
|
||
"group overflow-hidden rounded-2xl border bg-background text-left transition-all",
|
||
selected
|
||
? "border-primary shadow-[0_0_0_1px_rgba(59,130,246,0.18)]"
|
||
: "border-border hover:border-primary/40 hover:shadow-sm"
|
||
)}
|
||
>
|
||
<div className="relative aspect-square overflow-hidden bg-muted/40">
|
||
<img
|
||
src={image.imageUrl}
|
||
alt={image.label}
|
||
className="h-full w-full object-cover"
|
||
loading="lazy"
|
||
decoding="async"
|
||
onError={(event) => {
|
||
event.currentTarget.style.display = "none";
|
||
}}
|
||
/>
|
||
<div className="absolute inset-x-0 top-0 flex items-center justify-between px-2 py-2">
|
||
<span className="rounded-full bg-black/60 px-2 py-1 text-[11px] text-white">
|
||
{image.group === "fixed" ? "固定角度" : "其他图片"}
|
||
</span>
|
||
<span
|
||
className={cn(
|
||
"flex h-6 w-6 items-center justify-center rounded-full border text-white transition-colors",
|
||
selected
|
||
? "border-primary bg-primary"
|
||
: "border-white/60 bg-black/35 opacity-0 group-hover:opacity-100"
|
||
)}
|
||
>
|
||
<Check className="h-3.5 w-3.5" />
|
||
</span>
|
||
</div>
|
||
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/75 via-black/25 to-transparent px-3 py-2 text-[11px] text-white">
|
||
{image.label}
|
||
</div>
|
||
</div>
|
||
<div className="space-y-1 px-3 py-2">
|
||
<div className="truncate text-xs font-medium">{image.fileName}</div>
|
||
<div className="text-[11px] text-muted-foreground">
|
||
点击{selected ? "取消" : "选择"},可多选带入生图模式
|
||
</div>
|
||
</div>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
) : (
|
||
<div className="flex min-h-[124px] flex-col items-center justify-center rounded-2xl border border-dashed bg-background/60 px-4 text-center text-sm text-muted-foreground">
|
||
{disabled ? (
|
||
<>
|
||
<ImageIcon className="mb-2 h-5 w-5 opacity-60" />
|
||
先选择生图模式,再搜索并选择素材图。
|
||
</>
|
||
) : isLoading ? (
|
||
<>
|
||
<Loader2 className="mb-2 h-5 w-5 animate-spin" />
|
||
正在搜索并导入图片,请稍候...
|
||
</>
|
||
) : (
|
||
<>
|
||
<ImageIcon className="mb-2 h-5 w-5 opacity-60" />
|
||
{value.trim() ? "输入后点击搜索,从素材库拉取白底图。" : "先输入关键词,再搜索素材图。"}
|
||
</>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
{result?.images?.length ? (
|
||
<div className="flex items-start gap-2 rounded-xl bg-background px-3 py-2 text-xs text-muted-foreground">
|
||
<RefreshCw className="mt-0.5 h-3.5 w-3.5 shrink-0" />
|
||
已选中的图片会在下一步优先带入右侧“生图模式”槽位,你也可以进入后继续替换。
|
||
</div>
|
||
) : null}
|
||
</section>
|
||
);
|
||
}
|