-
Notifications
You must be signed in to change notification settings - Fork 32
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 #3782 from udecode/docs/upload
Docs/upload
- Loading branch information
Showing
8 changed files
with
115 additions
and
121 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
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 was deleted.
Oops, something went wrong.
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,5 +1 @@ | ||
export * from './handle-error'; | ||
|
||
export * from './uploadthing'; | ||
|
||
export * from './use-upload-file'; |
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,6 +1,102 @@ | ||
import * as React from 'react'; | ||
|
||
import type { OurFileRouter } from '@/app/api/uploadthing/core'; | ||
import type { | ||
ClientUploadedFileData, | ||
UploadFilesOptions, | ||
} from 'uploadthing/types'; | ||
|
||
import { generateReactHelpers } from '@uploadthing/react'; | ||
import { isRedirectError } from 'next/dist/client/components/redirect'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
export interface UploadedFile<T = unknown> extends ClientUploadedFileData<T> {} | ||
|
||
interface UseUploadFileProps | ||
extends Pick< | ||
UploadFilesOptions<OurFileRouter, keyof OurFileRouter>, | ||
'headers' | 'onUploadBegin' | 'onUploadProgress' | 'skipPolling' | ||
> { | ||
onUploadComplete?: (file: UploadedFile) => void; | ||
onUploadError?: (error: unknown) => void; | ||
} | ||
|
||
export function useUploadFile( | ||
endpoint: keyof OurFileRouter, | ||
{ onUploadComplete, onUploadError, ...props }: UseUploadFileProps = {} | ||
) { | ||
const [uploadedFile, setUploadedFile] = React.useState<UploadedFile>(); | ||
const [uploadingFile, setUploadingFile] = React.useState<File>(); | ||
const [progress, setProgress] = React.useState<number>(0); | ||
const [isUploading, setIsUploading] = React.useState(false); | ||
|
||
async function uploadThing(file: File) { | ||
setIsUploading(true); | ||
setUploadingFile(file); | ||
|
||
try { | ||
const res = await uploadFiles(endpoint, { | ||
...props, | ||
files: [file], | ||
onUploadProgress: ({ progress }) => { | ||
setProgress(Math.min(progress, 100)); | ||
}, | ||
}); | ||
|
||
setUploadedFile(res[0]); | ||
|
||
onUploadComplete?.(res[0]); | ||
|
||
return uploadedFile; | ||
} catch (error) { | ||
const errorMessage = getErrorMessage(error); | ||
|
||
const message = | ||
errorMessage.length > 0 | ||
? errorMessage | ||
: 'Something went wrong, please try again later.'; | ||
|
||
toast.error(message); | ||
onUploadError?.(error); | ||
} finally { | ||
setProgress(0); | ||
setIsUploading(false); | ||
setUploadingFile(undefined); | ||
} | ||
} | ||
|
||
return { | ||
isUploading, | ||
progress, | ||
uploadFile: uploadThing, | ||
uploadedFile, | ||
uploadingFile, | ||
}; | ||
} | ||
export const { uploadFiles, useUploadThing } = | ||
generateReactHelpers<OurFileRouter>(); | ||
|
||
export function getErrorMessage(err: unknown) { | ||
const unknownError = 'Something went wrong, please try again later.'; | ||
|
||
if (err instanceof z.ZodError) { | ||
const errors = err.issues.map((issue) => { | ||
return issue.message; | ||
}); | ||
|
||
return errors.join('\n'); | ||
} else if (err instanceof Error) { | ||
return err.message; | ||
} else if (isRedirectError(err)) { | ||
throw err; | ||
} else { | ||
return unknownError; | ||
} | ||
} | ||
|
||
export function showErrorToast(err: unknown) { | ||
const errorMessage = getErrorMessage(err); | ||
|
||
return toast.error(errorMessage); | ||
} |
This file was deleted.
Oops, something went wrong.