Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats Locations: add support for preserving geo mode in summary view #99451

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { QueryStatsParams } from 'calypso/my-sites/stats/hooks/utils';
import StatsCardUpsell from 'calypso/my-sites/stats/stats-card-upsell';
import StatsListCard from 'calypso/my-sites/stats/stats-list/stats-list-card';
import StatsModulePlaceholder from 'calypso/my-sites/stats/stats-module/placeholder';
import { trackStatsAnalyticsEvent } from 'calypso/my-sites/stats/utils';
import {
getPathWithUpdatedQueryString,
trackStatsAnalyticsEvent,
} from 'calypso/my-sites/stats/utils';
import { useSelector } from 'calypso/state';
import getEnvStatsFeatureSupportChecks from 'calypso/state/sites/selectors/get-env-stats-feature-supports';
import { getSiteStatsNormalizedData } from 'calypso/state/stats/lists/selectors';
Expand Down Expand Up @@ -57,16 +60,25 @@ type SelectOptionType = {
interface StatsModuleLocationsProps {
query: QueryStatsParams;
summaryUrl?: string;
initialGeoMode?: string;
}

const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summaryUrl } ) => {
const StatsLocations: React.FC< StatsModuleLocationsProps > = ( {
query,
summaryUrl,
initialGeoMode,
} ) => {
const translate = useTranslate();
const siteId = useSelector( getSelectedSiteId ) as number;
const statType = STAT_TYPE_COUNTRY_VIEWS;
const isOdysseyStats = config.isEnabled( 'is_running_in_jetpack_site' );
const supportUrl = isOdysseyStats ? JETPACK_SUPPORT_URL_TRAFFIC : SUPPORT_URL;

const [ selectedOption, setSelectedOption ] = useState( OPTION_KEYS.COUNTRIES );
const urlGeoMode =
initialGeoMode || new URLSearchParams( window.location.search ).get( 'geoMode' );
const [ selectedOption, setSelectedOption ] = useState(
urlGeoMode && urlGeoMode in GEO_MODES ? urlGeoMode : OPTION_KEYS.COUNTRIES
);

const [ countryFilter, setCountryFilter ] = useState< string | null >( null );

Expand Down Expand Up @@ -274,6 +286,19 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
return null;
};

const getFinalSummaryUrl = () => {
if ( ! summaryUrl ) {
return undefined;
}

return getPathWithUpdatedQueryString(
{
geoMode: selectedOption,
},
summaryUrl
);
};

const moduleOverlay = getModuleOverlay();

return (
Expand Down Expand Up @@ -314,7 +339,7 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
showMore={
summaryUrl
? {
url: summaryUrl,
url: getFinalSummaryUrl(),
label:
Array.isArray( locationData ) && locationData.length >= 10
? translate( 'View all', {
Expand All @@ -340,7 +365,7 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
footerAction={
summaryUrl
? {
url: summaryUrl,
url: getFinalSummaryUrl(),
label: translate( 'View more' ),
}
: undefined
Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/stats/summary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class StatsSummary extends Component {
title = translate( 'Locations' );
path = 'locations';
statType = 'statsCountryViews';

summaryView = (
<Fragment key="countries-summary">
{ this.renderSummaryHeader( path, statType, false, moduleQuery ) }
Expand All @@ -185,6 +184,7 @@ class StatsSummary extends Component {
query={ moduleQuery }
summary
listItemClassName={ listItemClassName }
initialGeoMode={ urlParams.get( 'geoMode' ) }
/>
</Fragment>
);
Expand Down
Loading