feat(components): add image drag-drop support to DragDropContainer
This commit is contained in:
@@ -1,11 +1,21 @@
|
|||||||
import { ReactNode } from "react";
|
import { ReactNode } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
const IMAGE_EXTENSIONS = [".png", ".jpg", ".jpeg", ".webp"];
|
||||||
|
|
||||||
|
const getFileType = (file: File): "ics" | "image" | null => {
|
||||||
|
const name = file.name.toLowerCase();
|
||||||
|
if (name.endsWith(".ics")) return "ics";
|
||||||
|
if (IMAGE_EXTENSIONS.some((ext) => name.endsWith(ext))) return "image";
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
interface DragDropContainerProps {
|
interface DragDropContainerProps {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
isDragOver: boolean;
|
isDragOver: boolean;
|
||||||
setIsDragOver: (isDragOver: boolean) => void;
|
setIsDragOver: (isDragOver: boolean) => void;
|
||||||
onImport: (file: File) => void;
|
onImport: (file: File) => void;
|
||||||
|
onImageDrop?: (file: File) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DragDropContainer = ({
|
export const DragDropContainer = ({
|
||||||
@@ -13,6 +23,7 @@ export const DragDropContainer = ({
|
|||||||
isDragOver,
|
isDragOver,
|
||||||
setIsDragOver,
|
setIsDragOver,
|
||||||
onImport,
|
onImport,
|
||||||
|
onImageDrop,
|
||||||
}: DragDropContainerProps) => {
|
}: DragDropContainerProps) => {
|
||||||
const handleDragOver = (e: React.DragEvent) => {
|
const handleDragOver = (e: React.DragEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -29,10 +40,14 @@ export const DragDropContainer = ({
|
|||||||
setIsDragOver(false);
|
setIsDragOver(false);
|
||||||
if (e.dataTransfer.files?.length) {
|
if (e.dataTransfer.files?.length) {
|
||||||
const file = e.dataTransfer.files[0];
|
const file = e.dataTransfer.files[0];
|
||||||
if (file.name.endsWith(".ics")) {
|
const fileType = getFileType(file);
|
||||||
|
|
||||||
|
if (fileType === "ics") {
|
||||||
onImport(file);
|
onImport(file);
|
||||||
|
} else if (fileType === "image" && onImageDrop) {
|
||||||
|
onImageDrop(file);
|
||||||
} else {
|
} else {
|
||||||
toast.warning("Please drop an .ics file");
|
toast.warning("Please drop an .ics file or an image");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -48,7 +63,9 @@ export const DragDropContainer = ({
|
|||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
<div className="mt-auto w-full pb-4 text-gray-400">
|
<div className="mt-auto w-full pb-4 text-gray-400">
|
||||||
<div className="max-w-fit m-auto">Drag & Drop *.ics here</div>
|
<div className="max-w-fit m-auto">
|
||||||
|
Drag & Drop *.ics or an event screenshot here
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user