Skip to content

Commit

Permalink
optimize image source handling in ImageUpload component using useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
Precious-Macaulay committed Nov 13, 2024
1 parent f755a74 commit ae3df33
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/PromptForm/ImageUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { XMarkIcon } from "@heroicons/react/24/outline";
import { useImageUpload } from "@/hooks/useImageUpload";
import { usePromptFormStore } from "@/store/promptFormStore";
import { useMemo } from "react";

interface ImageUploadProps {
showControls: boolean;
Expand All @@ -23,6 +24,13 @@ export const ImageUpload: React.FC<ImageUploadProps> = ({ showControls, onImageU
onImageUpload(null, null);
};

const imageSrc = useMemo(() => {
if (image) {
return URL.createObjectURL(image);
}
return urlImage;
}, [image, urlImage]);

if (!showControls && !image && !urlImage) return null;

return (
Expand All @@ -36,7 +44,7 @@ export const ImageUpload: React.FC<ImageUploadProps> = ({ showControls, onImageU
{image || urlImage ? (
<div className="relative w-full h-full group">
<img
src={image ? URL.createObjectURL(image) : urlImage!}
src={imageSrc!}
alt="Uploaded"
className="object-cover h-full rounded-md"
/>
Expand Down

0 comments on commit ae3df33

Please sign in to comment.