-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from midday-ai/feature/inbox-batch-upload
Batch upload
- Loading branch information
Showing
16 changed files
with
325 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
## Dashboard | ||
## | ||
## Dashboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
"use client"; | ||
|
||
import { invalidateCacheAction } from "@/actions/invalidate-cache-action"; | ||
import { resumableUpload } from "@/utils/upload"; | ||
import { createClient } from "@midday/supabase/client"; | ||
import { cn } from "@midday/ui/cn"; | ||
import { useToast } from "@midday/ui/use-toast"; | ||
import { nanoid } from "nanoid"; | ||
import { type ReactNode, useEffect, useRef, useState } from "react"; | ||
import { useDropzone } from "react-dropzone"; | ||
|
||
type Props = { | ||
teamId: string; | ||
children: ReactNode; | ||
}; | ||
|
||
export function UploadZone({ children, teamId }: Props) { | ||
const supabase = createClient(); | ||
const [progress, setProgress] = useState(0); | ||
const [showProgress, setShowProgress] = useState(false); | ||
const [toastId, setToastId] = useState(null); | ||
const uploadProgress = useRef([]); | ||
const { toast, dismiss, update } = useToast(); | ||
|
||
useEffect(() => { | ||
if (!toastId && showProgress) { | ||
const { id } = toast({ | ||
title: `Uploading ${uploadProgress.current.length} files`, | ||
progress, | ||
variant: "progress", | ||
description: "Please do not close browser until completed", | ||
duration: Infinity, | ||
}); | ||
|
||
setToastId(id); | ||
} else { | ||
update(toastId, { | ||
progress, | ||
title: `Uploading ${uploadProgress.current.length} files`, | ||
}); | ||
} | ||
}, [showProgress, progress, toastId]); | ||
|
||
const onDrop = async (files) => { | ||
// NOTE: If onDropRejected | ||
if (!files.length) { | ||
return; | ||
} | ||
|
||
// Set default progress | ||
uploadProgress.current = files.map(() => 0); | ||
|
||
setShowProgress(true); | ||
|
||
// Add uploaded folder so we can filter background job on this | ||
const filePath = [teamId, nanoid(), "inbox", "uploaded"]; | ||
|
||
try { | ||
await Promise.all( | ||
files.map(async (file, idx) => | ||
resumableUpload(supabase, { | ||
bucket: "vault", | ||
path: filePath, | ||
file, | ||
onProgress: (bytesUploaded, bytesTotal) => { | ||
uploadProgress.current[idx] = (bytesUploaded / bytesTotal) * 100; | ||
|
||
const _progress = uploadProgress.current.reduce( | ||
(acc, currentValue) => { | ||
return acc + currentValue; | ||
}, | ||
0 | ||
); | ||
|
||
setProgress(Math.round(_progress / files.length)); | ||
}, | ||
}) | ||
) | ||
); | ||
|
||
// Reset once done | ||
uploadProgress.current = []; | ||
|
||
setProgress(0); | ||
toast({ | ||
title: "Upload successful.", | ||
variant: "success", | ||
duration: 2000, | ||
}); | ||
|
||
setShowProgress(false); | ||
setToastId(null); | ||
dismiss(toastId); | ||
invalidateCacheAction([`vault_${teamId}`]); | ||
} catch { | ||
toast({ | ||
duration: 2500, | ||
variant: "error", | ||
title: "Something went wrong please try again.", | ||
}); | ||
} | ||
}; | ||
|
||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ | ||
onDrop, | ||
onDropRejected: ([reject]) => { | ||
if (reject?.errors.find(({ code }) => code === "file-too-large")) { | ||
toast({ | ||
duration: 2500, | ||
variant: "error", | ||
title: "File size to large.", | ||
}); | ||
} | ||
|
||
if (reject?.errors.find(({ code }) => code === "file-invalid-type")) { | ||
toast({ | ||
duration: 2500, | ||
variant: "error", | ||
title: "File type not supported.", | ||
}); | ||
} | ||
}, | ||
maxSize: 3000000, // 3MB | ||
maxFiles: 10, | ||
accept: { | ||
"image/png": [".png"], | ||
"image/jpeg": [".jpg", ".jpeg"], | ||
"application/pdf": [".pdf"], | ||
}, | ||
}); | ||
|
||
return ( | ||
<div | ||
{...getRootProps({ onClick: (evt) => evt.stopPropagation() })} | ||
className="relative h-full" | ||
> | ||
<div className="absolute top-0 bottom-0 right-0 left-0 z-[51] pointer-events-none"> | ||
<div | ||
className={cn( | ||
"bg-background dark:bg-[#1A1A1A] h-full flex items-center justify-center text-center invisible", | ||
isDragActive && "visible" | ||
)} | ||
> | ||
<input {...getInputProps()} id="upload-files" /> | ||
<p className="text-xs"> | ||
Drop your receipts here. <br /> | ||
Maximum of 10 files at a time. | ||
</p> | ||
</div> | ||
</div> | ||
|
||
{children} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./match"; | ||
export * from "./document"; | ||
export * from "./upload"; |
Oops, something went wrong.