Back

93.75% Statements 15/16
100% Branches 8/8
83.33% Functions 5/6
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                              5x   5x                               12x 12x 5x     12x   12x 12x 12x       12x   12x 12x       12x                        
/**
 * External dependencies
 */
import {
	EDGE_ROLLUP_MAX_DAYS,
	normalizeReportParams,
	useStatsAiProviders,
	type ReportParams,
} from '@jetpack-premium-analytics/data';
import { dateToISOStringWithLocalTZ, localTZDate } from '@jetpack-premium-analytics/datetime';
import { useCallback, useMemo } from '@wordpress/element';
import { useSearch } from '@wordpress/route';
import { startOfDay, subDays } from 'date-fns';
/**
 * Internal dependencies
 */
import type { EntitySummary } from '../adapter';
 
export type AiCompanySummary = EntitySummary & {
	/** The company's bots that requested the site in the page's window. */
	botCount?: number;
	/** Whether the company's name and bots are on their way; the widgets do not wait for them. */
	isIdentityLoading: boolean;
};
 
/**
 * The longest window the edge serves in one request, ending today, in whole site days.
 *
 * @return The window as report params.
 */
function edgeHistoryParams(): ReportParams {
	const today = localTZDate();
 
	return {
		from: dateToISOStringWithLocalTZ( startOfDay( subDays( today, EDGE_ROLLUP_MAX_DAYS - 1 ) ) ),
		to: dateToISOStringWithLocalTZ( today ),
		interval: 'day',
	};
}
 
/**
 * Resolve an AI company's name and bots from its URL key, over the page's window. A company the window
 * misses is named from the edge history, and one the history misses too (outside the top companies, or
 * newer than the rollup) keeps its key: the widgets show its data either way.
 *
 * @param companyKey - The company key, as the URL carries it.
 * @return The resolved company summary.
 */
export function useAiCompanySummary( companyKey: string ): AiCompanySummary {
	const search = useSearch( { strict: false } );
	const windowParams = useMemo(
		() => normalizeReportParams( search as Parameters< typeof normalizeReportParams >[ 0 ] ),
		[ search ]
	);
	const historyParams = useMemo( edgeHistoryParams, [] );
 
	const inWindow = useStatsAiProviders( windowParams );
	const windowCompany = inWindow.primary.data?.data.find( provider => provider.key === companyKey );
	const history = useStatsAiProviders( historyParams, {
		enabled: ! inWindow.isLoading && ! windowCompany,
	} );
	const company =
		windowCompany ?? history.primary.data?.data.find( provider => provider.key === companyKey );
 
	const { refetch } = inWindow;
	const retry = useCallback( () => {
		void refetch();
	}, [ refetch ] );
 
	return {
		title: company?.name ?? companyKey,
		botCount: windowCompany?.bots.length,
		allTimeStart: historyParams.from,
		isIdentityLoading: inWindow.isLoading || history.isLoading,
		isLoading: false,
		isError: false,
		error: null,
		isNotFound: false,
		refetch: retry,
	};
}