forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhancer.js
163 lines (148 loc) · 5 KB
/
enhancer.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
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
import { compose } from 'redux'
import ReduxQuerySync from 'redux-query-sync'
import history from '../options/history'
import * as notifActions from '../notifications/actions'
import * as notifSelectors from '../notifications/selectors'
import * as constants from './constants'
import { selectors as filters, actions as filterActs } from '../search-filters'
import { selectors as searchBar, acts as searchBarActs } from './search-bar'
import { selectors as results, acts as resultsActs } from './results'
import {
selectors as sidebarLeft,
actions as sidebarLeftActs,
} from './sidebar-left'
import {
constants as optConsts,
actions as optActs,
defaultState as defOptState,
} from 'src/options/settings'
const parseBool = str => str === 'true'
const parseNumber = str => Number(str)
const stringifyArr = arr => arr.join(',')
// Keep search query in sync with the query parameter in the window location.
const locationSync = ReduxQuerySync.enhancer({
replaceState: true, // We don't want back/forward to stop at every change.
initialTruth: 'location', // Initialise store from current location.
history,
params: {
startDate: {
selector: searchBar.startDate,
action: searchBarActs.setStartDate,
stringToValue: parseNumber,
},
endDate: {
selector: searchBar.endDate,
action: searchBarActs.setEndDate,
stringToValue: parseNumber,
},
showOnlyBookmarks: {
selector: filters.onlyBookmarks,
action: filterActs.toggleBookmarkFilter,
stringToValue: parseBool,
defaultValue: false,
},
tagsInc: {
selector: filters.tags,
action: filterActs.setTagFilters,
valueToString: stringifyArr,
defaultValue: [],
},
tagsExc: {
selector: filters.tagsExc,
action: filterActs.setExcTagFilters,
valueToString: stringifyArr,
defaultValue: [],
},
domainsInc: {
selector: filters.domainsInc,
action: filterActs.setIncDomainFilters,
valueToString: stringifyArr,
defaultValue: [],
},
domainsExc: {
selector: filters.domainsExc,
action: filterActs.setExcDomainFilters,
valueToString: stringifyArr,
defaultValue: [],
},
lists: {
selector: filters.listFilter,
action: filterActs.setListFilters,
defaultValue: '',
},
query: {
selector: searchBar.query,
action: searchBarActs.setQueryTagsDomains,
defaultValue: '',
},
showInbox: {
selector: notifSelectors.showInbox,
action: notifActions.toggleInbox,
stringToValue: parseBool,
defaultValue: false,
},
notes: {
selector: filters.notesFilter,
action: filterActs.toggleNotesFilter,
stringToValue: parseBool,
defaultValue: true,
},
highlights: {
selector: filters.highlightsFilter,
action: filterActs.toggleHighlightsFilter,
stringToValue: parseBool,
defaultValue: true,
},
websites: {
selector: filters.websitesFilter,
action: filterActs.toggleWebsitesFilter,
stringToValue: parseBool,
defaultValue: true,
},
},
})
const hydrateStateFromStorage = store => {
const hydrate = (key, action) =>
browser.storage.local.get(key).then(data => {
if (data[key] == null) {
return
}
store.dispatch(action(data[key]))
})
// Keep each of these storage keys in sync
hydrate(constants.SEARCH_COUNT_KEY, resultsActs.initSearchCount)
hydrate(
constants.ANNOTATIONS_EXPANDED_KEY,
resultsActs.setAreAnnotationsExpanded,
)
hydrate(constants.SIDEBAR_LOCKED_KEY, sidebarLeftActs.setSidebarLocked)
hydrate(
optConsts.STORAGE_KEYS.SCREENSHOTS,
optActs.initScreenshots,
defOptState.screenshots,
)
}
const syncStateToStorage = store =>
store.subscribe(() => {
const dump = (key, data) => browser.storage.local.set({ [key]: data })
const state = store.getState()
dump(constants.SEARCH_COUNT_KEY, results.searchCount(state))
dump(
constants.ANNOTATIONS_EXPANDED_KEY,
results.areAnnotationsExpanded(state),
)
dump(constants.SIDEBAR_LOCKED_KEY, sidebarLeft.sidebarLocked(state))
})
const storageSync = storeCreator => (reducer, initState, enhancer) => {
const store = storeCreator(reducer, initState, enhancer)
// Subscribe to changes and update local storage
syncStateToStorage(store)
// Rehydrate state from local storage
hydrateStateFromStorage(store)
return store
}
const enhancer = compose(
locationSync,
storageSync,
)
export default enhancer