From 8a0e666ffe6633829af86eeddb6f440b5d08e6f9 Mon Sep 17 00:00:00 2001 From: SwiftyOS Date: Thu, 19 Dec 2024 20:37:49 +0100 Subject: [PATCH] limit images to 5 --- .../agptui/PublishAgentSelectInfo.tsx | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/autogpt_platform/frontend/src/components/agptui/PublishAgentSelectInfo.tsx b/autogpt_platform/frontend/src/components/agptui/PublishAgentSelectInfo.tsx index fc2e637c9182..0c5c22d55023 100644 --- a/autogpt_platform/frontend/src/components/agptui/PublishAgentSelectInfo.tsx +++ b/autogpt_platform/frontend/src/components/agptui/PublishAgentSelectInfo.tsx @@ -61,11 +61,10 @@ export const PublishAgentInfo: React.FC = ({ ); const [slug, setSlug] = React.useState(initialData?.slug || ""); const thumbnailsContainerRef = React.useRef(null); - React.useEffect(() => { if (initialData) { setAgentId(initialData.agent_id); - setImages(initialData.additionalImages || []); + setImagesWithValidation(initialData.additionalImages || []); setSelectedImage(initialData.thumbnailSrc || null); setTitle(initialData.title); setSubheader(initialData.subheader); @@ -76,10 +75,18 @@ export const PublishAgentInfo: React.FC = ({ } }, [initialData]); + const setImagesWithValidation = (newImages: string[]) => { + // Remove duplicates + const uniqueImages = Array.from(new Set(newImages)); + // Keep only first 5 images + const limitedImages = uniqueImages.slice(0, 5); + setImages(limitedImages); + }; + const handleRemoveImage = (indexToRemove: number) => { const newImages = [...images]; newImages.splice(indexToRemove, 1); - setImages(newImages); + setImagesWithValidation(newImages); if (newImages[indexToRemove] === selectedImage) { setSelectedImage(newImages[0] || null); } @@ -89,7 +96,10 @@ export const PublishAgentInfo: React.FC = ({ console.log("images", newImages); } }; + const handleAddImage = async () => { + if (images.length >= 5) return; + const input = document.createElement("input"); input.type = "file"; input.accept = "image/*"; @@ -117,11 +127,7 @@ export const PublishAgentInfo: React.FC = ({ "$1", ); - setImages((prev) => { - const newImages = [...prev, imageUrl]; - console.log("Added image. Images now:", newImages); - return newImages; - }); + setImagesWithValidation([...images, imageUrl]); if (!selectedImage) { setSelectedImage(imageUrl); } @@ -133,7 +139,7 @@ export const PublishAgentInfo: React.FC = ({ const [isGenerating, setIsGenerating] = React.useState(false); const handleGenerateImage = async () => { - if (isGenerating) return; + if (isGenerating || images.length >= 5) return; setIsGenerating(true); try { @@ -143,7 +149,7 @@ export const PublishAgentInfo: React.FC = ({ } const { image_url } = await api.generateStoreSubmissionImage(agentId); console.log("image_url", image_url); - setImages(prev => [...prev, image_url]); + setImagesWithValidation([...images, image_url]); } catch (error) { console.error("Failed to generate image:", error); } finally { @@ -307,19 +313,21 @@ export const PublishAgentInfo: React.FC = ({ ))} - + {images.length < 5 && ( + + )} )} @@ -336,11 +344,13 @@ export const PublishAgentInfo: React.FC = ({