forked from Shiloh/remnantchat
refactor: add usePeerRoomAction hook
This commit is contained in:
parent
40b0b7047e
commit
9cc26895de
@ -1,25 +1,19 @@
|
|||||||
import { useState } from 'react'
|
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
import Button from '@mui/material/Button'
|
import Button from '@mui/material/Button'
|
||||||
import Typography from '@mui/material/Typography'
|
import Typography from '@mui/material/Typography'
|
||||||
|
|
||||||
import { usePeerRoom } from 'hooks/usePeerRoom'
|
import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom'
|
||||||
import { PeerRoom } from 'services/PeerRoom'
|
import { PeerRoom } from 'services/PeerRoom'
|
||||||
|
|
||||||
enum PeerActions {
|
|
||||||
MESSAGE = 'MESSAGE',
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RoomProps {
|
interface RoomProps {
|
||||||
peerRoom: PeerRoom
|
peerRoom: PeerRoom
|
||||||
roomId: string
|
roomId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
function Room({ peerRoom, roomId }: RoomProps) {
|
function Room({ peerRoom, roomId }: RoomProps) {
|
||||||
const { makeAction } = peerRoom
|
const [sendMessage, receiveMessage] = usePeerRoomAction<string>(
|
||||||
|
peerRoom,
|
||||||
const [[sendMessage, receiveMessage]] = useState(() =>
|
PeerActions.MESSAGE
|
||||||
makeAction<string>(PeerActions.MESSAGE)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
receiveMessage(message => {
|
receiveMessage(message => {
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './usePeerRoom'
|
export * from './usePeerRoom'
|
||||||
|
export * from './usePeerRoomAction'
|
||||||
|
@ -2,12 +2,12 @@ import { useEffect, useState } from 'react'
|
|||||||
|
|
||||||
import { PeerRoom, getPeerRoom } from 'services/PeerRoom'
|
import { PeerRoom, getPeerRoom } from 'services/PeerRoom'
|
||||||
|
|
||||||
interface PeerRoomProps {
|
interface UsePeerRoomProps {
|
||||||
appId: string
|
appId: string
|
||||||
roomId: string
|
roomId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function usePeerRoom({ appId, roomId }: PeerRoomProps) {
|
export function usePeerRoom({ appId, roomId }: UsePeerRoomProps) {
|
||||||
const [peerRoom, setPeerRoom] = useState<PeerRoom | null>(null)
|
const [peerRoom, setPeerRoom] = useState<PeerRoom | null>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
13
src/hooks/usePeerRoom/usePeerRoomAction.ts
Normal file
13
src/hooks/usePeerRoom/usePeerRoomAction.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
|
||||||
|
import { PeerRoom } from 'services/PeerRoom'
|
||||||
|
|
||||||
|
export enum PeerActions {
|
||||||
|
MESSAGE = 'MESSAGE',
|
||||||
|
}
|
||||||
|
|
||||||
|
export function usePeerRoomAction<T>(peerRoom: PeerRoom, action: PeerActions) {
|
||||||
|
const [peerRoomAction] = useState(() => peerRoom.makeAction<T>(action))
|
||||||
|
|
||||||
|
return peerRoomAction
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user