/** * WordPress dependencies */ import { useSelect } from '@safe-wordpress/data'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import clsx from 'clsx'; import type { OptionProps } from 'react-select'; import { store as NSR_DATA } from '@neliosr/data'; import type { Experiment } from '@neliosr/types'; import { useExperimentType } from '@neliosr/utils'; export type TestData = { readonly type: string; readonly value: number; readonly label: string; }; export const TestOption = ( { data: { type, value }, isFocused, isSelected, innerRef, innerProps, }: OptionProps< TestData > ): JSX.Element => { const post = usePost( type, value ) as Experiment | undefined; const { name, type: testType, status } = post || {}; const experimentType = useExperimentType( testType ?? '' ); const Icon = experimentType?.icon ?? ( () => <> ); return (
{ name }
{ `${ experimentType?.title ?? '' } • ${ getStatusLabel( status ?? 'draft' ) }` }
); }; // ===== // HOOKS // ===== const usePost = ( type: string, id: number ) => useSelect( ( select ) => type === 'nab_experiment' ? select( NSR_DATA ).getExperiment( `${ id }` || '0' ) : select( NSR_DATA ).getEntityRecord( type, id ) ); // ======= // HELPERS // ======= function getStatusLabel( status: Experiment[ 'status' ] ): string { switch ( status ) { case 'paused': return _x( 'Paused', 'text (experiment status)', 'nelio-ab-testing' ); case 'finished': return _x( 'Finished', 'text (experiment status)', 'nelio-ab-testing' ); default: return _x( 'Running', 'text (experiment status)', 'nelio-ab-testing' ); } //end switch } //end getStatusLabel()