Skip to content

Commit 1db1d17

Browse files
nikkikapadiaandrewshie-sentry
authored andcommitted
feat(widget-builder): Add limit field to widget builder hook (#81944)
Added the limit field to widget builder hook and made test. I put it as a number and use a deserializer to go from query param string to number. Let me know if there was a different way you were thinking of doing it.
1 parent 8e4858c commit 1db1d17

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

Diff for: static/app/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState.spec.tsx

+27
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,31 @@ describe('useWidgetBuilderState', () => {
266266
expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'asc'}]);
267267
});
268268
});
269+
270+
describe('limit', () => {
271+
it('can decode and update limit', () => {
272+
mockedUsedLocation.mockReturnValue(
273+
LocationFixture({
274+
query: {
275+
limit: '4',
276+
},
277+
})
278+
);
279+
280+
const {result} = renderHook(() => useWidgetBuilderState(), {
281+
wrapper: WidgetBuilderProvider,
282+
});
283+
284+
expect(result.current.state.limit).toEqual(4);
285+
286+
act(() => {
287+
result.current.dispatch({
288+
type: BuilderStateAction.SET_LIMIT,
289+
payload: 10,
290+
});
291+
});
292+
293+
expect(result.current.state.limit).toEqual(10);
294+
});
295+
});
269296
});

Diff for: static/app/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState.tsx

+30-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import {
66
generateFieldAsString,
77
type Sort,
88
} from 'sentry/utils/discover/fields';
9-
import {decodeList, decodeSorts} from 'sentry/utils/queryString';
9+
import {
10+
decodeInteger,
11+
decodeList,
12+
decodeScalar,
13+
decodeSorts,
14+
} from 'sentry/utils/queryString';
1015
import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
1116
import {useQueryParamState} from 'sentry/views/dashboards/widgetBuilder/hooks/useQueryParamState';
17+
import {DEFAULT_RESULTS_LIMIT} from 'sentry/views/dashboards/widgetBuilder/utils';
1218

1319
export type WidgetBuilderStateQueryParams = {
1420
dataset?: WidgetType;
@@ -30,6 +36,7 @@ export const BuilderStateAction = {
3036
SET_Y_AXIS: 'SET_Y_AXIS',
3137
SET_QUERY: 'SET_QUERY',
3238
SET_SORT: 'SET_SORT',
39+
SET_LIMIT: 'SET_LIMIT',
3340
} as const;
3441

3542
type WidgetAction =
@@ -40,13 +47,15 @@ type WidgetAction =
4047
| {payload: Column[]; type: typeof BuilderStateAction.SET_FIELDS}
4148
| {payload: Column[]; type: typeof BuilderStateAction.SET_Y_AXIS}
4249
| {payload: string[]; type: typeof BuilderStateAction.SET_QUERY}
43-
| {payload: Sort[]; type: typeof BuilderStateAction.SET_SORT};
50+
| {payload: Sort[]; type: typeof BuilderStateAction.SET_SORT}
51+
| {payload: number; type: typeof BuilderStateAction.SET_LIMIT};
4452

4553
export interface WidgetBuilderState {
4654
dataset?: WidgetType;
4755
description?: string;
4856
displayType?: DisplayType;
4957
fields?: Column[];
58+
limit?: number;
5059
query?: string[];
5160
sort?: Sort[];
5261
title?: string;
@@ -90,10 +99,15 @@ function useWidgetBuilderState(): {
9099
decoder: decodeSorts,
91100
serializer: serializeSorts,
92101
});
102+
const [limit, setLimit] = useQueryParamState<number>({
103+
fieldName: 'limit',
104+
decoder: decodeScalar,
105+
deserializer: deserializeLimit,
106+
});
93107

94108
const state = useMemo(
95-
() => ({title, description, displayType, dataset, fields, yAxis, query, sort}),
96-
[title, description, displayType, dataset, fields, yAxis, query, sort]
109+
() => ({title, description, displayType, dataset, fields, yAxis, query, sort, limit}),
110+
[title, description, displayType, dataset, fields, yAxis, query, sort, limit]
97111
);
98112

99113
const dispatch = useCallback(
@@ -126,6 +140,9 @@ function useWidgetBuilderState(): {
126140
case BuilderStateAction.SET_SORT:
127141
setSort(action.payload);
128142
break;
143+
case BuilderStateAction.SET_LIMIT:
144+
setLimit(action.payload);
145+
break;
129146
default:
130147
break;
131148
}
@@ -139,6 +156,7 @@ function useWidgetBuilderState(): {
139156
setYAxis,
140157
setQuery,
141158
setSort,
159+
setLimit,
142160
]
143161
);
144162

@@ -193,4 +211,12 @@ function serializeSorts(sorts: Sort[]): string[] {
193211
});
194212
}
195213

214+
/**
215+
* Decodes the limit from the query params
216+
* Returns the default limit if the value is not a valid limit
217+
*/
218+
function deserializeLimit(value: string): number {
219+
return decodeInteger(value, DEFAULT_RESULTS_LIMIT);
220+
}
221+
196222
export default useWidgetBuilderState;

0 commit comments

Comments
 (0)