1- import React , { useState , useEffect , useMemo } from 'react' ;
1+ import React , { useState , useEffect , useMemo , useRef } from 'react' ;
22import {
33 Dialog ,
44 DialogTitle ,
@@ -100,6 +100,15 @@ const SwitchAccountDialog: React.FC<SwitchAccountDialogProps> = ({
100100 const [ isLanguageReady , setIsLanguageReady ] = useState ( true ) ;
101101 const [ snackbarOpen , setSnackbarOpen ] = useState ( false ) ;
102102
103+ const callbackRef = useRef ( callbackFunction ) ;
104+ const onCloseRef = useRef ( onClose ) ;
105+ const autoConfirmedRef = useRef ( false ) ;
106+
107+ useEffect ( ( ) => {
108+ callbackRef . current = callbackFunction ;
109+ onCloseRef . current = onClose ;
110+ } , [ callbackFunction , onClose ] ) ;
111+
103112 const host = useMemo ( ( ) => {
104113 if ( typeof window !== 'undefined' ) {
105114 return window . location . host ;
@@ -243,13 +252,15 @@ const SwitchAccountDialog: React.FC<SwitchAccountDialogProps> = ({
243252 if ( ! open ) {
244253 // Reset snackbar when dialog closes
245254 setSnackbarOpen ( false ) ;
255+ autoConfirmedRef . current = false ;
246256 return ;
247257 }
248258
249259 setActiveStep ( 0 ) ;
250260 setSelectedTenant ( null ) ;
251261 setSelectedRole ( null ) ;
252262 setSnackbarOpen ( false ) ;
263+ autoConfirmedRef . current = false ;
253264
254265 // Immediate language sync on dialog open to avoid late reflection
255266 if ( typeof window !== 'undefined' ) {
@@ -309,6 +320,9 @@ const SwitchAccountDialog: React.FC<SwitchAccountDialogProps> = ({
309320 useEffect ( ( ) => {
310321 if ( ! open ) return ;
311322
323+ // Prevent repeated auto-confirm loops on rerenders
324+ if ( autoConfirmedRef . current ) return ;
325+
312326 const tenants = visibleTenants ?? [ ] ;
313327
314328 // If no tenants available, close dialog and show error snackbar
@@ -332,15 +346,16 @@ const SwitchAccountDialog: React.FC<SwitchAccountDialogProps> = ({
332346 if ( roles . length === 1 ) {
333347 // Single tenant & single role: confirm and close
334348 const role = roles [ 0 ] ;
349+ autoConfirmedRef . current = true ;
335350 setSelectedTenant ( tenant ) ;
336351 setSelectedRole ( role ) ;
337- callbackFunction (
352+ callbackRef . current (
338353 tenant . tenantId ,
339354 tenant . tenantName ,
340355 role . roleId ,
341356 role . roleName
342357 ) ;
343- onClose ( ) ;
358+ onCloseRef . current ( ) ;
344359 } else if ( roles . length > 1 ) {
345360 // Single tenant & multiple roles: preselect tenant and open role selection
346361 setSelectedTenant ( tenant ) ;
@@ -351,7 +366,7 @@ const SwitchAccountDialog: React.FC<SwitchAccountDialogProps> = ({
351366 setActiveStep ( 0 ) ;
352367 }
353368 // }, [open, visibleTenants, callbackFunction, onClose]);
354- } , [ open , visibleTenants , callbackFunction , onClose ] ) ;
369+ } , [ open , visibleTenants ] ) ;
355370
356371 const handleTenantSelect = ( tenant : TenantData ) => {
357372 // Prevent selection of archived tenants
0 commit comments