| 1 |
import React, { useRef, useState } from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { createInterpolateElement } from '@wordpress/element' |
| 4 |
import { ImportResultDisplay } from './SelectSnippets/ImportResultDisplay' |
| 5 |
import { SelectSnippets } from './SelectSnippets/SelectSnippets' |
| 6 |
import { SelectFiles } from './SelectFiles/SelectFiles' |
| 7 |
import { DuplicateActionSelector } from './SelectFiles/DuplicateActionSelector' |
| 8 |
import type { UploadedFile } from './SelectFiles/UploadButton' |
| 9 |
import type { ImportableSnippetSchema } from '../../../types/schema/ImportableSnippetSchema' |
| 10 |
import type { ImportResult } from './SelectSnippets/ImportResultDisplay' |
| 11 |
import type { DuplicateAction } from './SelectFiles/DuplicateActionSelector' |
| 12 |
|
| 13 |
type Step = 'upload' | 'select' |
| 14 |
|
| 15 |
export const UploadForm: React.FC = () => { |
| 16 |
const [duplicateAction, setDuplicateAction] = useState<DuplicateAction>('ignore') |
| 17 |
const [currentStep, setCurrentStep] = useState<Step>('upload') |
| 18 |
const [importResult, setImportResult] = useState<ImportResult>() |
| 19 |
|
| 20 |
const fileInputRef = useRef<HTMLInputElement>(null) |
| 21 |
const [selectedFiles, setSelectedFiles] = useState<UploadedFile[]>() |
| 22 |
|
| 23 |
const [availableSnippets, setAvailableSnippets] = useState<ImportableSnippetSchema[]>([]) |
| 24 |
|
| 25 |
return ( |
| 26 |
<> |
| 27 |
<p>{__('Upload one or more Code Snippets export files and the snippets will be imported.', 'code-snippets')}</p> |
| 28 |
|
| 29 |
<p> |
| 30 |
{createInterpolateElement( |
| 31 |
__('Afterward, you will need to visit the <a>All Snippets</a> page to activate the imported snippets.', 'code-snippets'), |
| 32 |
{ |
| 33 |
// eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/control-has-associated-label |
| 34 |
a: <a href={window.CODE_SNIPPETS?.urls.manage} /> |
| 35 |
} |
| 36 |
)} |
| 37 |
</p> |
| 38 |
|
| 39 |
{'upload' === currentStep && !importResult?.success && ( |
| 40 |
<> |
| 41 |
<DuplicateActionSelector value={duplicateAction} onChange={setDuplicateAction} /> |
| 42 |
|
| 43 |
<SelectFiles |
| 44 |
{...{ fileInputRef, selectedFiles, setSelectedFiles, setImportResult }} |
| 45 |
onSuccess={uploadedSnippets => { |
| 46 |
setAvailableSnippets(uploadedSnippets) |
| 47 |
setCurrentStep('select') |
| 48 |
}} |
| 49 |
/> |
| 50 |
</>)} |
| 51 |
|
| 52 |
{'select' === currentStep && 0 < availableSnippets.length && !importResult?.success && ( |
| 53 |
<SelectSnippets |
| 54 |
duplicateAction={duplicateAction} |
| 55 |
setImportResult={setImportResult} |
| 56 |
availableSnippets={availableSnippets} |
| 57 |
onCancel={() => { |
| 58 |
setSelectedFiles(undefined) |
| 59 |
|
| 60 |
if (fileInputRef.current) { |
| 61 |
fileInputRef.current.value = '' |
| 62 |
} |
| 63 |
|
| 64 |
setAvailableSnippets([]) |
| 65 |
setImportResult(undefined) |
| 66 |
setCurrentStep('upload') |
| 67 |
}} |
| 68 |
/>)} |
| 69 |
|
| 70 |
{importResult && <ImportResultDisplay {...importResult} />} |
| 71 |
</> |
| 72 |
) |
| 73 |
} |
| 74 |
|