Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: support upload of mutliple files

Brad Barbin3e6c4e1c4fdd
diff --git a/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityDocuments.tsx b/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityDocuments.tsx
index 39e192ea76..d8b420b68b 100644
--- a/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityDocuments.tsx
+++ b/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityDocuments.tsx
@@ -25,7 +25,13 @@ import { Outlet, useRevalidator, useSubmit } from "@remix-run/react";
import type { FileObject } from "@supabase/storage-js";
import type { ChangeEvent } from "react";
import { useCallback } from "react";
-import { LuRadioTower, LuShoppingCart, LuUpload } from "react-icons/lu";
+import { useDropzone } from "react-dropzone";
+import {
+ LuRadioTower,
+ LuShoppingCart,
+ LuUpload,
+ LuUploadCloud,
+} from "react-icons/lu";
import { MdMoreVert } from "react-icons/md";
import { DocumentPreview, Hyperlink } from "~/components";
import { usePermissions, useUser } from "~/hooks";
@@ -47,13 +53,22 @@ const OpportunityDocuments = ({
id,
type,
}: OpportunityDocumentsProps) => {
- const { canDelete, download, deleteAttachment, getPath } =
+ const { canDelete, download, deleteAttachment, getPath, upload } =
useOpportunityDocuments({
opportunityId: opportunity.id,
id,
type,
});
+ const onDrop = useCallback(
+ (acceptedFiles: File[]) => {
+ upload(acceptedFiles);
+ },
+ [upload]
+ );
+
+ const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
+
return (
<>
<Card>
@@ -162,6 +177,18 @@ const OpportunityDocuments = ({
)}
</Tbody>
</Table>
+ <div
+ {...getRootProps()}
+ className={`mt-4 border-2 border-dashed rounded-md p-6 text-center ${
+ isDragActive ? "border-primary bg-primary/10" : "border-muted"
+ }`}
+ >
+ <input {...getInputProps()} />
+ <LuUploadCloud className="mx-auto h-12 w-12 text-muted-foreground" />
+ <p className="mt-2 text-sm text-muted-foreground">
+ Drag and drop some files here, or click to select files
+ </p>
+ </div>
</CardContent>
</Card>
<Outlet />
@@ -264,30 +291,32 @@ export const useOpportunityDocuments = ({
);
const upload = useCallback(
- async (file: File) => {
+ async (files: File[]) => {
if (!supabase) {
toast.error("Supabase client not available");
return;
}
- const fileName = getPath(file);
- const fileUpload = await supabase.storage
- .from("private")
- .upload(fileName, file, {
- cacheControl: `${12 * 60 * 60}`,
- upsert: true,
- });
+ for (const file of files) {
+ const fileName = getPath(file);
- if (fileUpload.error) {
- toast.error("Failed to upload file");
- }
- if (fileUpload.data?.path) {
- toast.success("File uploaded");
- createDocumentRecord({
- path: fileUpload.data.path,
- name: file.name,
- size: file.size,
- });
+ const fileUpload = await supabase.storage
+ .from("private")
+ .upload(fileName, file, {
+ cacheControl: `${12 * 60 * 60}`,
+ upsert: true,
+ });
+
+ if (fileUpload.error) {
+ toast.error(`Failed to upload file: ${file.name}`);
+ } else if (fileUpload.data?.path) {
+ toast.success(`File uploaded: ${file.name}`);
+ createDocumentRecord({
+ path: fileUpload.data.path,
+ name: file.name,
+ size: file.size,
+ });
+ }
}
},
[getPath, createDocumentRecord, supabase]
@@ -309,10 +338,9 @@ const OpportunityDocumentForm = (props: OpportunityDocumentFormProps) => {
const { upload } = useOpportunityDocuments(props);
- const uploadFile = async (e: ChangeEvent<HTMLInputElement>) => {
+ const uploadFiles = async (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files && supabase && company) {
- const file = e.target.files[0];
- upload(file);
+ upload(Array.from(e.target.files));
}
};
@@ -320,7 +348,8 @@ const OpportunityDocumentForm = (props: OpportunityDocumentFormProps) => {
<File
isDisabled={!permissions.can("update", "sales")}
leftIcon={<LuUpload />}
- onChange={uploadFile}
+ onChange={uploadFiles}
+ multiple
>
New
</File>
diff --git a/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityLineDocuments.tsx b/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityLineDocuments.tsx
index db4bdc6f98..ec4833deda 100644
--- a/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityLineDocuments.tsx
+++ b/apps/carbon/app/modules/sales/ui/OpportunityDocuments/OpportunityLineDocuments.tsx
@@ -21,7 +21,7 @@ import {
} from "@carbon/react";
import { convertKbToString } from "@carbon/utils";
import type { FileObject } from "@supabase/storage-js";
-import { LuAxis3D, LuUpload } from "react-icons/lu";
+import { LuAxis3D, LuUpload, LuUploadCloud } from "react-icons/lu";
import { MdMoreVert } from "react-icons/md";
import { DocumentPreview, Hyperlink } from "~/components";
import { DocumentIcon, getDocumentType } from "~/modules/documents";
@@ -34,6 +34,7 @@ import { useSupabase } from "~/lib/supabase";
import { path } from "~/utils/path";
import { useCallback } from "react";
+import { useDropzone } from "react-dropzone";
const useOpportunityLineDocuments = ({
id,
@@ -159,30 +160,32 @@ const useOpportunityLineDocuments = ({
);
const upload = useCallback(
- async (file: File) => {
+ async (files: File[]) => {
if (!supabase) {
toast.error("Supabase client not available");
return;
}
- const fileName = getPath(file);
- const fileUpload = await supabase.storage
- .from("private")
- .upload(fileName, file, {
- cacheControl: `${12 * 60 * 60}`,
- upsert: true,
- });
+ for (const file of files) {
+ const fileName = getPath(file);
- if (fileUpload.error) {
- toast.error("Failed to upload file");
- }
- if (fileUpload.data?.path) {
- toast.success("File uploaded");
- createDocumentRecord({
- path: fileUpload.data.path,
- name: file.name,
- size: file.size,
- });
+ const fileUpload = await supabase.storage
+ .from("private")
+ .upload(fileName, file, {
+ cacheControl: `${12 * 60 * 60}`,
+ upsert: true,
+ });
+
+ if (fileUpload.error) {
+ toast.error(`Failed to upload file: ${file.name}`);
+ } else if (fileUpload.data?.path) {
+ toast.success(`File uploaded: ${file.name}`);
+ createDocumentRecord({
+ path: fileUpload.data.path,
+ name: file.name,
+ size: file.size,
+ });
+ }
}
},
[getPath, createDocumentRecord, supabase]
@@ -215,12 +218,28 @@ const OpportunityLineDocuments = ({
modelUpload,
type,
}: OpportunityLineDocumentsProps) => {
- const { canDelete, download, deleteFile, deleteModel, getPath, viewModel } =
- useOpportunityLineDocuments({
- id,
- lineId,
- type,
- });
+ const {
+ canDelete,
+ download,
+ deleteFile,
+ deleteModel,
+ getPath,
+ viewModel,
+ upload,
+ } = useOpportunityLineDocuments({
+ id,
+ lineId,
+ type,
+ });
+
+ const onDrop = useCallback(
+ (acceptedFiles: File[]) => {
+ upload(acceptedFiles);
+ },
+ [upload]
+ );
+
+ const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
return (
<Card className="flex-grow">
@@ -359,6 +378,18 @@ const OpportunityLineDocuments = ({
)}
</Tbody>
</Table>
+ <div
+ {...getRootProps()}
+ className={`mt-4 border-2 border-dashed rounded-md p-6 text-center ${
+ isDragActive ? "border-primary bg-primary/10" : "border-muted"
+ }`}
+ >
+ <input {...getInputProps()} />
+ <LuUploadCloud className="mx-auto h-12 w-12 text-muted-foreground" />
+ <p className="mt-2 text-sm text-muted-foreground">
+ Drag and drop some files here, or click to select files
+ </p>
+ </div>
</CardContent>
</Card>
);
@@ -380,10 +411,9 @@ const OpportunityLineDocumentForm = ({
const permissions = usePermissions();
const { upload } = useOpportunityLineDocuments({ id, lineId, type });
- const uploadFile = async (e: ChangeEvent<HTMLInputElement>) => {
+ const uploadFiles = async (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
- const file = e.target.files[0];
- upload(file);
+ upload(Array.from(e.target.files));
}
};
@@ -391,7 +421,8 @@ const OpportunityLineDocumentForm = ({
<File
isDisabled={!permissions.can("update", "sales")}
leftIcon={<LuUpload />}
- onChange={uploadFile}
+ onChange={uploadFiles}
+ multiple
>
New
</File>
diff --git a/packages/react/src/File.tsx b/packages/react/src/File.tsx
index 227dffd854..1eea0e899d 100644
--- a/packages/react/src/File.tsx
+++ b/packages/react/src/File.tsx
@@ -5,6 +5,7 @@ import { Button } from "./Button";
type FileProps = Omit<ButtonProps, "onChange"> & {
accept?: string;
+ multiple?: boolean;
onChange: (e: ChangeEvent<HTMLInputElement>) => Promise<void>;
};
@@ -12,6 +13,7 @@ const File = ({
accept,
className,
children,
+ multiple = false,
onChange,
...props
}: FileProps) => {
@@ -23,6 +25,7 @@ const File = ({
ref={fileInputRef}
type="file"
hidden
+ multiple={multiple}
accept={accept}
onChange={onChange}
/>