| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import classnames from 'classnames' |
| 3 |
import React, { useState } from 'react' |
| 4 |
import { isLicensed, shouldShowUpsell } from '../../utils/screen' |
| 5 |
import { buildUrl, fetchQueryParam } from '../../utils/urls' |
| 6 |
import { CommunityIcon, LibraryIcon, SettingsIcon, SnippetsIcon } from './icons/ToolbarIcons' |
| 7 |
import { UpsellDialog } from './UpsellDialog' |
| 8 |
import type { ReactNode } from 'react' |
| 9 |
|
| 10 |
interface NavLink { |
| 11 |
name: string |
| 12 |
url?: string |
| 13 |
label: string |
| 14 |
external?: boolean |
| 15 |
icon?: ReactNode |
| 16 |
pro?: boolean |
| 17 |
pageSlug?: string |
| 18 |
subpage?: string |
| 19 |
} |
| 20 |
|
| 21 |
const UPPER_NAV_LINKS: NavLink[] = [ |
| 22 |
{ |
| 23 |
name: 'docs', |
| 24 |
url: 'https://codesnippets.pro/docs', |
| 25 |
label: __('Docs', 'code-snippets'), |
| 26 |
external: true |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: 'cloud', |
| 30 |
url: 'https://codesnippets.cloud/', |
| 31 |
label: __('Cloud Dashboard', 'code-snippets'), |
| 32 |
external: true |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: 'welcome', |
| 36 |
url: window.CODE_SNIPPETS?.urls.welcome, |
| 37 |
label: __("What's New", 'code-snippets'), |
| 38 |
pageSlug: 'code-snippets-welcome' |
| 39 |
} |
| 40 |
] |
| 41 |
|
| 42 |
const LOWER_NAV_LINKS: NavLink[] = [ |
| 43 |
{ |
| 44 |
name: 'snippets', |
| 45 |
url: window.CODE_SNIPPETS?.urls.manage, |
| 46 |
label: __('Snippets', 'code-snippets'), |
| 47 |
icon: <SnippetsIcon aria-hidden="true" />, |
| 48 |
pageSlug: 'snippets' |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: 'cloud-community', |
| 52 |
label: __('Community Cloud', 'code-snippets'), |
| 53 |
icon: <CommunityIcon aria-hidden="true" />, |
| 54 |
subpage: 'cloud-community' |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: 'cloud-library', |
| 58 |
label: __('My Library', 'code-snippets'), |
| 59 |
icon: <LibraryIcon aria-hidden="true" />, |
| 60 |
pro: true, |
| 61 |
subpage: 'cloud-library' |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: 'settings', |
| 65 |
url: window.CODE_SNIPPETS?.urls.settings, |
| 66 |
label: __('Settings', 'code-snippets'), |
| 67 |
icon: <SettingsIcon aria-hidden="true" />, |
| 68 |
pageSlug: 'snippets-settings' |
| 69 |
} |
| 70 |
] |
| 71 |
|
| 72 |
interface NavProps { |
| 73 |
setIsUpsellDialogOpen: (isOpen: boolean) => void |
| 74 |
} |
| 75 |
|
| 76 |
const UpperNav: React.FC<NavProps> = ({ setIsUpsellDialogOpen }) => |
| 77 |
<div className="code-snippets-toolbar-upper"> |
| 78 |
<div className="logo"> |
| 79 |
<img |
| 80 |
src={`${window.CODE_SNIPPETS?.urls.plugin}/assets/icon.svg`} |
| 81 |
alt={__('Code Snippets logo', 'code-snippets')} |
| 82 |
aria-hidden="true" |
| 83 |
/> |
| 84 |
|
| 85 |
<h1>{__('Code Snippets', 'code-snippets')}</h1> |
| 86 |
</div> |
| 87 |
|
| 88 |
<nav aria-label={__('Main links', 'code-snippets')}> |
| 89 |
<ul> |
| 90 |
{UPPER_NAV_LINKS.map(link => |
| 91 |
<li key={link.name}> |
| 92 |
<a |
| 93 |
href={link.url} |
| 94 |
className={classnames(`${link.name}-link`, { 'active-link': isActiveLink(link) })} |
| 95 |
{...link.external && { target: '_blank', rel: 'noopener noreferrer' }} |
| 96 |
> |
| 97 |
{link.label} |
| 98 |
</a> |
| 99 |
</li> |
| 100 |
)} |
| 101 |
{shouldShowUpsell() |
| 102 |
? <li> |
| 103 |
<a |
| 104 |
className="button button-large button-secondary" |
| 105 |
href="https://codesnippets.pro/pricing/" |
| 106 |
target="_blank" rel="noopener noreferrer" |
| 107 |
onClick={event => { |
| 108 |
event.preventDefault() |
| 109 |
setIsUpsellDialogOpen(true) |
| 110 |
}} |
| 111 |
> |
| 112 |
{__('Upgrade to Pro', 'code-snippets')} |
| 113 |
</a> |
| 114 |
</li> |
| 115 |
: null} |
| 116 |
</ul> |
| 117 |
</nav> |
| 118 |
</div> |
| 119 |
|
| 120 |
const currentPage = fetchQueryParam('page') |
| 121 |
const currentSubpage = fetchQueryParam('subpage') |
| 122 |
|
| 123 |
const isActiveLink = ({ pageSlug, subpage }: NavLink): boolean => { |
| 124 |
if (subpage) { |
| 125 |
return currentSubpage === subpage |
| 126 |
} |
| 127 |
if (pageSlug) { |
| 128 |
return !currentSubpage && currentPage === pageSlug |
| 129 |
} |
| 130 |
return false |
| 131 |
} |
| 132 |
|
| 133 |
const LowerNav: React.FC<NavProps> = ({ setIsUpsellDialogOpen }) => |
| 134 |
<div className="code-snippets-toolbar-lower"> |
| 135 |
<nav aria-label={__('Main features', 'code-snippets')}> |
| 136 |
<ul> |
| 137 |
{LOWER_NAV_LINKS.map(link => |
| 138 |
<li key={link.name}> |
| 139 |
<a |
| 140 |
href={link.url ?? buildUrl(window.CODE_SNIPPETS?.urls.manage, { subpage: link.name })} |
| 141 |
className={classnames(`${link.name}-link`, { 'active-link': isActiveLink(link) })} |
| 142 |
onClick={event => { |
| 143 |
if (link.pro && !isLicensed()) { |
| 144 |
event.preventDefault() |
| 145 |
setIsUpsellDialogOpen(true) |
| 146 |
} |
| 147 |
}} |
| 148 |
> |
| 149 |
{link.icon} |
| 150 |
<span>{link.label}</span> |
| 151 |
{link.pro && !isLicensed() && <span className="pro-chip">{__('Pro', 'code-snippets')}</span>} |
| 152 |
</a> |
| 153 |
</li>)} |
| 154 |
</ul> |
| 155 |
</nav> |
| 156 |
</div> |
| 157 |
|
| 158 |
export const Toolbar = () => { |
| 159 |
const [isUpsellDialogOpen, setIsUpsellDialogOpen] = useState(false) |
| 160 |
|
| 161 |
return ( |
| 162 |
<div className="code-snippets-toolbar"> |
| 163 |
<UpperNav setIsUpsellDialogOpen={setIsUpsellDialogOpen} /> |
| 164 |
<LowerNav setIsUpsellDialogOpen={setIsUpsellDialogOpen} /> |
| 165 |
<UpsellDialog isOpen={isUpsellDialogOpen} setIsOpen={setIsUpsellDialogOpen} /> |
| 166 |
</div> |
| 167 |
) |
| 168 |
} |
| 169 |
|