-
Notifications
You must be signed in to change notification settings - Fork 74
/
loadLocalState.js
44 lines (38 loc) · 1.27 KB
/
loadLocalState.js
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
import * as db from '../data-providers/dexie'
import { EM_TOKEN, INITIAL_SETTINGS } from '../constants'
import { importText } from '../action-creators'
import { decodeThoughtsUrl } from '../selectors'
import { never } from '../util'
/** Loads the local state from the IndexedDB database. */
const loadLocalState = () => async (dispatch, getState) => {
// load helpers and settings from local database
const [{
cursor: localUrl,
lastUpdated,
recentlyEdited,
}, settings] = await Promise.all([
db.getHelpers(),
db.getContext([EM_TOKEN, 'Settings'])
])
// restore cursor from local db if url is at root
const isHome = window.location.pathname.length <= 1
const { contextViews, thoughtsRanked: cursor } = isHome && localUrl
? decodeThoughtsUrl(getState(), localUrl)
: {}
dispatch({
type: 'loadLocalState',
contextViews,
cursor,
lastUpdated,
recentlyEdited: recentlyEdited || {},
})
// initialize settings if they don't exist
if (!settings) {
// set lastUpdated to never so that any settings from remote are used over the initial settings
return await dispatch(importText([{ value: EM_TOKEN, rank: 0 }], INITIAL_SETTINGS, {
lastUpdated: never(),
preventSetCursor: true,
}))
}
}
export default loadLocalState