/**
* WordPress dependencies
*/
import { Button, Dashicon, Spinner } from '@safe-wordpress/components';
import { useDispatch } from '@safe-wordpress/data';
import { _x, sprintf } from '@safe-wordpress/i18n';
/**
* External dependencies
*/
import { store as NAB_DATA, useAdminUrl, usePageAttribute } from '@nab/data';
/**
* Internal dependencies
*/
import {
useActiveExperiment,
useProgress,
useRawResultsStatus,
useResultsStopper,
} from '../hooks';
import { VariantSwitcher } from '../variant-switcher';
export const Header = (): JSX.Element => {
const isHeatmapExperiment = useIsHeatmapExperiment();
const isRunning = useIsRunning();
const [ status ] = useRawResultsStatus();
const dataStatus = status.mode;
const [ progress ] = useProgress();
const [ isStopping, stopResultLoading ] = useResultsStopper();
const { stopExperiment } = useDispatch( NAB_DATA );
if (
'loading' === dataStatus ||
'error' === dataStatus ||
'missing' === dataStatus
) {
return (
{ ! isHeatmapExperiment && (
) }
);
}
if ( 'still-loading' === dataStatus || 'canceling' === dataStatus ) {
return (
{ !! progress ? (
{ sprintf(
/* translators: %d: Percentage. */
_x(
'Fetching more data… (%d%%)',
'text',
'nelio-ab-testing'
),
Math.min( progress, 99 )
) }
) : (
) }
{ 'canceling' !== dataStatus && (
) }
);
}
return (
{ isHeatmapExperiment && isRunning && (
) }
{ ! isHeatmapExperiment && (
) }
);
};
const BackButton = () => {
const [ isPublicView ] = usePageAttribute( 'results/isPublicView', false );
const backUrl = useBackUrl();
if ( isPublicView ) {
return null;
}
return (
);
};
// =====
// HOOKS
// =====
const useBackUrl = () => {
const experimentsUrl = useAdminUrl( 'edit.php', {
post_type: 'nab_experiment',
} );
const experiment = useActiveExperiment();
if ( ! experiment ) {
return;
}
return experiment.type === 'nab/heatmap'
? experimentsUrl
: experiment.links.edit;
};
const useIsHeatmapExperiment = () =>
useActiveExperiment()?.type === 'nab/heatmap';
const useIsRunning = () => useActiveExperiment()?.status === 'running';