@@ -2,9 +2,18 @@ import {
22 useChildAccountsInfiniteQuery ,
33 useMyDelegatedChildAccountsQuery ,
44} from '@linode/queries' ;
5- import { Drawer , LinkButton , Notice , Typography } from '@linode/ui' ;
5+ import {
6+ Button ,
7+ Drawer ,
8+ LinkButton ,
9+ Notice ,
10+ Stack ,
11+ Typography ,
12+ useTheme ,
13+ } from '@linode/ui' ;
614import React , { useMemo , useState } from 'react' ;
715
16+ import ErrorStateCloud from 'src/assets/icons/error-state-cloud.svg' ;
817import { DebouncedSearchTextField } from 'src/components/DebouncedSearchTextField' ;
918import { PARENT_USER_SESSION_EXPIRED } from 'src/features/Account/constants' ;
1019import { useParentChildAuthentication } from 'src/features/Account/SwitchAccounts/useParentChildAuthentication' ;
@@ -35,11 +44,14 @@ interface HandleSwitchToChildAccountProps {
3544
3645export const SwitchAccountDrawer = ( props : Props ) => {
3746 const { onClose, open, userType } = props ;
47+ const theme = useTheme ( ) ;
3848 const [ isSubmitting , setSubmitting ] = React . useState < boolean > ( false ) ;
3949 const [ isParentTokenError , setIsParentTokenError ] = React . useState <
4050 APIError [ ]
4151 > ( [ ] ) ;
4252 const [ searchQuery , setSearchQuery ] = React . useState < string > ( '' ) ;
53+ const [ page , setPage ] = useState ( 1 ) ;
54+ const [ pageSize , setPageSize ] = useState ( 25 ) ;
4355 const { isIAMDelegationEnabled } = useIsIAMDelegationEnabled ( ) ;
4456 const isParentUserType = userType === 'parent' ;
4557 const isProxyUserType = userType === 'proxy' ;
@@ -92,15 +104,14 @@ export const SwitchAccountDrawer = (props: Props) => {
92104 isRefetching : allChildAccountsIsRefetching ,
93105 refetch : refetchAllChildAccounts ,
94106 } = useMyDelegatedChildAccountsQuery ( {
95- params : { } ,
107+ params : {
108+ page,
109+ page_size : pageSize ,
110+ } ,
96111 filter,
97112 enabled : isIAMDelegationEnabled && isParentUserType ,
98113 } ) ;
99114
100- const refetchFn = isIAMDelegationEnabled
101- ? refetchAllChildAccounts
102- : refetchChildAccounts ;
103-
104115 const handleSwitchToChildAccount = React . useCallback (
105116 async ( {
106117 currentTokenWithBearer,
@@ -190,6 +201,17 @@ export const SwitchAccountDrawer = (props: Props) => {
190201 const [ isSwitchingChildAccounts , setIsSwitchingChildAccounts ] =
191202 useState < boolean > ( false ) ;
192203
204+ const isLoading =
205+ isInitialLoading ||
206+ isSubmitting ||
207+ isSwitchingChildAccounts ||
208+ isRefetching ||
209+ allChildAccountsLoading ||
210+ allChildAccountsIsRefetching ;
211+
212+ const refetchFn = isIAMDelegationEnabled
213+ ? refetchAllChildAccounts
214+ : refetchChildAccounts ;
193215 const handleClose = ( ) => {
194216 setIsSwitchingChildAccounts ( false ) ;
195217 setSearchQuery ( '' ) ;
@@ -203,6 +225,15 @@ export const SwitchAccountDrawer = (props: Props) => {
203225 return data ?. pages . flatMap ( ( page ) => page . data ) ;
204226 } , [ isIAMDelegationEnabled , allChildAccounts , data ] ) ;
205227
228+ const handlePageChange = ( newPage : number ) => {
229+ setPage ( newPage ) ;
230+ } ;
231+
232+ const handlePageSizeChange = ( newPageSize : number ) => {
233+ setPageSize ( newPageSize ) ;
234+ setPage ( 1 ) ; // Reset to first page when page size changes
235+ } ;
236+
206237 return (
207238 < Drawer onClose = { handleClose } open = { open } title = "Switch Account" >
208239 { createTokenErrorReason && (
@@ -234,50 +265,67 @@ export const SwitchAccountDrawer = (props: Props) => {
234265 ) }
235266 .
236267 </ Typography >
237- { isIAMDelegationEnabled && allChildAccounts && (
268+
269+ { ( childAccountInfiniteError || allChildAccountsError ) && (
270+ < Stack alignItems = "center" gap = { 1 } justifyContent = "center" >
271+ < ErrorStateCloud />
272+ < Typography > Unable to load data.</ Typography >
273+ < Typography >
274+ Try again or contact support if the issue persists.
275+ </ Typography >
276+ < Button
277+ buttonType = "primary"
278+ onClick = { ( ) => refetchFn ( ) }
279+ sx = { ( theme ) => ( {
280+ marginTop : theme . spacingFunction ( 16 ) ,
281+ } ) }
282+ >
283+ Try again
284+ </ Button >
285+ </ Stack >
286+ ) }
287+ { ! ( childAccountInfiniteError || allChildAccountsError ) && (
238288 < >
239289 < DebouncedSearchTextField
240290 clearable
241291 debounceTime = { 250 }
242292 hideLabel
243- key = { `iam -search-${ searchQuery } ` }
293+ key = { `switch -search-${ searchQuery } ` }
244294 label = "Search"
245295 onSearch = { setSearchQuery }
246296 placeholder = "Search"
247- sx = { { marginBottom : 2 } }
297+ sx = { { marginBottom : theme . spacingFunction ( 12 ) } }
248298 value = { searchQuery }
249299 />
250- { searchQuery && childAccounts && childAccounts . length === 0 && (
251- < Typography sx = { { fontStyle : 'italic' } } >
252- No search results
253- </ Typography >
254- ) }
300+ { searchQuery &&
301+ childAccounts &&
302+ childAccounts . length === 0 &&
303+ ! isLoading && (
304+ < Typography
305+ sx = { {
306+ fontStyle : 'italic' ,
307+ marginTop : theme . spacingFunction ( 6 ) ,
308+ } }
309+ >
310+ No search results
311+ </ Typography >
312+ ) }
255313 </ >
256314 ) }
257315 { isIAMDelegationEnabled && (
258316 < ChildAccountsTable
259317 childAccounts = { childAccounts }
260318 currentTokenWithBearer = { currentTokenWithBearer }
261- errors = { {
262- childAccountInfiniteError,
263- allChildAccountsError : allChildAccountsError ?. [ 0 ]
264- ? new Error ( allChildAccountsError [ 0 ] . reason )
265- : null ,
266- } }
267- filter = { filter }
268- isLoading = {
269- isInitialLoading ||
270- isSubmitting ||
271- isSwitchingChildAccounts ||
272- isRefetching ||
273- allChildAccountsLoading ||
274- allChildAccountsIsRefetching
275- }
319+ isLoading = { isLoading }
276320 isSwitchingChildAccounts = { isSwitchingChildAccounts }
277321 onClose = { onClose }
322+ onPageChange = { handlePageChange }
323+ onPageSizeChange = { handlePageSizeChange }
278324 onSwitchAccount = { handleSwitchToChildAccount }
279- refetchFn = { refetchFn }
325+ page = { page }
326+ pageSize = { pageSize }
280327 setIsSwitchingChildAccounts = { setIsSwitchingChildAccounts }
328+ totalResults = { allChildAccounts ?. results || 0 }
281329 userType = { userType }
282330 />
283331 ) }
@@ -299,14 +347,7 @@ export const SwitchAccountDrawer = (props: Props) => {
299347 filter = { filter }
300348 hasNextPage = { hasNextPage }
301349 isFetchingNextPage = { isFetchingNextPage }
302- isLoading = {
303- isInitialLoading ||
304- isSubmitting ||
305- isSwitchingChildAccounts ||
306- isRefetching ||
307- allChildAccountsLoading ||
308- allChildAccountsIsRefetching
309- }
350+ isLoading = { isLoading }
310351 isSwitchingChildAccounts = { isSwitchingChildAccounts }
311352 onClose = { onClose }
312353 onSwitchAccount = { handleSwitchToChildAccount }
0 commit comments