diff --git a/inventory/src/lib/react-spreadsheet-import/src/steps/ImageUploadStep/ImageUploadStep.tsx b/inventory/src/lib/react-spreadsheet-import/src/steps/ImageUploadStep/ImageUploadStep.tsx index a7d1558..2cadbb6 100644 --- a/inventory/src/lib/react-spreadsheet-import/src/steps/ImageUploadStep/ImageUploadStep.tsx +++ b/inventory/src/lib/react-spreadsheet-import/src/steps/ImageUploadStep/ImageUploadStep.tsx @@ -42,6 +42,7 @@ import { PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; +import { createPortal } from "react-dom"; type Props = { data: any[]; @@ -767,24 +768,31 @@ export const ImageUploadStep = ({ if (!image) return; try { - // Extract the filename from the URL - const urlParts = image.imageUrl.split('/'); - const filename = urlParts[urlParts.length - 1]; + // Check if this is an external URL-based image or an uploaded image + const isExternalUrl = image.imageUrl.startsWith('http') && + !image.imageUrl.includes(config.apiUrl.replace(/^https?:\/\//, '')); - // Call API to delete the image - const response = await fetch(`${config.apiUrl}/import/delete-image`, { - method: 'DELETE', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - imageUrl: image.imageUrl, - filename - }), - }); - - if (!response.ok) { - throw new Error('Failed to delete image'); + // Only call the API to delete the file if it's an uploaded image + if (!isExternalUrl) { + // Extract the filename from the URL + const urlParts = image.imageUrl.split('/'); + const filename = urlParts[urlParts.length - 1]; + + // Call API to delete the image + const response = await fetch(`${config.apiUrl}/import/delete-image`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + imageUrl: image.imageUrl, + filename + }), + }); + + if (!response.ok) { + throw new Error('Failed to delete image'); + } } // Remove the image from our state @@ -1232,84 +1240,37 @@ export const ImageUploadStep = ({ ); }; - // Add a URL input component + // Add a URL input component that doesn't expand/collapse const ImageUrlInput = ({ productIndex }: { productIndex: number }) => { - const [isOpen, setIsOpen] = useState(false); - - // Add input reference to maintain focus - const inputRef = useRef(null); - + // Use a stable format that won't get affected by DndContext events return ( - - - - - { - // Prevent closing when interacting with the input - if (inputRef.current?.contains(e.target as Node)) { - e.preventDefault(); - } - }} - onOpenAutoFocus={() => { - // Focus the input field when the popover opens - setTimeout(() => inputRef.current?.focus(), 0); - }} +
{ + e.preventDefault(); + if (urlInputs[productIndex]) { + handleAddImageFromUrl(productIndex, urlInputs[productIndex]); + } + }} + > + updateUrlInput(productIndex, e.target.value)} + className="text-xs h-8 max-w-[180px]" + /> + - - - - + {processingUrls[productIndex] ? + : + } + Add + + ); }; @@ -1504,7 +1465,34 @@ export const ImageUploadStep = ({
- +
{ + e.preventDefault(); + if (urlInputs[index]) { + handleAddImageFromUrl(index, urlInputs[index]); + updateUrlInput(index, ''); + } + }} + > + updateUrlInput(index, e.target.value)} + className="text-xs h-8 w-[180px]" + /> + +