Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crisp(bug): fixing incompatibility with turbo and logoff only when needed #2595

Merged
merged 2 commits into from
Feb 6, 2025
Merged
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
67 changes: 64 additions & 3 deletions app/javascript/controllers/crisp_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ export default class extends Controller {

connect() {
if (!this.displayCrispValue) {
// If the user is logged out from the app but crisp is still loaded, we loggout the user from crisp
if (window.$crisp) { this.logout(); };
// If the user is logged out (displayCrispValue is false) but crisp is still loaded, we logout the user from crisp
if (window.$crisp) {
this.logout();
}
return;
}

// safe mode is enabled to avoid console warnings related to MutationObserver modifications
window.$crisp = window.$crisp || [];
window.$crisp.push(["safe", true]);
this.#setupMutationObserverOverride();

if (window.CRISP_TOKEN_ID === this.userCrispTokenValue) {
// If the user is already logged in, we don't need to do anything
return;
Expand All @@ -34,6 +41,9 @@ export default class extends Controller {
window.$crisp = [];
window.CRISP_WEBSITE_ID = process.env.CRISP_WEBSITE_ID;

// Ajout du safe mode pour supprimer l'avertissement dans la console lié aux modifs du MutationObserver
window.$crisp.push(["safe", true]);

if (user) {
window.CRISP_TOKEN_ID = user.crispToken;
window.$crisp.push(["set", "user:email", [user.email]]);
Expand Down Expand Up @@ -62,10 +72,61 @@ export default class extends Controller {

logout() {
if (window.$crisp) {
window.CRISP_TOKEN_ID = null;
window.CRISP_WEBSITE_ID = undefined;
window.CRISP_TOKEN_ID = undefined;
window.$crisp.push(["do", "session:reset"]);
window.$crisp.push(["do", "session:destroy"]);
window.$crisp.push(["do", "chat:hide"]);
}
}

#setupMutationObserverOverride() {
// Fix for solving Crisp/Turbo incompatibility : https://github.com/crisp-im/crisp-sdk-web/issues/39
if (!window.originalMutationObserver) {
window.originalMutationObserver = window.MutationObserver
window.listOfObservers = [];

window.MutationObserver = function(aFunction) {
/* eslint new-cap: ["error", { "newIsCap": false }] */

const observer = new window.originalMutationObserver(aFunction);
const { stack } = new Error();

if (stack?.includes("crisp")) {
window.listOfObservers.push(observer);
}

return observer;
};

window.CRISP_READY_TRIGGER = () => this.#onCrispReady();
}
}

#disconnectAllObservers() {
window.listOfObservers?.forEach((observer) => {
observer.disconnect();
});
}

#reconnectAllObservers() {
window.listOfObservers?.forEach((observer) => {
observer.reconnect();
});
}

#moveCrispToPermanentContainer() {
const crispWidget = document.querySelector(".crisp-client");
const crispWrapper = document.getElementById("crisp-wrapper");

if (crispWidget && crispWrapper && !crispWrapper.contains(crispWidget)) {
this.#disconnectAllObservers();
crispWrapper.appendChild(crispWidget);
this.#reconnectAllObservers();
}
}

#onCrispReady() {
this.#moveCrispToPermanentContainer();
}
}
1 change: 1 addition & 0 deletions app/views/common/_crisp.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
data-crisp-user-nickname-value="<%= current_agent&.to_s %>"
data-crisp-user-crisp-token-value="<%= current_agent&.crisp_token %>">
</div>
<div id="crisp-wrapper" data-turbo-permanent></div>
Holist marked this conversation as resolved.
Show resolved Hide resolved