import {
useSelect,
withSelect,
} from '@wordpress/data';
import { __, sprintf, _n } from '@wordpress/i18n';
import { store as audiencesStore } from '../../audiences/data';
import { Audience, Pagination, Post } from '../../audiences/types';
import { Button } from '@/components/ui/button';
/**
* Compact horizontal estimate readout for the audience picker rows.
* The full DynamicEstimate is designed for the edit screen (220Ă—60
* sparkline, vertical stack) and breaks the row layout here.
*/
function InlineEstimate( { audience }: { audience: Audience } ) {
const estimate = useSelect( select => select( audiencesStore ).getEstimate( audience ), [ audience ] );
if ( ! estimate || estimate.isLoading ) {
return (
{ __( 'Estimating…', 'altis' ) }
);
}
const percent = estimate.total ? Math.round( ( estimate.count / estimate.total ) * 100 ) : 0;
const count = estimate.count ?? 0;
return (
{ percent }
%
{ sprintf(
/* translators: %d: count of unique visitors */
_n( '%d unique / 7d', '%d uniques / 7d', count, 'altis' ),
count
) }
);
}
// Selection mode types
export type SelectionMode = 'all' | 'audience' | 'custom';
interface OwnProps {
selectedMode: SelectionMode,
selectedAudienceId?: number,
onSelectAllTraffic(): void,
onSelectAudience( audience: Audience, id: number, title: string ): void,
onSelectCustomFilter(): void,
}
interface Props {
canCreate: boolean,
loading: boolean,
pagination: Pagination,
posts: Post[],
onGetPosts: any,
}
function FilterAudiencePicker( props: Props & OwnProps ) {
const {
selectedMode,
selectedAudienceId,
onSelectAllTraffic,
onSelectAudience,
onSelectCustomFilter,
posts,
} = props;
return (
{ /* All Traffic option - always at top */ }
-
{ __( 'All Traffic', 'altis' ) }
{ __( 'Show analytics for all visitors without filtering', 'altis' ) }
{ selectedMode === 'all' ? (
{ __( 'Selected', 'altis' ) }
) : (
) }
{ /* Saved audiences */ }
{ posts.map( post => {
const isSelected = selectedMode === 'audience' && selectedAudienceId === post.id;
return (
-
{ isSelected ? (
{ __( 'Selected', 'altis' ) }
) : (
) }
);
} ) }
{ /* Custom filter option - always at bottom */ }
-
{ __( 'Custom Filter', 'altis' ) }
{ __( 'Create your own filter rules for this session', 'altis' ) }
{ selectedMode === 'custom' ? (
{ __( 'Selected', 'altis' ) }
) : (
) }
);
}
const applyWithSelect = withSelect( ( select : any ) => {
const {
getPosts,
getIsLoading,
getPagination,
} = select( 'audience' );
const canCreate = select( 'core' ).canUser( 'create', 'audiences' );
const loading = getIsLoading();
const pagination = getPagination();
const queryArgs = {
context: canCreate ? 'edit' : 'view',
status: canCreate ? 'publish,draft' : 'publish',
};
const posts = getPosts( queryArgs );
return {
canCreate,
loading,
pagination,
posts,
onGetPosts: getPosts,
};
} ) as unknown as ( component: React.ComponentType ) => React.ComponentType;
export default applyWithSelect( FilterAudiencePicker );