import { memo, useMemo, useState } from 'react'; import { __ } from '@wordpress/i18n'; import * as Checkbox from '@radix-ui/react-checkbox'; import { useNavigate, useRouterState } from '@tanstack/react-router'; import { Block } from '@/components/Blocks/Block'; import { BlockHeading } from '@/components/Blocks/BlockHeading'; import { BlockContent } from '@/components/Blocks/BlockContent'; import Icon from '@/utils/Icon'; import { BarDataTable } from '@/components/DataTable/BarDataTable'; import { useSearchTermsData } from './useSearchTermsData'; import { getSearchTermsColumns } from './columns'; type SearchTermsBlockProps = { /** Additional CSS class names passed to the wrapping Block. */ className?: string; }; /** Maximum rows shown in the compact block view. */ const TOP_N = 10; /** * Compact dashboard block showing the top site-search terms. * * Features an embedded volume bar, a clickable results column, a toggle to * filter for zero-result queries, and an expand button that opens the full * table in the DataTableOverlay. * * @param {Object} props - Component props. * @param {string} props.className - Additional CSS classes for the Block wrapper. * @return {JSX.Element} The search terms block. */ const SearchTermsBlock = memo( ({ className = '' }: SearchTermsBlockProps ) => { const { data, isLoading } = useSearchTermsData(); const [ noResultsOnly, setNoResultsOnly ] = useState( false ); const navigate = useNavigate(); const location = useRouterState({ select: ( s ) => s.location }); const siteUrl = ( window as unknown as { burst_settings?: { site_url?: string } }) ?.burst_settings?.site_url ?? window.location.origin; const columns = useMemo( () => getSearchTermsColumns({ siteUrl }), [ siteUrl ] ); const filteredData = useMemo( () => { const base = noResultsOnly ? data.filter( ( r ) => 0 === r.results ) : data; return base.slice( 0, TOP_N ); }, [ data, noResultsOnly ]); /** * Navigate to the fullscreen overlay with the search_terms variant active. */ const handleExpand = () => { navigate({ to: '/table/$variant', params: { variant: 'search_terms' }, search: { from: location.pathname, allowed: 'search_terms', dataTableId: 'search-terms', ...location.search } }); }; return ( {__( 'Search terms', 'burst-statistics' )} {/* Expand to overlay. */} } controls={
{/* No-results toggle. */}
} /> row.term as string} barColumnKey="volume" isLoading={isLoading} emptyState={ noResultsOnly ? __( 'No zero-result searches found.', 'burst-statistics' ) : __( 'No search terms recorded yet.', 'burst-statistics' ) } />
); }); SearchTermsBlock.displayName = 'SearchTermsBlock'; export default SearchTermsBlock;