Skip to content

Commit

Permalink
handle file drop rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmclean committed Jan 1, 2025
1 parent e038477 commit 1629346
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions apps/sovoli.com/src/app/(dashboard)/new/components/AssetManager.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from "react";
import { useCallback, useState } from "react";
import { ne } from "@sovoli/db";

Check failure on line 2 in apps/sovoli.com/src/app/(dashboard)/new/components/AssetManager.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

'ne' is defined but never used. Allowed unused vars must match /^_/u
import { CloudUpload } from "lucide-react";
import { useDropzone } from "react-dropzone";
import { FileRejection, useDropzone } from "react-dropzone";

Check warning on line 4 in apps/sovoli.com/src/app/(dashboard)/new/components/AssetManager.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

Imports "FileRejection" are only used as type

export interface AssetManagerProps {
onFileUploaded: (file: File, id: string, path: string) => void;
Expand All @@ -15,22 +16,30 @@ interface FileState {

export const AssetManager = ({ onFileUploaded }: AssetManagerProps) => {
const [files, setFiles] = useState<FileState[]>([]);
const [fileRejections, setFileRejections] = useState<FileRejection[]>([]);

const onDrop = useCallback((acceptedFiles: File[]) => {
const newFiles = acceptedFiles.map((file) => ({
file,
preview: URL.createObjectURL(file),
status: "idle" as FileStatus,
}));
setFiles((current) => [...current, ...newFiles]);

for (const file of newFiles) {
void uploadFile(file);
}
}, []);

Check warning on line 32 in apps/sovoli.com/src/app/(dashboard)/new/components/AssetManager.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

React Hook useCallback has a missing dependency: 'uploadFile'. Either include it or remove the dependency array

const { getRootProps, getInputProps } = useDropzone({
multiple: true,
accept: {
"image/jpeg": [],
"image/png": [],
},
onDrop: (acceptedFiles) => {
const newFiles = acceptedFiles.map((file) => ({
file,
preview: URL.createObjectURL(file),
status: "idle" as FileStatus,
}));
setFiles((current) => [...current, ...newFiles]);

// eslint-disable-next-line @typescript-eslint/no-misused-promises
newFiles.forEach((fileState) => uploadFile(fileState));
onDrop: (acceptedFiles, fileRejections) => {
onDrop(acceptedFiles);
setFileRejections(fileRejections);
},
});

Expand All @@ -41,7 +50,7 @@ export const AssetManager = ({ onFileUploaded }: AssetManagerProps) => {
);

try {
const signedUrlResponse = await fetch("/api/images", {
const signedUrlResponse = await fetch("/api/assets", {
method: "POST",
body: JSON.stringify({ fileName: file.name, type: file.type }),
});
Expand Down Expand Up @@ -100,6 +109,22 @@ export const AssetManager = ({ onFileUploaded }: AssetManagerProps) => {
</p>
</div>
</div>
{fileRejections.length > 0 && (
<div className="flex flex-col gap-2">
{fileRejections.map((fileRejection) => {
const { file } = fileRejection;
return (
<div key={file.name} className="flex flex-col items-center gap-2">
<div>
{fileRejection.errors.map((error) => (
<p key={error.code}>{error.message}</p>
))}
</div>
</div>
);
})}
</div>
)}
<div className="flex flex-col gap-2">
{files.map((file) => (
<div
Expand Down
File renamed without changes.

0 comments on commit 1629346

Please sign in to comment.