Skip to content

Commit

Permalink
feat: add dynamic file size formatting to ImageUploadComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoDePatta committed Sep 10, 2024
1 parent 51c4dc5 commit 8c64c79
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions components/ds/ImageUploadComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { DragEvent, useCallback, useRef, useState } from "react";
import { DragEvent, useCallback, useMemo, useRef, useState } from "react";
import UploadIcon from "@/components/icons/UploadIcon";

type Status = "idle" | "loading" | "error" | "unsupported" | "hover";
Expand All @@ -17,6 +17,13 @@ const ImageUploadComponent = ({
const [status, setStatus] = useState<Status>("idle");
const inputRef = useRef<HTMLInputElement>(null);

const formattedMaxSize = useMemo((): string => {
const sizeInMB = maxFileSize / (1024 * 1024);
return Number.isInteger(sizeInMB)
? `${sizeInMB}MB`
: `${sizeInMB.toFixed(2)}MB`;
}, [maxFileSize]);

const handleDrop = useCallback(
(event: DragEvent<HTMLDivElement>) => {
event.preventDefault();
Expand Down Expand Up @@ -90,7 +97,7 @@ const ImageUploadComponent = ({
onChange={handleFileChange}
/>
<UploadIcon />
{statusComponents[status]}
{statusComponents[status](formattedMaxSize)}
</div>
);
};
Expand All @@ -108,17 +115,19 @@ const StatusComponent = ({
</div>
);

const statusComponents: Record<Status, JSX.Element> = {
idle: (
const statusComponents: Record<Status, (maxSize: string) => JSX.Element> = {
idle: (maxSize) => (
<StatusComponent
title="Drag and drop your image here, or click to select"
message="Max size 4MB"
message={`Max size ${maxSize}`}
/>
),
loading: <StatusComponent title="Loading..." />,
error: <StatusComponent title="Image is too big!" message="4MB max" />,
unsupported: <StatusComponent title="Please provide a valid image" />,
hover: <StatusComponent title="Drop it like it's hot! 🔥" />,
loading: () => <StatusComponent title="Loading..." />,
error: (maxSize) => (
<StatusComponent title="Image is too big!" message={`${maxSize} max`} />
),
unsupported: () => <StatusComponent title="Please provide a valid image" />,
hover: () => <StatusComponent title="Drop it like it's hot! 🔥" />,
};

export { ImageUploadComponent };

0 comments on commit 8c64c79

Please sign in to comment.