Showing
2 changed files
with
75 additions
and
69 deletions
... | @@ -4,74 +4,11 @@ import SocketContext from '../../contexts/SocketContext'; | ... | @@ -4,74 +4,11 @@ import SocketContext from '../../contexts/SocketContext'; |
4 | import { MessageResponse, MessageType, RawMessage } from '../common/types'; | 4 | import { MessageResponse, MessageType, RawMessage } from '../common/types'; |
5 | import { RoomData, UpdateRoomUser } from './types'; | 5 | import { RoomData, UpdateRoomUser } from './types'; |
6 | 6 | ||
7 | -interface RoomInfoLocation { | 7 | +interface RoomInfoProps { |
8 | - state: { roomData: RoomData } | 8 | + roomData: RoomData; |
9 | } | 9 | } |
10 | 10 | ||
11 | -export const RoomInfo: React.FC = () => { | 11 | +export const RoomInfo: React.FC<RoomInfoProps> = ({ roomData }) => { |
12 | - const history = useHistory(); | ||
13 | - const socket = useContext(SocketContext); | ||
14 | - const location: RoomInfoLocation = useLocation(); | ||
15 | - | ||
16 | - const [ roomData, setRoomData ] = useState<RoomData>({ | ||
17 | - // 기본값 | ||
18 | - uuid: '0', | ||
19 | - name: 'loading...', | ||
20 | - maxUsers: 9, | ||
21 | - users: [] | ||
22 | - }); | ||
23 | - | ||
24 | - const handleUpdateRoomUser = useCallback((rawMessage: RawMessage) => { | ||
25 | - if (rawMessage.type == MessageType.ROOM_USER_UPDATE) { | ||
26 | - const data = rawMessage.message as UpdateRoomUser; | ||
27 | - console.log(data); | ||
28 | - if (data.state == 'removed') { | ||
29 | - const newUsers = roomData.users; | ||
30 | - const index = newUsers.indexOf(data.user); | ||
31 | - if (index < 0) { | ||
32 | - console.log('존재하지 않는 유저를 제거 시도'); | ||
33 | - } else { | ||
34 | - newUsers.splice(index, 1); | ||
35 | - } | ||
36 | - setRoomData({ | ||
37 | - ...roomData, | ||
38 | - users: newUsers | ||
39 | - }); | ||
40 | - } else if (data.state == 'added') { | ||
41 | - setRoomData({ | ||
42 | - ...roomData, | ||
43 | - users: [data.user, ...roomData.users] | ||
44 | - }); | ||
45 | - } | ||
46 | - } | ||
47 | - }, [roomData]); | ||
48 | - | ||
49 | - useEffect(() => { | ||
50 | - socket.on('msg', handleUpdateRoomUser); | ||
51 | - | ||
52 | - return () => { | ||
53 | - socket.off('msg', handleUpdateRoomUser); | ||
54 | - } | ||
55 | - }, [roomData]); | ||
56 | - | ||
57 | - useEffect(() => { | ||
58 | - // 비정상적인 루트로 방을 들어오면 로그인 화면으로 푸시 | ||
59 | - if (location.state === undefined) { | ||
60 | - history.push('/'); | ||
61 | - return; | ||
62 | - } | ||
63 | - | ||
64 | - setRoomData(location.state.roomData); | ||
65 | - | ||
66 | - return () => { | ||
67 | - const rawMessage: RawMessage = { | ||
68 | - type: MessageType.ROOM_LEAVE, | ||
69 | - message: '' | ||
70 | - } | ||
71 | - socket.emit('msg', rawMessage, (response : MessageResponse<undefined>) => {}); | ||
72 | - } | ||
73 | - }, []) | ||
74 | - | ||
75 | return ( | 12 | return ( |
76 | <div className='m-3 w-5/6 flex items-center place-content-between'> | 13 | <div className='m-3 w-5/6 flex items-center place-content-between'> |
77 | <div>{roomData.name}</div> | 14 | <div>{roomData.name}</div> | ... | ... |
1 | -import React, { useContext } from 'react'; | 1 | +import React, { useCallback, useContext, useEffect, useState } from 'react'; |
2 | -import { useLocation } from 'react-router'; | 2 | +import { useHistory, useLocation } from 'react-router'; |
3 | import { Main } from '../components/common/Main'; | 3 | import { Main } from '../components/common/Main'; |
4 | +import { MessageResponse, MessageType, RawMessage } from '../components/common/types'; | ||
4 | import { Canvas } from '../components/room/Canvas'; | 5 | import { Canvas } from '../components/room/Canvas'; |
5 | import { Chat } from '../components/room/Chat'; | 6 | import { Chat } from '../components/room/Chat'; |
6 | import { RoomInfo } from '../components/room/RoomInfo'; | 7 | import { RoomInfo } from '../components/room/RoomInfo'; |
8 | +import { RoomData, UpdateRoomUser } from '../components/room/types'; | ||
7 | import SocketContext from '../contexts/SocketContext'; | 9 | import SocketContext from '../contexts/SocketContext'; |
8 | 10 | ||
11 | +interface RoomLocation { | ||
12 | + state: { roomData: RoomData } | ||
13 | +} | ||
14 | + | ||
9 | export const Room: React.FC = () => { | 15 | export const Room: React.FC = () => { |
16 | + const history = useHistory(); | ||
17 | + const socket = useContext(SocketContext); | ||
18 | + const location: RoomLocation = useLocation(); | ||
19 | + | ||
20 | + const [ roomData, setRoomData ] = useState<RoomData>({ | ||
21 | + // 기본값 | ||
22 | + uuid: '0', | ||
23 | + name: 'loading...', | ||
24 | + maxUsers: 9, | ||
25 | + users: [] | ||
26 | + }); | ||
27 | + | ||
28 | + const handleUpdateRoomUser = useCallback((rawMessage: RawMessage) => { | ||
29 | + if (rawMessage.type == MessageType.ROOM_USER_UPDATE) { | ||
30 | + const data = rawMessage.message as UpdateRoomUser; | ||
31 | + console.log(data); | ||
32 | + if (data.state == 'removed') { | ||
33 | + const newUsers = roomData.users; | ||
34 | + const index = newUsers.indexOf(data.user); | ||
35 | + if (index < 0) { | ||
36 | + console.log('존재하지 않는 유저를 제거 시도'); | ||
37 | + } else { | ||
38 | + newUsers.splice(index, 1); | ||
39 | + } | ||
40 | + setRoomData({ | ||
41 | + ...roomData, | ||
42 | + users: newUsers | ||
43 | + }); | ||
44 | + } else if (data.state == 'added') { | ||
45 | + setRoomData({ | ||
46 | + ...roomData, | ||
47 | + users: [data.user, ...roomData.users] | ||
48 | + }); | ||
49 | + } | ||
50 | + } | ||
51 | + }, [roomData]); | ||
52 | + | ||
53 | + useEffect(() => { | ||
54 | + socket.on('msg', handleUpdateRoomUser); | ||
55 | + | ||
56 | + return () => { | ||
57 | + socket.off('msg', handleUpdateRoomUser); | ||
58 | + } | ||
59 | + }, [roomData]); | ||
60 | + | ||
61 | + useEffect(() => { | ||
62 | + // 비정상적인 루트로 방을 들어오면 로그인 화면으로 푸시 | ||
63 | + if (location.state === undefined) { | ||
64 | + history.push('/'); | ||
65 | + return; | ||
66 | + } | ||
67 | + | ||
68 | + setRoomData(location.state.roomData); | ||
69 | + | ||
70 | + return () => { | ||
71 | + const rawMessage: RawMessage = { | ||
72 | + type: MessageType.ROOM_LEAVE, | ||
73 | + message: '' | ||
74 | + } | ||
75 | + socket.emit('msg', rawMessage, (response : MessageResponse<undefined>) => {}); | ||
76 | + } | ||
77 | + }, []) | ||
78 | + | ||
10 | return ( | 79 | return ( |
11 | <Main> | 80 | <Main> |
12 | - <RoomInfo /> | 81 | + <RoomInfo roomData={roomData}/> |
13 | <div className='w-full flex'> | 82 | <div className='w-full flex'> |
14 | <Canvas /> | 83 | <Canvas /> |
15 | <Chat /> | 84 | <Chat /> | ... | ... |
-
Please register or login to post a comment