From 2c7fabdf1a2aee9352b064bbd3d238f0f8308ef0 Mon Sep 17 00:00:00 2001 From: Ariel Barreiro Date: Sun, 8 Aug 2021 23:17:25 -0300 Subject: [PATCH 1/2] reset hook --- src/lib/hooks.js | 4 ++++ src/store/Store.js | 12 ++++++++++-- src/widget.js | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib/hooks.js b/src/lib/hooks.js index 036ee645c..654323734 100644 --- a/src/lib/hooks.js +++ b/src/lib/hooks.js @@ -151,6 +151,10 @@ const api = { store.setState({ minimized: false }); parentCall('openWidget'); }, + + reset() { + store.resetState(); + } }; const onNewMessage = (event) => { diff --git a/src/store/Store.js b/src/store/Store.js index 58e78f4b0..73137c5c1 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -1,6 +1,7 @@ +import { route } from 'preact-router'; import mitt from 'mitt'; - import { parentCall } from '../lib/parentCall'; +import { loadConfig } from '../lib/main'; const { localStorage, sessionStorage } = window; @@ -21,7 +22,8 @@ export default class Store { storedState = typeof storedState === 'object' ? storedState : {}; } - this._state = { ...initialState, ...storedState }; + this._initialState = initialState; + this._state = { ...initialState, ...storedState }; window.addEventListener('storage', (e) => { // Cross-tab communication @@ -87,4 +89,10 @@ export default class Store { this._state = { ...storedState, ...nonPeristable }; this.emit('change', [this._state, prevState]); } + + resetState() { + this._state = this._initialState; + loadConfig(); + route(''); + } } diff --git a/src/widget.js b/src/widget.js index ba60fb47f..42c1195a7 100644 --- a/src/widget.js +++ b/src/widget.js @@ -310,6 +310,10 @@ function minimizeWidget() { callHook('minimizeWidget'); } +function reset() { + callHook('reset'); +} + function initialize(params) { for (const method in params) { if (!params.hasOwnProperty(method)) { @@ -439,6 +443,7 @@ window.RocketChat.livechat = { hideWidget, maximizeWidget, minimizeWidget, + reset, // callbacks onChatMaximized(fn) { registerCallback('chat-maximized', fn); }, From e8f22c0d19fa93c158574d1fcff70cbc32b879d6 Mon Sep 17 00:00:00 2001 From: Ariel Barreiro Date: Sun, 22 Aug 2021 18:10:34 -0300 Subject: [PATCH 2/2] Different approach to reset It was hard to really reset state, so I am simply clearing the localstorage and redirecting the iframe. Also provide a way to reset it on initialization through a querystring --- src/store/Store.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/store/Store.js b/src/store/Store.js index 73137c5c1..532f9d173 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -2,6 +2,7 @@ import { route } from 'preact-router'; import mitt from 'mitt'; import { parentCall } from '../lib/parentCall'; import { loadConfig } from '../lib/main'; +import queryString from 'query-string'; const { localStorage, sessionStorage } = window; @@ -15,7 +16,10 @@ export default class Store { let storedState; try { - storedState = JSON.parse(localStorage.getItem(this.localStorageKey)); + const reset = queryString.parse(window.location.search).reset === 'true'; + if (!reset) { + storedState = JSON.parse(localStorage.getItem(this.localStorageKey)); + } } catch (e) { storedState = {}; } finally { @@ -49,7 +53,7 @@ export default class Store { }); window.addEventListener('visibilitychange', () => { - !this._state.minimized && !this._state.triggered && parentCall('openWidget'); + !this._state.minimized && parentCall('openWidget'); this._state.iframe.visible ? parentCall('showWidget') : parentCall('hideWidget'); }); @@ -91,8 +95,9 @@ export default class Store { } resetState() { - this._state = this._initialState; - loadConfig(); - route(''); + window.addEventListener('beforeunload', () => { + localStorage.removeItem(this.localStorageKey); + }); + document.location.href = document.location.href; } }