xonecas.io
8058
Ir

irvc / files

irvc / web / src / components / CallView.tsx

2.2 KB
1import { useEffect, useRef } from "react";
2import { cn } from "cn";
3import type { Camera, Voice } from "../lib/voice";
4
5// Tile shows one camera publication. Our own preview is muted (it would feed
6// back) and mirrored so it reads like a mirror, matching every video app.
7function Tile({ cam, className }: { cam: Camera; className?: string }) {
8  const ref = useRef<HTMLVideoElement>(null);
9
10  useEffect(() => {
11    const el = ref.current;
12    if (!el) return;
13    cam.track.attach(el);
14    return () => {
15      cam.track.detach(el);
16    };
17  }, [cam.track]);
18
19  return (
20    <div className={cn("relative min-h-0 overflow-hidden rounded-md border border-line bg-black", className)}>
21      <video
22        ref={ref}
23        autoPlay
24        playsInline
25        muted={cam.self}
26        className={cn("absolute inset-0 h-full w-full object-cover", cam.self && "-scale-x-100")}
27      />
28      <span className="absolute bottom-1 left-1 rounded bg-scrim px-1.5 py-0.5 text-xs text-fg">
29        {cam.self ? "you" : cam.name}
30      </span>
31    </div>
32  );
33}
34
35// CallView fills the call pane of a DM: peers fill the pane, our own camera
36// sits as a small picture-in-picture in the corner so we can see what we send.
37export default function CallView({ voice, className }: { voice: Voice; className?: string }) {
38  const { cameras } = voice;
39  const self = cameras.find((c) => c.self) ?? null;
40  const remotes = cameras.filter((c) => !c.self);
41  return (
42    <div className={cn("relative min-h-0 bg-surface", className)}>
43      <div
44        className={cn(
45          "grid h-full min-h-0 auto-rows-fr gap-1 p-1",
46          remotes.length > 1 ? "grid-cols-2" : "grid-cols-1",
47        )}
48      >
49        {remotes.length > 0 ? (
50          remotes.map((c) => <Tile key={c.id} cam={c} />)
51        ) : (
52          <div className="grid place-items-center text-sm text-faint">
53            {voice.status === "live" ? "waiting for video…" : voice.error || voice.status}
54          </div>
55        )}
56      </div>
57      {self && (
58        <div className="absolute right-2 bottom-2 aspect-video w-28 overflow-hidden rounded-md border border-line shadow-lg md:w-40">
59          <Tile cam={self} className="h-full" />
60        </div>
61      )}
62    </div>
63  );
64}