| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { SWRConfig } from 'swr' | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  | import { useCallback, useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  | import type { ReactNode } from 'react' | 
					
						
							| 
									
										
										
										
											2024-10-23 17:48:57 +08:00
										 |  |  | import { usePathname, useRouter, useSearchParams } from 'next/navigation' | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  | import { fetchSetupStatus } from '@/service/common' | 
					
						
							| 
									
										
										
										
											2025-04-01 13:58:10 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   EDUCATION_VERIFYING_LOCALSTORAGE_ITEM, | 
					
						
							|  |  |  |   EDUCATION_VERIFY_URL_SEARCHPARAMS_ACTION, | 
					
						
							|  |  |  | } from '@/app/education-apply/constants' | 
					
						
							| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  | type SwrInitorProps = { | 
					
						
							| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  |   children: ReactNode | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | const SwrInitor = ({ | 
					
						
							|  |  |  |   children, | 
					
						
							|  |  |  | }: SwrInitorProps) => { | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |   const router = useRouter() | 
					
						
							|  |  |  |   const searchParams = useSearchParams() | 
					
						
							| 
									
										
										
										
											2024-11-05 12:38:31 +08:00
										 |  |  |   const consoleToken = decodeURIComponent(searchParams.get('access_token') || '') | 
					
						
							|  |  |  |   const refreshToken = decodeURIComponent(searchParams.get('refresh_token') || '') | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |   const consoleTokenFromLocalStorage = localStorage?.getItem('console_token') | 
					
						
							| 
									
										
										
										
											2024-10-12 23:40:38 +08:00
										 |  |  |   const refreshTokenFromLocalStorage = localStorage?.getItem('refresh_token') | 
					
						
							| 
									
										
										
										
											2024-11-05 12:38:31 +08:00
										 |  |  |   const pathname = usePathname() | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |   const [init, setInit] = useState(false) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |   const isSetupFinished = useCallback(async () => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       if (localStorage.getItem('setup_status') === 'finished') | 
					
						
							|  |  |  |         return true | 
					
						
							|  |  |  |       const setUpStatus = await fetchSetupStatus() | 
					
						
							|  |  |  |       if (setUpStatus.step !== 'finished') { | 
					
						
							|  |  |  |         localStorage.removeItem('setup_status') | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       localStorage.setItem('setup_status', 'finished') | 
					
						
							|  |  |  |       return true | 
					
						
							| 
									
										
										
										
											2024-10-12 23:40:38 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |     catch (error) { | 
					
						
							|  |  |  |       console.error(error) | 
					
						
							|  |  |  |       return false | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     (async () => { | 
					
						
							| 
									
										
										
										
											2025-04-01 13:58:10 +08:00
										 |  |  |       const action = searchParams.get('action') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (action === EDUCATION_VERIFY_URL_SEARCHPARAMS_ACTION) | 
					
						
							|  |  |  |         localStorage.setItem(EDUCATION_VERIFYING_LOCALSTORAGE_ITEM, 'yes') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |       try { | 
					
						
							|  |  |  |         const isFinished = await isSetupFinished() | 
					
						
							|  |  |  |         if (!isFinished) { | 
					
						
							|  |  |  |           router.replace('/install') | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-05 12:38:31 +08:00
										 |  |  |         if (!((consoleToken && refreshToken) || (consoleTokenFromLocalStorage && refreshTokenFromLocalStorage))) { | 
					
						
							|  |  |  |           router.replace('/signin') | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (searchParams.has('access_token') || searchParams.has('refresh_token')) { | 
					
						
							|  |  |  |           consoleToken && localStorage.setItem('console_token', consoleToken) | 
					
						
							|  |  |  |           refreshToken && localStorage.setItem('refresh_token', refreshToken) | 
					
						
							| 
									
										
										
										
											2024-10-23 17:48:57 +08:00
										 |  |  |           router.replace(pathname) | 
					
						
							| 
									
										
										
										
											2024-11-05 12:38:31 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-23 17:48:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |         setInit(true) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-04-14 11:27:14 +08:00
										 |  |  |       catch { | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |         router.replace('/signin') | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     })() | 
					
						
							| 
									
										
										
										
											2024-11-05 12:38:31 +08:00
										 |  |  |   }, [isSetupFinished, router, pathname, searchParams, consoleToken, refreshToken, consoleTokenFromLocalStorage, refreshTokenFromLocalStorage]) | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |   return init | 
					
						
							|  |  |  |     ? ( | 
					
						
							|  |  |  |       <SWRConfig value={{ | 
					
						
							|  |  |  |         shouldRetryOnError: false, | 
					
						
							| 
									
										
										
										
											2024-04-07 22:43:44 +08:00
										 |  |  |         revalidateOnFocus: false, | 
					
						
							| 
									
										
										
										
											2023-09-25 12:49:16 +08:00
										 |  |  |       }}> | 
					
						
							|  |  |  |         {children} | 
					
						
							|  |  |  |       </SWRConfig> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     : null | 
					
						
							| 
									
										
										
										
											2023-06-29 15:30:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default SwrInitor |