@@ -5,23 +5,46 @@ import Stomp from "stompjs";
55const Profile = ( { userInfo } ) => {
66 const [ seasonCommitCount , setSeasonCommitCount ] = useState ( userInfo . seasonCommitCount ) ;
77 const [ petExp , setPetExp ] = useState ( userInfo . petExp ) ;
8- const [ maxExp ] = useState ( 100 ) ; // 최대 경험치 (필요하면 변경 가능)
8+ const [ username , setUsername ] = useState ( userInfo . username ) ;
9+ const [ client , setClient ] = useState ( null ) ; // WebSocket 클라이언트 상태
10+ const maxExp = userInfo . petGrow === 'EGG' ? 150 : userInfo . petGrow === 'HATCH' ? 300 : 300 ;
11+ const [ commitCount , setCommitCount ] = useState ( 0 ) ;
12+ const tierEmojis = {
13+ SEED : "🌱" ,
14+ SPROUT : "🌿" ,
15+ FLOWER : "🌺" ,
16+ FRUIT : "🍎" ,
17+ TREE : "🌳" ,
18+ } ;
919
1020 // 경험치 바 계산
1121 const progress = ( petExp / maxExp ) * 100 ;
1222
1323 useEffect ( ( ) => {
14- // WebSocket 설정
24+ // username이 없으면 대기 (WebSocket 연결 X)
25+ if ( ! username ) {
26+ console . log ( "Username is not available yet, waiting..." ) ;
27+ return ;
28+ }
29+
30+ // 기존 WebSocket 연결이 있으면 끊기
31+ if ( client ) {
32+ client . disconnect ( ( ) => {
33+ console . log ( "Previous WebSocket disconnected." ) ;
34+ } ) ;
35+ }
36+
37+ // 새로운 WebSocket 연결
1538 const socket = new SockJS ( "http://localhost:8090/ws" ) ;
16- const client = Stomp . over ( socket ) ;
39+ const newClient = Stomp . over ( socket ) ;
1740
18- client . connect ( { } , ( ) => {
41+ newClient . connect ( { } , ( ) => {
1942 console . log ( "WebSocket connected!" ) ;
2043
2144 // 커밋 수 업데이트 메시지 수신
22- client . subscribe ( `/topic/commit/${ userInfo . username } ` , ( message ) => {
45+ newClient . subscribe ( `/topic/commit/${ userInfo . username } ` , ( message ) => {
2346 const newCommitCount = JSON . parse ( message . body ) ;
24-
47+ setCommitCount ( newCommitCount ) ;
2548 // 시즌 커밋 수 업데이트
2649 setSeasonCommitCount ( ( prev ) => prev + newCommitCount ) ;
2750
@@ -32,14 +55,17 @@ const Profile = ({ userInfo }) => {
3255 console . error ( "WebSocket error:" , error ) ;
3356 } ) ;
3457
58+ // WebSocket 클라이언트 저장
59+ setClient ( newClient ) ;
60+
3561 return ( ) => {
36- if ( client ) {
37- client . disconnect ( ( ) => {
62+ if ( newClient ) {
63+ newClient . disconnect ( ( ) => {
3864 console . log ( "WebSocket disconnected." ) ;
3965 } ) ;
4066 }
4167 } ;
42- } , [ userInfo . username ] ) ;
68+ } , [ username ] ) ; // username이 변경될 때마다 WebSocket 연결
4369
4470 return (
4571 < div className = "flex-box" >
@@ -59,7 +85,8 @@ const Profile = ({ userInfo }) => {
5985 < img src = { userInfo . avatarUrl } alt = "User Avatar" className = "avatar" /> { userInfo . username }
6086 </ div >
6187 < div > 이번 시즌 커밋 수: { seasonCommitCount } </ div >
62- < div > 티어: { userInfo . tier } / 마지막 커밋 날짜: { new Date ( userInfo . lastCommitted ) . toLocaleDateString ( ) } </ div >
88+ < div > 업데이트 커밋 수: { commitCount } </ div >
89+ < div > 티어: { tierEmojis [ userInfo . tier ] || userInfo . tier } / 마지막 커밋 날짜: { new Date ( userInfo . lastCommitted ) . toLocaleDateString ( ) } </ div >
6390
6491 { /* 펫 정보 */ }
6592 < div > 🐾 펫 정보</ div >
0 commit comments