diff --git a/components/agents/AgentGenerationWorkspaceDarkV2.tsx b/components/agents/AgentGenerationWorkspaceDarkV2.tsx index c456e5267..b21c84cf9 100644 --- a/components/agents/AgentGenerationWorkspaceDarkV2.tsx +++ b/components/agents/AgentGenerationWorkspaceDarkV2.tsx @@ -481,7 +481,13 @@ export function AgentGenerationWorkspaceDarkV2({ imageUrl: String(item.imageUrl || "").trim(), candidateId: item.candidateId || null, sortOrder: item.sortOrder ?? 0, - assignedProductIndex: item.assignedProductIndex ?? null, + assignedProductIndex: + selectedProductAttachments.length > 0 + ? Math.min( + item.assignedProductIndex ?? 0, + Math.max(selectedProductAttachments.length - 1, 0) + ) + : null, })), } : undefined; @@ -1294,26 +1300,6 @@ export function AgentGenerationWorkspaceDarkV2({ ), })); }, [updateSkuReplacementState]); - useEffect(() => { - if (!isSkuReplacementPreset) return; - updateSkuReplacementState((current) => ({ - ...current, - references: current.references.map((card, index) => ({ - ...card, - assignedProductIndex: - selectedProductAttachments.length > 0 - ? Math.min( - card.assignedProductIndex ?? index, - Math.max(selectedProductAttachments.length - 1, 0) - ) - : null, - })), - })); - }, [ - isSkuReplacementPreset, - selectedProductAttachments.length, - updateSkuReplacementState, - ]); const handleRemoveSkuReferenceCard = useCallback((cardId: string) => { updateSkuReplacementState((current) => ({ ...current, @@ -2388,6 +2374,18 @@ export function AgentGenerationWorkspaceDarkV2({ key={card.id} className="overflow-hidden rounded-[22px] border border-white/10 bg-black/20" > + {(() => { + const resolvedAssignedProductIndex = + selectedProductAttachments.length > 0 + ? Math.min( + typeof card.assignedProductIndex === "number" + ? card.assignedProductIndex + : index, + Math.max(selectedProductAttachments.length - 1, 0) + ) + : null; + return ( + <>