| 1 |
import React from 'react'; |
| 2 |
import * as WpPluginsLib from '@wordpress/plugins'; |
| 3 |
import { PluginSidebar } from '@wordpress/edit-post'; |
| 4 |
import { PanelBody, Icon } from '@wordpress/components'; |
| 5 |
import { withSelect } from '@wordpress/data'; |
| 6 |
import UISidebarSelectControl from '../UIComponents/UISidebarSelectControl'; |
| 7 |
import SidebarSprocketIcon from '../Common/SidebarSprocketIcon'; |
| 8 |
import styled from 'styled-components'; |
| 9 |
import { __ } from '@wordpress/i18n'; |
| 10 |
import { BackgroudAppContext } from '../../iframe/useBackgroundApp'; |
| 11 |
import { refreshToken } from '../../constants/leadinConfig'; |
| 12 |
import { getOrCreateBackgroundApp } from '../../utils/backgroundAppUtils'; |
| 13 |
|
| 14 |
export function registerHubspotSidebar() { |
| 15 |
const ContentTypeLabelStyle = styled.div` |
| 16 |
white-space: normal; |
| 17 |
text-transform: none; |
| 18 |
`; |
| 19 |
|
| 20 |
const ContentTypeLabel = ( |
| 21 |
<ContentTypeLabelStyle> |
| 22 |
{__( |
| 23 |
'Select the content type HubSpot Analytics uses to track this page', |
| 24 |
'leadin' |
| 25 |
)} |
| 26 |
</ContentTypeLabelStyle> |
| 27 |
); |
| 28 |
|
| 29 |
const LeadinPluginSidebar = ({ postType }: { postType: string }) => |
| 30 |
postType ? ( |
| 31 |
<PluginSidebar |
| 32 |
name="leadin" |
| 33 |
title="HubSpot" |
| 34 |
icon={ |
| 35 |
<Icon |
| 36 |
className="hs-plugin-sidebar-sprocket" |
| 37 |
icon={SidebarSprocketIcon()} |
| 38 |
/> |
| 39 |
} |
| 40 |
> |
| 41 |
<PanelBody title={__('HubSpot Analytics', 'leadin')} initialOpen={true}> |
| 42 |
<BackgroudAppContext.Provider |
| 43 |
value={refreshToken && getOrCreateBackgroundApp(refreshToken)} |
| 44 |
> |
| 45 |
<UISidebarSelectControl |
| 46 |
metaKey="content-type" |
| 47 |
className="select-content-type" |
| 48 |
label={ContentTypeLabel} |
| 49 |
options={[ |
| 50 |
{ label: __('Detect Automatically', 'leadin'), value: '' }, |
| 51 |
{ label: __('Blog Post', 'leadin'), value: 'blog-post' }, |
| 52 |
{ |
| 53 |
label: __('Knowledge Article', 'leadin'), |
| 54 |
value: 'knowledge-article', |
| 55 |
}, |
| 56 |
{ label: __('Landing Page', 'leadin'), value: 'landing-page' }, |
| 57 |
{ label: __('Listing Page', 'leadin'), value: 'listing-page' }, |
| 58 |
{ |
| 59 |
label: __('Standard Page', 'leadin'), |
| 60 |
value: 'standard-page', |
| 61 |
}, |
| 62 |
]} |
| 63 |
/> |
| 64 |
</BackgroudAppContext.Provider> |
| 65 |
</PanelBody> |
| 66 |
</PluginSidebar> |
| 67 |
) : null; |
| 68 |
const LeadinPluginSidebarWrapper = withSelect((select: Function) => { |
| 69 |
const data = select('core/editor'); |
| 70 |
return { |
| 71 |
postType: |
| 72 |
data && |
| 73 |
data.getCurrentPostType() && |
| 74 |
data.getEditedPostAttribute('meta'), |
| 75 |
}; |
| 76 |
})(LeadinPluginSidebar); |
| 77 |
if (WpPluginsLib) { |
| 78 |
WpPluginsLib.registerPlugin('leadin', { |
| 79 |
render: LeadinPluginSidebarWrapper, |
| 80 |
icon: SidebarSprocketIcon, |
| 81 |
}); |
| 82 |
} |
| 83 |
} |
| 84 |
|