@@ -36,7 +36,7 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
3636
3737 const getUser = useCallback (
3838 async ( forceRefetch : boolean = false ) : Promise < UserProps | null > => {
39- if ( user && ! forceRefetch ) {
39+ if ( user && ! forceRefetch && loading === false ) {
4040 // If user data already exists and we're not forcing a refetch, return it without making a new request
4141 return user
4242 }
@@ -60,7 +60,14 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
6060 }
6161 }
6262
63- setLoading ( true )
63+ // Only set loading to true if this is not a force refetch
64+ // This prevents flickering when user data is being revalidated
65+ if ( forceRefetch ) {
66+ // Don't change the loading state when forcing refetch for smoother UX
67+ } else {
68+ setLoading ( true )
69+ }
70+
6471 setError ( null )
6572
6673 const response = await api . get < UserProps > ( `${ ROUTES . USER_INFO } ` )
@@ -92,15 +99,26 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
9299 return null
93100 }
94101 } finally {
95- setLoading ( false )
102+ // Only set loading to false if not forcing a refetch (to maintain current state during refresh)
103+ if ( ! forceRefetch ) {
104+ setLoading ( false )
105+ } else {
106+ // For forced refetch, ensure loading is eventually set to false
107+ setTimeout ( ( ) => {
108+ if ( typeof window !== 'undefined' ) {
109+ setLoading ( false )
110+ }
111+ } , 0 )
112+ }
96113 }
97114 } ,
98- [ user ] ,
115+ [ user , loading ] ,
99116 )
100117
101- const refetch = useCallback ( ( ) => {
102- setUser ( null )
103- void getUser ( true ) // force refetch
118+ const refetch = useCallback ( async ( ) => {
119+ setLoading ( true ) // Set loading state during refetch
120+ // Don't reset user to null during refetch to avoid UI flickering
121+ return getUser ( true ) // force refetch and return the result
104122 } , [ getUser ] )
105123
106124 // Initialize user data on mount
@@ -132,6 +150,67 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
132150 }
133151 } , [ getUser ] )
134152
153+ // Listen for token refresh events to update user info
154+ useEffect ( ( ) => {
155+ const handleTokenRefresh = ( ) => {
156+ if ( typeof window !== 'undefined' ) {
157+ const accessToken = getCookie ( 'access' )
158+ if ( accessToken ) {
159+ // Token was refreshed, refetch user data
160+ void refetch ( )
161+ }
162+ }
163+ }
164+
165+ if ( typeof window !== 'undefined' ) {
166+ window . addEventListener ( 'tokenRefreshed' , handleTokenRefresh )
167+ }
168+
169+ return ( ) => {
170+ if ( typeof window !== 'undefined' ) {
171+ window . removeEventListener ( 'tokenRefreshed' , handleTokenRefresh )
172+ }
173+ }
174+ } , [ refetch ] )
175+
176+ // Listen for login and logout events to update user state accordingly
177+ useEffect ( ( ) => {
178+ const handleLoginCompleted = ( ) => {
179+ if ( typeof window !== 'undefined' ) {
180+ const accessToken = getCookie ( 'access' )
181+ if ( accessToken ) {
182+ // New login, fetch user data
183+ void getUser ( true ) // force refetch
184+ }
185+ }
186+ }
187+
188+ const handleLogoutCompleted = ( ) => {
189+ // Clear user data on logout
190+ setUser ( null )
191+ setLoading ( false )
192+ setError ( null )
193+ }
194+
195+ if ( typeof window !== 'undefined' ) {
196+ window . addEventListener ( 'loginCompleted' , handleLoginCompleted )
197+ window . addEventListener ( 'logoutCompleted' , handleLogoutCompleted )
198+ }
199+
200+ return ( ) => {
201+ if ( typeof window !== 'undefined' ) {
202+ window . removeEventListener (
203+ 'loginCompleted' ,
204+ handleLoginCompleted ,
205+ )
206+ window . removeEventListener (
207+ 'logoutCompleted' ,
208+ handleLogoutCompleted ,
209+ )
210+ }
211+ }
212+ } , [ getUser ] )
213+
135214 const value = {
136215 user,
137216 loading,
0 commit comments