Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Reset hook to clear internal state and go back to a first loaded state #628

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ const api = {
store.setState({ minimized: false });
parentCall('openWidget');
},

reset() {
store.resetState();
}
};

const onNewMessage = (event) => {
Expand Down
21 changes: 17 additions & 4 deletions src/store/Store.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this new variable assignment?

this._state = { ...initialState, ...storedState };

window.addEventListener('storage', (e) => {
// Cross-tab communication
Expand All @@ -47,7 +53,7 @@ export default class Store {
});

window.addEventListener('visibilitychange', () => {
!this._state.minimized && !this._state.triggered && parentCall('openWidget');
!this._state.minimized && parentCall('openWidget');
Copy link
Contributor

@murtaza98 murtaza98 Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't get this change. Why are we not checking for the triggered property now? Could you please elaborate on the logic here?

this._state.iframe.visible ? parentCall('showWidget') : parentCall('hideWidget');
});

Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ function minimizeWidget() {
callHook('minimizeWidget');
}

function reset() {
callHook('reset');
}

function initialize(params) {
for (const method in params) {
if (!params.hasOwnProperty(method)) {
Expand Down Expand Up @@ -439,6 +443,7 @@ window.RocketChat.livechat = {
hideWidget,
maximizeWidget,
minimizeWidget,
reset,

// callbacks
onChatMaximized(fn) { registerCallback('chat-maximized', fn); },
Expand Down