/**
* WordPress dependencies
*/
import {
CheckboxControl,
Dashicon,
ExternalLink,
TextControl,
} from '@safe-wordpress/components';
import { useDispatch, useSelect } from '@safe-wordpress/data';
import { createInterpolateElement } from '@safe-wordpress/element';
import { _x, sprintf } from '@safe-wordpress/i18n';
/**
* External dependencies
*/
import { ErrorText } from '@nab/components';
import { getLocalUrlError } from '@nab/utils';
import { store as NAB_DATA } from '@nab/data';
import { store as NAB_EDITOR } from '@nab/editor';
import type { EntityId, EntityKindName, Url } from '@nab/types';
/**
* Internal dependencies
*/
import './style.scss';
import { PostSearcher } from './post-searcher';
export const TrackedPage = (): JSX.Element => {
const [ attributes, setAttributes ] = useAttributes();
const { mode, postId, postType, url, urlError } = attributes;
return (
{ createInterpolateElement(
sprintf(
/* translators: %s: Dashicon. */
_x( '%s WordPress Page', 'text', 'nelio-ab-testing' ),
''
),
{
icon: (
),
}
) }
{ 'url' !== mode ? (
<>
{ _x(
'Select the page you want to generate a heatmap for:',
'user',
'nelio-ab-testing'
) }
>
) : (
<>
{ _x(
'Type in the URL of the page you want to generate a heatmap for:',
'user',
'nelio-ab-testing'
) }
setAttributes( { url: value } )
}
placeholder={ _x(
'URL…',
'text',
'nelio-ab-testing'
) }
/>
{ urlError ? (
{ _x(
'View',
'command',
'nelio-ab-testing'
) }
) : (
{ _x(
'View',
'command',
'nelio-ab-testing'
) }
) }
{ !! urlError && (
) }
>
) }
setAttributes( { mode: checked ? 'url' : 'post' } )
}
/>
);
};
// =====
// HOOKS
// =====
const useAttributes = () => {
const attributes = useSelect( ( select ) => {
const { getPluginSetting } = select( NAB_DATA );
const { getHeatmapAttribute } = select( NAB_EDITOR );
const url = getHeatmapAttribute( 'trackedUrl' ) || '';
const homeUrl = getPluginSetting( 'homeUrl' );
return {
mode: getHeatmapAttribute( 'trackingMode' ) || 'post',
postId: getHeatmapAttribute( 'trackedPostId' ) || undefined,
postType: getHeatmapAttribute( 'trackedPostType' ) || 'page',
url,
urlError: getLocalUrlError( url, homeUrl ),
};
}, [] );
type Attributes = {
readonly mode: 'post' | 'url';
readonly postId: EntityId;
readonly postType: EntityKindName;
readonly url: string;
};
const { setHeatmapData } = useDispatch( NAB_EDITOR );
const setAttributes = ( attrs: Partial< Attributes > ): void =>
void setHeatmapData( {
trackingMode: attrs.mode ?? attributes.mode,
trackedPostId: attrs.postId ?? attributes.postId,
trackedPostType: attrs.postType ?? attributes.postType,
trackedUrl: ( attrs.url ?? attributes.url ) as Url,
} );
return [ attributes, setAttributes ] as const;
};