-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameView.tsx
More file actions
86 lines (77 loc) · 2.39 KB
/
GameView.tsx
File metadata and controls
86 lines (77 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { usePeers } from '@fishjam-cloud/react-client';
import type { FC } from 'react';
import { useMemo, useEffect, useRef } from 'react';
import { useQuery } from '@tanstack/react-query';
import AgentPanel from '@/components/AgentPanel';
import { PeerTile } from '@/components/PeerTile';
import RoomControls from '@/components/RoomControls';
import { useTRPCClient } from '@/contexts/trpc';
export type GameViewProps = {
roomId: string;
};
const GameView: FC<GameViewProps> = ({ roomId }) => {
const { remotePeers, localPeer } = usePeers<{ name: string }>();
const trpcClient = useTRPCClient();
const agentAudioRef = useRef<HTMLAudioElement>(null);
console.log(remotePeers);
const { data: roomData } = useQuery({
queryKey: ['room', roomId],
queryFn: () => trpcClient.getRoom.query({ roomId }),
staleTime: Infinity,
});
const agentPeerId = useMemo(
() => roomData?.peers?.find((peer: any) => peer.type === 'agent')?.id,
[roomData?.peers],
);
const displayedPeers = useMemo(
() =>
agentPeerId
? remotePeers.filter((peer) => String(peer.id) !== String(agentPeerId))
: remotePeers,
[remotePeers, agentPeerId],
);
const agentPeer = useMemo(
() =>
agentPeerId
? remotePeers.find((peer) => String(peer.id) === String(agentPeerId))
: undefined,
[remotePeers, agentPeerId],
);
useEffect(() => {
if (!agentAudioRef.current) return;
const audioStream = agentPeer?.tracks[0]?.stream;
agentAudioRef.current.srcObject = audioStream ?? null;
}, [agentPeer?.microphoneTrack?.stream]);
const gridColumns = displayedPeers.length + 1;
return (
<>
<section className="w-full h-1/2 flex gap-8 pt-10 px-10">
<AgentPanel />
<RoomControls roomId={roomId} />
</section>
<section
className="w-full h-1/2 grid place-items-center gap-4 py-10 px-10"
style={{ gridTemplateColumns: `repeat(${gridColumns}, minmax(0, 1fr))` }}
>
<PeerTile
className="max-w-2xl"
name="You"
stream={localPeer?.cameraTrack?.stream}
/>
{displayedPeers.map((peer) => (
<PeerTile
className="max-w-2xl"
name={peer.metadata?.peer?.name ?? peer.id}
key={peer.id}
stream={
peer.customVideoTracks[0]?.stream ?? peer.cameraTrack?.stream
}
audioStream={ peer.microphoneTrack?.stream }
/>
))}
</section>
<audio ref={agentAudioRef} autoPlay playsInline title={"Agent audio"} />
</>
);
};
export default GameView;