diff --git a/src/hooks/useSubscribe.ts b/src/hooks/useSubscribe.ts index 772d243..22880bc 100644 --- a/src/hooks/useSubscribe.ts +++ b/src/hooks/useSubscribe.ts @@ -8,11 +8,21 @@ import { queryKey } from '~constants/queryKey' import { usePushNotificationStore } from '~stores/usePushNotificationStore' import { APIResponse } from '~types/api' +const getToken = () => { + try { + return localStorage.getItem('token') + } catch (error) { + console.error('Error accessing localStorage:', error) + return null + } +} + export default function useSubscribe() { + const token = getToken() const { data } = useQuery({ queryKey: queryKey.home(), queryFn: () => fetchHomePageData().then(data => data.data), - enabled: !!localStorage.getItem('token'), + enabled: !!token, }) const { isConnected, subscribe } = useWebSocket() @@ -20,161 +30,157 @@ export default function useSubscribe() { const { showNotification } = usePushNotificationStore() useEffect(() => { - if (isConnected) { - subscribe(`/user/queue/errors`, message => { - const response = JSON.parse(message.body) - console.log('에러 구독', response) - }) + if (!token || !isConnected) return - subscribe(`/sub/message/${data?.email || ''}`, message => { - const response = JSON.parse(message.body) - if (response.code === 1000) { - //* 첫 연결 시 모든 채팅방 반환 - type Data = { - chatRoomId: number - unreadCount: number - } - const data = response.data as Data[] + subscribe(`/user/queue/errors`, message => { + const response = JSON.parse(message.body) + console.log('에러 구독', response) + }) + + subscribe(`/sub/message/${data?.email || ''}`, message => { + const response = JSON.parse(message.body) + if (response.code === 1000) { + type Data = { + chatRoomId: number + unreadCount: number + } + const data = response.data as Data[] - console.log('이메일 구독', response) + console.log('이메일 구독', response) - data.forEach((chatRoom: Data) => { - subscribe(`/sub/chat/${chatRoom.chatRoomId}`, message => { - const res = JSON.parse(message.body) as APIResponse - console.log('채팅방 구독', res) - queryClient.invalidateQueries({ - queryKey: queryKey.social.chatRoomList(), - }) - if (res.data.chatId) - queryClient.setQueryData>>( - queryKey.social.chatMessageList(res.data.chatRoomId), - oldData => { - if (!oldData) { - const initialPage: APIResponse = { - code: 200, - status: 'OK', - message: 'Success', - data: { - content: [res.data], - size: 1, - number: 0, - numberOfElements: 1, - first: true, - last: true, - empty: false, + data.forEach((chatRoom: Data) => { + subscribe(`/sub/chat/${chatRoom.chatRoomId}`, message => { + const res = JSON.parse(message.body) as APIResponse + console.log('채팅방 구독', res) + queryClient.invalidateQueries({ + queryKey: queryKey.social.chatRoomList(), + }) + if (res.data.chatId) + queryClient.setQueryData>>( + queryKey.social.chatMessageList(res.data.chatRoomId), + oldData => { + if (!oldData) { + const initialPage: APIResponse = { + code: 200, + status: 'OK', + message: 'Success', + data: { + content: [res.data], + size: 1, + number: 0, + numberOfElements: 1, + first: true, + last: true, + empty: false, + sort: { + empty: true, + sorted: false, + unsorted: true, + }, + pageable: { + offset: 0, sort: { empty: true, sorted: false, unsorted: true, }, - pageable: { - offset: 0, - sort: { - empty: true, - sorted: false, - unsorted: true, - }, - pageSize: 1, - paged: true, - pageNumber: 0, - unpaged: false, - }, + pageSize: 1, + paged: true, + pageNumber: 0, + unpaged: false, }, - } - return { - pages: [initialPage], - pageParams: [null], - } + }, } return { - ...oldData, - pages: oldData.pages.map((page, index) => { - if (index === 0) { - return { - ...page, - data: { - ...page.data, - content: [...page.data.content, res.data], - numberOfElements: page.data.numberOfElements + 1, - }, - } - } - return page - }), + pages: [initialPage], + pageParams: [null], } } - ) - }) + return { + ...oldData, + pages: oldData.pages.map((page, index) => { + if (index === 0) { + return { + ...page, + data: { + ...page.data, + content: [...page.data.content, res.data], + numberOfElements: page.data.numberOfElements + 1, + }, + } + } + return page + }), + } + } + ) }) - } - if (response.code === 1001) { - //* 첫 연결 이후부터 새로운 채팅방 생성 시 - // const data = response.data as CreateChatRoomResponse - //todo 새로운 채팅방 추가 - queryClient.invalidateQueries({ queryKey: queryKey.social.chatRoomList() }) - } - }) + }) + } + if (response.code === 1001) { + queryClient.invalidateQueries({ queryKey: queryKey.social.chatRoomList() }) + } + }) - subscribe(`/sub/notification/${data?.email || ''}`, message => { - const response = JSON.parse(message.body) as APIResponse - console.log('알림 구독', response) - if (!response.data.content) { - return - } - showNotification(response.data.content) - console.log(response) - queryClient.setQueryData>>( - queryKey.notification(), - oldData => { - if (!oldData) { - return { - pages: [ - { - code: 200, - status: 'OK', - message: 'Success', - data: { - content: [response.data], - pageable: { - offset: 0, - sort: { empty: true, sorted: false, unsorted: true }, - pageSize: 1, - paged: true, - pageNumber: 0, - unpaged: false, - }, - last: true, - size: 1, - number: 0, + subscribe(`/sub/notification/${data?.email || ''}`, message => { + const response = JSON.parse(message.body) as APIResponse + console.log('알림 구독', response) + if (!response.data.content) { + return + } + showNotification(response.data.content) + console.log(response) + queryClient.setQueryData>>( + queryKey.notification(), + oldData => { + if (!oldData) { + return { + pages: [ + { + code: 200, + status: 'OK', + message: 'Success', + data: { + content: [response.data], + pageable: { + offset: 0, sort: { empty: true, sorted: false, unsorted: true }, - first: true, - numberOfElements: 1, - empty: false, + pageSize: 1, + paged: true, + pageNumber: 0, + unpaged: false, }, + last: true, + size: 1, + number: 0, + sort: { empty: true, sorted: false, unsorted: true }, + first: true, + numberOfElements: 1, + empty: false, }, - ], - pageParams: [0], - } + }, + ], + pageParams: [0], } + } - return { - ...oldData, - pages: oldData.pages.map((page, index) => - index === 0 - ? { - ...page, - data: { - ...page.data, - content: [response.data, ...page.data.content], - numberOfElements: page.data.numberOfElements + 1, - }, - } - : page - ), - } + return { + ...oldData, + pages: oldData.pages.map((page, index) => + index === 0 + ? { + ...page, + data: { + ...page.data, + content: [response.data, ...page.data.content], + numberOfElements: page.data.numberOfElements + 1, + }, + } + : page + ), } - ) - }) - } - }, [isConnected]) + } + ) + }) + }, [isConnected, token]) }