@@ -2,6 +2,7 @@ import { InfiniteData, useQueryClient } from '@tanstack/react-query'
2
2
import { useEffect } from 'react'
3
3
import { useWebSocket } from '~/WebSocketContext'
4
4
import { FetchChatMessageListResponse } from '~apis/chat/fetchChatMessageList'
5
+ import { CreateChatRoomResponse } from '~apis/chatRoom/createChatRoom'
5
6
import { useHomePageData } from '~apis/main/useHomePageData'
6
7
import { FetchNotificationListResponse } from '~apis/notification/fetchNotificationList'
7
8
import { queryKey } from '~constants/queryKey'
@@ -15,94 +16,108 @@ export default function useSubscribe() {
15
16
const { isConnected, subscribe } = useWebSocket ( )
16
17
const queryClient = useQueryClient ( )
17
18
const { showNotification } = usePushNotificationStore ( )
19
+
18
20
useEffect ( ( ) => {
19
21
if ( isConnected ) {
20
- console . log ( '구독!' )
21
22
subscribe ( `/user/queue/errors` , message => {
22
23
const response = JSON . parse ( message . body )
23
24
console . log ( '에러 구독' , response )
24
25
} )
25
26
26
27
subscribe ( `/sub/message/${ email } ` , message => {
27
- const response = JSON . parse ( message . body ) as {
28
- data : {
28
+ const response = JSON . parse ( message . body )
29
+ if ( response . code === 1000 ) {
30
+ //* 첫 연결 시 모든 채팅방 반환
31
+ type Data = {
29
32
chatRoomId : number
30
33
unreadCount : number
31
- } [ ]
32
- }
33
- console . log ( '이메일 구독' , response )
34
- response . data . forEach ( chatRoom => {
35
- subscribe ( `/sub/chat/${ chatRoom . chatRoomId } ` , message => {
36
- const res = JSON . parse ( message . body ) as APIResponse < FetchChatMessageListResponse [ 'content' ] [ number ] >
37
- console . log ( '채팅방 구독' , res )
38
- queryClient . invalidateQueries ( {
39
- queryKey : queryKey . social . chatRoomList ( ) ,
40
- } )
41
- if ( res . data . chatId )
42
- queryClient . setQueryData < InfiniteData < APIResponse < FetchChatMessageListResponse > > > (
43
- queryKey . social . chatMessageList ( res . data . chatRoomId ) ,
44
- oldData => {
45
- if ( ! oldData ) {
46
- const initialPage : APIResponse < FetchChatMessageListResponse > = {
47
- code : 200 ,
48
- status : 'OK' ,
49
- message : 'Success' ,
50
- data : {
51
- content : [ res . data ] ,
52
- size : 1 ,
53
- number : 0 ,
54
- numberOfElements : 1 ,
55
- first : true ,
56
- last : true ,
57
- empty : false ,
58
- sort : {
59
- empty : true ,
60
- sorted : false ,
61
- unsorted : true ,
62
- } ,
63
- pageable : {
64
- offset : 0 ,
34
+ }
35
+ const data = response . data as Data [ ]
36
+
37
+ console . log ( '이메일 구독' , response )
38
+
39
+ data . forEach ( ( chatRoom : Data ) => {
40
+ subscribe ( `/sub/chat/${ chatRoom . chatRoomId } ` , message => {
41
+ const res = JSON . parse ( message . body ) as APIResponse < FetchChatMessageListResponse [ 'content' ] [ number ] >
42
+ console . log ( '채팅방 구독' , res )
43
+ queryClient . invalidateQueries ( {
44
+ queryKey : queryKey . social . chatRoomList ( ) ,
45
+ } )
46
+ if ( res . data . chatId )
47
+ queryClient . setQueryData < InfiniteData < APIResponse < FetchChatMessageListResponse > > > (
48
+ queryKey . social . chatMessageList ( res . data . chatRoomId ) ,
49
+ oldData => {
50
+ if ( ! oldData ) {
51
+ const initialPage : APIResponse < FetchChatMessageListResponse > = {
52
+ code : 200 ,
53
+ status : 'OK' ,
54
+ message : 'Success' ,
55
+ data : {
56
+ content : [ res . data ] ,
57
+ size : 1 ,
58
+ number : 0 ,
59
+ numberOfElements : 1 ,
60
+ first : true ,
61
+ last : true ,
62
+ empty : false ,
65
63
sort : {
66
64
empty : true ,
67
65
sorted : false ,
68
66
unsorted : true ,
69
67
} ,
70
- pageSize : 1 ,
71
- paged : true ,
72
- pageNumber : 0 ,
73
- unpaged : false ,
68
+ pageable : {
69
+ offset : 0 ,
70
+ sort : {
71
+ empty : true ,
72
+ sorted : false ,
73
+ unsorted : true ,
74
+ } ,
75
+ pageSize : 1 ,
76
+ paged : true ,
77
+ pageNumber : 0 ,
78
+ unpaged : false ,
79
+ } ,
74
80
} ,
75
- } ,
81
+ }
82
+ return {
83
+ pages : [ initialPage ] ,
84
+ pageParams : [ null ] ,
85
+ }
76
86
}
77
87
return {
78
- pages : [ initialPage ] ,
79
- pageParams : [ null ] ,
80
- }
81
- }
82
- return {
83
- ...oldData ,
84
- pages : oldData . pages . map ( ( page , index ) => {
85
- if ( index === 0 ) {
86
- return {
87
- ...page ,
88
- data : {
89
- ...page . data ,
90
- content : [ ...page . data . content , res . data ] ,
91
- numberOfElements : page . data . numberOfElements + 1 ,
92
- } ,
88
+ ...oldData ,
89
+ pages : oldData . pages . map ( ( page , index ) => {
90
+ if ( index === 0 ) {
91
+ return {
92
+ ...page ,
93
+ data : {
94
+ ...page . data ,
95
+ content : [ ...page . data . content , res . data ] ,
96
+ numberOfElements : page . data . numberOfElements + 1 ,
97
+ } ,
98
+ }
93
99
}
94
- }
95
- return page
96
- } ) ,
100
+ return page
101
+ } ) ,
102
+ }
97
103
}
98
- }
99
- )
104
+ )
105
+ } )
100
106
} )
101
- } )
107
+ }
108
+ if ( response . code === 1001 ) {
109
+ //* 첫 연결 이후부터 새로운 채팅방 생성 시
110
+ const data = response . data as CreateChatRoomResponse
111
+ //todo 새로운 채팅방 추가
112
+ }
102
113
} )
103
114
104
- subscribe ( `sub/notification/${ email } ` , message => {
115
+ subscribe ( `/ sub/notification/${ email } ` , message => {
105
116
const response = JSON . parse ( message . body ) as APIResponse < FetchNotificationListResponse [ 'content' ] [ number ] >
117
+ console . log ( '알림 구독' )
118
+ if ( ! response . data . content ) {
119
+ return
120
+ }
106
121
showNotification ( response . data . content )
107
122
console . log ( response )
108
123
queryClient . setQueryData < InfiniteData < APIResponse < FetchNotificationListResponse > > > (
0 commit comments