1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313
14- import { useCallback , useMemo , useState } from 'react' ;
14+ import { useState } from 'react' ;
1515
1616import { QueryServiceClient } from '@parca/client' ;
1717import { DateTimeRange } from '@parca/components' ;
@@ -84,46 +84,40 @@ export const useSumBySelection = (
8484 }
8585 }
8686
87- const setSumBy = useCallback (
88- ( sumBy : string [ ] ) => {
89- setUserSelectedSumBy ( prev => {
90- if ( profileType == null ) {
91- return prev ;
92- }
93-
94- return {
95- ...prev ,
96- [ profileType . toString ( ) ] : sumBy ,
97- } ;
98- } ) ;
99- } ,
100- [ setUserSelectedSumBy , profileType ]
101- ) ;
87+ const setSumBy = ( sumBy : string [ ] ) : void => {
88+ setUserSelectedSumBy ( prev => {
89+ if ( profileType == null ) {
90+ return prev ;
91+ }
10292
103- const { defaultSumBy} = useDefaultSumBy ( profileType , labelNamesLoading , labels ) ;
93+ return {
94+ ...prev ,
95+ [ profileType . toString ( ) ] : sumBy ,
96+ } ;
97+ } ) ;
98+ } ;
10499
105- const sumBy = useMemo ( ( ) => {
106- // For smoother UX, return draftSumBy first if available during loading
107- // as this must be recently computed with the draft time range labels.
108- if ( labelNamesLoading && draftSumBy !== undefined ) {
109- return draftSumBy ;
110- }
100+ const { defaultSumBy} = useDefaultSumBy ( profileType , labelNamesLoading , labels ) ;
111101
102+ // For smoother UX, return draftSumBy first if available during loading
103+ // as this must be recently computed with the draft time range labels.
104+ let sumBy : string [ ] | undefined ;
105+ if ( labelNamesLoading && draftSumBy !== undefined ) {
106+ sumBy = draftSumBy ;
107+ } else {
112108 // Prefer non-empty URL default over auto-computed default to avoid a
113109 // one-render race where defaultSumBy overwrites the default value from upstream.
114110 const hasExplicitDefault = defaultValue != null && defaultValue . length > 0 ;
115- let result =
111+ sumBy =
116112 userSelectedSumBy [ profileType ?. toString ( ) ?? '' ] ??
117113 ( hasExplicitDefault ? defaultValue : undefined ) ??
118114 defaultSumBy ??
119115 DEFAULT_EMPTY_SUM_BY ;
120116
121117 if ( profileType ?. delta !== true ) {
122- result = DEFAULT_EMPTY_SUM_BY ;
118+ sumBy = DEFAULT_EMPTY_SUM_BY ;
123119 }
124-
125- return result ;
126- } , [ userSelectedSumBy , profileType , defaultSumBy , labelNamesLoading , draftSumBy , defaultValue ] ) ;
120+ }
127121
128122 return [
129123 sumBy ,
@@ -139,9 +133,7 @@ export const useDefaultSumBy = (
139133 labelNamesLoading : boolean ,
140134 labels : string [ ] | undefined
141135) : { defaultSumBy : string [ ] | undefined ; isLoading : boolean } => {
142- const defaultSumBy = useMemo ( ( ) => {
143- return getDefaultSumBy ( profileType , labels ) ;
144- } , [ profileType , labels ] ) ;
136+ const defaultSumBy = getDefaultSumBy ( profileType , labels ) ;
145137
146138 return { defaultSumBy, isLoading : labelNamesLoading } ;
147139} ;
@@ -170,11 +162,7 @@ const getSumByFromParam = (param: string | string[] | undefined): string[] | und
170162} ;
171163
172164export const useSumByFromParams = ( param : string | string [ ] | undefined ) : string [ ] | undefined => {
173- const sumBy = useMemo ( ( ) => {
174- return getSumByFromParam ( param ) ;
175- } , [ param ] ) ;
176-
177- return sumBy ;
165+ return getSumByFromParam ( param ) ;
178166} ;
179167
180168export const sumByToParam = ( sumBy : string [ ] | undefined ) : string | string [ ] | undefined => {
@@ -219,9 +207,7 @@ export const useSumBy = (
219207 defaultValue
220208 ) ;
221209
222- const labels = useMemo ( ( ) => {
223- return result . response ?. labelNames === undefined ? [ ] : result . response . labelNames ;
224- } , [ result ] ) ;
210+ const labels = result . response ?. labelNames === undefined ? [ ] : result . response . labelNames ;
225211
226212 const [ sumBySelection , setSumByInternal , { isLoading} ] = useSumBySelection (
227213 profileType ,
@@ -259,9 +245,7 @@ export const useDraftSumBy = (
259245 timeRange . getToMs ( )
260246 ) ;
261247
262- const labels = useMemo ( ( ) => {
263- return result . response ?. labelNames === undefined ? [ ] : result . response . labelNames ;
264- } , [ result ] ) ;
248+ const labels = result . response ?. labelNames === undefined ? [ ] : result . response . labelNames ;
265249
266250 const { defaultSumBy, isLoading} = useDefaultSumBy ( profileType , labelNamesLoading , labels ) ;
267251
0 commit comments