| 1 |
import React, { useState } from 'react' |
| 2 |
import classnames from 'classnames' |
| 3 |
import { __, sprintf } from '@wordpress/i18n' |
| 4 |
import { WithRestAPIContext } from '../../hooks/useRestAPI' |
| 5 |
import { ScreenMetaSlot } from '../common/ScreenMetaSlot' |
| 6 |
import { SubnavTabs, getTabFromQuery } from '../common/SubnavTabs' |
| 7 |
import { Toolbar } from '../common/Toolbar' |
| 8 |
import { UploadForm } from './UploadForm/UploadForm' |
| 9 |
import { MigrateForm } from './MigrateForm/MigrateForm' |
| 10 |
import type { SubnavTab} from '../common/SubnavTabs' |
| 11 |
import type { ReactNode } from 'react' |
| 12 |
|
| 13 |
const TAB_QUERY_PARAM = 'tab' |
| 14 |
|
| 15 |
interface ImportMenuTab extends SubnavTab<'upload' | 'migrate'> { |
| 16 |
content: ReactNode |
| 17 |
} |
| 18 |
|
| 19 |
const TABS: ImportMenuTab[] = [ |
| 20 |
{ |
| 21 |
name: 'upload', |
| 22 |
label: __('Upload snippets', 'code-snippets'), |
| 23 |
content: <UploadForm /> |
| 24 |
}, |
| 25 |
{ |
| 26 |
name: 'migrate', |
| 27 |
label: __('Migrate from other plugins', 'code-snippets'), |
| 28 |
content: <MigrateForm /> |
| 29 |
} |
| 30 |
] |
| 31 |
|
| 32 |
export const ImportMenu: React.FC = () => { |
| 33 |
const [currentTab, setCurrentTab] = useState<ImportMenuTab>(() => getTabFromQuery(TABS, TAB_QUERY_PARAM)) |
| 34 |
|
| 35 |
return ( |
| 36 |
<> |
| 37 |
<Toolbar /> |
| 38 |
|
| 39 |
<SubnavTabs |
| 40 |
className="import-type-nav" |
| 41 |
ariaLabel={__('Import sources', 'code-snippets')} |
| 42 |
tabs={TABS} |
| 43 |
currentTab={currentTab} |
| 44 |
queryParamName={TAB_QUERY_PARAM} |
| 45 |
setCurrentTab={setCurrentTab} |
| 46 |
/> |
| 47 |
|
| 48 |
<ScreenMetaSlot /> |
| 49 |
|
| 50 |
<div className="snippets-page-header"> |
| 51 |
<h1>{// translators: %s: label of the currently selected import tab. |
| 52 |
sprintf(__('Import: %s', 'code-snippets'), currentTab.label)}</h1> |
| 53 |
</div> |
| 54 |
|
| 55 |
<hr className="wp-header-end" /> |
| 56 |
|
| 57 |
<WithRestAPIContext> |
| 58 |
{TABS.map(tab => |
| 59 |
<div |
| 60 |
key={tab.name} |
| 61 |
className={classnames('import-snippets-section', { 'active-section': tab.name === currentTab.name })} |
| 62 |
> |
| 63 |
{tab.content} |
| 64 |
</div>)} |
| 65 |
</WithRestAPIContext> |
| 66 |
</> |
| 67 |
) |
| 68 |
} |
| 69 |
|