From 1be5b08bf41906b3c388b7a9af111e4a3ffd69cf Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Wed, 23 Oct 2024 15:24:24 +1300 Subject: [PATCH] Stats: Fix possible undefined value (#95601) * fix possible empty events * possibly undefined value * Apply suggestions from code review * use a default value for possibly not found selected period * use empty array as default data to avoid undefined value * use empty array as default data to avoid undefined value * current_usage could be undefined --- .../src/components/odyssey-query-site-purchases.ts | 2 +- client/my-sites/stats/stats-module/all-time-nav.jsx | 3 ++- client/my-sites/stats/stats-notices/index.tsx | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/odyssey-stats/src/components/odyssey-query-site-purchases.ts b/apps/odyssey-stats/src/components/odyssey-query-site-purchases.ts index bc5750174e952..1767836ce7b6d 100644 --- a/apps/odyssey-stats/src/components/odyssey-query-site-purchases.ts +++ b/apps/odyssey-stats/src/components/odyssey-query-site-purchases.ts @@ -164,7 +164,7 @@ export default function OdysseyQuerySitePurchases( { siteId }: { siteId: number reduxDispatch( { type: PURCHASES_SITE_FETCH_COMPLETED, siteId, - purchases, + purchases: purchases ?? [], } ); } }, [ purchases, isFetching, reduxDispatch, hasOtherErrors, siteId, purchasesFromMyJetpack ] ); diff --git a/client/my-sites/stats/stats-module/all-time-nav.jsx b/client/my-sites/stats/stats-module/all-time-nav.jsx index 397b09c87dbd2..bf78cb33663d7 100644 --- a/client/my-sites/stats/stats-module/all-time-nav.jsx +++ b/client/my-sites/stats/stats-module/all-time-nav.jsx @@ -120,7 +120,8 @@ export const StatsModuleSummaryLinks = ( props ) => { ]; const numberDays = get( query, 'num', '0' ); - const selected = find( options, { value: numberDays } ); + let selected = find( options, { value: numberDays } ); + selected = selected || options[ 0 ]; const tabs = ( = SIGNIFICANT_VIEWS_AMOUNT ); const { data } = usePlanUsageQuery( siteId ); - const currentUsage = data?.current_usage.views_count || 0; + const currentUsage = data?.current_usage?.views_count || 0; const tierLimit = data?.views_limit || null; const isNearLimit = tierLimit ? currentUsage / tierLimit >= 0.9 : false; const isOverLimit = tierLimit ? currentUsage / tierLimit >= 1 : false;