2023-06-29 15:30:12 +08:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import { SWRConfig } from 'swr'
|
2023-09-25 12:49:16 +08:00
|
|
|
import { useEffect, useState } from 'react'
|
2023-06-29 15:30:12 +08:00
|
|
|
import type { ReactNode } from 'react'
|
2023-09-25 12:49:16 +08:00
|
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
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()
|
|
|
|
const consoleToken = searchParams.get('console_token')
|
|
|
|
const consoleTokenFromLocalStorage = localStorage?.getItem('console_token')
|
|
|
|
const [init, setInit] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!(consoleToken || consoleTokenFromLocalStorage))
|
|
|
|
router.replace('/signin')
|
|
|
|
|
|
|
|
if (consoleToken) {
|
|
|
|
localStorage?.setItem('console_token', consoleToken!)
|
2024-01-24 20:14:21 +08:00
|
|
|
router.replace('/apps', { forceOptimisticNavigation: false } as any)
|
2023-09-25 12:49:16 +08:00
|
|
|
}
|
|
|
|
setInit(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
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
|