Skip to content

Commit f0d4a2d

Browse files
committed
✨Feat: 알림 구독
1 parent cf8ed00 commit f0d4a2d

File tree

2 files changed

+80
-64
lines changed

2 files changed

+80
-64
lines changed

src/components/SendMessageForm/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default function SendMessageForm({ chatRoomId, ...rest }: SendMessageForm
2424
if (!message.trim()) return
2525
sendMessage(message)
2626
$form.reset()
27+
$form['message'].focus()
2728
}
2829

2930
return (

src/hooks/useSubscribe.ts

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { InfiniteData, useQueryClient } from '@tanstack/react-query'
22
import { useEffect } from 'react'
33
import { useWebSocket } from '~/WebSocketContext'
44
import { FetchChatMessageListResponse } from '~apis/chat/fetchChatMessageList'
5+
import { CreateChatRoomResponse } from '~apis/chatRoom/createChatRoom'
56
import { useHomePageData } from '~apis/main/useHomePageData'
67
import { FetchNotificationListResponse } from '~apis/notification/fetchNotificationList'
78
import { queryKey } from '~constants/queryKey'
@@ -15,94 +16,108 @@ export default function useSubscribe() {
1516
const { isConnected, subscribe } = useWebSocket()
1617
const queryClient = useQueryClient()
1718
const { showNotification } = usePushNotificationStore()
19+
1820
useEffect(() => {
1921
if (isConnected) {
20-
console.log('구독!')
2122
subscribe(`/user/queue/errors`, message => {
2223
const response = JSON.parse(message.body)
2324
console.log('에러 구독', response)
2425
})
2526

2627
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 = {
2932
chatRoomId: number
3033
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,
6563
sort: {
6664
empty: true,
6765
sorted: false,
6866
unsorted: true,
6967
},
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+
},
7480
},
75-
},
81+
}
82+
return {
83+
pages: [initialPage],
84+
pageParams: [null],
85+
}
7686
}
7787
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+
}
9399
}
94-
}
95-
return page
96-
}),
100+
return page
101+
}),
102+
}
97103
}
98-
}
99-
)
104+
)
105+
})
100106
})
101-
})
107+
}
108+
if (response.code === 1001) {
109+
//* 첫 연결 이후부터 새로운 채팅방 생성 시
110+
const data = response.data as CreateChatRoomResponse
111+
//todo 새로운 채팅방 추가
112+
}
102113
})
103114

104-
subscribe(`sub/notification/${email}`, message => {
115+
subscribe(`/sub/notification/${email}`, message => {
105116
const response = JSON.parse(message.body) as APIResponse<FetchNotificationListResponse['content'][number]>
117+
console.log('알림 구독')
118+
if (!response.data.content) {
119+
return
120+
}
106121
showNotification(response.data.content)
107122
console.log(response)
108123
queryClient.setQueryData<InfiniteData<APIResponse<FetchNotificationListResponse>>>(

0 commit comments

Comments
 (0)