-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathSettingsContext.tsx
More file actions
113 lines (97 loc) · 3.34 KB
/
SettingsContext.tsx
File metadata and controls
113 lines (97 loc) · 3.34 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react"
import { client } from "@/client"
import { getKeys, LocalStorageToFileMigrationBackend, Store } from "@/lib"
import { TUnsubscribeFn } from "@/types"
import { TSetting, TSettings, TSettingsContext, SettingsContext } from "./useSettings"
const initialSettings: TSettings = {
sidebarPosition: "left",
debugFlag: false,
partyParrot: false,
fixedIDE: false,
zoom: "md",
transparency: false,
autoUpdate: true,
additionalCliFlags: "",
additionalEnvVars: "",
dotfilesUrl: "",
sshKeyPath: "",
httpProxyUrl: "",
httpsProxyUrl: "",
noProxy: "",
experimental_colorMode: "light",
experimental_multiDevcontainer: false,
experimental_fleet: true,
experimental_jupyterNotebooks: true,
experimental_vscodeInsiders: true,
experimental_cursor: true,
experimental_positron: true,
experimental_zed: true,
experimental_codium: true,
experimental_rstudio: true,
experimental_windsurf: true,
experimental_antigravity: true,
experimental_devPodPro: false,
}
function getSettingKeys(): readonly TSetting[] {
return getKeys(initialSettings)
}
// WARN: needs to match the filename on the rust side
const SETTING_STORE_KEY = "settings"
const settingsStore = new Store<Record<TSetting, string | boolean | unknown>>(
new LocalStorageToFileMigrationBackend(SETTING_STORE_KEY)
)
export function SettingsProvider({ children }: Readonly<{ children?: ReactNode }>) {
const [settings, setSettings] = useState(initialSettings)
useEffect(() => {
;(async () => {
const initialOptions = await Promise.all(
getSettingKeys().map((option) =>
settingsStore
.get(option)
.then((value) => [option, value ?? initialSettings[option]] as const)
.catch(() => [option, false] as const)
)
)
setSettings(
initialOptions.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), initialSettings)
)
})()
}, [])
useEffect(() => {
const subscriptions: TUnsubscribeFn[] = []
for (const setting of getSettingKeys()) {
subscriptions.push(
settingsStore.subscribe(setting, (newValue) =>
setSettings((current) => ({ ...current, [setting]: newValue }))
)
)
}
return () => {
for (const unsubscribe of subscriptions) {
unsubscribe()
}
}
}, [])
useEffect(() => {
client.setSetting("debugFlag", settings.debugFlag)
}, [settings.debugFlag])
useEffect(() => {
client.setSetting("additionalCliFlags", settings.additionalCliFlags)
}, [settings.additionalCliFlags])
useEffect(() => {
client.setSetting("dotfilesUrl", settings.dotfilesUrl)
}, [settings.dotfilesUrl])
useEffect(() => {
client.setSetting("additionalEnvVars", settings.additionalEnvVars)
}, [settings.additionalEnvVars])
useEffect(() => {
client.setSetting("httpProxyUrl", settings.httpProxyUrl)
client.setSetting("httpsProxyUrl", settings.httpsProxyUrl)
client.setSetting("noProxy", settings.noProxy)
}, [settings.httpProxyUrl, settings.httpsProxyUrl, settings.noProxy])
const set = useCallback<TSettingsContext["set"]>((key, value) => {
settingsStore.set(key, value)
}, [])
const value = useMemo(() => ({ settings, set }), [set, settings])
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>
}