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-12 23:40:38 +08:00
|
|
|
import useRefreshToken from '@/hooks/use-refresh-token'
|
2024-10-21 09:23:20 +08:00
|
|
|
import { fetchSetupStatus } from '@/service/common'
|
2023-06-29 15:30:12 +08:00
|
|
|
|
|
|
|
type SwrInitorProps = {
|
|
|
|
children: ReactNode
|
|
|
|
}
|
|
|
|
const SwrInitor = ({
|
|
|
|
children,
|
|
|
|
}: SwrInitorProps) => {
|
2023-09-25 12:49:16 +08:00
|
|
|
const router = useRouter()
|
|
|
|
const searchParams = useSearchParams()
|
2024-10-23 17:48:57 +08:00
|
|
|
const pathname = usePathname()
|
2024-10-14 16:54:23 +08:00
|
|
|
const { getNewAccessToken } = useRefreshToken()
|
2024-10-12 23:40:38 +08:00
|
|
|
const consoleToken = searchParams.get('access_token')
|
|
|
|
const refreshToken = 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')
|
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
|
|
|
const setRefreshToken = useCallback(async () => {
|
|
|
|
try {
|
|
|
|
if (!(consoleToken || refreshToken || consoleTokenFromLocalStorage || refreshTokenFromLocalStorage))
|
|
|
|
return Promise.reject(new Error('No token found'))
|
|
|
|
|
|
|
|
if (consoleTokenFromLocalStorage && refreshTokenFromLocalStorage)
|
|
|
|
await getNewAccessToken()
|
|
|
|
|
|
|
|
if (consoleToken && refreshToken) {
|
|
|
|
localStorage.setItem('console_token', consoleToken)
|
|
|
|
localStorage.setItem('refresh_token', refreshToken)
|
|
|
|
await getNewAccessToken()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
|
|
|
}, [consoleToken, refreshToken, consoleTokenFromLocalStorage, refreshTokenFromLocalStorage, getNewAccessToken])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const isFinished = await isSetupFinished()
|
|
|
|
if (!isFinished) {
|
|
|
|
router.replace('/install')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await setRefreshToken()
|
2024-10-23 17:48:57 +08:00
|
|
|
if (searchParams.has('access_token') || searchParams.has('refresh_token'))
|
|
|
|
router.replace(pathname)
|
|
|
|
|
2024-10-21 09:23:20 +08:00
|
|
|
setInit(true)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
router.replace('/signin')
|
|
|
|
}
|
|
|
|
})()
|
2024-10-23 17:48:57 +08:00
|
|
|
}, [isSetupFinished, setRefreshToken, router, pathname, searchParams])
|
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
|