-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
240 lines (237 loc) · 8.08 KB
/
Copy pathsettings.ts
File metadata and controls
240 lines (237 loc) · 8.08 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { defineStore } from 'pinia'
import { EventsOff, EventsOn } from '@/lib/web-runtime'
import { api } from '@/lib/web-api'
import { i18n, setI18nLocale } from '@/i18n'
import type { AppSettings, ConnectionResult, ConnectionsResponse, ConnectionSummary, CreateConnectionResponse, SchedulerStatus } from '@/types'
import { createDefaultScheduleSettings, createDefaultSettings, validateSettings } from '@/utils/settings'
import { toErrorMessage } from '@/utils/errors'
import { detectPreferredLocale, normalizeLocaleCode } from '@/utils/locale'
import { useTasksStore } from '@/stores/tasks'
interface SettingsState {
settings: AppSettings
connection: ConnectionResult | null
connections: ConnectionSummary[]
activeConnectionId: string
schedulerStatus: SchedulerStatus
loading: boolean
saving: boolean
errors: Record<string, string>
schedulerBridgeReady: boolean
}
function createDefaultSchedulerStatus(): SchedulerStatus {
return {
enabled: false,
mode: 'scan',
cron: '',
valid: true,
validationMessage: '',
running: false,
nextRunAt: '',
lastStartedAt: '',
lastFinishedAt: '',
lastStatus: '',
lastMessage: '',
}
}
export const useSettingsStore = defineStore('settingsStore', {
state: (): SettingsState => ({
settings: createDefaultSettings(),
connection: null,
connections: [],
activeConnectionId: '',
schedulerStatus: createDefaultSchedulerStatus(),
loading: false,
saving: false,
errors: {},
schedulerBridgeReady: false,
}),
getters: {
connectionTone: (state) => {
if (!state.connection) {
return 'idle'
}
return state.connection.ok ? 'ok' : 'error'
},
currentLocale: (state) => normalizeLocaleCode(state.settings.locale || i18n.global.locale.value),
},
actions: {
mergeSettings(result: Partial<AppSettings>) {
const previousToken = this.settings.managementToken
this.settings = {
...createDefaultSettings(),
...result,
managementToken: result.managementToken && result.managementToken.trim()
? result.managementToken
: previousToken,
hasSavedManagementToken: Boolean(result.hasSavedManagementToken || previousToken),
schedule: {
...createDefaultScheduleSettings(),
...(result.schedule ?? {}),
},
}
this.applyLocale(this.settings.locale)
},
applyConnections(payload?: Partial<ConnectionsResponse> | null) {
this.activeConnectionId = payload?.activeConnectionId ?? ''
this.connections = Array.isArray(payload?.connections) ? payload.connections as ConnectionSummary[] : []
},
applySchedulerStatus(status?: Partial<SchedulerStatus> | null) {
this.schedulerStatus = {
...createDefaultSchedulerStatus(),
...(status ?? {}),
}
},
initSchedulerBridge() {
if (this.schedulerBridgeReady) {
return
}
EventsOn('scheduler:status', (status: SchedulerStatus) => this.applySchedulerStatus(status))
this.schedulerBridgeReady = true
},
destroySchedulerBridge() {
if (!this.schedulerBridgeReady) {
return
}
EventsOff('scheduler:status')
this.schedulerBridgeReady = false
},
async loadConnections() {
const result = await api.getConnections()
this.applyConnections(result)
return this.connections
},
async switchConnection(connectionId: string) {
const result = await api.setActiveConnection(connectionId)
this.applyConnections(result)
useTasksStore().switchActiveConnection(connectionId)
await this.loadSettings()
await this.verifyActiveConnection()
return this.activeConnectionId
},
async verifyActiveConnection() {
this.connection = await api.testConnection(this.settings)
return this.connection
},
async createConnection(name: string, settings: AppSettings): Promise<CreateConnectionResponse> {
const result = await api.createConnection(name, settings)
this.applyConnections(result.connections)
await this.loadSettings()
this.connection = result.connectionResult
return result
},
async renameConnection(connectionId: string, name: string) {
const result = await api.renameConnection(connectionId, name)
this.applyConnections(result)
return this.connections
},
async deleteConnection(connectionId: string) {
const result = await api.deleteConnection(connectionId)
this.applyConnections(result)
await this.loadSettings()
return this.connections
},
applyLocale(locale?: string) {
const next = setI18nLocale(locale || detectPreferredLocale())
this.settings.locale = next
},
async loadSchedulerStatus() {
const status = await api.getSchedulerStatus()
this.applySchedulerStatus(status as unknown as Partial<SchedulerStatus>)
return this.schedulerStatus
},
async persistSettings() {
const saved = await api.saveSettings(this.settings)
const submittedToken = this.settings.managementToken
this.mergeSettings(saved as unknown as Partial<AppSettings>)
if (submittedToken.trim()) {
this.settings.managementToken = submittedToken
this.settings.hasSavedManagementToken = true
}
await this.loadSchedulerStatus()
return this.settings
},
async loadSettings() {
this.loading = true
try {
await this.loadConnections()
const result = await api.getSettings()
this.mergeSettings(result as unknown as Partial<AppSettings>)
if (this.activeConnectionId) {
useTasksStore().switchActiveConnection(this.activeConnectionId)
}
await this.loadSchedulerStatus()
} finally {
this.loading = false
}
},
async saveLocalePreference(locale: string) {
const previous = this.currentLocale
this.applyLocale(locale)
try {
await this.persistSettings()
} catch (error) {
this.applyLocale(previous)
throw new Error(toErrorMessage(error))
}
},
async saveLayoutModePreference(layoutMode: AppSettings['layoutMode']) {
const previous = this.settings.layoutMode
this.settings.layoutMode = layoutMode
try {
await this.persistSettings()
} catch (error) {
this.settings.layoutMode = previous
throw new Error(toErrorMessage(error))
}
},
async testConnectionWithSettings(input: AppSettings) {
const errors = validateSettings(input, i18n.global.t)
if (Object.keys(errors).length > 0) {
throw new Error(i18n.global.t('validation.fixBeforeTesting'))
}
return await api.testConnection(input)
},
async testConnection() {
this.errors = validateSettings(this.settings, i18n.global.t)
if (Object.keys(this.errors).length > 0) {
throw new Error(i18n.global.t('validation.fixBeforeTesting'))
}
this.connection = await api.testConnection(this.settings)
return this.connection
},
async saveSettings() {
this.errors = validateSettings(this.settings, i18n.global.t)
if (Object.keys(this.errors).length > 0) {
throw new Error(i18n.global.t('validation.fixBeforeSaving'))
}
this.saving = true
try {
return await this.persistSettings()
} finally {
this.saving = false
}
},
async testAndSave() {
try {
this.errors = validateSettings(this.settings, i18n.global.t)
if (Object.keys(this.errors).length > 0) {
throw new Error(i18n.global.t('validation.fixBeforeSaving'))
}
this.saving = true
const connection = await api.testAndSaveSettings(this.settings)
const submittedToken = this.settings.managementToken
await this.loadSettings()
if (submittedToken.trim()) {
this.settings.managementToken = submittedToken
this.settings.hasSavedManagementToken = true
}
this.connection = connection
return connection
} catch (error) {
throw new Error(toErrorMessage(error))
} finally {
this.saving = false
}
},
},
})