import React, { useEffect, useRef, useState } from 'react' import { __, sprintf } from '@wordpress/i18n' import { useRestAPI } from '../../../../hooks/useRestAPI' import { unpackErrorResponse } from '../../../../utils/errors' import { REST_BASES } from '../../../../utils/restAPI' import { isNetworkAdmin } from '../../../../utils/screen' import { Button } from '../../../common/Button' import { ImportCard } from '../../common/ImportCard' import { useSelection } from '../../../../hooks/useSelection' import { SnippetSelectionTable } from './SnippetSelectionTable' import type { FormEventHandler, ReactNode } from 'react' import type { UseSelection } from '../../../../hooks/useSelection' import type { ImportResult } from './ImportResultDisplay' import type { ImportableSnippetSchema } from '../../../../types/schema/ImportableSnippetSchema' import type { DuplicateAction } from '../SelectFiles/DuplicateActionSelector' export interface SnippetImportRequest { snippets: ImportableSnippetSchema[] duplicate_action: 'ignore' | 'replace' | 'skip' network?: boolean } export interface SnippetImportResponse { imported: number imported_ids: number[] message: string } interface ReturnLinkProps { onCancel: VoidFunction clearSelection: VoidFunction } const ReturnLink: React.FC = ({ onCancel, clearSelection }) =>
interface SelectAllButtonProps { selectAll: VoidFunction isAllSelected: boolean } const SelectAllButton: React.FC = ({ selectAll, isAllSelected }) => interface SubmitButtonProps { isImporting: boolean selectedCount: number } const SubmitButton: React.FC = ({ isImporting, selectedCount }) => interface SelectSnippetsFormProps { isImporting: boolean availableSnippets: ImportableSnippetSchema[] snippetSelection: UseSelection } const SelectSnippetsForm: React.FC = ({ availableSnippets, snippetSelection, isImporting }) => <>

{// translators: %d: number of available snippets. sprintf(__('Available snippets (%d)', 'code-snippets'), availableSnippets.length)}

{__('Select the snippets you would like to import.', 'code-snippets')}

interface SubmitFormProps { children: ReactNode duplicateAction: DuplicateAction setImportResult: (result: ImportResult | undefined) => void snippetSelection: UseSelection setIsImporting: (isImporting: boolean) => void } const SubmitForm: React.FC = ({ children, duplicateAction, setImportResult, setIsImporting, snippetSelection }) => { const { api } = useRestAPI() const buildRequest = (): SnippetImportRequest | undefined => { const snippetsToImport = snippetSelection.getSelectedItems() if (0 === snippetsToImport.length) { alert(__('Please select snippets to import.', 'code-snippets')) return undefined } return { snippets: snippetsToImport, duplicate_action: duplicateAction, network: isNetworkAdmin() } } const handleImportSelected: FormEventHandler = event => { event.preventDefault() const request = buildRequest() if (request) { setIsImporting(true) setImportResult(undefined) api .post(`${REST_BASES.importFiles}/import`, request) .then(({ message, imported }) => { setImportResult({ step: 'select', success: true, message, imported }) }) .catch((error: unknown) => { console.error('Import error:', error) setImportResult({ step: 'select', success: false, message: unpackErrorResponse(error) }) }) .finally(() => setIsImporting(false)) } } return
{children}
} export interface SelectSnippetsStepProps { onCancel: VoidFunction duplicateAction: DuplicateAction setImportResult: (result: ImportResult | undefined) => void availableSnippets: ImportableSnippetSchema[] } export const SelectSnippets: React.FC = ({ onCancel, setImportResult, duplicateAction, availableSnippets }) => { const snippetSelection = useSelection(availableSnippets, snippet => snippet.table_data.id) const [isImporting, setIsImporting] = useState(false) const selectSectionRef = useRef(null) useEffect(() => { if (selectSectionRef.current) { selectSectionRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' }) } }, [selectSectionRef]) return ( ) }