-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(explore): Add hooks for date page filter props based on data cat… #103931
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
Merged
Zylphrex
merged 2 commits into
master
from
txiao/feat/add-hooks-for-date-page-filter-props-based-on-data-category-2
Nov 25, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import {useMemo, type ReactNode} from 'react'; | ||
|
|
||
| import type {SelectOptionWithKey} from 'sentry/components/core/compactSelect/types'; | ||
| import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter'; | ||
| import {t} from 'sentry/locale'; | ||
| import {isEmptyObject} from 'sentry/utils/object/isEmptyObject'; | ||
| import type {MaxPickableDaysOptions} from 'sentry/utils/useMaxPickableDays'; | ||
|
|
||
| interface UseDatePageFilterPropsProps extends MaxPickableDaysOptions {} | ||
|
|
||
| export function useDatePageFilterProps({ | ||
| defaultPeriod, | ||
| maxPickableDays, | ||
| maxUpgradableDays, | ||
| upsellFooter, | ||
| }: UseDatePageFilterPropsProps): DatePageFilterProps { | ||
| return useMemo(() => { | ||
| // ensure the available relative options are always sorted | ||
| const availableRelativeOptions: Array<[number, string, ReactNode]> = [ | ||
| [1 / 24, '1h', t('Last hour')], | ||
| [1, '24h', t('Last 24 hours')], | ||
| [7, '7d', t('Last 7 days')], | ||
| [14, '14d', t('Last 14 days')], | ||
| [30, '30d', t('Last 30 days')], | ||
| [90, '90d', t('Last 90 days')], | ||
| ]; | ||
|
|
||
| // find the relative options that should be enabled based on the maxPickableDays | ||
| const pickableIndex = | ||
| availableRelativeOptions.findLastIndex(([days]) => days <= maxPickableDays) + 1; | ||
| const enabledOptions = Object.fromEntries( | ||
| availableRelativeOptions | ||
| .slice(0, pickableIndex) | ||
| .map(([_, period, label]) => [period, label]) | ||
| ); | ||
|
|
||
| // find the relative options that should be disabled based on the maxUpgradableDays | ||
| const upgradableIndex = | ||
| availableRelativeOptions.findLastIndex(([days]) => days <= maxUpgradableDays) + 1; | ||
| const disabledOptions = Object.fromEntries( | ||
| availableRelativeOptions | ||
| .slice(pickableIndex, upgradableIndex) | ||
| .map(([_, period, label]) => [period, label]) | ||
| ); | ||
|
|
||
| const isOptionDisabled = (option: SelectOptionWithKey<string>): boolean => { | ||
| return disabledOptions.hasOwnProperty(option.value); | ||
| }; | ||
|
|
||
| const menuFooter = isEmptyObject(disabledOptions) ? null : (upsellFooter ?? null); | ||
|
|
||
| return { | ||
| defaultPeriod, | ||
| isOptionDisabled, | ||
| maxPickableDays, | ||
| menuFooter, | ||
| relativeOptions: ({arbitraryOptions}) => ({ | ||
| ...arbitraryOptions, | ||
| ...enabledOptions, | ||
| ...disabledOptions, | ||
| }), | ||
| }; | ||
| }, [defaultPeriod, maxPickableDays, maxUpgradableDays, upsellFooter]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import {OrganizationFixture} from 'sentry-fixture/organization'; | ||
|
|
||
| import {renderHook} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {DataCategory} from 'sentry/types/core'; | ||
|
|
||
| import {useMaxPickableDays} from './useMaxPickableDays'; | ||
|
|
||
| describe('useMaxPickableDays', () => { | ||
| it('returns 30/90 for spans without flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.SPANS], | ||
| organization: OrganizationFixture({features: []}), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 90/90 for spans with flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.SPANS], | ||
| organization: OrganizationFixture({features: ['visibility-explore-range-high']}), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 90, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/30 days for tracemetrics', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.TRACE_METRICS], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| defaultPeriod: '24h', | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/30 days for logs', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.LOG_BYTE, DataCategory.LOG_ITEM], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| defaultPeriod: '24h', | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/90 for many without flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [ | ||
| DataCategory.SPANS, | ||
| DataCategory.SPANS_INDEXED, | ||
| DataCategory.TRACE_METRICS, | ||
| DataCategory.LOG_BYTE, | ||
| DataCategory.LOG_ITEM, | ||
| ], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import {useMemo, type ReactNode} from 'react'; | ||
|
|
||
| import HookOrDefault from 'sentry/components/hookOrDefault'; | ||
| import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter'; | ||
| import {t} from 'sentry/locale'; | ||
| import {DataCategory} from 'sentry/types/core'; | ||
| import type {Organization} from 'sentry/types/organization'; | ||
|
|
||
| export interface MaxPickableDaysOptions { | ||
| /** | ||
| * The maximum number of days the user is allowed to pick on the date page filter | ||
| */ | ||
| maxPickableDays: NonNullable<DatePageFilterProps['maxPickableDays']>; | ||
| /** | ||
| * The maximum number of days the user can upgrade to on the date page filter | ||
| */ | ||
| maxUpgradableDays: NonNullable<DatePageFilterProps['maxPickableDays']>; | ||
| defaultPeriod?: DatePageFilterProps['defaultPeriod']; | ||
| upsellFooter?: ReactNode; | ||
| } | ||
|
|
||
| interface UseMaxPickableDaysProps { | ||
| dataCategories: readonly [DataCategory, ...DataCategory[]]; | ||
| organization: Organization; | ||
| } | ||
|
|
||
| export function useMaxPickableDays({ | ||
| dataCategories, | ||
| organization, | ||
| }: UseMaxPickableDaysProps): MaxPickableDaysOptions { | ||
| return useMemo(() => { | ||
| function getMaxPickableDaysFor(dataCategory: DataCategory) { | ||
| return getMaxPickableDays(dataCategory, organization); | ||
| } | ||
|
|
||
| return getBestMaxPickableDays(dataCategories, getMaxPickableDaysFor); | ||
| }, [dataCategories, organization]); | ||
| } | ||
|
|
||
| function getBestMaxPickableDays( | ||
| dataCategories: readonly [DataCategory, ...DataCategory[]], | ||
| getMaxPickableDaysFor: (dataCategory: DataCategory) => MaxPickableDaysOptions | ||
| ) { | ||
| let maxPickableDays: MaxPickableDaysOptions = getMaxPickableDaysFor(dataCategories[0]); | ||
|
|
||
| for (let i = 1; i < dataCategories.length; i++) { | ||
| const dataCategory = dataCategories[i]!; | ||
| const maxPickableDaysForDataCategory = getMaxPickableDaysFor(dataCategory); | ||
| maxPickableDays = max(maxPickableDays, maxPickableDaysForDataCategory); | ||
| } | ||
|
|
||
| return maxPickableDays; | ||
| } | ||
|
|
||
| function max( | ||
| a: MaxPickableDaysOptions, | ||
| b: MaxPickableDaysOptions | ||
| ): MaxPickableDaysOptions { | ||
| if (a.maxPickableDays < b.maxPickableDays) { | ||
| return b; | ||
| } | ||
|
|
||
| if (a.maxUpgradableDays < b.maxUpgradableDays) { | ||
| return b; | ||
| } | ||
|
|
||
| return a; | ||
| } | ||
|
|
||
| const DESCRIPTION = t('To query over longer time ranges, upgrade to Business'); | ||
|
|
||
| function getMaxPickableDays( | ||
| dataCategory: DataCategory, | ||
| organization: Organization | ||
| ): MaxPickableDaysOptions { | ||
| switch (dataCategory) { | ||
| case DataCategory.SPANS: | ||
| case DataCategory.SPANS_INDEXED: { | ||
| const maxPickableDays = organization.features.includes( | ||
| 'visibility-explore-range-high' | ||
| ) | ||
| ? 90 | ||
| : 30; | ||
| return { | ||
| maxPickableDays, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: <UpsellFooterHook description={DESCRIPTION} source="spans" />, | ||
| }; | ||
| } | ||
| case DataCategory.TRACE_METRICS: | ||
| case DataCategory.LOG_BYTE: | ||
| case DataCategory.LOG_ITEM: | ||
| return { | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| defaultPeriod: '24h', | ||
| }; | ||
| default: | ||
| throw new Error( | ||
| `Unsupported data category: ${dataCategory} for getMaxPickableDays` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const UpsellFooterHook = HookOrDefault({ | ||
| hookName: 'component:header-date-page-filter-upsell-footer', | ||
| defaultComponent: () => null, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Lost defaultPeriod when mixing data categories
The
maxfunction only returns one of the twoMaxPickableDaysOptionsobjects without merging their properties. When combining data categories where some specifydefaultPeriod(like TRACE_METRICS, LOG_BYTE, LOG_ITEM) with others that don't (like SPANS), thedefaultPeriodgets lost. This causes the date filter to use an incorrect default period when multiple data categories are mixed.