|
|
|
|
@ -1,7 +1,9 @@ |
|
|
|
|
import React, { ReactNode, useMemo, memo, useEffect } from 'react'; |
|
|
|
|
|
|
|
|
|
import { UserAction } from '../../../../app/ui'; |
|
|
|
|
import { roomTypes } from '../../../../app/utils/client'; |
|
|
|
|
import { IRoom } from '../../../../definition/IRoom'; |
|
|
|
|
import { useUserSubscription } from '../../../contexts/UserContext'; |
|
|
|
|
import { RoomManager, useHandleRoom } from '../../../lib/RoomManager'; |
|
|
|
|
import { AsyncStatePhase } from '../../../lib/asyncState'; |
|
|
|
|
import Skeleton from '../Room/Skeleton'; |
|
|
|
|
@ -13,18 +15,23 @@ export type Props = { |
|
|
|
|
rid: IRoom['_id']; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const fields = {}; |
|
|
|
|
|
|
|
|
|
const RoomProvider = ({ rid, children }: Props): JSX.Element => { |
|
|
|
|
const { phase, value: room } = useHandleRoom(rid); |
|
|
|
|
|
|
|
|
|
const subscribed = Boolean(useUserSubscription(rid, fields)); |
|
|
|
|
const context = useMemo(() => { |
|
|
|
|
if (!room) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
room._id = rid; |
|
|
|
|
return { |
|
|
|
|
subscribed, |
|
|
|
|
rid, |
|
|
|
|
room: { ...room, name: roomTypes.getRoomName(room.t, room) }, |
|
|
|
|
}; |
|
|
|
|
}, [room, rid]); |
|
|
|
|
}, [room, rid, subscribed]); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
RoomManager.open(rid); |
|
|
|
|
@ -33,6 +40,17 @@ const RoomProvider = ({ rid, children }: Props): JSX.Element => { |
|
|
|
|
}; |
|
|
|
|
}, [rid]); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (!subscribed) { |
|
|
|
|
return (): void => undefined; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
UserAction.addStream(rid); |
|
|
|
|
return (): void => { |
|
|
|
|
UserAction.cancel(rid); |
|
|
|
|
}; |
|
|
|
|
}, [rid, subscribed]); |
|
|
|
|
|
|
|
|
|
if (phase === AsyncStatePhase.LOADING || !room) { |
|
|
|
|
return <Skeleton />; |
|
|
|
|
} |
|
|
|
|
|