| 1 |
import React, { useState } from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { ImportCard } from '../../common/ImportCard' |
| 4 |
import { DragDropUploadArea } from './DragDropUploadArea' |
| 5 |
import { SelectedFilesList } from './SelectedFilesList' |
| 6 |
import { UploadButton } from './UploadButton' |
| 7 |
import type { UploadedFile } from './UploadButton' |
| 8 |
import type { ImportResult } from '../SelectSnippets/ImportResultDisplay' |
| 9 |
import type { ImportableSnippetSchema } from '../../../../types/schema/ImportableSnippetSchema' |
| 10 |
import type { Dispatch, RefObject, SetStateAction } from 'react' |
| 11 |
|
| 12 |
const processFileList = (fileList: FileList): UploadedFile[] => { |
| 13 |
const uuids = new Uint32Array(fileList.length) |
| 14 |
window.crypto.getRandomValues(uuids) |
| 15 |
|
| 16 |
return Array.from(uuids).map((uuid, index) => ({ |
| 17 |
id: uuid.toString(), |
| 18 |
name: fileList[index].name, |
| 19 |
file: fileList[index] |
| 20 |
})) |
| 21 |
} |
| 22 |
|
| 23 |
const buildFileList = (files: UploadedFile[]): FileList => { |
| 24 |
const dataTransfer = new DataTransfer() |
| 25 |
files.forEach(newFile => dataTransfer.items.add(newFile.file)) |
| 26 |
return dataTransfer.files |
| 27 |
} |
| 28 |
|
| 29 |
export interface SelectFilesProps { |
| 30 |
onSuccess: (snippets: ImportableSnippetSchema[]) => void |
| 31 |
fileInputRef: RefObject<HTMLInputElement> |
| 32 |
selectedFiles: UploadedFile[] | undefined |
| 33 |
setImportResult: (result: ImportResult | undefined) => void |
| 34 |
setSelectedFiles: Dispatch<SetStateAction<UploadedFile[] | undefined>> |
| 35 |
} |
| 36 |
|
| 37 |
export const SelectFiles: React.FC<SelectFilesProps> = ({ |
| 38 |
onSuccess, |
| 39 |
fileInputRef, |
| 40 |
selectedFiles, |
| 41 |
setImportResult, |
| 42 |
setSelectedFiles |
| 43 |
}) => { |
| 44 |
const [isUploading, setIsUploading] = useState(false) |
| 45 |
|
| 46 |
const handleRemoveFile = (file: UploadedFile) => { |
| 47 |
setSelectedFiles(previous => { |
| 48 |
const newFiles = previous?.filter(fileItem => fileItem.id !== file.id) |
| 49 |
|
| 50 |
if (newFiles && fileInputRef.current) { |
| 51 |
fileInputRef.current.files = buildFileList(newFiles) |
| 52 |
} |
| 53 |
|
| 54 |
return newFiles |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
return ( |
| 59 |
<ImportCard className="import-upload-card"> |
| 60 |
<h2>{__('Choose files', 'code-snippets')}</h2> |
| 61 |
<p className="description"> |
| 62 |
{__('Choose one or more Code Snippets (.xml or .json) files to parse and preview.', 'code-snippets')} |
| 63 |
</p> |
| 64 |
|
| 65 |
<DragDropUploadArea |
| 66 |
fileInputRef={fileInputRef} |
| 67 |
disabled={isUploading} |
| 68 |
onFileSelect={fileList => { |
| 69 |
setSelectedFiles(fileList && processFileList(fileList)) |
| 70 |
setImportResult(undefined) |
| 71 |
}} |
| 72 |
/> |
| 73 |
|
| 74 |
{selectedFiles && 0 < selectedFiles.length && ( |
| 75 |
<SelectedFilesList |
| 76 |
files={selectedFiles} |
| 77 |
onRemoveFile={handleRemoveFile} |
| 78 |
/>)} |
| 79 |
|
| 80 |
<footer> |
| 81 |
<UploadButton {...{ isUploading, setIsUploading, onSuccess, selectedFiles, setImportResult }} /> |
| 82 |
</footer> |
| 83 |
</ImportCard> |
| 84 |
) |
| 85 |
} |
| 86 |
|