PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / ImportMenu / UploadForm / UploadForm.tsx

UploadForm.tsx in Code Snippets 3.10.2, at js/components/ImportMenu/UploadForm/UploadForm.tsx

74 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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