-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: useEffect 하나로 동작하도록 변경 * feat: Pagination을 위한 데이터 구조, 상태, API 변경 * feat: Pagination 버튼 컴포넌트 데이터 사용 * feat: 페이지네이션 기능 추가 SSE, RoomList, RoomListPage 로직 변경 및 상수 추가 * docs: README.md 업데이트
- Loading branch information
1 parent
83a62b2
commit ea36048
Showing
11 changed files
with
163 additions
and
118 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export const RULES = Object.freeze({ | ||
maxPage: 9, | ||
maxPlayer: 4, | ||
pageLimit: 9, | ||
}); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,47 +1,53 @@ | ||
import useRoomStore from '@/stores/zustand/useRoomStore'; | ||
import { useEffect } from 'react'; | ||
import { Room } from '@/types/roomTypes'; | ||
import { ENV } from '@/config/env'; | ||
import { getRoomsQuery } from '@/stores/queries/getRoomsQuery'; | ||
|
||
export const useRoomsSSE = () => { | ||
const { data: initialRooms } = getRoomsQuery(); | ||
const { setRooms } = useRoomStore(); | ||
let eventSource: EventSource | null = null; | ||
|
||
useEffect(() => { | ||
// 초기 데이터 설정 | ||
if (initialRooms) { | ||
setRooms(initialRooms); | ||
} | ||
export const useRoomsSSE = () => { | ||
const { setRooms, setPagination, setUserPage } = useRoomStore(); | ||
const userPage = useRoomStore((state) => state.userPage); | ||
const { data } = getRoomsQuery(userPage); | ||
|
||
// SSE 연결 | ||
const eventSource = new EventSource(ENV.SSE_URL); | ||
const connectSSE = (userPage: number) => { | ||
eventSource = new EventSource(`${ENV.SSE_URL}?page=${userPage}`); | ||
|
||
// rooms 데이터 수신 처리 | ||
eventSource.onmessage = (event) => { | ||
try { | ||
const rooms = JSON.parse(event.data) as Room[]; | ||
setRooms(rooms); | ||
const sseData = JSON.parse(event.data); | ||
setRooms(sseData.rooms); | ||
setPagination(sseData.pagination); | ||
|
||
if (!sseData.rooms.length && userPage > 0) { | ||
setUserPage(sseData.pagination.currentPage - 1); | ||
return; | ||
} | ||
|
||
setUserPage(sseData.pagination.currentPage); | ||
} catch (error) { | ||
console.error('Failed to parse rooms data:', error); | ||
} | ||
}; | ||
|
||
// 연결 시작 | ||
eventSource.onopen = () => { | ||
console.log('SSE Connection opened'); | ||
}; | ||
|
||
// 에러 처리 | ||
eventSource.onerror = (error) => { | ||
console.error('SSE Error:', error); | ||
eventSource.close(); | ||
}; | ||
}; | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setRooms(data.rooms); | ||
setPagination(data.pagination); | ||
connectSSE(userPage); | ||
} | ||
|
||
// 컴포넌트 언마운트 시 연결 정리 (메모리 누수 예방) | ||
return () => { | ||
console.log('Closing SSE connection'); | ||
eventSource.close(); | ||
if (eventSource) { | ||
eventSource.close(); | ||
eventSource = null; | ||
} | ||
}; | ||
}, [initialRooms, setRooms]); | ||
}, [data?.pagination, data?.rooms, userPage]); | ||
}; |
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
Oops, something went wrong.