-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.ts
82 lines (75 loc) · 1.85 KB
/
config.ts
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
import { buildActionCreator, createReducer, Reducer } from "hard-reducer"
import v8n from "v8n"
v8n.extend({
acceptableTheme() {
return (value: string) => ["dark", "default"].includes(value)
}
})
export const rules = {
committerName: v8n().string(),
committerEmail: v8n().string(),
githubApiToken: v8n().string(),
editorFontScale: v8n().number(),
editorFontFamily: v8n().string(),
editorSpellCheck: v8n().boolean(),
corsProxy: v8n().string(),
theme: v8n()
.string()
.acceptableTheme(),
isFirstVisit: v8n().boolean(),
doneTutorial: v8n().boolean(),
gitEasyMode: v8n().boolean()
}
const { createAction } = buildActionCreator({
prefix: "config/"
})
export type ConfigState = {
committerName: string
committerEmail: string
githubApiToken: string
corsProxy: string
editorSpellCheck: boolean
editorFontFamily: string
editorFontScale: number
isFirstVisit: boolean
doneTutorial: boolean
theme: string
gitEasyMode: boolean
}
export const setConfigValue = createAction(
"set-config-value",
(input: { key: string; value: number | string | boolean }) => {
const rule = (rules as any)[input.key]
return {
...input,
valid: rule.test(input.value)
}
}
)
const initalState: ConfigState = {
committerName: "",
committerEmail: "",
githubApiToken: "",
editorFontScale: 1.2,
editorFontFamily: "Inconsolata, monospace",
editorSpellCheck: false,
corsProxy: "https://cors-buster-zashozaqfk.now.sh",
theme: "dark",
isFirstVisit: true,
doneTutorial: false,
gitEasyMode: true
}
export const reducer: Reducer<ConfigState> = createReducer(initalState).case(
setConfigValue,
(state, { key, value, valid }) => {
if (valid) {
return {
...state,
[key]: value
} as any
} else {
console.warn(`You can not set ${key}: ${value}`)
return state
}
}
)