mirror of
https://github.com/kyantech/Palmr.git
synced 2025-11-02 04:53:26 +00:00
refactor: optimize file upload handling in GlobalDropZone component
- Converted generateFileId and createFileUpload functions to use useCallback for improved performance and memoization. - Updated dependencies in useEffect hooks to include createFileUpload, ensuring proper functionality during file uploads and pasting events.
This commit is contained in:
@@ -43,19 +43,22 @@ export function GlobalDropZone({ onSuccess, children }: GlobalDropZoneProps) {
|
|||||||
const [fileUploads, setFileUploads] = useState<FileUpload[]>([]);
|
const [fileUploads, setFileUploads] = useState<FileUpload[]>([]);
|
||||||
const [hasShownSuccessToast, setHasShownSuccessToast] = useState(false);
|
const [hasShownSuccessToast, setHasShownSuccessToast] = useState(false);
|
||||||
|
|
||||||
const generateFileId = () => {
|
const generateFileId = useCallback(() => {
|
||||||
return Date.now().toString() + Math.random().toString(36).substr(2, 9);
|
return Date.now().toString() + Math.random().toString(36).substr(2, 9);
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const createFileUpload = (file: File): FileUpload => {
|
const createFileUpload = useCallback(
|
||||||
const id = generateFileId();
|
(file: File): FileUpload => {
|
||||||
return {
|
const id = generateFileId();
|
||||||
id,
|
return {
|
||||||
file,
|
id,
|
||||||
status: UploadStatus.PENDING,
|
file,
|
||||||
progress: 0,
|
status: UploadStatus.PENDING,
|
||||||
};
|
progress: 0,
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
[generateFileId]
|
||||||
|
);
|
||||||
|
|
||||||
const handleDragOver = useCallback((event: DragEvent) => {
|
const handleDragOver = useCallback((event: DragEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -181,7 +184,7 @@ export function GlobalDropZone({ onSuccess, children }: GlobalDropZoneProps) {
|
|||||||
|
|
||||||
newUploads.forEach((upload) => uploadFile(upload));
|
newUploads.forEach((upload) => uploadFile(upload));
|
||||||
},
|
},
|
||||||
[uploadFile]
|
[uploadFile, createFileUpload]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handlePaste = useCallback(
|
const handlePaste = useCallback(
|
||||||
@@ -220,7 +223,7 @@ export function GlobalDropZone({ onSuccess, children }: GlobalDropZoneProps) {
|
|||||||
toast.success(t("uploadFile.pasteSuccess", { count: newUploads.length }));
|
toast.success(t("uploadFile.pasteSuccess", { count: newUploads.length }));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[uploadFile, t]
|
[uploadFile, t, createFileUpload]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user