Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug (UI): Prevent user form uploading duplicate files #212

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions ui/src/components/features/build-panel/components/files-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Plus, SquarePlus } from "lucide-react";
import MultiSelect from "@/components/ui/multiselect";
import { useFiles, useUploadFile } from "@/data-provider/query-service";
import Spinner from "@/components/ui/spinner";
import { useEffect, useState } from "react";
import { ChangeEvent, useEffect, useState } from "react";
import {
FormControl,
FormField,
Expand Down Expand Up @@ -96,6 +96,26 @@ export default function FilesDialog({ form, classNames }: TFilesDialog) {
}
};

const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.files) {
const selectedFiles = Array.from(event.target.files);
const duplicateFiles = selectedFiles.filter((file) =>
files?.some(
(uploadedFile) =>
uploadedFile.filename === file.name &&
uploadedFile.bytes === file.size,
),
);
if (duplicateFiles.length > 0) {
return toast({
variant: "destructive",
title: "Cannot add duplicate files.",
});
}
setFileUpload(event.target.files[0]);
}
};

if (isLoading) return <Spinner />;

return (
Expand Down Expand Up @@ -148,9 +168,7 @@ export default function FilesDialog({ form, classNames }: TFilesDialog) {
type="file"
accept="image/*, application/pdf"
onChange={(event) => {
if (event.target.files) {
setFileUpload(event.target.files[0]);
}
handleFileChange(event);
}}
/>
</div>
Expand Down
Loading