| 1 |
import React, { useState } from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import classnames from 'classnames' |
| 4 |
import { WithRestAPIContext } from '../../../hooks/useRestAPI' |
| 5 |
import { isLicensed } from '../../../utils/screen' |
| 6 |
import { buildUrl, fetchQueryParam, updateQueryParams } from '../../../utils/urls' |
| 7 |
import { UpsellDialog } from '../../common/UpsellDialog' |
| 8 |
import { WithCloudSearchContext } from './WithCloudSearchContext' |
| 9 |
import { CloudSearch } from './CloudSearch' |
| 10 |
|
| 11 |
const TABS = ['snippets', 'bundles'] as const |
| 12 |
type TabName = typeof TABS[number] |
| 13 |
|
| 14 |
const TAB_LABELS: Record<TabName, string> = { |
| 15 |
snippets: __('Code Snippets', 'code-snippets'), |
| 16 |
bundles: __('Bundles', 'code-snippets') |
| 17 |
} |
| 18 |
|
| 19 |
const PRO_TABS: TabName[] = ['bundles'] |
| 20 |
|
| 21 |
export const CommunityCloud = () => { |
| 22 |
const [currentTab, setCurrentTab] = useState(() => fetchQueryParam('tab') as TabName | null ?? TABS[0]) |
| 23 |
const [isUpsellDialogOpen, setIsUpsellDialogOpen] = useState(false) |
| 24 |
|
| 25 |
return ( |
| 26 |
<> |
| 27 |
<h2>{__('Community Cloud', 'code-snippets')}</h2> |
| 28 |
|
| 29 |
<hr className="wp-header-end" /> |
| 30 |
|
| 31 |
<nav |
| 32 |
className="nav-tab-wrapper community-cloud-tabs" |
| 33 |
aria-label={__('Community Cloud types', 'code-snippets')} |
| 34 |
> |
| 35 |
{TABS.map(tab => |
| 36 |
<a |
| 37 |
key={tab} |
| 38 |
href={buildUrl(window.location.href, { type: tab })} |
| 39 |
className={classnames('nav-tab', `${tab}-tab`, { 'nav-tab-active': tab === currentTab })} |
| 40 |
aria-current={tab === currentTab ? 'page' : undefined} |
| 41 |
onClick={event => { |
| 42 |
event.preventDefault() |
| 43 |
|
| 44 |
if (PRO_TABS.includes(tab) && !isLicensed()) { |
| 45 |
setIsUpsellDialogOpen(true) |
| 46 |
} else { |
| 47 |
updateQueryParams({ tab }) |
| 48 |
setCurrentTab(tab) |
| 49 |
} |
| 50 |
}} |
| 51 |
> |
| 52 |
{TAB_LABELS[tab]} |
| 53 |
{PRO_TABS.includes(tab) && !isLicensed() && <span className="pro-chip">{__('Pro', 'code-snippets')}</span>} |
| 54 |
</a>)} |
| 55 |
</nav> |
| 56 |
|
| 57 |
{'snippets' === currentTab && ( |
| 58 |
<WithRestAPIContext> |
| 59 |
<WithCloudSearchContext> |
| 60 |
<CloudSearch /> |
| 61 |
</WithCloudSearchContext> |
| 62 |
</WithRestAPIContext>)} |
| 63 |
|
| 64 |
<UpsellDialog isOpen={isUpsellDialogOpen} setIsOpen={setIsUpsellDialogOpen} /> |
| 65 |
</> |
| 66 |
) |
| 67 |
} |
| 68 |
|