|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import React from 'react'; |
| 17 | + |
| 18 | +import { FileContent } from '../types'; |
| 19 | +import { isSupportedFileType, readFileAsText } from '../utils/attachment-utils'; |
| 20 | + |
| 21 | +type UploadError = { type?: 'info' | 'danger'; message: string | null }; |
| 22 | +interface FileAttachmentContextType { |
| 23 | + showAlert: boolean; |
| 24 | + uploadError: UploadError; |
| 25 | + fileContents: FileContent[]; |
| 26 | + isLoadingFile: Record<string, boolean>; |
| 27 | + handleFileUpload: (files: File[]) => void; |
| 28 | + setFileContents: React.Dispatch<React.SetStateAction<FileContent[]>>; |
| 29 | + setUploadError: React.Dispatch<React.SetStateAction<UploadError>>; |
| 30 | + currentFileContent?: FileContent; |
| 31 | + setCurrentFileContent: React.Dispatch< |
| 32 | + React.SetStateAction<FileContent | undefined> |
| 33 | + >; |
| 34 | + modalState: { |
| 35 | + previewModalKey: number; |
| 36 | + setPreviewModalKey: React.Dispatch<React.SetStateAction<number>>; |
| 37 | + isPreviewModalOpen: boolean; |
| 38 | + setIsPreviewModalOpen: React.Dispatch<React.SetStateAction<boolean>>; |
| 39 | + isEditModalOpen: boolean; |
| 40 | + setIsEditModalOpen: React.Dispatch<React.SetStateAction<boolean>>; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +export const FileAttachmentContext = |
| 45 | + React.createContext<FileAttachmentContextType | null>(null); |
| 46 | + |
| 47 | +const FileAttachmentContextProvider: React.FC<{ |
| 48 | + children: React.ReactNode; |
| 49 | +}> = ({ children }) => { |
| 50 | + const [currentFileContent, setCurrentFileContent] = React.useState< |
| 51 | + FileContent | undefined |
| 52 | + >(); |
| 53 | + const [isLoadingFile, setIsLoadingFile] = React.useState< |
| 54 | + Record<string, boolean> |
| 55 | + >({}); |
| 56 | + const [previewModalKey, setPreviewModalKey] = React.useState<number>(0); |
| 57 | + const [showAlert, setShowAlert] = React.useState<boolean>(false); |
| 58 | + const [uploadError, setUploadError] = React.useState<UploadError>({ |
| 59 | + message: null, |
| 60 | + }); |
| 61 | + const [fileContents, setFileContents] = React.useState<FileContent[]>([]); |
| 62 | + const [isPreviewModalOpen, setIsPreviewModalOpen] = |
| 63 | + React.useState<boolean>(false); |
| 64 | + const [isEditModalOpen, setIsEditModalOpen] = React.useState<boolean>(false); |
| 65 | + const handleFileUpload = (fileArr: File[]) => { |
| 66 | + const existingFile = fileContents.find( |
| 67 | + file => file.name === fileArr[0].name, |
| 68 | + ); |
| 69 | + if (existingFile) { |
| 70 | + setShowAlert(true); |
| 71 | + setUploadError({ type: 'info', message: 'File already exists.' }); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + setIsLoadingFile({ [fileArr[0].name]: true }); |
| 76 | + |
| 77 | + if (fileArr.length > 1) { |
| 78 | + setShowAlert(true); |
| 79 | + setUploadError({ |
| 80 | + message: 'Uploaded more than one file.', |
| 81 | + }); |
| 82 | + return; |
| 83 | + } |
| 84 | + if (!isSupportedFileType(fileArr[0])) { |
| 85 | + setShowAlert(true); |
| 86 | + setUploadError({ |
| 87 | + message: |
| 88 | + 'Unsupported file type. Supported types are: .txt, .yaml, .json.', |
| 89 | + }); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // this is 25MB in bytes; size is in bytes |
| 94 | + if (fileArr[0].size > 25000000) { |
| 95 | + setShowAlert(true); |
| 96 | + setUploadError({ |
| 97 | + message: |
| 98 | + 'Your file size is too large. Please ensure that your file is less than 25 MB.', |
| 99 | + }); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + readFileAsText(fileArr[0]) |
| 104 | + .then(data => { |
| 105 | + setFileContents(prev => [ |
| 106 | + ...prev, |
| 107 | + { |
| 108 | + name: fileArr[0].name, |
| 109 | + type: fileArr[0].type, |
| 110 | + content: data as string, |
| 111 | + }, |
| 112 | + ]); |
| 113 | + setShowAlert(false); |
| 114 | + setUploadError({ message: null }); |
| 115 | + setIsLoadingFile({ [fileArr[0].name]: false }); |
| 116 | + }) |
| 117 | + .catch((error: DOMException) => { |
| 118 | + setUploadError({ |
| 119 | + message: `Failed to read file: ${error.message}`, |
| 120 | + }); |
| 121 | + }); |
| 122 | + }; |
| 123 | + |
| 124 | + const modalState = { |
| 125 | + isPreviewModalOpen, |
| 126 | + setIsPreviewModalOpen, |
| 127 | + isEditModalOpen, |
| 128 | + setIsEditModalOpen, |
| 129 | + previewModalKey, |
| 130 | + setPreviewModalKey, |
| 131 | + }; |
| 132 | + return ( |
| 133 | + <FileAttachmentContext.Provider |
| 134 | + value={{ |
| 135 | + modalState, |
| 136 | + fileContents, |
| 137 | + setFileContents, |
| 138 | + handleFileUpload, |
| 139 | + isLoadingFile, |
| 140 | + showAlert, |
| 141 | + uploadError, |
| 142 | + setUploadError, |
| 143 | + currentFileContent, |
| 144 | + setCurrentFileContent, |
| 145 | + }} |
| 146 | + > |
| 147 | + {children} |
| 148 | + </FileAttachmentContext.Provider> |
| 149 | + ); |
| 150 | +}; |
| 151 | + |
| 152 | +export default FileAttachmentContextProvider; |
| 153 | + |
| 154 | +export const useFileAttachmentContext = (): FileAttachmentContextType => { |
| 155 | + const context = React.useContext<FileAttachmentContextType | null>( |
| 156 | + FileAttachmentContext, |
| 157 | + ); |
| 158 | + |
| 159 | + if (context === null) { |
| 160 | + throw new Error( |
| 161 | + 'useFileAttachmentContext must be within a FileAttachmentContextProvider', |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + return context; |
| 166 | +}; |
0 commit comments