diff --git a/src/components/Room/Room.tsx b/src/components/Room/Room.tsx index 6e15e05..c1b566f 100644 --- a/src/components/Room/Room.tsx +++ b/src/components/Room/Room.tsx @@ -1,25 +1,19 @@ -import { useState } from 'react' import { useParams } from 'react-router-dom' import Button from '@mui/material/Button' import Typography from '@mui/material/Typography' -import { usePeerRoom } from 'hooks/usePeerRoom' +import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom' import { PeerRoom } from 'services/PeerRoom' -enum PeerActions { - MESSAGE = 'MESSAGE', -} - interface RoomProps { peerRoom: PeerRoom roomId: string } function Room({ peerRoom, roomId }: RoomProps) { - const { makeAction } = peerRoom - - const [[sendMessage, receiveMessage]] = useState(() => - makeAction(PeerActions.MESSAGE) + const [sendMessage, receiveMessage] = usePeerRoomAction( + peerRoom, + PeerActions.MESSAGE ) receiveMessage(message => { diff --git a/src/hooks/usePeerRoom/index.ts b/src/hooks/usePeerRoom/index.ts index 0ae1bf4..9241b36 100644 --- a/src/hooks/usePeerRoom/index.ts +++ b/src/hooks/usePeerRoom/index.ts @@ -1 +1,2 @@ export * from './usePeerRoom' +export * from './usePeerRoomAction' diff --git a/src/hooks/usePeerRoom/usePeerRoom.ts b/src/hooks/usePeerRoom/usePeerRoom.ts index ac9fc02..96728e5 100644 --- a/src/hooks/usePeerRoom/usePeerRoom.ts +++ b/src/hooks/usePeerRoom/usePeerRoom.ts @@ -2,12 +2,12 @@ import { useEffect, useState } from 'react' import { PeerRoom, getPeerRoom } from 'services/PeerRoom' -interface PeerRoomProps { +interface UsePeerRoomProps { appId: string roomId: string } -export function usePeerRoom({ appId, roomId }: PeerRoomProps) { +export function usePeerRoom({ appId, roomId }: UsePeerRoomProps) { const [peerRoom, setPeerRoom] = useState(null) useEffect(() => { diff --git a/src/hooks/usePeerRoom/usePeerRoomAction.ts b/src/hooks/usePeerRoom/usePeerRoomAction.ts new file mode 100644 index 0000000..30ed7ca --- /dev/null +++ b/src/hooks/usePeerRoom/usePeerRoomAction.ts @@ -0,0 +1,13 @@ +import { useState } from 'react' + +import { PeerRoom } from 'services/PeerRoom' + +export enum PeerActions { + MESSAGE = 'MESSAGE', +} + +export function usePeerRoomAction(peerRoom: PeerRoom, action: PeerActions) { + const [peerRoomAction] = useState(() => peerRoom.makeAction(action)) + + return peerRoomAction +}