({
limitMb,
acceptedTypes,
onChange,
}: UseFileInputProps)
| 9 | } |
| 10 | |
| 11 | export const useFileInput = ({ |
| 12 | limitMb, |
| 13 | acceptedTypes, |
| 14 | onChange, |
| 15 | }: UseFileInputProps) => { |
| 16 | const { displayToast, dismissToast } = useToastNotification(); |
| 17 | const onFileChange = async (file?: File | null) => { |
| 18 | if (!file) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (file.size > limitMb * MEGABYTE) { |
| 23 | displayToast(`Maximum image size is ${limitMb} MB`); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | if (acceptedTypes && !acceptedTypes.includes(file.type)) { |
| 28 | displayToast(`File type is not allowed`); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const base64 = await blobToBase64(file); |
| 33 | dismissToast(); |
| 34 | onChange(base64, file); |
| 35 | }; |
| 36 | |
| 37 | return { onFileChange }; |
| 38 | }; |
no test coverage detected