Skip to content

Commit

Permalink
feat(widget-builder): Add limit field to widget builder hook (#81944)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikkikapadia authored and andrewshie-sentry committed Jan 2, 2025
1 parent 8e4858c commit 1db1d17
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,31 @@ describe('useWidgetBuilderState', () => {
expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'asc'}]);
});
});

describe('limit', () => {
it('can decode and update limit', () => {
mockedUsedLocation.mockReturnValue(
LocationFixture({
query: {
limit: '4',
},
})
);

const {result} = renderHook(() => useWidgetBuilderState(), {
wrapper: WidgetBuilderProvider,
});

expect(result.current.state.limit).toEqual(4);

act(() => {
result.current.dispatch({
type: BuilderStateAction.SET_LIMIT,
payload: 10,
});
});

expect(result.current.state.limit).toEqual(10);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {
generateFieldAsString,
type Sort,
} from 'sentry/utils/discover/fields';
import {decodeList, decodeSorts} from 'sentry/utils/queryString';
import {
decodeInteger,
decodeList,
decodeScalar,
decodeSorts,
} from 'sentry/utils/queryString';
import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
import {useQueryParamState} from 'sentry/views/dashboards/widgetBuilder/hooks/useQueryParamState';
import {DEFAULT_RESULTS_LIMIT} from 'sentry/views/dashboards/widgetBuilder/utils';

export type WidgetBuilderStateQueryParams = {
dataset?: WidgetType;
Expand All @@ -30,6 +36,7 @@ export const BuilderStateAction = {
SET_Y_AXIS: 'SET_Y_AXIS',
SET_QUERY: 'SET_QUERY',
SET_SORT: 'SET_SORT',
SET_LIMIT: 'SET_LIMIT',
} as const;

type WidgetAction =
Expand All @@ -40,13 +47,15 @@ type WidgetAction =
| {payload: Column[]; type: typeof BuilderStateAction.SET_FIELDS}
| {payload: Column[]; type: typeof BuilderStateAction.SET_Y_AXIS}
| {payload: string[]; type: typeof BuilderStateAction.SET_QUERY}
| {payload: Sort[]; type: typeof BuilderStateAction.SET_SORT};
| {payload: Sort[]; type: typeof BuilderStateAction.SET_SORT}
| {payload: number; type: typeof BuilderStateAction.SET_LIMIT};

export interface WidgetBuilderState {
dataset?: WidgetType;
description?: string;
displayType?: DisplayType;
fields?: Column[];
limit?: number;
query?: string[];
sort?: Sort[];
title?: string;
Expand Down Expand Up @@ -90,10 +99,15 @@ function useWidgetBuilderState(): {
decoder: decodeSorts,
serializer: serializeSorts,
});
const [limit, setLimit] = useQueryParamState<number>({
fieldName: 'limit',
decoder: decodeScalar,
deserializer: deserializeLimit,
});

const state = useMemo(
() => ({title, description, displayType, dataset, fields, yAxis, query, sort}),
[title, description, displayType, dataset, fields, yAxis, query, sort]
() => ({title, description, displayType, dataset, fields, yAxis, query, sort, limit}),
[title, description, displayType, dataset, fields, yAxis, query, sort, limit]
);

const dispatch = useCallback(
Expand Down Expand Up @@ -126,6 +140,9 @@ function useWidgetBuilderState(): {
case BuilderStateAction.SET_SORT:
setSort(action.payload);
break;
case BuilderStateAction.SET_LIMIT:
setLimit(action.payload);
break;
default:
break;
}
Expand All @@ -139,6 +156,7 @@ function useWidgetBuilderState(): {
setYAxis,
setQuery,
setSort,
setLimit,
]
);

Expand Down Expand Up @@ -193,4 +211,12 @@ function serializeSorts(sorts: Sort[]): string[] {
});
}

/**
* Decodes the limit from the query params
* Returns the default limit if the value is not a valid limit
*/
function deserializeLimit(value: string): number {
return decodeInteger(value, DEFAULT_RESULTS_LIMIT);
}

export default useWidgetBuilderState;

0 comments on commit 1db1d17

Please sign in to comment.