11<script setup lang="ts">
2- import { onUnmounted , ref , watch } from " vue" ;
2+ import { computed , watch , onMounted , ref } from " vue" ;
33import { useAuthStore } from " ../stores/auth-store" ;
44import NavbarComponent from " ./navbar-component.vue" ;
55import { services , sockets } from " ../services/api" ;
@@ -8,50 +8,71 @@ const authStore = useAuthStore();
88
99const totalUnreadMessages = ref (0 );
1010const isSocketConnected = ref (false );
11+ const isAuthenticated = computed (() => authStore .isAuthenticated .value && !! authStore .userId .value );
1112
12- watch (authStore .isAuthenticated , async (_ ) => {
13- if (authStore .isAuthenticated ) {
14- try {
15- await sockets .hub .initialize ();
16- sockets .hub .registerToEvent (" NewTotalUnreadChatMessage" ,
17- (newTotal : number ) => {
18- totalUnreadMessages .value = newTotal ;
19- });
20- sockets .hub .joinMessaging ();
21- isSocketConnected .value = true ;
22- } catch (error ) {
23- console .warn (' Failed to connect to real-time messaging:' , error );
24- isSocketConnected .value = false ;
25- }
26-
27- const userId = authStore .userId ;
28- await checkUnreadMessages (userId .value ! );
29- }
30- }, {immediate: true });
13+ const initSocket = async () => {
14+ if (isSocketConnected .value ) return ;
15+
16+ try {
17+ await sockets .hub .initialize ();
18+
19+ await sockets .hub .registerToEvent (" NewTotalUnreadChatMessage" , (newTotal : number ) => {
20+ totalUnreadMessages .value = newTotal ;
21+ });
22+
23+ await sockets .hub .joinMessaging ();
24+ isSocketConnected .value = true ;
25+
26+ const userId = authStore .userId ;
27+ if (userId ?.value ) await checkUnreadMessages (userId .value );
28+ } catch (error ) {
29+ console .warn (" Failed to connect to real-time messaging:" , error );
30+ isSocketConnected .value = false ;
31+ }
32+ };
3133
3234const checkUnreadMessages = async (userId : string ) => {
33- try {
34- const res = await services .userChatMessageStatus .getTotalUnreadMessages (userId );
35- if (res .isSuccess && res .data ) {
36- totalUnreadMessages .value = res .data .countUnreadMessages ;
37- }
38- } catch (error ) {
39- console .warn (' Failed to fetch unread message count:' , error );
35+ try {
36+ const res = await services .userChatMessageStatus .getTotalUnreadMessages (userId );
37+ if (res .isSuccess && res .data ) {
38+ totalUnreadMessages .value = res .data .countUnreadMessages ;
4039 }
41- }
40+ } catch (error ) {
41+ console .warn (" Failed to fetch unread message count:" , error );
42+ }
43+ };
4244
45+ const cleanupSocket = async () => {
46+ if (! isSocketConnected .value ) return ;
47+ try {
48+ await sockets .hub .leaveMessaging ();
49+ } catch (e ) {
50+ console .warn (" Error leaving messaging:" , e );
51+ } finally {
52+ sockets .hub .flush ();
53+ isSocketConnected .value = false ;
54+ }
55+ };
4356
44- window .addEventListener (' beforeunload' , () => {
45- if (isSocketConnected .value ) {
46- sockets .hub .leaveMessaging ();
57+ watch (
58+ isAuthenticated ,
59+ async (newVal , oldVal ) => {
60+ if (newVal && ! oldVal ) {
61+ await initSocket ();
62+ } else if (! newVal && oldVal ) {
63+ await cleanupSocket ();
4764 }
48- })
65+ },
66+ { immediate: false }
67+ );
4968
50- onUnmounted (() => {
51- if (isSocketConnected .value ) {
52- sockets .hub .leaveMessaging ();
53- }
54- })
69+ onMounted (async () => {
70+ if (isAuthenticated .value ) {
71+ await initSocket ();
72+ }
73+ });
74+
75+ window .addEventListener (" beforeunload" , cleanupSocket );
5576 </script >
5677
5778<template >
0 commit comments