PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 / ImportMenu.tsx

ImportMenu.tsx in Code Snippets 4.0.0-beta.1, at js/components/ImportMenu/ImportMenu.tsx

72 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useState } from 'react'
2 import classnames from 'classnames'
3 import { __ } from '@wordpress/i18n'
4 import { WithRestAPIContext } from '../../hooks/useRestAPI'
5 import { fetchQueryParam, updateQueryParams } from '../../utils/urls'
6 import { Toolbar } from '../common/Toolbar'
7 import { UploadForm } from './UploadForm/UploadForm'
8 import { MigrateForm } from './MigrateForm/MigrateForm'
9 import type { ReactNode } from 'react'
10
11 const TABS = ['upload', 'migrate'] as const
12
13 type TabType = typeof TABS[number]
14
15 const TAB_CONTENT: Record<TabType, ReactNode> = {
16 upload: <UploadForm />,
17 migrate: <MigrateForm />
18 }
19
20 const TAB_LABELS: Record<TabType, string> = {
21 upload: __('Upload snippets', 'code-snippets'),
22 migrate: __('Migrate from other plugins', 'code-snippets')
23 }
24
25 const isValidTab = (value: string): value is TabType =>
26 TABS.includes(value as TabType)
27
28 const getDefaultTab = (): TabType => {
29 const tabParam = fetchQueryParam('tab')
30 return tabParam && isValidTab(tabParam) ? tabParam : TABS[0]
31 }
32
33 export const ImportMenu: React.FC = () => {
34 const [activeTab, setActiveTab] = useState<TabType>(getDefaultTab)
35
36 return (
37 <>
38 <Toolbar />
39 <>
40 <h2>{__('Import Snippets', 'code-snippets')}</h2>
41
42 <hr className="wp-header-end" />
43
44 <nav
45 className="nav-tab-wrapper"
46 aria-label={__('Import sources', 'code-snippets')}
47 >
48 {TABS.map(tab =>
49 <button
50 key={tab}
51 type="button"
52 className={classnames('nav-tab', { 'nav-tab-active': tab === activeTab })}
53 onClick={() => {
54 setActiveTab(tab)
55 updateQueryParams({ tab })
56 }}
57 >
58 {TAB_LABELS[tab]}
59 </button>)}
60 </nav>
61
62 <WithRestAPIContext>
63 {TABS.map(tab =>
64 <div key={tab} className={classnames('import-snippets-section', { 'active-section': tab === activeTab })}>
65 {TAB_CONTENT[tab]}
66 </div>)}
67 </WithRestAPIContext>
68 </>
69 </>
70 )
71 }
72