From e9214f7d1364224dc1030b48fb1b3bcaaf29c175 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 29 Apr 2020 14:49:30 +0100 Subject: [PATCH 1/2] Make new device toasts appear above review toasts ...but below incoming verification toasts, which means we now need to actually handle priority insertion correctly. Oh well. Fixes https://github.com/vector-im/riot-web/issues/13442 --- src/DeviceListener.js | 1 + src/stores/ToastStore.js | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/DeviceListener.js b/src/DeviceListener.js index c4beb6c01e..84c6b1d230 100644 --- a/src/DeviceListener.js +++ b/src/DeviceListener.js @@ -267,6 +267,7 @@ export default class DeviceListener { key: OTHER_DEVICES_TOAST_KEY, title: _t("Review where you’re logged in"), icon: "verification_warning", + priority: ToastStore.PRIORITY_LOW, props: { deviceIds: oldUnverifiedDeviceIds, }, diff --git a/src/stores/ToastStore.js b/src/stores/ToastStore.js index f17d13bf9e..ad185e42db 100644 --- a/src/stores/ToastStore.js +++ b/src/stores/ToastStore.js @@ -20,8 +20,9 @@ import EventEmitter from 'events'; * Holds the active toasts */ export default class ToastStore extends EventEmitter { - static PRIORITY_REALTIME = 1; - static PRIORITY_DEFAULT = 0; + static PRIORITY_REALTIME = 0; + static PRIORITY_DEFAULT = 1; + static PRIORITY_LOW = 2; static sharedInstance() { if (!global.mx_ToastStore) global.mx_ToastStore = new ToastStore(); @@ -43,12 +44,9 @@ export default class ToastStore extends EventEmitter { const oldIndex = this._toasts.findIndex(t => t.key === newToast.key); if (oldIndex === -1) { - // we only have two priorities so just push realtime ones onto the front - if (newToast.priority) { - this._toasts.unshift(newToast); - } else { - this._toasts.push(newToast); - } + let newIndex = this._toasts.length; + while (newIndex > 0 && this._toasts[newIndex - 1].priority > newToast.priority) --newIndex; + this._toasts.splice(newIndex, 0, newToast); } else { this._toasts[oldIndex] = newToast; } From 58c0cfe315752c512835703142cd6e705b1e8f45 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 29 Apr 2020 15:10:23 +0100 Subject: [PATCH 2/2] Add jsdoc explaining ordering behaviour --- src/stores/ToastStore.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/stores/ToastStore.js b/src/stores/ToastStore.js index ad185e42db..8901736739 100644 --- a/src/stores/ToastStore.js +++ b/src/stores/ToastStore.js @@ -39,6 +39,15 @@ export default class ToastStore extends EventEmitter { this._toasts = []; } + /** + * Add or replace a toast + * If a toast with the same toastKey already exists, the given toast will replace it + * Toasts are always added underneath any toasts of the same priority, so existing + * toasts stay at the top unless a higher priority one arrives (better to not change the + * toast unless necessary). + * + * @param {boject} newToast The new toast + */ addOrReplaceToast(newToast) { if (newToast.priority === undefined) newToast.priority = ToastStore.PRIORITY_DEFAULT;