mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
5a23d570b5
Co-authored-by: mazhanwen <mazhanwen@tal.com>
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
'use client'
|
|
|
|
import { SWRConfig } from 'swr'
|
|
import { useEffect, useState } from 'react'
|
|
import type { ReactNode } from 'react'
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
|
|
|
type SwrInitorProps = {
|
|
children: ReactNode
|
|
}
|
|
const SwrInitor = ({
|
|
children,
|
|
}: SwrInitorProps) => {
|
|
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!)
|
|
router.replace('/apps', { forceOptimisticNavigation: false } as any)
|
|
}
|
|
setInit(true)
|
|
}, [])
|
|
|
|
return init
|
|
? (
|
|
<SWRConfig value={{
|
|
shouldRetryOnError: false,
|
|
revalidateOnFocus: false,
|
|
}}>
|
|
{children}
|
|
</SWRConfig>
|
|
)
|
|
: null
|
|
}
|
|
|
|
export default SwrInitor
|