Skip to content

Commit 157d15f

Browse files
committed
ref(ts): Remove dropdownAutoComplete usage in timeRangeSelector
1 parent 9ed4ed8 commit 157d15f

File tree

6 files changed

+54
-69
lines changed

6 files changed

+54
-69
lines changed

static/app/components/charts/intervalSelector.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {useState} from 'react';
22

33
import {getInterval} from 'sentry/components/charts/utils';
4-
import {CompactSelect} from 'sentry/components/core/compactSelect';
4+
import {CompactSelect, type SelectOption} from 'sentry/components/core/compactSelect';
55
import {
66
_timeRangeAutoCompleteFilter,
77
makeItem,
@@ -143,13 +143,6 @@ function bindInterval(
143143
return intervalHours < intervalOption.min || intervalHours > optionMax;
144144
}
145145

146-
type Item = {
147-
label: React.ReactNode;
148-
textValue: string;
149-
value: string;
150-
searchKey?: string;
151-
};
152-
153146
export default function IntervalSelector({
154147
displayMode,
155148
eventView,
@@ -176,10 +169,9 @@ export default function IntervalSelector({
176169
}
177170
}
178171

179-
const [items, setItems] = useState<Item[]>(() =>
172+
const [items, setItems] = useState<Array<SelectOption<string>>>(() =>
180173
intervalOption.options.map(option => ({
181174
value: option,
182-
searchKey: option,
183175
textValue: option,
184176
label: option,
185177
}))
@@ -205,12 +197,7 @@ export default function IntervalSelector({
205197
if (newItem) {
206198
const [amount, unit] = formatHoursToInterval(newItem);
207199
filteredResults.push(
208-
makeItem(
209-
amount,
210-
unit,
211-
SUPPORTED_RELATIVE_PERIOD_UNITS[unit]!.label,
212-
results.length + 1
213-
)
200+
makeItem(amount, unit, SUPPORTED_RELATIVE_PERIOD_UNITS[unit]!.label)
214201
);
215202
}
216203

static/app/components/timeRangeSelector/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import styled from '@emotion/styled';
44
import {Button} from 'sentry/components/core/button';
55
import type {SelectOption, SingleSelectProps} from 'sentry/components/core/compactSelect';
66
import {CompactSelect} from 'sentry/components/core/compactSelect';
7-
import type {Item} from 'sentry/components/dropdownAutoComplete/types';
87
import DropdownButton from 'sentry/components/dropdownButton';
98
import HookOrDefault from 'sentry/components/hookOrDefault';
109
import {DesyncedFilterIndicator} from 'sentry/components/organizations/pageFilters/desyncedFilter';
@@ -29,6 +28,7 @@ import useRouter from 'sentry/utils/useRouter';
2928

3029
import DateRange from './dateRange';
3130
import SelectorItems from './selectorItems';
31+
import type {TimeRangeItem} from './types';
3232
import {
3333
getAbsoluteSummary,
3434
getArbitraryRelativePeriod,
@@ -187,8 +187,8 @@ export function TimeRangeSelector({
187187
});
188188

189189
const getOptions = useCallback(
190-
(items: Item[]): Array<SelectOption<string>> => {
191-
const makeOption = (item: Item): SelectOption<string> => {
190+
(items: TimeRangeItem[]): Array<SelectOption<string>> => {
191+
const makeOption = (item: TimeRangeItem): SelectOption<string> => {
192192
if (item.value === 'absolute') {
193193
return {
194194
value: item.value,
@@ -206,14 +206,14 @@ export function TimeRangeSelector({
206206
color={isFocused || isSelected ? undefined : 'subText'}
207207
/>
208208
),
209-
textValue: item.searchKey,
209+
textValue: item.textValue,
210210
};
211211
}
212212

213213
return {
214214
value: item.value,
215215
label: <OptionLabel>{item.label}</OptionLabel>,
216-
textValue: item.searchKey,
216+
textValue: item.textValue,
217217
};
218218
};
219219

@@ -223,7 +223,7 @@ export function TimeRangeSelector({
223223
}
224224

225225
const filteredItems = disallowArbitraryRelativeRanges
226-
? items.filter(i => i.searchKey?.includes(search))
226+
? items.filter(i => i.textValue?.includes(search))
227227
: // If arbitrary relative ranges are allowed, then generate a list of them based
228228
// on the search query
229229
timeRangeAutoCompleteFilter(items, search, {
@@ -340,8 +340,8 @@ export function TimeRangeSelector({
340340
value={start && end ? ABSOLUTE_OPTION_VALUE : (relative ?? '')}
341341
onChange={option => {
342342
const item = items.find(i => i.value === option.value);
343-
if (item?.onClick) {
344-
item.onClick();
343+
if (item?.onSelect) {
344+
item.onSelect();
345345
} else {
346346
handleChange(option);
347347
}

static/app/components/timeRangeSelector/selectorItems.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type {Item} from 'sentry/components/dropdownAutoComplete/types';
21
import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
32
import {t} from 'sentry/locale';
43

54
import TimeRangeItemLabel from './timeRangeItemLabel';
5+
import type {TimeRangeItem} from './types';
66

77
type Props = {
8-
children: (items: Item[]) => React.ReactElement;
8+
children: (items: TimeRangeItem[]) => React.ReactElement;
99
handleSelectRelative: (value: string) => void;
1010
relativePeriods?: Record<string, React.ReactNode>;
1111
shouldShowAbsolute?: boolean;
@@ -20,24 +20,20 @@ const SelectorItems = ({
2020
}: Props) => {
2121
const relativeArr = Object.entries(relativePeriods ?? DEFAULT_RELATIVE_PERIODS);
2222

23-
const items: Item[] = [
23+
const items: TimeRangeItem[] = [
2424
...(shouldShowRelative
25-
? relativeArr.map(([value, itemLabel], index) => ({
26-
index,
25+
? relativeArr.map(([value, itemLabel]) => ({
2726
value,
28-
searchKey: typeof itemLabel === 'string' ? itemLabel : value,
27+
textValue: typeof itemLabel === 'string' ? itemLabel : value,
2928
label: <TimeRangeItemLabel>{itemLabel}</TimeRangeItemLabel>,
30-
'data-test-id': value,
3129
}))
3230
: []),
3331
...(shouldShowAbsolute
3432
? [
3533
{
36-
index: relativeArr.length,
3734
value: 'absolute',
38-
searchKey: 'absolute',
35+
textValue: 'absolute',
3936
label: <TimeRangeItemLabel>{t('Absolute date')}</TimeRangeItemLabel>,
40-
'data-test-id': 'absolute',
4137
},
4238
]
4339
: []),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {type SelectOption} from 'sentry/components/core/compactSelect';
2+
3+
export interface TimeRangeItem extends SelectOption<string> {
4+
/**
5+
* Altenrative action handler for the time range item
6+
*/
7+
onSelect?: () => void;
8+
}

static/app/components/timeRangeSelector/utils.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {Fragment} from 'react';
22
import moment from 'moment-timezone';
33

44
import {DateTime} from 'sentry/components/dateTime';
5-
import autoCompleteFilter from 'sentry/components/dropdownAutoComplete/autoCompleteFilter';
6-
import type {ItemsBeforeFilter} from 'sentry/components/dropdownAutoComplete/types';
75
import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
86
import {t, tn} from 'sentry/locale';
97
import type {DateString} from 'sentry/types/core';
@@ -14,6 +12,7 @@ import {
1412
} from 'sentry/utils/dates';
1513

1614
import TimeRangeItemLabel from './timeRangeItemLabel';
15+
import type {TimeRangeItem} from './types';
1716

1817
type PeriodUnit = 's' | 'm' | 'h' | 'd' | 'w';
1918
type RelativePeriodUnit = Exclude<PeriodUnit, 's'>;
@@ -166,15 +165,12 @@ export function getAbsoluteSummary(
166165
export function makeItem(
167166
amount: number,
168167
unit: string,
169-
label: (num: number) => string,
170-
index: number
171-
) {
168+
label: (num: number) => string
169+
): TimeRangeItem {
172170
return {
173171
value: `${amount}${unit}`,
174-
['data-test-id']: `${amount}${unit}`,
175172
label: <TimeRangeItemLabel>{label(amount)}</TimeRangeItemLabel>,
176-
searchKey: `${amount}${unit}`,
177-
index,
173+
textValue: `${amount}${unit}`,
178174
};
179175
}
180176

@@ -199,6 +195,17 @@ function timePeriodIsWithinLimit<T extends RelativeUnitsMapping>({
199195
return numberOfDays <= maxDays;
200196
}
201197

198+
function filterItems(items: TimeRangeItem[], inputValue: string): TimeRangeItem[] {
199+
return items.filter(item =>
200+
(typeof item.textValue === 'string' && item.textValue.length > 0
201+
? item.textValue
202+
: `${item.value}`
203+
)
204+
.toLowerCase()
205+
.includes(inputValue.toLowerCase())
206+
);
207+
}
208+
202209
/**
203210
* A custom autocomplete implementation for <TimeRangeSelector />
204211
* This function generates relative time ranges based on the user's input (not limited to those present in the initial set).
@@ -213,7 +220,7 @@ function timePeriodIsWithinLimit<T extends RelativeUnitsMapping>({
213220
* If the input does not begin with a number, we do a simple filter of the preset options.
214221
*/
215222
export const _timeRangeAutoCompleteFilter = function <T extends RelativeUnitsMapping>(
216-
items: ItemsBeforeFilter | null,
223+
items: TimeRangeItem[] | null,
217224
filterValue: string,
218225
{
219226
supportedPeriods,
@@ -226,7 +233,7 @@ export const _timeRangeAutoCompleteFilter = function <T extends RelativeUnitsMap
226233
maxDateRange?: number;
227234
maxDays?: number;
228235
}
229-
): ReturnType<typeof autoCompleteFilter> {
236+
): TimeRangeItem[] {
230237
if (!items) {
231238
return [];
232239
}
@@ -249,9 +256,7 @@ export const _timeRangeAutoCompleteFilter = function <T extends RelativeUnitsMap
249256
supportedPeriods,
250257
})
251258
)
252-
.map((unit, index) =>
253-
makeItem(userSuppliedAmount, unit, supportedPeriods[unit]!.label, index)
254-
);
259+
.map(unit => makeItem(userSuppliedAmount, unit, supportedPeriods[unit]!.label));
255260
}
256261

257262
// If there is a number followed by units, show the matching number/unit option
@@ -274,30 +279,25 @@ export const _timeRangeAutoCompleteFilter = function <T extends RelativeUnitsMap
274279
})
275280
) {
276281
return [
277-
makeItem(
278-
userSuppliedAmount,
279-
matchingUnit,
280-
supportedPeriods[matchingUnit]!.label,
281-
0
282-
),
282+
makeItem(userSuppliedAmount, matchingUnit, supportedPeriods[matchingUnit]!.label),
283283
];
284284
}
285285
}
286286

287287
// Otherwise, do a normal filter search
288-
return autoCompleteFilter(items, filterValue);
288+
return filterItems(items, filterValue);
289289
};
290290

291291
export const timeRangeAutoCompleteFilter = function (
292-
items: ItemsBeforeFilter | null,
292+
items: TimeRangeItem[] | null,
293293
filterValue: string,
294294
options: {
295295
maxDateRange?: number;
296296
maxDays?: number;
297297
supportedPeriods?: RelativeUnitsMapping;
298298
supportedUnits?: RelativePeriodUnit[];
299299
}
300-
): ReturnType<typeof autoCompleteFilter> {
300+
): TimeRangeItem[] {
301301
return _timeRangeAutoCompleteFilter(items, filterValue, {
302302
supportedPeriods: SUPPORTED_RELATIVE_PERIOD_UNITS,
303303
supportedUnits: SUPPORTED_RELATIVE_UNITS_LIST,

static/gsApp/components/features/disabledSelectorItems.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import styled from '@emotion/styled';
22
import omit from 'lodash/omit';
33

4-
import type {Item} from 'sentry/components/dropdownAutoComplete/types';
54
import type SelectorItems from 'sentry/components/timeRangeSelector/selectorItems';
5+
import type {TimeRangeItem} from 'sentry/components/timeRangeSelector/types';
66
import {DEFAULT_RELATIVE_PERIODS, MAX_PICKABLE_DAYS} from 'sentry/constants';
77
import {IconBusiness} from 'sentry/icons';
88
import {t} from 'sentry/locale';
@@ -45,22 +45,19 @@ function DisabledSelectorItems({
4545
: relativePeriods;
4646
const omittedRelativeArr = Object.entries(omittedRelativePeriods);
4747

48-
const items = (onClick: () => void, canTrial: boolean): Item[] => [
48+
const items = (onSelect: () => void, canTrial: boolean): TimeRangeItem[] => [
4949
...(shouldShowRelative
50-
? omittedRelativeArr.map(([value, itemLabel], index) => ({
51-
index,
50+
? omittedRelativeArr.map(([value, itemLabel]) => ({
5251
value,
53-
searchKey: typeof itemLabel === 'string' ? itemLabel : String(value),
52+
textValue: typeof itemLabel === 'string' ? itemLabel : String(value),
5453
label: <SelectorItemLabel>{itemLabel}</SelectorItemLabel>,
55-
'data-test-id': value,
5654
}))
5755
: []),
5856
...(shouldOmitPremiumPeriods
5957
? [
6058
{
61-
index: omittedRelativeArr.length,
6259
value: '90d-trial',
63-
searchKey:
60+
textValue:
6461
typeof relativePeriods['90d'] === 'string'
6562
? relativePeriods['90d']
6663
: '90d-trial',
@@ -78,19 +75,16 @@ function DisabledSelectorItems({
7875
</UpsellMessage>
7976
</SelectorItemLabel>
8077
),
81-
'data-test-id': '90d',
82-
onClick,
78+
onSelect,
8379
},
8480
]
8581
: []),
8682
...(shouldShowAbsolute
8783
? [
8884
{
89-
index: omittedRelativeArr.length + 1,
9085
value: 'absolute',
91-
searchKey: 'absolute',
86+
textValue: 'absolute',
9287
label: <SelectorItemLabel>{t('Absolute date')}</SelectorItemLabel>,
93-
'data-test-id': 'absolute',
9488
},
9589
]
9690
: []),

0 commit comments

Comments
 (0)