-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat : 실시간 알림 전역 상태관리 구현 및 애니메이션 적용 #104
- Loading branch information
Showing
7 changed files
with
140 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import useAuthStore from '@store/authStore'; | ||
import useNotificationStore from '@store/notificationStore'; | ||
import { useCallback, useEffect } from 'react'; | ||
import NotiContainer from './NotiContainer'; | ||
|
||
const NotificationProvider = () => { | ||
const { isLogin } = useAuthStore(); | ||
const { message, connect, state } = useNotificationStore(); | ||
|
||
const connectSSE = useCallback(() => { | ||
connect(); | ||
}, [connect]); | ||
|
||
useEffect(() => { | ||
if (!isLogin) return; | ||
connectSSE(); | ||
|
||
if (!state) { | ||
connectSSE(); | ||
} | ||
}, [isLogin, connectSSE, state]); | ||
|
||
return <>{isLogin && message && <NotiContainer message={message} />}</>; | ||
}; | ||
|
||
export default NotificationProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { create } from 'zustand'; | ||
import { EventSourcePolyfill, NativeEventSource } from 'event-source-polyfill'; | ||
import { getAuthToken, getRole } from '@utils/auth'; | ||
import { BASE_URL } from '@constants/constants'; | ||
import { SseAlarm } from '@typings/types'; | ||
|
||
interface NotificationState { | ||
message: string | null; | ||
connect: () => void; | ||
disconnect: () => void; | ||
state: boolean; | ||
} | ||
|
||
const EventSource = EventSourcePolyfill || NativeEventSource; | ||
const role = getRole(); | ||
const ssePath = | ||
role === 'ROLE_USER' | ||
? `${BASE_URL}/api/subscribe/user` | ||
: `${BASE_URL}/api/subscribe`; | ||
|
||
const useNotificationStore = create<NotificationState>((set) => ({ | ||
message: null, | ||
state: true, | ||
link: '/', | ||
|
||
connect: () => { | ||
const token = getAuthToken() || ''; | ||
|
||
const eventSource = new EventSource(ssePath, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
withCredentials: true, | ||
heartbeatTimeout: 1860000, | ||
}); | ||
|
||
eventSource.onopen = () => { | ||
console.log('연결됨'); | ||
set(() => ({ state: true })); | ||
}; | ||
|
||
eventSource.onmessage = (event) => { | ||
const newMessage: SseAlarm = JSON.parse(event.data); | ||
console.log(newMessage); | ||
|
||
if ( | ||
newMessage.content === 'connected!' || | ||
newMessage.notificationType === undefined | ||
) { | ||
return; | ||
} | ||
|
||
if (newMessage.content !== 'connected!' && newMessage.notificationType) { | ||
const newText = | ||
newMessage.notificationType === 'REVIEW_CREATED' | ||
? '새 리뷰가 등록되었습니다.' | ||
: '새로운 예약이 등록되었습니다.'; | ||
|
||
set((state) => { | ||
if (state.message === newText) { | ||
return state; | ||
} | ||
return { message: newText }; | ||
}); | ||
|
||
setTimeout(() => { | ||
set(() => ({ message: null })); | ||
}, 2000); | ||
} | ||
}; | ||
|
||
// sse 에러 발생하면 연결 종료 | ||
eventSource.onerror = (error) => { | ||
console.error('SSE Error:', error); | ||
eventSource.close(); | ||
set(() => ({ message: null })); | ||
set(() => ({ state: false })); | ||
}; | ||
|
||
set(() => ({ | ||
disconnect: () => { | ||
console.log('연결 해제'); | ||
eventSource.close(); | ||
}, | ||
})); | ||
}, | ||
|
||
disconnect: () => { | ||
console.warn('연결 해제 시도'); | ||
}, | ||
})); | ||
|
||
export default useNotificationStore; |