| 1 |
import React, { useEffect, useRef, useState } from 'react' |
| 2 |
import { __, sprintf } from '@wordpress/i18n' |
| 3 |
import { useRestAPI } from '../../../../hooks/useRestAPI' |
| 4 |
import { unpackErrorResponse } from '../../../../utils/errors' |
| 5 |
import { REST_BASES } from '../../../../utils/restAPI' |
| 6 |
import { isNetworkAdmin } from '../../../../utils/screen' |
| 7 |
import { Button } from '../../../common/Button' |
| 8 |
import { ImportCard } from '../../common/ImportCard' |
| 9 |
import { useSelection } from '../../../../hooks/useSelection' |
| 10 |
import { SnippetSelectionTable } from './SnippetSelectionTable' |
| 11 |
import type { FormEventHandler, ReactNode} from 'react' |
| 12 |
import type { UseSelection } from '../../../../hooks/useSelection' |
| 13 |
import type { ImportResult } from './ImportResultDisplay' |
| 14 |
import type { ImportableSnippetSchema } from '../../../../types/schema/ImportableSnippetSchema' |
| 15 |
import type { DuplicateAction } from '../SelectFiles/DuplicateActionSelector' |
| 16 |
|
| 17 |
export interface SnippetImportRequest { |
| 18 |
snippets: ImportableSnippetSchema[] |
| 19 |
duplicate_action: 'ignore' | 'replace' | 'skip' |
| 20 |
network?: boolean |
| 21 |
} |
| 22 |
|
| 23 |
export interface SnippetImportResponse { |
| 24 |
imported: number |
| 25 |
imported_ids: number[] |
| 26 |
message: string |
| 27 |
} |
| 28 |
|
| 29 |
interface ReturnLinkProps { |
| 30 |
onCancel: VoidFunction |
| 31 |
clearSelection: VoidFunction |
| 32 |
} |
| 33 |
|
| 34 |
const ReturnLink: React.FC<ReturnLinkProps> = ({ onCancel, clearSelection }) => |
| 35 |
<div className="return-link"> |
| 36 |
<Button link onClick={() => { |
| 37 |
clearSelection() |
| 38 |
onCancel() |
| 39 |
}}> |
| 40 |
{__('← Upload Different Files', 'code-snippets')} |
| 41 |
</Button> |
| 42 |
</div> |
| 43 |
|
| 44 |
interface SelectSnippetsFormProps { |
| 45 |
isImporting: boolean |
| 46 |
availableSnippets: ImportableSnippetSchema[] |
| 47 |
snippetSelection: UseSelection<ImportableSnippetSchema, ImportableSnippetSchema['table_data']['id']> |
| 48 |
} |
| 49 |
|
| 50 |
const SelectSnippetsForm: React.FC<SelectSnippetsFormProps> = ({ availableSnippets, snippetSelection, isImporting }) => { |
| 51 |
const SelectAllButton = () => |
| 52 |
<Button onClick={snippetSelection.selectAll}> |
| 53 |
{snippetSelection.isAllSelected |
| 54 |
? __('Deselect All', 'code-snippets') |
| 55 |
: __('Select All', 'code-snippets')} |
| 56 |
</Button> |
| 57 |
|
| 58 |
const SubmitButton = () => |
| 59 |
<Button type="submit" primary disabled={0 === snippetSelection.selectedItems.size || isImporting}> |
| 60 |
{isImporting |
| 61 |
? __('Importing…', 'code-snippets') |
| 62 |
// translators: %d: number of selected snippets. |
| 63 |
: sprintf(__('Import Selected (%d)', 'code-snippets'), snippetSelection.selectedItems.size)} |
| 64 |
</Button> |
| 65 |
|
| 66 |
return ( |
| 67 |
<> |
| 68 |
<div className="tablenav top"> |
| 69 |
<div> |
| 70 |
<h4>{sprintf( |
| 71 |
// translators: %d: number of available snippets. |
| 72 |
__('Available snippets (%d)', 'code-snippets'), |
| 73 |
availableSnippets.length |
| 74 |
)}</h4> |
| 75 |
<p>{__('Select the snippets you would like to import.', 'code-snippets')}</p> |
| 76 |
</div> |
| 77 |
<div className="table-actions"> |
| 78 |
<SelectAllButton /> |
| 79 |
<SubmitButton /> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
|
| 83 |
<SnippetSelectionTable snippets={availableSnippets} selection={snippetSelection} /> |
| 84 |
|
| 85 |
<div className="tablenav bottom"> |
| 86 |
<SelectAllButton /> |
| 87 |
<SubmitButton /> |
| 88 |
</div> |
| 89 |
</> |
| 90 |
) |
| 91 |
} |
| 92 |
|
| 93 |
interface SubmitFormProps { |
| 94 |
children: ReactNode |
| 95 |
duplicateAction: DuplicateAction |
| 96 |
setImportResult: (result: ImportResult | undefined) => void |
| 97 |
snippetSelection: UseSelection<ImportableSnippetSchema, ImportableSnippetSchema['table_data']['id']> |
| 98 |
setIsImporting: (isImporting: boolean) => void |
| 99 |
} |
| 100 |
|
| 101 |
const SubmitForm: React.FC<SubmitFormProps> = ({ children, duplicateAction, setImportResult, setIsImporting, snippetSelection }) => { |
| 102 |
const { api } = useRestAPI() |
| 103 |
|
| 104 |
const buildRequest = (): SnippetImportRequest | undefined => { |
| 105 |
const snippetsToImport = snippetSelection.getSelectedItems() |
| 106 |
|
| 107 |
if (0 === snippetsToImport.length) { |
| 108 |
alert(__('Please select snippets to import.', 'code-snippets')) |
| 109 |
return undefined |
| 110 |
} |
| 111 |
|
| 112 |
return { |
| 113 |
snippets: snippetsToImport, |
| 114 |
duplicate_action: duplicateAction, |
| 115 |
network: isNetworkAdmin() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
const handleImportSelected: FormEventHandler<HTMLFormElement> = event => { |
| 120 |
event.preventDefault() |
| 121 |
const request = buildRequest() |
| 122 |
|
| 123 |
if (request) { |
| 124 |
setIsImporting(true) |
| 125 |
setImportResult(undefined) |
| 126 |
|
| 127 |
api |
| 128 |
.post<SnippetImportResponse, SnippetImportRequest>(`${REST_BASES.importFiles}/import`, request) |
| 129 |
.then(({ message, imported }) => { |
| 130 |
setImportResult({ step: 'select', success: true, message, imported }) |
| 131 |
}) |
| 132 |
.catch((error: unknown) => { |
| 133 |
console.error('Import error:', error) |
| 134 |
setImportResult({ step: 'select', success: false, message: unpackErrorResponse(error) }) |
| 135 |
}) |
| 136 |
.finally(() => setIsImporting(false)) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return <form onSubmit={handleImportSelected}>{children}</form> |
| 141 |
} |
| 142 |
|
| 143 |
export interface SelectSnippetsStepProps { |
| 144 |
onCancel: VoidFunction |
| 145 |
duplicateAction: DuplicateAction |
| 146 |
setImportResult: (result: ImportResult | undefined) => void |
| 147 |
availableSnippets: ImportableSnippetSchema[] |
| 148 |
} |
| 149 |
|
| 150 |
export const SelectSnippets: React.FC<SelectSnippetsStepProps> = ({ |
| 151 |
onCancel, |
| 152 |
setImportResult, |
| 153 |
duplicateAction, |
| 154 |
availableSnippets |
| 155 |
}) => { |
| 156 |
const snippetSelection = useSelection(availableSnippets, snippet => snippet.table_data.id) |
| 157 |
const [isImporting, setIsImporting] = useState(false) |
| 158 |
|
| 159 |
const selectSectionRef = useRef<HTMLDivElement>(null) |
| 160 |
|
| 161 |
useEffect(() => { |
| 162 |
if (selectSectionRef.current) { |
| 163 |
selectSectionRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' }) |
| 164 |
} |
| 165 |
}, [selectSectionRef]) |
| 166 |
|
| 167 |
return ( |
| 168 |
<ImportCard ref={selectSectionRef} className="import-select-card snippets-table-card"> |
| 169 |
<SubmitForm {...{ duplicateAction, snippetSelection, setImportResult, setIsImporting }}> |
| 170 |
<ReturnLink onCancel={onCancel} clearSelection={snippetSelection.clearSelection} /> |
| 171 |
<SelectSnippetsForm isImporting={isImporting} snippetSelection={snippetSelection} availableSnippets={availableSnippets} /> |
| 172 |
</SubmitForm> |
| 173 |
</ImportCard> |
| 174 |
) |
| 175 |
} |
| 176 |
|