From fa4fdd7a9ddc6642095737be8cd75c5a4da16954 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Mon, 16 Aug 2021 17:43:08 +0200 Subject: chore: replace moment.js with day.js (#1804) --- src/stores/AppStore.js | 62 ++++++++++++++++++++++++---------------------- src/stores/UserStore.js | 66 +++++++++++++++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 9c8cce679..71213774d 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js @@ -8,7 +8,7 @@ import { process as remoteProcess, } from '@electron/remote'; import { action, computed, observable } from 'mobx'; -import moment from 'moment'; +import dayjs from 'dayjs'; import AutoLaunch from 'auto-launch'; import ms from 'ms'; import { URL } from 'url'; @@ -48,7 +48,8 @@ const autoLauncher = new AutoLaunch({ path: executablePath, }); -const CATALINA_NOTIFICATION_HACK_KEY = '_temp_askedForCatalinaNotificationPermissions'; +const CATALINA_NOTIFICATION_HACK_KEY = + '_temp_askedForCatalinaNotificationPermissions'; export default class AppStore extends Store { updateStatusTypes = { @@ -74,7 +75,7 @@ export default class AppStore extends Store { @observable authRequestFailed = false; - @observable timeSuspensionStart = moment(); + @observable timeSuspensionStart = dayjs(); @observable timeOfflineStart; @@ -228,7 +229,7 @@ export default class AppStore extends Store { powerMonitor.on('suspend', () => { debug('System suspended starting timer'); - this.timeSuspensionStart = moment(); + this.timeSuspensionStart = dayjs(); }); powerMonitor.on('resume', () => { @@ -236,8 +237,8 @@ export default class AppStore extends Store { this.actions.service.resetLastPollTimer(); if ( - this.timeSuspensionStart.add(10, 'm').isBefore(moment()) - && this.stores.settings.app.get('reloadAfterResume') + this.timeSuspensionStart.add(10, 'm').isBefore(dayjs()) && + this.stores.settings.app.get('reloadAfterResume') ) { debug('Reloading services, user info and features'); @@ -283,15 +284,15 @@ export default class AppStore extends Store { ferdi: { version: ferdiVersion, electron: electronVersion, - installedRecipes: this.stores.recipes.all.map((recipe) => ({ + installedRecipes: this.stores.recipes.all.map(recipe => ({ id: recipe.id, version: recipe.version, })), - devRecipes: this.stores.recipePreviews.dev.map((recipe) => ({ + devRecipes: this.stores.recipePreviews.dev.map(recipe => ({ id: recipe.id, version: recipe.version, })), - services: this.stores.services.all.map((service) => ({ + services: this.stores.services.all.map(service => ({ id: service.id, recipe: service.recipe.id, isAttached: service.isAttached, @@ -302,7 +303,7 @@ export default class AppStore extends Store { isDarkModeEnabled: service.isDarkModeEnabled, })), messages: this.stores.globalError.messages, - workspaces: this.stores.workspaces.workspaces.map((workspace) => ({ + workspaces: this.stores.workspaces.workspaces.map(workspace => ({ id: workspace.id, services: workspace.services, })), @@ -315,9 +316,7 @@ export default class AppStore extends Store { } // Actions - @action _notify({ - title, options, notificationId, serviceId = null, - }) { + @action _notify({ title, options, notificationId, serviceId = null }) { if (this.stores.settings.all.app.isAppMuted) return; // TODO: is there a simple way to use blobs for notifications without storing them on disk? @@ -359,8 +358,8 @@ export default class AppStore extends Store { if (indicator === 0 && unreadIndirectMessageCount !== 0) { indicator = '•'; } else if ( - unreadDirectMessageCount === 0 - && unreadIndirectMessageCount === 0 + unreadDirectMessageCount === 0 && + unreadIndirectMessageCount === 0 ) { indicator = 0; } else { @@ -441,22 +440,25 @@ export default class AppStore extends Store { const clearAppCache = this.clearAppCacheRequest.execute(); const allServiceIds = await getServiceIdsFromPartitions(); const allOrphanedServiceIds = allServiceIds.filter( - (id) => !this.stores.services.all.find( - (s) => id.replace('service-', '') === s.id, - ), + id => + !this.stores.services.all.find( + s => id.replace('service-', '') === s.id, + ), ); try { await Promise.all( - allOrphanedServiceIds.map((id) => removeServicePartitionDirectory(id)), + allOrphanedServiceIds.map(id => removeServicePartitionDirectory(id)), ); } catch (ex) { console.log('Error while deleting service partition directory - ', ex); } await Promise.all( - this.stores.services.all.map((s) => this.actions.service.clearCache({ - serviceId: s.id, - })), + this.stores.services.all.map(s => + this.actions.service.clearCache({ + serviceId: s.id, + }), + ), ); await clearAppCache._promise; @@ -471,9 +473,9 @@ export default class AppStore extends Store { // Reactions _offlineCheck() { if (!this.isOnline) { - this.timeOfflineStart = moment(); + this.timeOfflineStart = dayjs(); } else { - const deltaTime = moment().diff(this.timeOfflineStart); + const deltaTime = dayjs().diff(this.timeOfflineStart); if (deltaTime > ms('30m')) { this.actions.service.reloadAll(); @@ -488,16 +490,16 @@ export default class AppStore extends Store { } if ( - locale - && Object.prototype.hasOwnProperty.call(locales, locale) - && locale !== this.locale + locale && + Object.prototype.hasOwnProperty.call(locales, locale) && + locale !== this.locale ) { this.locale = locale; } else if (!locale) { this.locale = this._getDefaultLocale(); } - moment.locale(this.locale); + dayjs.locale(this.locale); debug(`Set locale to "${this.locale}"`); } @@ -569,8 +571,8 @@ export default class AppStore extends Store { debug('Do not disturb mode is', dnd); // ipcRenderer.on('autoUpdate', (event, data) => { if ( - dnd !== this.stores.settings.all.app.isAppMuted - && !this.isSystemMuteOverridden + dnd !== this.stores.settings.all.app.isAppMuted && + !this.isSystemMuteOverridden ) { this.actions.app.muteApp({ isMuted: dnd, diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index 2e009893a..066638613 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js @@ -1,5 +1,5 @@ import { observable, computed, action } from 'mobx'; -import moment from 'moment'; +import dayjs from 'dayjs'; import jwt from 'jsonwebtoken'; import localStorage from 'mobx-localstorage'; import { session } from '@electron/remote'; @@ -46,7 +46,10 @@ export default class UserStore extends Store { @observable updateUserInfoRequest = new Request(this.api.user, 'updateInfo'); - @observable getLegacyServicesRequest = new CachedRequest(this.api.user, 'getLegacyServices'); + @observable getLegacyServicesRequest = new CachedRequest( + this.api.user, + 'getLegacyServices', + ); @observable deleteAccountRequest = new CachedRequest(this.api.user, 'delete'); @@ -81,13 +84,17 @@ export default class UserStore extends Store { // Register action handlers this.actions.user.login.listen(this._login.bind(this)); - this.actions.user.retrievePassword.listen(this._retrievePassword.bind(this)); + this.actions.user.retrievePassword.listen( + this._retrievePassword.bind(this), + ); this.actions.user.logout.listen(this._logout.bind(this)); this.actions.user.signup.listen(this._signup.bind(this)); this.actions.user.invite.listen(this._invite.bind(this)); this.actions.user.update.listen(this._update.bind(this)); this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); - this.actions.user.importLegacyServices.listen(this._importLegacyServices.bind(this)); + this.actions.user.importLegacyServices.listen( + this._importLegacyServices.bind(this), + ); this.actions.user.delete.listen(this._delete.bind(this)); // Reactions @@ -144,7 +151,7 @@ export default class UserStore extends Store { if (!this.authToken) return false; const { tokenExpiry } = this._parseToken(this.authToken); - return this.authToken !== null && moment(tokenExpiry).isBefore(moment()); + return this.authToken !== null && dayjs(tokenExpiry).isBefore(dayjs()); } @computed get data() { @@ -176,7 +183,14 @@ export default class UserStore extends Store { } @action async _signup({ - firstname, lastname, email, password, accountType, company, plan, currency, + firstname, + lastname, + email, + password, + accountType, + company, + plan, + currency, }) { const authToken = await this.signupRequest.execute({ firstname, @@ -205,7 +219,7 @@ export default class UserStore extends Store { } @action async _invite({ invites }) { - const data = invites.filter((invite) => invite.email !== ''); + const data = invites.filter(invite => invite.email !== ''); const response = await this.inviteRequest.execute(data)._promise; @@ -220,7 +234,8 @@ export default class UserStore extends Store { @action async _update({ userData }) { if (!this.isLoggedIn) return; - const response = await this.updateUserInfoRequest.execute(userData)._promise; + const response = await this.updateUserInfoRequest.execute(userData) + ._promise; this.getUserInfoRequest.patch(() => response.data); this.actionStatus = response.status || []; @@ -250,15 +265,20 @@ export default class UserStore extends Store { this.isImportLegacyServicesExecuting = true; // Reduces recipe duplicates - const recipes = services.filter((obj, pos, arr) => arr.map((mapObj) => mapObj.recipe.id).indexOf(obj.recipe.id) === pos).map((s) => s.recipe.id); + const recipes = services + .filter( + (obj, pos, arr) => + arr.map(mapObj => mapObj.recipe.id).indexOf(obj.recipe.id) === pos, + ) + .map(s => s.recipe.id); // Install recipes - for (const recipe of recipes) { // eslint-disable-line no-unused-vars + for (const recipe of recipes) { // eslint-disable-next-line await this.stores.recipes._install({ recipeId: recipe }); } - for (const service of services) { // eslint-disable-line no-unused-vars + for (const service of services) { this.actions.service.createFromLegacyService({ data: service, }); @@ -281,8 +301,7 @@ export default class UserStore extends Store { const { router } = this.stores; const currentRoute = window.location.hash; - if (!this.isLoggedIn - && currentRoute.includes('token=')) { + if (!this.isLoggedIn && currentRoute.includes('token=')) { router.push(this.WELCOME_ROUTE); const token = currentRoute.split('=')[1]; @@ -293,17 +312,16 @@ export default class UserStore extends Store { this._tokenLogin(token); }, 1000); } - } else if (!this.isLoggedIn - && !currentRoute.includes(this.BASE_ROUTE)) { + } else if (!this.isLoggedIn && !currentRoute.includes(this.BASE_ROUTE)) { router.push(this.WELCOME_ROUTE); - } else if (this.isLoggedIn - && currentRoute === this.LOGOUT_ROUTE) { + } else if (this.isLoggedIn && currentRoute === this.LOGOUT_ROUTE) { this.actions.user.logout(); router.push(this.LOGIN_ROUTE); - } else if (this.isLoggedIn - && currentRoute.includes(this.BASE_ROUTE) - && (this.hasCompletedSignup - || this.hasCompletedSignup === null)) { + } else if ( + this.isLoggedIn && + currentRoute.includes(this.BASE_ROUTE) && + (this.hasCompletedSignup || this.hasCompletedSignup === null) + ) { if (!isDevMode) { this.stores.router.push('/'); } @@ -336,11 +354,11 @@ export default class UserStore extends Store { try { const decoded = jwt.decode(authToken); - return ({ + return { id: decoded.userId, - tokenExpiry: moment.unix(decoded.exp).toISOString(), + tokenExpiry: dayjs.unix(decoded.exp).toISOString(), authToken, - }); + }; } catch (err) { this._logout(); return false; -- cgit v1.2.3-70-g09d2