swr-initor.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use client'
  2. import { SWRConfig } from 'swr'
  3. import { useCallback, useEffect, useState } from 'react'
  4. import type { ReactNode } from 'react'
  5. import { usePathname, useRouter, useSearchParams } from 'next/navigation'
  6. import useRefreshToken from '@/hooks/use-refresh-token'
  7. import { fetchSetupStatus } from '@/service/common'
  8. type SwrInitorProps = {
  9. children: ReactNode
  10. }
  11. const SwrInitor = ({
  12. children,
  13. }: SwrInitorProps) => {
  14. const router = useRouter()
  15. const searchParams = useSearchParams()
  16. const pathname = usePathname()
  17. const { getNewAccessToken } = useRefreshToken()
  18. const consoleToken = searchParams.get('access_token')
  19. const refreshToken = searchParams.get('refresh_token')
  20. const consoleTokenFromLocalStorage = localStorage?.getItem('console_token')
  21. const refreshTokenFromLocalStorage = localStorage?.getItem('refresh_token')
  22. const [init, setInit] = useState(false)
  23. const isSetupFinished = useCallback(async () => {
  24. try {
  25. if (localStorage.getItem('setup_status') === 'finished')
  26. return true
  27. const setUpStatus = await fetchSetupStatus()
  28. if (setUpStatus.step !== 'finished') {
  29. localStorage.removeItem('setup_status')
  30. return false
  31. }
  32. localStorage.setItem('setup_status', 'finished')
  33. return true
  34. }
  35. catch (error) {
  36. console.error(error)
  37. return false
  38. }
  39. }, [])
  40. const setRefreshToken = useCallback(async () => {
  41. try {
  42. if (!(consoleToken || refreshToken || consoleTokenFromLocalStorage || refreshTokenFromLocalStorage))
  43. return Promise.reject(new Error('No token found'))
  44. if (consoleTokenFromLocalStorage && refreshTokenFromLocalStorage)
  45. await getNewAccessToken()
  46. if (consoleToken && refreshToken) {
  47. localStorage.setItem('console_token', consoleToken)
  48. localStorage.setItem('refresh_token', refreshToken)
  49. await getNewAccessToken()
  50. }
  51. }
  52. catch (error) {
  53. return Promise.reject(error)
  54. }
  55. }, [consoleToken, refreshToken, consoleTokenFromLocalStorage, refreshTokenFromLocalStorage, getNewAccessToken])
  56. useEffect(() => {
  57. (async () => {
  58. try {
  59. const isFinished = await isSetupFinished()
  60. if (!isFinished) {
  61. router.replace('/install')
  62. return
  63. }
  64. await setRefreshToken()
  65. if (searchParams.has('access_token') || searchParams.has('refresh_token'))
  66. router.replace(pathname)
  67. setInit(true)
  68. }
  69. catch (error) {
  70. router.replace('/signin')
  71. }
  72. })()
  73. }, [isSetupFinished, setRefreshToken, router, pathname, searchParams])
  74. return init
  75. ? (
  76. <SWRConfig value={{
  77. shouldRetryOnError: false,
  78. revalidateOnFocus: false,
  79. }}>
  80. {children}
  81. </SWRConfig>
  82. )
  83. : null
  84. }
  85. export default SwrInitor