Rooms.tsx 1.16 KB
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType, RawMessage } from '../components/common/types';
import { RoomBlock } from '../components/rooms/RoomBlock';
import { Room } from '../components/rooms/types';
import SocketContext from '../contexts/SocketContext';

export const Rooms: React.FC = () => {
  const socket = useContext(SocketContext);
  const [ rooms, setRooms ] = useState<Room[]>([]);

  const refreshRooms = useCallback(() => {
    const rawMessage: RawMessage = {
      type: MessageType.ROOM_LIST_REQUEST,
      message: {}
    }
    socket.emit('msg', rawMessage, (response: MessageResponse<Room[]>) => {
      if (response.ok) {
        setRooms(response.result!);
      } else {
        // TODO: 에러 핸들링
        console.log("방 목록을 수신하지 못함");
        console.log(response);
      }
    });
  }, []);

  useEffect(refreshRooms, []);

  return (
    <Main>
      <div className='mt-auto w-screen flex flex-col items-center'>
        {rooms.map((room) => (<RoomBlock key={room.uuid} room={room} />))}
      </div>
    </Main>
  )
}