File size: 1,158 Bytes
87337b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use client"

import { ReactNode, useEffect } from "react"
import {
  useAppDispatch,
  getOptionsFromLocal,
  getRandomUserId,
  getRandomChannel,
  genRandomString,
} from "@/common"
import {
  setOptions,
  reset,
  setAgentSettings,
  setCozeSettings,
  setDifySettings,
} from "@/store/reducers/global"

interface AuthInitializerProps {
  children: ReactNode
}

const AuthInitializer = (props: AuthInitializerProps) => {
  const { children } = props
  const dispatch = useAppDispatch()

  useEffect(() => {
    if (typeof window !== "undefined") {
      const data = getOptionsFromLocal()
      if (data && data?.options?.channel) {
        dispatch(reset())
        dispatch(setOptions(data.options))
        dispatch(setAgentSettings(data.settings))
        dispatch(setCozeSettings(data.cozeSettings))
        dispatch(setDifySettings(data.difySettings))
      } else {
        const newOptions = {
          userName: genRandomString(8),
          channel: getRandomChannel(),
          userId: getRandomUserId(),
        }
        dispatch(setOptions(newOptions))
      }
    }
  }, [dispatch])

  return children
}

export default AuthInitializer