|
| 1 | +import {useCallback} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | + |
| 4 | +import type {SelectOption, SingleSelectProps} from 'sentry/components/core/compactSelect'; |
| 5 | +import {CompactSelect} from 'sentry/components/core/compactSelect'; |
| 6 | +import type {Item} from 'sentry/components/dropdownAutoComplete/types'; |
| 7 | +import DropdownButton from 'sentry/components/dropdownButton'; |
| 8 | +import HookOrDefault from 'sentry/components/hookOrDefault'; |
| 9 | +import {DesyncedFilterIndicator} from 'sentry/components/organizations/pageFilters/desyncedFilter'; |
| 10 | +import SelectorItems from 'sentry/components/timeRangeSelector/selectorItems'; |
| 11 | +import { |
| 12 | + getArbitraryRelativePeriod, |
| 13 | + getSortedRelativePeriods, |
| 14 | +} from 'sentry/components/timeRangeSelector/utils'; |
| 15 | +import {t} from 'sentry/locale'; |
| 16 | +import {parsePeriodToHours} from 'sentry/utils/duration/parsePeriodToHours'; |
| 17 | + |
| 18 | +import {CODECOV_DEFAULT_RELATIVE_PERIODS} from './datePicker'; |
| 19 | + |
| 20 | +const SelectorItemsHook = HookOrDefault({ |
| 21 | + hookName: 'component:header-selector-items', |
| 22 | + defaultComponent: SelectorItems, |
| 23 | +}); |
| 24 | + |
| 25 | +export type ChangeData = { |
| 26 | + relative: string | null; |
| 27 | +}; |
| 28 | + |
| 29 | +export interface DateSelectorProps |
| 30 | + extends Omit< |
| 31 | + SingleSelectProps<string>, |
| 32 | + 'disableSearchFilter' | 'onChange' | 'onClose' | 'options' | 'value' |
| 33 | + > { |
| 34 | + /** |
| 35 | + * Whether the current value is out of sync with the stored persistent value. |
| 36 | + */ |
| 37 | + desynced?: boolean; |
| 38 | + /** |
| 39 | + * Custom width value for relative compact select |
| 40 | + */ |
| 41 | + menuWidth?: string; |
| 42 | + onChange?: (data: ChangeData) => void; |
| 43 | + onClose?: () => void; |
| 44 | + /** |
| 45 | + * Relative date value |
| 46 | + */ |
| 47 | + relative?: string | null; |
| 48 | +} |
| 49 | + |
| 50 | +export function DateSelector({ |
| 51 | + relative, |
| 52 | + onChange, |
| 53 | + onClose, |
| 54 | + trigger, |
| 55 | + menuWidth, |
| 56 | + desynced, |
| 57 | + ...selectProps |
| 58 | +}: DateSelectorProps) { |
| 59 | + const getOptions = useCallback((items: Item[]): Array<SelectOption<string>> => { |
| 60 | + return items.map((item: Item): SelectOption<string> => { |
| 61 | + return { |
| 62 | + value: item.value, |
| 63 | + label: <OptionLabel>{item.label}</OptionLabel>, |
| 64 | + textValue: item.searchKey, |
| 65 | + }; |
| 66 | + }); |
| 67 | + }, []); |
| 68 | + |
| 69 | + const handleChange = useCallback<NonNullable<SingleSelectProps<string>['onChange']>>( |
| 70 | + option => { |
| 71 | + onChange?.({relative: option.value}); |
| 72 | + }, |
| 73 | + [onChange] |
| 74 | + ); |
| 75 | + |
| 76 | + // Currently selected relative period |
| 77 | + const arbitraryRelativePeriods = getArbitraryRelativePeriod(relative); |
| 78 | + // Periods from default relative periods object |
| 79 | + const restrictedDefaultPeriods = Object.fromEntries( |
| 80 | + Object.entries(CODECOV_DEFAULT_RELATIVE_PERIODS).filter(([period]) => |
| 81 | + parsePeriodToHours(period) |
| 82 | + ) |
| 83 | + ); |
| 84 | + const defaultRelativePeriods = { |
| 85 | + ...restrictedDefaultPeriods, |
| 86 | + ...arbitraryRelativePeriods, |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <SelectorItemsHook |
| 91 | + shouldShowRelative |
| 92 | + relativePeriods={getSortedRelativePeriods(defaultRelativePeriods)} |
| 93 | + handleSelectRelative={value => handleChange({value})} |
| 94 | + > |
| 95 | + {items => ( |
| 96 | + <CompactSelect |
| 97 | + {...selectProps} |
| 98 | + disableSearchFilter |
| 99 | + options={getOptions(items)} |
| 100 | + value={relative ?? ''} |
| 101 | + onChange={handleChange} |
| 102 | + menuWidth={menuWidth ?? '16rem'} |
| 103 | + onClose={() => { |
| 104 | + onClose?.(); |
| 105 | + }} |
| 106 | + trigger={ |
| 107 | + trigger ?? |
| 108 | + ((triggerProps, isOpen) => { |
| 109 | + const defaultLabel = items.some(item => item.value === relative) |
| 110 | + ? relative?.toUpperCase() |
| 111 | + : t('Invalid Period'); |
| 112 | + |
| 113 | + return ( |
| 114 | + <DropdownButton |
| 115 | + isOpen={isOpen} |
| 116 | + size={selectProps.size} |
| 117 | + data-test-id="page-filter-codecov-time-selector" |
| 118 | + {...triggerProps} |
| 119 | + {...selectProps.triggerProps} |
| 120 | + > |
| 121 | + <TriggerLabelWrap> |
| 122 | + <TriggerLabel> |
| 123 | + {selectProps.triggerLabel ?? defaultLabel} |
| 124 | + </TriggerLabel> |
| 125 | + {desynced && <DesyncedFilterIndicator />} |
| 126 | + </TriggerLabelWrap> |
| 127 | + </DropdownButton> |
| 128 | + ); |
| 129 | + }) |
| 130 | + } |
| 131 | + /> |
| 132 | + )} |
| 133 | + </SelectorItemsHook> |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +const TriggerLabelWrap = styled('span')` |
| 138 | + position: relative; |
| 139 | + min-width: 0; |
| 140 | +`; |
| 141 | + |
| 142 | +const TriggerLabel = styled('span')` |
| 143 | + ${p => p.theme.overflowEllipsis} |
| 144 | + width: auto; |
| 145 | +`; |
| 146 | + |
| 147 | +const OptionLabel = styled('span')` |
| 148 | + div { |
| 149 | + margin: 0; |
| 150 | + } |
| 151 | +`; |
0 commit comments