mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 19:48:08 +00:00
feat: Reconnect logic (#9453)
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
@@ -1,26 +1,133 @@
|
||||
<script setup>
|
||||
import { ref, computed, onBeforeUnmount } from 'vue';
|
||||
import { useI18n } from 'dashboard/composables/useI18n';
|
||||
import { useRoute } from 'dashboard/composables/route';
|
||||
import { useEmitter } from 'dashboard/composables/emitter';
|
||||
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
||||
import {
|
||||
isAConversationRoute,
|
||||
isAInboxViewRoute,
|
||||
isNotificationRoute,
|
||||
} from 'dashboard/helper/routeHelpers';
|
||||
import { useEventListener } from '@vueuse/core';
|
||||
|
||||
const { t } = useI18n();
|
||||
const route = useRoute();
|
||||
|
||||
const RECONNECTED_BANNER_TIMEOUT = 2000;
|
||||
|
||||
const showNotification = ref(!navigator.onLine);
|
||||
const isDisconnected = ref(false);
|
||||
const isReconnecting = ref(false);
|
||||
const isReconnected = ref(false);
|
||||
let reconnectTimeout = null;
|
||||
|
||||
const bannerText = computed(() => {
|
||||
if (isReconnecting.value) return t('NETWORK.NOTIFICATION.RECONNECTING');
|
||||
if (isReconnected.value) return t('NETWORK.NOTIFICATION.RECONNECT_SUCCESS');
|
||||
return t('NETWORK.NOTIFICATION.OFFLINE');
|
||||
});
|
||||
|
||||
const iconName = computed(() => (isReconnected.value ? 'wifi' : 'wifi-off'));
|
||||
const canRefresh = computed(
|
||||
() => !isReconnecting.value && !isReconnected.value
|
||||
);
|
||||
|
||||
const refreshPage = () => {
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
const closeNotification = () => {
|
||||
showNotification.value = false;
|
||||
isReconnected.value = false;
|
||||
clearTimeout(reconnectTimeout);
|
||||
};
|
||||
|
||||
const isInAnyOfTheRoutes = routeName => {
|
||||
return (
|
||||
isAConversationRoute(routeName, true) ||
|
||||
isAInboxViewRoute(routeName, true) ||
|
||||
isNotificationRoute(routeName, true)
|
||||
);
|
||||
};
|
||||
|
||||
const updateWebsocketStatus = () => {
|
||||
isDisconnected.value = true;
|
||||
showNotification.value = true;
|
||||
};
|
||||
|
||||
const handleReconnectionCompleted = () => {
|
||||
isDisconnected.value = false;
|
||||
isReconnecting.value = false;
|
||||
isReconnected.value = true;
|
||||
showNotification.value = true;
|
||||
reconnectTimeout = setTimeout(closeNotification, RECONNECTED_BANNER_TIMEOUT);
|
||||
};
|
||||
|
||||
const handleReconnecting = () => {
|
||||
if (isInAnyOfTheRoutes(route.name)) {
|
||||
isReconnecting.value = true;
|
||||
isReconnected.value = false;
|
||||
showNotification.value = true;
|
||||
} else {
|
||||
handleReconnectionCompleted();
|
||||
}
|
||||
};
|
||||
|
||||
const updateOnlineStatus = event => {
|
||||
// Case: Websocket is not disconnected
|
||||
// If the app goes offline, show the notification
|
||||
// If the app goes online, close the notification
|
||||
|
||||
// Case: Websocket is disconnected
|
||||
// If the app goes offline, show the notification
|
||||
// If the app goes online but the websocket is disconnected, don't close the notification
|
||||
// If the app goes online and the websocket is not disconnected, close the notification
|
||||
|
||||
if (event.type === 'offline') {
|
||||
showNotification.value = true;
|
||||
} else if (event.type === 'online' && !isDisconnected.value) {
|
||||
handleReconnectionCompleted();
|
||||
}
|
||||
};
|
||||
|
||||
useEventListener('online', updateOnlineStatus);
|
||||
useEventListener('offline', updateOnlineStatus);
|
||||
useEmitter(BUS_EVENTS.WEBSOCKET_DISCONNECT, updateWebsocketStatus);
|
||||
useEmitter(
|
||||
BUS_EVENTS.WEBSOCKET_RECONNECT_COMPLETED,
|
||||
handleReconnectionCompleted
|
||||
);
|
||||
useEmitter(BUS_EVENTS.WEBSOCKET_RECONNECT, handleReconnecting);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
clearTimeout(reconnectTimeout);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<transition name="network-notification-fade" tag="div">
|
||||
<div v-show="showNotification" class="fixed top-4 left-2 z-50 group">
|
||||
<div v-show="showNotification" class="fixed z-50 top-4 left-2 group">
|
||||
<div
|
||||
class="flex items-center justify-between py-1 px-2 w-full rounded-lg shadow-lg bg-yellow-200 dark:bg-yellow-700 relative"
|
||||
class="relative flex items-center justify-between w-full px-2 py-1 bg-yellow-200 rounded-lg shadow-lg dark:bg-yellow-700"
|
||||
>
|
||||
<fluent-icon
|
||||
icon="wifi-off"
|
||||
:icon="iconName"
|
||||
class="text-yellow-700/50 dark:text-yellow-50"
|
||||
size="18"
|
||||
/>
|
||||
<span
|
||||
class="text-xs tracking-wide font-medium px-2 text-yellow-700/70 dark:text-yellow-50"
|
||||
class="px-2 text-xs font-medium tracking-wide text-yellow-700/70 dark:text-yellow-50"
|
||||
>
|
||||
{{ $t('NETWORK.NOTIFICATION.OFFLINE') }}
|
||||
{{ bannerText }}
|
||||
</span>
|
||||
<woot-button
|
||||
v-if="canRefresh"
|
||||
:title="$t('NETWORK.BUTTON.REFRESH')"
|
||||
variant="clear"
|
||||
size="small"
|
||||
color-scheme="warning"
|
||||
icon="arrow-clockwise"
|
||||
class="visible transition-all duration-500 ease-in-out ml-1"
|
||||
@click="refreshPage"
|
||||
/>
|
||||
<woot-button
|
||||
@@ -34,55 +141,3 @@
|
||||
</div>
|
||||
</transition>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import globalConfigMixin from 'shared/mixins/globalConfigMixin';
|
||||
import { mapGetters } from 'vuex';
|
||||
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
||||
|
||||
export default {
|
||||
mixins: [globalConfigMixin],
|
||||
|
||||
data() {
|
||||
return {
|
||||
showNotification: !navigator.onLine,
|
||||
};
|
||||
},
|
||||
|
||||
computed: {
|
||||
...mapGetters({ globalConfig: 'globalConfig/get' }),
|
||||
},
|
||||
|
||||
mounted() {
|
||||
window.addEventListener('offline', this.updateOnlineStatus);
|
||||
this.$emitter.on(BUS_EVENTS.WEBSOCKET_DISCONNECT, () => {
|
||||
// TODO: Remove this after completing the conversation list refetching
|
||||
// TODO: DIRTY FIX : CLEAN UP THIS WITH PROPER FIX, DELAYING THE RECONNECT FOR NOW
|
||||
// THE CABLE IS FIRING IS VERY COMMON AND THUS INTERFERING USER EXPERIENCE
|
||||
setTimeout(() => {
|
||||
this.updateOnlineStatus({ type: 'offline' });
|
||||
}, 4000);
|
||||
});
|
||||
},
|
||||
|
||||
beforeDestroy() {
|
||||
window.removeEventListener('offline', this.updateOnlineStatus);
|
||||
},
|
||||
|
||||
methods: {
|
||||
refreshPage() {
|
||||
window.location.reload();
|
||||
},
|
||||
|
||||
closeNotification() {
|
||||
this.showNotification = false;
|
||||
},
|
||||
|
||||
updateOnlineStatus(event) {
|
||||
if (event.type === 'offline') {
|
||||
this.showNotification = true;
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user