Skip to content

Commit 9ce1f0c

Browse files
luannmoreiraotavio
authored andcommitted
fix(ui): ensure Chatwoot is properly reset on logout
Centralizes Chatwoot toggle and reset logic in AppBar component to ensure proper cleanup on logout. Removes duplicate logic from auth module. Uses native event listener for chatwoot:on-message and dispatches chatwoot:ready when widget is available.
1 parent ee1be49 commit 9ce1f0c

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

ui/src/components/AppBar/AppBar.vue

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import {
100100
} from "vue";
101101
import { useRouter, useRoute, RouteLocationRaw, RouteLocation } from "vue-router";
102102
import { useChatWoot } from "@productdevbook/chatwoot/vue";
103-
import { useEventListener } from "@vueuse/core";
104103
import { useStore } from "@/store";
105104
import handleError from "@/utils/handleError";
106105
import UserIcon from "../User/UserIcon.vue";
@@ -126,13 +125,15 @@ defineOptions({
126125
inheritAttrs: false,
127126
});
128127
128+
const { setUser, setConversationCustomAttributes, toggle, reset } = useChatWoot();
129129
const store = useStore();
130130
const router = useRouter();
131131
const route = useRoute();
132132
const snackbar = useSnackbar();
133133
const getStatusDarkMode = computed(
134134
() => store.getters["layout/getStatusDarkMode"],
135135
);
136+
const isChatCreated = computed(() => store.getters["support/getCreatedStatus"]);
136137
const tenant = computed(() => store.getters["auth/tenant"]);
137138
const userEmail = computed(() => store.getters["auth/email"]);
138139
const userId = computed(() => store.getters["auth/id"]);
@@ -161,6 +162,11 @@ const logout = async () => {
161162
await store.dispatch("auth/logout");
162163
await store.dispatch("stats/clear");
163164
await store.dispatch("namespaces/clearNamespaceList");
165+
if (isChatCreated.value) {
166+
toggle("close");
167+
reset();
168+
store.commit("support/setCreatedStatus", false);
169+
}
164170
await router.push({ name: "Login" });
165171
} catch (error: unknown) {
166172
handleError(error);
@@ -174,8 +180,6 @@ const toggleDarkMode = () => {
174180
175181
const openChatwoot = async (): Promise<void> => {
176182
try {
177-
const { setUser, setConversationCustomAttributes, toggle } = useChatWoot();
178-
179183
await store.dispatch("support/get", tenant.value);
180184
181185
setUser(userId.value, {
@@ -184,13 +188,22 @@ const openChatwoot = async (): Promise<void> => {
184188
identifier_hash: identifier.value,
185189
});
186190
187-
useEventListener(window, "chatwoot:on-message", () => {
188-
setConversationCustomAttributes({
189-
namespace: store.getters["namespaces/get"].name,
190-
tenant: tenant.value,
191-
domain: window.location.hostname,
192-
});
193-
});
191+
window.addEventListener(
192+
"chatwoot:on-message",
193+
() => {
194+
setConversationCustomAttributes({
195+
namespace: store.getters["namespaces/get"].name,
196+
tenant: tenant.value,
197+
domain: window.location.hostname,
198+
});
199+
},
200+
{ once: true },
201+
);
202+
203+
const holder = document.querySelector(".woot-widget-holder");
204+
if (holder) {
205+
window.dispatchEvent(new CustomEvent("chatwoot:ready"));
206+
}
194207
195208
store.commit("support/setCreatedStatus", true);
196209
toggle("open");

ui/src/store/modules/auth.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Module } from "vuex";
22
import { AxiosError } from "axios";
3-
import { useChatWoot } from "@productdevbook/chatwoot/vue";
43
import * as apiAuth from "../api/auth";
54
import * as apiNamespace from "../api/namespaces";
65
import { IUserLogin } from "@/interfaces/IUserLogin";
@@ -348,12 +347,6 @@ export const auth: Module<AuthState, State> = {
348347
localStorage.removeItem("role");
349348
localStorage.removeItem("mfa");
350349
localStorage.removeItem("recovery_email");
351-
const chatIsCreated = context.rootGetters["support/getCreatedStatus"];
352-
if (chatIsCreated) {
353-
const { reset, toggle } = useChatWoot();
354-
toggle("close");
355-
reset();
356-
}
357350
},
358351

359352
changeUserData(context, data) {

ui/src/store/modules/support.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Module } from "vuex";
2+
import { useChatWoot } from "@productdevbook/chatwoot/vue";
23
import * as apiSupport from "../api/namespaces";
34
import { State } from "..";
45

@@ -31,6 +32,7 @@ export const support: Module<SupportState, State> = {
3132

3233
actions: {
3334
get: async (context, data) => {
35+
useChatWoot().reset();
3436
const res = await apiSupport.getSupportID(data);
3537
context.commit("setIdentifier", res.data.identifier);
3638
},

0 commit comments

Comments
 (0)