diff --git a/static/app/components/pageFilters/actions.tsx b/static/app/components/pageFilters/actions.tsx index 23b969fc75a1..314b182d1e13 100644 --- a/static/app/components/pageFilters/actions.tsx +++ b/static/app/components/pageFilters/actions.tsx @@ -19,7 +19,10 @@ import { setPageFiltersStorage, } from 'sentry/components/pageFilters/persistence'; import {PageFiltersStore} from 'sentry/components/pageFilters/store'; -import {parseStatsPeriod} from 'sentry/components/timeRangeSelector/utils'; +import { + parseStatsPeriod, + STATS_PERIOD_REGEX, +} from 'sentry/components/timeRangeSelector/utils'; import {OrganizationStore} from 'sentry/stores/organizationStore'; import type {DateString, PageFilters, PinnedPageFilter} from 'sentry/types/core'; import type {Organization} from 'sentry/types/organization'; @@ -318,6 +321,15 @@ export function initializeUrlState({ if (maxPickableDays && pageFilters.datetime) { let {start, end} = pageFilters.datetime; + if ( + pageFilters.datetime.period && + !STATS_PERIOD_REGEX.test(pageFilters.datetime.period) + ) { + // Invalid period from localStorage or URL — fall back to the default period + // rather than letting parseStatsPeriod throw and crash the page. + pageFilters.datetime.period = defaultDatetime.period; + } + if (pageFilters.datetime.period) { const parsedPeriod = parseStatsPeriod(pageFilters.datetime.period); start = parsedPeriod.start; diff --git a/static/app/components/timeRangeSelector/utils.tsx b/static/app/components/timeRangeSelector/utils.tsx index a6e1d5250806..5f00f6d80a7d 100644 --- a/static/app/components/timeRangeSelector/utils.tsx +++ b/static/app/components/timeRangeSelector/utils.tsx @@ -29,7 +29,7 @@ type RelativeUnitsMapping = Record< const DATE_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ss'; -const STATS_PERIOD_REGEX = /^(\d+)([mhdw])$/; +export const STATS_PERIOD_REGEX = /^(\d+)([mhdw])$/; const SUPPORTED_RELATIVE_PERIOD_UNITS: RelativeUnitsMapping = { m: {