Skip to content

feat(widget-builder): Add limit field to widget builder hook #81944

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
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading