@@ -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' ;
1015import { DisplayType , WidgetType } from 'sentry/views/dashboards/types' ;
1116import { useQueryParamState } from 'sentry/views/dashboards/widgetBuilder/hooks/useQueryParamState' ;
17+ import { DEFAULT_RESULTS_LIMIT } from 'sentry/views/dashboards/widgetBuilder/utils' ;
1218
1319export 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
3542type 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
4553export 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+
196222export default useWidgetBuilderState ;
0 commit comments