File size: 2,380 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { IAgentSettings, IOptions, ICozeSettings, IDifySettings } from "@/types"
import {
  OPTIONS_KEY,
  DEFAULT_OPTIONS,
  AGENT_SETTINGS_KEY,
  DEFAULT_AGENT_SETTINGS,
  COZE_SETTINGS_KEY,
  DEFAULT_COZE_SETTINGS,
  DIFY_SETTINGS_KEY,
  DEFAULT_DIFY_SETTINGS,
} from "./constant"

export const getOptionsFromLocal = (): {
  options: IOptions
  settings: IAgentSettings
  cozeSettings: ICozeSettings
  difySettings: IDifySettings
} => {
  let data = {
    options: DEFAULT_OPTIONS,
    settings: DEFAULT_AGENT_SETTINGS,
    cozeSettings: DEFAULT_COZE_SETTINGS,
    difySettings: DEFAULT_DIFY_SETTINGS,
  }
  if (typeof window !== "undefined") {
    const options = localStorage.getItem(OPTIONS_KEY)
    if (options) {
      data.options = JSON.parse(options)
    }
    const settings = localStorage.getItem(AGENT_SETTINGS_KEY)
    if (settings) {
      data.settings = JSON.parse(settings)
    }
    const cozeSettings = localStorage.getItem(COZE_SETTINGS_KEY)
    if (cozeSettings) {
      data.cozeSettings = JSON.parse(cozeSettings)
    }
    const difySettings = localStorage.getItem(DIFY_SETTINGS_KEY)
    if (difySettings) {
      data.difySettings = JSON.parse(difySettings)
    }
  }
  return data
}

export const setOptionsToLocal = (options: IOptions) => {
  if (typeof window !== "undefined") {
    localStorage.setItem(OPTIONS_KEY, JSON.stringify(options))
  }
}

export const setAgentSettingsToLocal = (settings: IAgentSettings) => {
  if (typeof window !== "undefined") {
    localStorage.setItem(AGENT_SETTINGS_KEY, JSON.stringify(settings))
  }
}

export const setCozeSettingsToLocal = (settings: ICozeSettings) => {
  if (typeof window !== "undefined") {
    localStorage.setItem(COZE_SETTINGS_KEY, JSON.stringify(settings))
  }
}

export const setDifySettingsToLocal = (settings: IDifySettings) => {
  if (typeof window !== "undefined") {
    localStorage.setItem(DIFY_SETTINGS_KEY, JSON.stringify(settings))
  }
}

export const resetSettingsByKeys = (keys: string | string[]) => {
  if (typeof window !== "undefined") {
    if (Array.isArray(keys)) {
      keys.forEach((key) => {
        localStorage.removeItem(key)
      })
    } else {
      localStorage.removeItem(keys)
    }
  }
}

export const resetCozeSettings = () => {
  resetSettingsByKeys(COZE_SETTINGS_KEY)
}

export const resetDifySettings = () => {
  resetSettingsByKeys(DIFY_SETTINGS_KEY)
}