@@ -6,6 +6,7 @@ import dayjs from 'dayjs';
66import { useEffect , useMemo , useRef , useState } from 'react' ;
77import { useSearchParams } from 'react-router-dom' ;
88import { api } from '@/services/api' ;
9+ import { patchSearchParams } from '@/utils/helper' ;
910import { useAppList , useUserInfo } from '@/utils/hooks' ;
1011
1112const { Title } = Typography ;
@@ -85,22 +86,22 @@ const attributeOptions = [
8586] ;
8687
8788export const Component = ( ) => {
88- const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
89+ const [ searchParams , setSearchParams ] = useSearchParams ( { attribute : 'hash' } ) ;
8990 const [ dateRange , setDateRange ] = useState < [ Dayjs , Dayjs ] > ( [
9091 dayjs ( ) . subtract ( 24 , 'hour' ) ,
9192 dayjs ( ) ,
9293 ] ) ;
93- const [ selectedAppKey , setSelectedAppKey ] = useState < string | undefined > (
94- searchParams . get ( 'appKey' ) || undefined ,
95- ) ;
9694 const [ manualAppKey , setManualAppKey ] = useState ( '' ) ;
97- const [ selectedAttribute , setSelectedAttribute ] =
98- useState < MetricAttribute > ( 'hash' ) ;
9995 const legendValuesRef = useRef < string [ ] > ( [ ] ) ;
10096
10197 const { apps } = useAppList ( ) ;
10298 const { user } = useUserInfo ( ) ;
10399 const isAdmin = user ?. admin === true ;
100+ const urlAppKey = searchParams . get ( 'appKey' ) || undefined ;
101+ const selectedAttribute : MetricAttribute =
102+ searchParams . get ( 'attribute' ) === 'packageVersion_buildTime'
103+ ? 'packageVersion_buildTime'
104+ : 'hash' ;
104105
105106 const appOptions = useMemo ( ( ) => {
106107 return ( apps || [ ] ) . reduce < { label : string ; value : string } [ ] > (
@@ -116,42 +117,36 @@ export const Component = () => {
116117 [ ] ,
117118 ) ;
118119 } , [ apps ] ) ;
119-
120- // Sync URL param to state on mount or URL change
121- useEffect ( ( ) => {
122- const urlAppKey = searchParams . get ( 'appKey' ) ;
123- if ( urlAppKey && urlAppKey !== selectedAppKey ) {
124- // Admin can access any appKey, non-admin can only access their own
125- if ( isAdmin || appOptions . some ( ( opt ) => opt . value === urlAppKey ) ) {
126- setSelectedAppKey ( urlAppKey ) ;
127- }
120+ const selectedAppKey = useMemo ( ( ) => {
121+ if ( ! urlAppKey ) {
122+ return undefined ;
123+ }
124+ if ( isAdmin || appOptions . some ( ( opt ) => opt . value === urlAppKey ) ) {
125+ return urlAppKey ;
128126 }
129- } , [ searchParams , isAdmin , appOptions , selectedAppKey ] ) ;
127+ return undefined ;
128+ } , [ appOptions , isAdmin , urlAppKey ] ) ;
130129
131130 // Default to first app if no selection
132131 useEffect ( ( ) => {
133- if (
134- ! selectedAppKey &&
135- appOptions . length > 0 &&
136- ! searchParams . get ( 'appKey' )
137- ) {
138- const firstAppKey = appOptions [ 0 ] . value ;
139- setSelectedAppKey ( firstAppKey ) ;
140- setSearchParams ( { appKey : firstAppKey } , { replace : true } ) ;
132+ if ( ! urlAppKey && appOptions . length > 0 ) {
133+ patchSearchParams ( setSearchParams , {
134+ appKey : appOptions [ 0 ] . value ,
135+ } ) ;
141136 }
142- } , [ appOptions , selectedAppKey , searchParams , setSearchParams ] ) ;
137+ } , [ appOptions , setSearchParams , urlAppKey ] ) ;
143138
144139 // Update URL when selection changes
145140 const handleAppChange = ( appKey : string ) => {
146- setSelectedAppKey ( appKey ) ;
147- setSearchParams ( { appKey } , { replace : true } ) ;
141+ patchSearchParams ( setSearchParams , { appKey } ) ;
148142 } ;
149143
150144 // Admin manual appKey input
151145 const handleManualAppKeySubmit = ( ) => {
152146 if ( manualAppKey . trim ( ) ) {
153- setSelectedAppKey ( manualAppKey . trim ( ) ) ;
154- setSearchParams ( { appKey : manualAppKey . trim ( ) } , { replace : true } ) ;
147+ patchSearchParams ( setSearchParams , {
148+ appKey : manualAppKey . trim ( ) ,
149+ } ) ;
155150 setManualAppKey ( '' ) ;
156151 }
157152 } ;
@@ -355,7 +350,11 @@ export const Component = () => {
355350 </ div >
356351 < Radio . Group
357352 value = { selectedAttribute }
358- onChange = { ( e ) => setSelectedAttribute ( e . target . value ) }
353+ onChange = { ( e ) => {
354+ patchSearchParams ( setSearchParams , {
355+ attribute : e . target . value as MetricAttribute ,
356+ } ) ;
357+ } }
359358 optionType = "button"
360359 buttonStyle = "solid"
361360 >
0 commit comments