import React, { useRef, useState } from 'react' import { __ } from '@wordpress/i18n' import { createInterpolateElement } from '@wordpress/element' import { ImportResultDisplay } from './SelectSnippets/ImportResultDisplay' import { SelectSnippets } from './SelectSnippets/SelectSnippets' import { SelectFiles } from './SelectFiles/SelectFiles' import { DuplicateActionSelector } from './SelectFiles/DuplicateActionSelector' import type { UploadedFile } from './SelectFiles/UploadButton' import type { ImportableSnippetSchema } from '../../../types/schema/ImportableSnippetSchema' import type { ImportResult } from './SelectSnippets/ImportResultDisplay' import type { DuplicateAction } from './SelectFiles/DuplicateActionSelector' type Step = 'upload' | 'select' export const UploadForm: React.FC = () => { const [duplicateAction, setDuplicateAction] = useState('ignore') const [currentStep, setCurrentStep] = useState('upload') const [importResult, setImportResult] = useState() const fileInputRef = useRef(null) const [selectedFiles, setSelectedFiles] = useState() const [availableSnippets, setAvailableSnippets] = useState([]) return ( <>

{__('Upload one or more Code Snippets export files and the snippets will be imported.', 'code-snippets')}

{createInterpolateElement( __('Afterward, you will need to visit the All Snippets page to activate the imported snippets.', 'code-snippets'), { // eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/control-has-associated-label a: } )}

{'upload' === currentStep && !importResult?.success && ( <> { setAvailableSnippets(uploadedSnippets) setCurrentStep('select') }} /> )} {'select' === currentStep && 0 < availableSnippets.length && !importResult?.success && ( { setSelectedFiles(undefined) if (fileInputRef.current) { fileInputRef.current.value = '' } setAvailableSnippets([]) setImportResult(undefined) setCurrentStep('upload') }} />)} {importResult && } ) }