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..532f9d173 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -1,6 +1,8 @@ +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; @@ -14,14 +16,18 @@ 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 { storedState = typeof storedState === 'object' ? storedState : {}; } - this._state = { ...initialState, ...storedState }; + this._initialState = initialState; + this._state = { ...initialState, ...storedState }; window.addEventListener('storage', (e) => { // Cross-tab communication @@ -47,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'); }); @@ -87,4 +93,11 @@ export default class Store { this._state = { ...storedState, ...nonPeristable }; this.emit('change', [this._state, prevState]); } + + resetState() { + window.addEventListener('beforeunload', () => { + localStorage.removeItem(this.localStorageKey); + }); + document.location.href = document.location.href; + } } 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); },