-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
239 additions
and
67 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,41 +1,60 @@ | ||
"use client" | ||
|
||
import * as React from "react" | ||
import type { UploadedFile } from "@/types" | ||
import { generateReactHelpers } from "@uploadthing/react/hooks" | ||
import Image from "next/image" | ||
|
||
import { useFileUpload } from "@/hooks/use-file-upload" | ||
import { | ||
Card, | ||
CardContent, | ||
CardDescription, | ||
CardHeader, | ||
CardTitle, | ||
} from "@/components/ui/card" | ||
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area" | ||
import { FileUploader } from "@/components/file-uploader" | ||
import { type OurFileRouter } from "@/app/api/uploadthing/core" | ||
|
||
const { useUploadThing } = generateReactHelpers<OurFileRouter>() | ||
|
||
export function BasicUploaderDemo() { | ||
const [uploadedFiles, setUploadedFiles] = React.useState< | ||
UploadedFile[] | null | ||
>([]) | ||
const [progress, setProgress] = React.useState(0) | ||
const { startUpload, isUploading } = useUploadThing("imageUploader", { | ||
onUploadProgress: setProgress, | ||
}) | ||
|
||
console.log({ uploadedFiles, progress }) | ||
const { uploadFiles, progresses, uploadedFiles, isUploading } = useFileUpload( | ||
"imageUploader", | ||
{ defaultUploadedFiles: [] } | ||
) | ||
|
||
return ( | ||
<FileUploader | ||
maxFiles={4} | ||
maxSize={1 * 1024 * 1024} | ||
onUpload={async (files) => { | ||
const uploadedFiles = await startUpload(files).then((res) => { | ||
const formattedImages = res?.map((image) => ({ | ||
id: image.key, | ||
name: image.key.split("_")[1] ?? image.key, | ||
url: image.url, | ||
})) | ||
return formattedImages ?? null | ||
}) | ||
setUploadedFiles(uploadedFiles) | ||
}} | ||
disabled={isUploading} | ||
/> | ||
<div className="flex flex-col space-y-6"> | ||
<FileUploader | ||
maxFiles={4} | ||
maxSize={4 * 1024 * 1024} | ||
progresses={progresses} | ||
onUpload={uploadFiles} | ||
disabled={isUploading} | ||
/> | ||
<Card> | ||
<CardHeader> | ||
<CardTitle>Uploaded files</CardTitle> | ||
<CardDescription>View the uploaded files here</CardDescription> | ||
</CardHeader> | ||
<CardContent> | ||
<ScrollArea className="py-4"> | ||
<div className="flex w-max space-x-2.5"> | ||
{Array.from({ length: 11 }).map(() => | ||
uploadedFiles?.slice(0, 1).map((file) => ( | ||
<div key={file.key} className="relative aspect-video w-64"> | ||
<Image | ||
src={file.url} | ||
alt={file.name} | ||
fill | ||
sizes="(min-width: 640px) 640px, 100vw" | ||
loading="lazy" | ||
className="rounded-md object-cover" | ||
/> | ||
</div> | ||
)) | ||
)} | ||
</div> | ||
<ScrollBar orientation="horizontal" /> | ||
</ScrollArea> | ||
</CardContent> | ||
</Card> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from "react" | ||
|
||
export function useDebounce<T>(value: T, delay?: number): T { | ||
const [debouncedValue, setDebouncedValue] = React.useState<T>(value) | ||
|
||
React.useEffect(() => { | ||
const timer = setTimeout(() => setDebouncedValue(value), delay ?? 500) | ||
|
||
return () => { | ||
clearTimeout(timer) | ||
} | ||
}, [value, delay]) | ||
|
||
return debouncedValue | ||
} |
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,61 @@ | ||
import * as React from "react" | ||
import type { UploadedFile } from "@/types" | ||
import { toast } from "sonner" | ||
import type { UploadFilesOptions } from "uploadthing/types" | ||
|
||
import { getErrorMessage } from "@/lib/handle-error" | ||
import { uploadFiles } from "@/lib/uploadthing" | ||
import { type OurFileRouter } from "@/app/api/uploadthing/core" | ||
|
||
interface UseFileUploadProps | ||
extends Pick< | ||
UploadFilesOptions<OurFileRouter, keyof OurFileRouter>, | ||
"headers" | "onUploadBegin" | "onUploadProgress" | "skipPolling" | ||
> { | ||
defaultUploadedFiles?: UploadedFile[] | ||
} | ||
|
||
export function useFileUpload( | ||
endpoint: keyof OurFileRouter, | ||
{ defaultUploadedFiles = [], ...props }: UseFileUploadProps = {} | ||
) { | ||
const [uploadedFiles, setUploadedFiles] = React.useState< | ||
UploadedFile[] | null | ||
>(defaultUploadedFiles) | ||
const [progresses, setProgresses] = React.useState<Record<string, number>>({}) | ||
const [isUploading, setIsUploading] = React.useState(false) | ||
|
||
async function uploadThings(files: File[]) { | ||
setIsUploading(true) | ||
try { | ||
const res = await uploadFiles(endpoint, { | ||
...props, | ||
files, | ||
onUploadProgress: ({ file, progress }) => { | ||
setProgresses((prev) => { | ||
return { | ||
...prev, | ||
[file]: progress, | ||
} | ||
}) | ||
}, | ||
}) | ||
|
||
setUploadedFiles((prev) => { | ||
return prev ? [...prev, ...res] : res | ||
}) | ||
} catch (err) { | ||
toast.error(getErrorMessage(err)) | ||
} finally { | ||
setProgresses({}) | ||
setIsUploading(false) | ||
} | ||
} | ||
|
||
return { | ||
uploadedFiles, | ||
progresses, | ||
uploadFiles: uploadThings, | ||
isUploading, | ||
} | ||
} |
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,27 @@ | ||
import * as React from "react" | ||
import { type UseUploadthingProps as UseUploadFileProps } from "@uploadthing/react" | ||
|
||
import { useUploadThing as useUploadFile } from "@/lib/uploadthing" | ||
import { type OurFileRouter } from "@/app/api/uploadthing/core" | ||
|
||
interface UseUploadthingProps | ||
extends UseUploadFileProps<OurFileRouter, keyof OurFileRouter> {} | ||
|
||
export function useUploadThing( | ||
endpoint: keyof OurFileRouter, | ||
props: UseUploadthingProps = {} | ||
) { | ||
const [progress, setProgress] = React.useState(0) | ||
const { startUpload, isUploading } = useUploadFile(endpoint, { | ||
onUploadProgress: () => { | ||
setProgress(progress) | ||
}, | ||
...props, | ||
}) | ||
|
||
return { | ||
startUpload, | ||
isUploading, | ||
progress, | ||
} | ||
} |
Oops, something went wrong.