diff options
author | 2022-11-24 02:56:10 +0530 | |
---|---|---|
committer | 2022-11-25 05:04:25 +0530 | |
commit | f92933c396db9e94ffd297c41add86de88dfc6c6 (patch) | |
tree | 57789f7f64c618086c3792833d076244d879aa75 /src/features/todos/store.js | |
parent | fix: use 'Route' from 'react-router-dom' package (diff) | |
download | ferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.tar.gz ferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.tar.zst ferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.zip |
chore: transform workspace action store and todo store into ts
Diffstat (limited to 'src/features/todos/store.js')
-rw-r--r-- | src/features/todos/store.js | 309 |
1 files changed, 0 insertions, 309 deletions
diff --git a/src/features/todos/store.js b/src/features/todos/store.js deleted file mode 100644 index 0f195f10d..000000000 --- a/src/features/todos/store.js +++ /dev/null | |||
@@ -1,309 +0,0 @@ | |||
1 | import { computed, action, observable, makeObservable } from 'mobx'; | ||
2 | import localStorage from 'mobx-localstorage'; | ||
3 | |||
4 | import { ThemeType } from '../../themes'; | ||
5 | import { todoActions } from './actions'; | ||
6 | import { | ||
7 | CUSTOM_TODO_SERVICE, | ||
8 | TODO_SERVICE_RECIPE_IDS, | ||
9 | DEFAULT_TODOS_WIDTH, | ||
10 | TODOS_MIN_WIDTH, | ||
11 | DEFAULT_TODOS_VISIBLE, | ||
12 | DEFAULT_IS_TODO_FEATURE_ENABLED_BY_USER, | ||
13 | } from '../../config'; | ||
14 | import { isValidExternalURL } from '../../helpers/url-helpers'; | ||
15 | import FeatureStore from '../utils/FeatureStore'; | ||
16 | import { createReactions } from '../../stores/lib/Reaction'; | ||
17 | import { createActionBindings } from '../utils/ActionBinding'; | ||
18 | import { IPC, TODOS_ROUTES } from './constants'; | ||
19 | import UserAgent from '../../models/UserAgent'; | ||
20 | |||
21 | const debug = require('../../preload-safe-debug')( | ||
22 | 'Ferdium:feature:todos:store', | ||
23 | ); | ||
24 | |||
25 | export default class TodoStore extends FeatureStore { | ||
26 | @observable stores = null; | ||
27 | |||
28 | @observable isFeatureActive = false; | ||
29 | |||
30 | @observable webview = null; | ||
31 | |||
32 | @observable userAgentModel = new UserAgent(); | ||
33 | |||
34 | isInitialized = false; | ||
35 | |||
36 | constructor() { | ||
37 | super(); | ||
38 | |||
39 | makeObservable(this); | ||
40 | } | ||
41 | |||
42 | @computed get width() { | ||
43 | const width = this.settings.width || DEFAULT_TODOS_WIDTH; | ||
44 | |||
45 | return width < TODOS_MIN_WIDTH ? TODOS_MIN_WIDTH : width; | ||
46 | } | ||
47 | |||
48 | @computed get isTodosPanelForceHidden() { | ||
49 | return !this.isFeatureEnabledByUser; | ||
50 | } | ||
51 | |||
52 | @computed get isTodosPanelVisible() { | ||
53 | if (this.settings.isTodosPanelVisible === undefined) { | ||
54 | return DEFAULT_TODOS_VISIBLE; | ||
55 | } | ||
56 | return this.settings.isTodosPanelVisible; | ||
57 | } | ||
58 | |||
59 | @computed get isFeatureEnabledByUser() { | ||
60 | return this.settings.isFeatureEnabledByUser; | ||
61 | } | ||
62 | |||
63 | @computed get settings() { | ||
64 | return localStorage.getItem('todos') || {}; | ||
65 | } | ||
66 | |||
67 | @computed get userAgent() { | ||
68 | return this.userAgentModel.userAgent; | ||
69 | } | ||
70 | |||
71 | @computed get isUsingPredefinedTodoServer() { | ||
72 | return ( | ||
73 | this.stores && | ||
74 | this.stores.settings.app.predefinedTodoServer !== CUSTOM_TODO_SERVICE | ||
75 | ); | ||
76 | } | ||
77 | |||
78 | @computed get todoUrl() { | ||
79 | if (!this.stores) { | ||
80 | return null; | ||
81 | } | ||
82 | return this.isUsingPredefinedTodoServer | ||
83 | ? this.stores.settings.app.predefinedTodoServer | ||
84 | : this.stores.settings.app.customTodoServer; | ||
85 | } | ||
86 | |||
87 | @computed get isTodoUrlValid() { | ||
88 | return ( | ||
89 | !this.isUsingPredefinedTodoServer || isValidExternalURL(this.todoUrl) | ||
90 | ); | ||
91 | } | ||
92 | |||
93 | @computed get todoRecipeId() { | ||
94 | if ( | ||
95 | this.isFeatureEnabledByUser && | ||
96 | this.isUsingPredefinedTodoServer && | ||
97 | this.todoUrl in TODO_SERVICE_RECIPE_IDS | ||
98 | ) { | ||
99 | return TODO_SERVICE_RECIPE_IDS[this.todoUrl]; | ||
100 | } | ||
101 | return null; | ||
102 | } | ||
103 | |||
104 | // ========== PUBLIC API ========= // | ||
105 | |||
106 | @action start(stores, actions) { | ||
107 | debug('TodoStore::start'); | ||
108 | this.stores = stores; | ||
109 | this.actions = actions; | ||
110 | |||
111 | // ACTIONS | ||
112 | |||
113 | this._registerActions( | ||
114 | createActionBindings([ | ||
115 | [todoActions.resize, this._resize], | ||
116 | [todoActions.toggleTodosPanel, this._toggleTodosPanel], | ||
117 | [todoActions.setTodosWebview, this._setTodosWebview], | ||
118 | [todoActions.handleHostMessage, this._handleHostMessage], | ||
119 | [todoActions.handleClientMessage, this._handleClientMessage], | ||
120 | [ | ||
121 | todoActions.toggleTodosFeatureVisibility, | ||
122 | this._toggleTodosFeatureVisibility, | ||
123 | ], | ||
124 | [todoActions.openDevTools, this._openDevTools], | ||
125 | [todoActions.reload, this._reload], | ||
126 | ]), | ||
127 | ); | ||
128 | |||
129 | // REACTIONS | ||
130 | |||
131 | this._allReactions = createReactions([ | ||
132 | this._updateTodosConfig, | ||
133 | this._firstLaunchReaction, | ||
134 | this._routeCheckReaction, | ||
135 | ]); | ||
136 | |||
137 | this._registerReactions(this._allReactions); | ||
138 | |||
139 | this.isFeatureActive = true; | ||
140 | } | ||
141 | |||
142 | @action stop() { | ||
143 | super.stop(); | ||
144 | debug('TodoStore::stop'); | ||
145 | this.reset(); | ||
146 | this.isFeatureActive = false; | ||
147 | } | ||
148 | |||
149 | // ========== PRIVATE METHODS ========= // | ||
150 | |||
151 | _updateSettings = changes => { | ||
152 | localStorage.setItem('todos', { | ||
153 | ...this.settings, | ||
154 | ...changes, | ||
155 | }); | ||
156 | }; | ||
157 | |||
158 | // Actions | ||
159 | |||
160 | @action _resize = ({ width }) => { | ||
161 | this._updateSettings({ | ||
162 | width, | ||
163 | }); | ||
164 | }; | ||
165 | |||
166 | @action _toggleTodosPanel = () => { | ||
167 | this._updateSettings({ | ||
168 | isTodosPanelVisible: !this.isTodosPanelVisible, | ||
169 | }); | ||
170 | }; | ||
171 | |||
172 | @action _setTodosWebview = ({ webview }) => { | ||
173 | debug('_setTodosWebview', webview); | ||
174 | if (this.webview !== webview) { | ||
175 | this.webview = webview; | ||
176 | this.userAgentModel.setWebviewReference(webview); | ||
177 | } | ||
178 | }; | ||
179 | |||
180 | @action _handleHostMessage = message => { | ||
181 | debug('_handleHostMessage', message); | ||
182 | if (message.action === 'todos:create') { | ||
183 | this.webview.send(IPC.TODOS_HOST_CHANNEL, message); | ||
184 | } | ||
185 | }; | ||
186 | |||
187 | @action _handleClientMessage = ({ channel, message = {} }) => { | ||
188 | debug('_handleClientMessage', channel, message); | ||
189 | switch (message.action) { | ||
190 | case 'todos:initialized': | ||
191 | this._onTodosClientInitialized(); | ||
192 | break; | ||
193 | case 'todos:goToService': | ||
194 | this._goToService(message.data); | ||
195 | break; | ||
196 | default: | ||
197 | debug('Other message received', channel, message); | ||
198 | if (this.stores.services.isTodosServiceAdded) { | ||
199 | this.actions.service.handleIPCMessage({ | ||
200 | serviceId: this.stores.services.isTodosServiceAdded.id, | ||
201 | channel, | ||
202 | args: message, | ||
203 | }); | ||
204 | } | ||
205 | } | ||
206 | }; | ||
207 | |||
208 | _handleNewWindowEvent = ({ url }) => { | ||
209 | this.actions.app.openExternalUrl({ url }); | ||
210 | }; | ||
211 | |||
212 | @action _toggleTodosFeatureVisibility = () => { | ||
213 | debug('_toggleTodosFeatureVisibility'); | ||
214 | |||
215 | const isFeatureEnabled = !this.settings.isFeatureEnabledByUser; | ||
216 | this._updateSettings({ | ||
217 | isFeatureEnabledByUser: isFeatureEnabled, | ||
218 | isTodosPanelVisible: isFeatureEnabled, | ||
219 | }); | ||
220 | }; | ||
221 | |||
222 | _openDevTools = () => { | ||
223 | debug('_openDevTools'); | ||
224 | |||
225 | const webview = document.querySelector('#todos-panel webview'); | ||
226 | if (webview) webview.openDevTools(); | ||
227 | }; | ||
228 | |||
229 | _reload = () => { | ||
230 | debug('_reload'); | ||
231 | |||
232 | const webview = document.querySelector('#todos-panel webview'); | ||
233 | if (webview) webview.reload(); | ||
234 | }; | ||
235 | |||
236 | // Todos client message handlers | ||
237 | |||
238 | _onTodosClientInitialized = async () => { | ||
239 | const { authToken } = this.stores.user; | ||
240 | const { isDarkThemeActive } = this.stores.ui; | ||
241 | const { locale } = this.stores.app; | ||
242 | if (!this.webview) return; | ||
243 | await this.webview.send(IPC.TODOS_HOST_CHANNEL, { | ||
244 | action: 'todos:configure', | ||
245 | data: { | ||
246 | authToken, | ||
247 | locale, | ||
248 | theme: isDarkThemeActive ? ThemeType.dark : ThemeType.default, | ||
249 | }, | ||
250 | }); | ||
251 | |||
252 | if (!this.isInitialized) { | ||
253 | this.webview.addEventListener('new-window', this._handleNewWindowEvent); | ||
254 | |||
255 | this.isInitialized = true; | ||
256 | } | ||
257 | }; | ||
258 | |||
259 | _goToService = ({ url, serviceId }) => { | ||
260 | if (url) { | ||
261 | this.stores.services.one(serviceId).webview.loadURL(url); | ||
262 | } | ||
263 | this.actions.service.setActive({ serviceId }); | ||
264 | }; | ||
265 | |||
266 | // Reactions | ||
267 | |||
268 | _updateTodosConfig = () => { | ||
269 | // Resend the config if any part changes in Franz: | ||
270 | this._onTodosClientInitialized(); | ||
271 | }; | ||
272 | |||
273 | _firstLaunchReaction = () => { | ||
274 | const { stats } = this.stores.settings.all; | ||
275 | |||
276 | if (this.settings.isFeatureEnabledByUser === undefined) { | ||
277 | this._updateSettings({ | ||
278 | isFeatureEnabledByUser: DEFAULT_IS_TODO_FEATURE_ENABLED_BY_USER, | ||
279 | }); | ||
280 | } | ||
281 | |||
282 | // Hide todos layer on first app start but show on second | ||
283 | if (stats.appStarts <= 1) { | ||
284 | this._updateSettings({ | ||
285 | isTodosPanelVisible: false, | ||
286 | }); | ||
287 | } else if (stats.appStarts <= 2) { | ||
288 | this._updateSettings({ | ||
289 | isTodosPanelVisible: true, | ||
290 | }); | ||
291 | } | ||
292 | }; | ||
293 | |||
294 | _routeCheckReaction = () => { | ||
295 | const { pathname } = this.stores.router.location; | ||
296 | |||
297 | if (pathname === TODOS_ROUTES.TARGET) { | ||
298 | debug('Router is on todos route, show todos panel'); | ||
299 | // todosStore.start(stores, actions); | ||
300 | this.stores.router.push('/'); | ||
301 | |||
302 | if (!this.isTodosPanelVisible) { | ||
303 | this._updateSettings({ | ||
304 | isTodosPanelVisible: true, | ||
305 | }); | ||
306 | } | ||
307 | } | ||
308 | }; | ||
309 | } | ||