| 1 | "use client"; |
| 2 | |
| 3 | import { PlaceholderPlugin, UploadErrorCode } from "@platejs/media/react"; |
| 4 | import { usePluginOption } from "platejs/react"; |
| 5 | import * as React from "react"; |
| 6 | import { toast } from "sonner"; |
| 7 | |
| 8 | export function MediaUploadToast() { |
| 9 | useUploadErrorToast(); |
| 10 | |
| 11 | return null; |
| 12 | } |
| 13 | |
| 14 | const useUploadErrorToast = () => { |
| 15 | const uploadError = usePluginOption(PlaceholderPlugin, "error"); |
| 16 | |
| 17 | React.useEffect(() => { |
| 18 | if (!uploadError) return; |
| 19 | |
| 20 | const { code, data } = uploadError; |
| 21 | |
| 22 | switch (code) { |
| 23 | case UploadErrorCode.INVALID_FILE_SIZE: { |
| 24 | toast.error( |
| 25 | `The size of files ${data.files |
| 26 | .map((f) => f.name) |
| 27 | .join(", ")} is invalid`, |
| 28 | ); |
| 29 | |
| 30 | break; |
| 31 | } |
| 32 | case UploadErrorCode.INVALID_FILE_TYPE: { |
| 33 | toast.error( |
| 34 | `The type of files ${data.files |
| 35 | .map((f) => f.name) |
| 36 | .join(", ")} is invalid`, |
| 37 | ); |
| 38 | |
| 39 | break; |
| 40 | } |
| 41 | case UploadErrorCode.TOO_LARGE: { |
| 42 | toast.error( |
| 43 | `The size of files ${data.files |
| 44 | .map((f) => f.name) |
| 45 | .join(", ")} is too large than ${data.maxFileSize}`, |
| 46 | ); |
| 47 | |
| 48 | break; |
| 49 | } |
| 50 | case UploadErrorCode.TOO_LESS_FILES: { |
| 51 | toast.error( |
| 52 | `The mini um number of files is ${data.minFileCount} for ${data.fileType}`, |
| 53 | ); |
| 54 | |
| 55 | break; |
| 56 | } |
| 57 | case UploadErrorCode.TOO_MANY_FILES: { |
| 58 | toast.error( |
| 59 | `The maximum number of files is ${data.maxFileCount} ${ |
| 60 | data.fileType ? `for ${data.fileType}` : "" |
| 61 | }`, |
| 62 | ); |
| 63 | |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | }, [uploadError]); |
| 68 | }; |
| 69 |