@@ -6,9 +6,15 @@ import {
6
6
generateFieldAsString ,
7
7
type Sort ,
8
8
} 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' ;
10
15
import { DisplayType , WidgetType } from 'sentry/views/dashboards/types' ;
11
16
import { useQueryParamState } from 'sentry/views/dashboards/widgetBuilder/hooks/useQueryParamState' ;
17
+ import { DEFAULT_RESULTS_LIMIT } from 'sentry/views/dashboards/widgetBuilder/utils' ;
12
18
13
19
export type WidgetBuilderStateQueryParams = {
14
20
dataset ?: WidgetType ;
@@ -30,6 +36,7 @@ export const BuilderStateAction = {
30
36
SET_Y_AXIS : 'SET_Y_AXIS' ,
31
37
SET_QUERY : 'SET_QUERY' ,
32
38
SET_SORT : 'SET_SORT' ,
39
+ SET_LIMIT : 'SET_LIMIT' ,
33
40
} as const ;
34
41
35
42
type WidgetAction =
@@ -40,13 +47,15 @@ type WidgetAction =
40
47
| { payload : Column [ ] ; type : typeof BuilderStateAction . SET_FIELDS }
41
48
| { payload : Column [ ] ; type : typeof BuilderStateAction . SET_Y_AXIS }
42
49
| { 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 } ;
44
52
45
53
export interface WidgetBuilderState {
46
54
dataset ?: WidgetType ;
47
55
description ?: string ;
48
56
displayType ?: DisplayType ;
49
57
fields ?: Column [ ] ;
58
+ limit ?: number ;
50
59
query ?: string [ ] ;
51
60
sort ?: Sort [ ] ;
52
61
title ?: string ;
@@ -90,10 +99,15 @@ function useWidgetBuilderState(): {
90
99
decoder : decodeSorts ,
91
100
serializer : serializeSorts ,
92
101
} ) ;
102
+ const [ limit , setLimit ] = useQueryParamState < number > ( {
103
+ fieldName : 'limit' ,
104
+ decoder : decodeScalar ,
105
+ deserializer : deserializeLimit ,
106
+ } ) ;
93
107
94
108
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 ]
97
111
) ;
98
112
99
113
const dispatch = useCallback (
@@ -126,6 +140,9 @@ function useWidgetBuilderState(): {
126
140
case BuilderStateAction . SET_SORT :
127
141
setSort ( action . payload ) ;
128
142
break ;
143
+ case BuilderStateAction . SET_LIMIT :
144
+ setLimit ( action . payload ) ;
145
+ break ;
129
146
default :
130
147
break ;
131
148
}
@@ -139,6 +156,7 @@ function useWidgetBuilderState(): {
139
156
setYAxis ,
140
157
setQuery ,
141
158
setSort ,
159
+ setLimit ,
142
160
]
143
161
) ;
144
162
@@ -193,4 +211,12 @@ function serializeSorts(sorts: Sort[]): string[] {
193
211
} ) ;
194
212
}
195
213
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
+
196
222
export default useWidgetBuilderState ;
0 commit comments