| 1 |
import React, { useState } from 'react' |
| 2 |
import { __, sprintf } from '@wordpress/i18n' |
| 3 |
import { WithRestAPIContext } from '../../../hooks/useRestAPI' |
| 4 |
import { useSnippetView } from '../../../hooks/useSnippetView' |
| 5 |
import { isLicensed } from '../../../utils/screen' |
| 6 |
import { updateQueryParams } from '../../../utils/urls' |
| 7 |
import { ScreenMetaSlot } from '../../common/ScreenMetaSlot' |
| 8 |
import { SubnavTabs, getTabFromQuery } from '../../common/SubnavTabs' |
| 9 |
import { WithCloudSnippetDownloadsContext } from '../../common/cloud/WithCloudSnippetDownloadsContext' |
| 10 |
import { UpsellPage } from '../../common/UpsellDialog' |
| 11 |
import { WithCloudSearchContext, useCloudSearch } from './WithCloudSearchContext' |
| 12 |
import { CloudSearch } from './CloudSearch' |
| 13 |
import type { SubnavTab } from '../../common/SubnavTabs' |
| 14 |
|
| 15 |
const TAB_QUERY_PARAM = 'tab' |
| 16 |
|
| 17 |
const TABS: SubnavTab<'snippets' | 'bundles'>[] = [ |
| 18 |
{ |
| 19 |
name: 'snippets', |
| 20 |
label: __('Snippets', 'code-snippets') |
| 21 |
}, |
| 22 |
{ |
| 23 |
name: 'bundles', |
| 24 |
label: __('Bundles', 'code-snippets'), |
| 25 |
pro: true |
| 26 |
} |
| 27 |
] |
| 28 |
|
| 29 |
const CommunityCloudInner = () => { |
| 30 |
const [currentTab, setCurrentTab] = useState(() => getTabFromQuery(TABS, TAB_QUERY_PARAM)) |
| 31 |
const { snippetView, setSnippetView } = useSnippetView() |
| 32 |
const { searchResults } = useCloudSearch() |
| 33 |
|
| 34 |
return ( |
| 35 |
<> |
| 36 |
<SubnavTabs |
| 37 |
className="community-cloud-nav" |
| 38 |
ariaLabel={__('Community Cloud types', 'code-snippets')} |
| 39 |
tabs={TABS} |
| 40 |
getTabCount={tab => 'snippets' === tab.name ? searchResults?.totalItems : undefined} |
| 41 |
currentTab={currentTab} |
| 42 |
setCurrentTab={tab => { |
| 43 |
updateQueryParams({ [TAB_QUERY_PARAM]: tab.name }) |
| 44 |
setCurrentTab(tab) |
| 45 |
}} |
| 46 |
/> |
| 47 |
|
| 48 |
<ScreenMetaSlot hidden={'bundles' === currentTab.name} /> |
| 49 |
|
| 50 |
{(!currentTab.pro || isLicensed()) && ( |
| 51 |
<> |
| 52 |
<div className="snippets-page-header"> |
| 53 |
<h1>{ |
| 54 |
// translators: %s: label of the currently selected community cloud tab. |
| 55 |
sprintf(__('Community Cloud: %s', 'code-snippets'), currentTab.label)}</h1> |
| 56 |
</div> |
| 57 |
|
| 58 |
<hr className="wp-header-end" /> |
| 59 |
|
| 60 |
<p className="snippets-page-description"> |
| 61 |
{__('Search the community cloud and download user-shared snippets directly to your site.', 'code-snippets')} |
| 62 |
</p> |
| 63 |
</>)} |
| 64 |
|
| 65 |
{(() => { |
| 66 |
switch (currentTab.name) { |
| 67 |
case 'snippets': |
| 68 |
return <CloudSearch snippetView={snippetView} setSnippetView={setSnippetView} /> |
| 69 |
|
| 70 |
case 'bundles': |
| 71 |
return <UpsellPage /> |
| 72 |
} |
| 73 |
})()} |
| 74 |
</> |
| 75 |
) |
| 76 |
} |
| 77 |
|
| 78 |
export const CommunityCloud = () => |
| 79 |
<WithRestAPIContext> |
| 80 |
<WithCloudSnippetDownloadsContext> |
| 81 |
<WithCloudSearchContext> |
| 82 |
<CommunityCloudInner /> |
| 83 |
</WithCloudSearchContext> |
| 84 |
</WithCloudSnippetDownloadsContext> |
| 85 |
</WithRestAPIContext> |
| 86 |
|