import React, { useEffect, useState } from 'react';
import * as WpPluginsLib from '@wordpress/plugins';
import { PluginSidebar } from '@wordpress/edit-post';
import { PanelBody, Icon } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import UISidebarSelectControl from '../UIComponents/UISidebarSelectControl';
import SidebarSprocketIcon from '../Common/SidebarSprocketIcon';
import styled from 'styled-components';
import { __ } from '@wordpress/i18n';
import { BackgroudAppContext } from '../../iframe/useBackgroundApp';
import { getOrCreateBackgroundApp } from '../../utils/backgroundAppUtils';
import { isFullSiteEditor } from '../../utils/withMetaData';
import { isRefreshTokenAvailable } from '../../utils/isRefreshTokenAvailable';
import { fetchAccessToken } from '../../api/wordpressApiClient';
export function registerHubspotSidebar() {
const ContentTypeLabelStyle = styled.div`
white-space: normal;
text-transform: none;
`;
const ContentTypeLabel = (
{__(
'Select the content type HubSpot Analytics uses to track this page',
'leadin'
)}
);
const LeadinPluginSidebar = ({ postType }: { postType: string }) => {
const [embedder, setEmbedder] = useState(null);
useEffect(() => {
if (isRefreshTokenAvailable()) {
fetchAccessToken()
.then(
({
accessToken,
expiresIn,
}: {
accessToken: string;
expiresIn: number;
}) => {
setEmbedder(getOrCreateBackgroundApp(accessToken, expiresIn));
}
)
.catch(() => {});
}
}, []);
return postType && !isFullSiteEditor() ? (
}
>
) : null;
};
const LeadinPluginSidebarWrapper = withSelect((select: Function) => {
const data = select('core/editor');
return {
postType:
data &&
data.getCurrentPostType() &&
data.getEditedPostAttribute('meta'),
};
})(LeadinPluginSidebar);
if (WpPluginsLib) {
WpPluginsLib.registerPlugin('leadin', {
render: LeadinPluginSidebarWrapper,
icon: SidebarSprocketIcon,
});
}
}