2018-01-08 19:13:42 +01:00
|
|
|
import { Store } from 'svelte/store.js'
|
|
|
|
|
2018-01-23 06:16:27 +01:00
|
|
|
const DONT_STORE_THESE_KEYS = [
|
|
|
|
'cachedAccountNames'
|
|
|
|
]
|
|
|
|
|
2018-01-13 07:05:57 +01:00
|
|
|
const LS = process.browser && localStorage
|
2018-01-08 19:13:42 +01:00
|
|
|
class LocalStorageStore extends Store {
|
2018-01-13 07:05:57 +01:00
|
|
|
|
2018-01-08 19:13:42 +01:00
|
|
|
constructor(state) {
|
|
|
|
super(state)
|
|
|
|
if (process.browser) {
|
2018-01-13 07:05:57 +01:00
|
|
|
this.lastChanged = {}
|
|
|
|
let newState = {}
|
|
|
|
for (let i = 0, len = LS.length; i < len; i++) {
|
|
|
|
let key = LS.key(i)
|
|
|
|
if (key.startsWith('store_')) {
|
|
|
|
let item = LS.getItem(key)
|
|
|
|
newState[key.substring(6)] = item === 'undefined' ? undefined : JSON.parse(item)
|
|
|
|
}
|
2018-01-08 19:13:42 +01:00
|
|
|
}
|
2018-01-13 07:05:57 +01:00
|
|
|
this.set(newState)
|
|
|
|
this.onchange((state, changed) => {
|
|
|
|
Object.keys(changed).forEach(change => {
|
2018-01-23 06:16:27 +01:00
|
|
|
if (!DONT_STORE_THESE_KEYS.includes(change) &&
|
|
|
|
!this._computed[change]) { // TODO: better way to ignore computed values?
|
2018-01-13 07:05:57 +01:00
|
|
|
this.lastChanged[change] = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-01-08 19:13:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
if (process.browser) {
|
2018-01-13 07:05:57 +01:00
|
|
|
Object.keys(this.lastChanged).forEach(key => {
|
|
|
|
LS.setItem(`store_${key}`, JSON.stringify(this.get(key)))
|
|
|
|
})
|
|
|
|
this.lastChanged = {}
|
2018-01-08 19:13:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const store = new LocalStorageStore({
|
2018-01-13 07:24:54 +01:00
|
|
|
instanceNameInSearch: '',
|
|
|
|
currentRegisteredInstance: null,
|
|
|
|
currentRegisteredInstanceName: '',
|
|
|
|
currentInstance: null,
|
|
|
|
loggedInInstances: {},
|
|
|
|
loggedInInstancesInOrder: [],
|
|
|
|
instanceThemes: {}
|
2018-01-08 19:13:42 +01:00
|
|
|
})
|
|
|
|
|
2018-01-09 03:14:21 +01:00
|
|
|
store.compute(
|
|
|
|
'isUserLoggedIn',
|
2018-01-13 07:24:54 +01:00
|
|
|
['currentInstance', 'loggedInInstances'],
|
|
|
|
(currentInstance, loggedInInstances) => !!(currentInstance && Object.keys(loggedInInstances).includes(currentInstance))
|
2018-01-09 03:14:21 +01:00
|
|
|
)
|
|
|
|
|
2018-01-13 20:11:35 +01:00
|
|
|
store.compute(
|
|
|
|
'loggedInInstancesAsList',
|
|
|
|
['currentInstance', 'loggedInInstances', 'loggedInInstancesInOrder'],
|
|
|
|
(currentInstance, loggedInInstances, loggedInInstancesInOrder) => {
|
|
|
|
return loggedInInstancesInOrder.map(instanceName => {
|
|
|
|
return Object.assign({
|
|
|
|
current: currentInstance === instanceName,
|
|
|
|
name: instanceName
|
|
|
|
}, loggedInInstances[instanceName])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-01-13 23:19:51 +01:00
|
|
|
store.compute(
|
|
|
|
'currentInstanceData',
|
|
|
|
['currentInstance', 'loggedInInstances'],
|
|
|
|
(currentInstance, loggedInInstances) => {
|
|
|
|
return Object.assign({
|
|
|
|
name: currentInstance
|
|
|
|
}, loggedInInstances[currentInstance])
|
|
|
|
})
|
|
|
|
|
2018-01-23 06:16:27 +01:00
|
|
|
store.compute(
|
|
|
|
'accessToken',
|
|
|
|
['currentInstanceData'],
|
|
|
|
(currentInstanceData) => currentInstanceData.access_token
|
|
|
|
)
|
|
|
|
|
2018-01-14 02:41:15 +01:00
|
|
|
store.compute(
|
|
|
|
'currentTheme',
|
|
|
|
['currentInstance', 'instanceThemes'],
|
|
|
|
(currentInstance, instanceThemes) => {
|
|
|
|
return instanceThemes[currentInstance] || 'default'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-01-09 03:14:21 +01:00
|
|
|
if (process.browser && process.env.NODE_ENV !== 'production') {
|
|
|
|
window.store = store // for debugging
|
2018-01-08 19:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export { store }
|