@@ -1,13 +1,14 @@
11 import { useCallback, useEffect, useRef, useState } from "react";
2-import { AtSign, Hash, Menu, MoreHorizontal, ScreenShare, Settings as SettingsIcon } from "lucide-react";
2+import { AtSign, Hash, Menu, MoreHorizontal, PhoneOff, ScreenShare, Settings as SettingsIcon, Video } from "lucide-react";
33 import { cn } from "cn";
44 import { Button, Centered, IconButton, Menu as MenuPrimitive, MenuItem } from "@/ui";
55 import { api } from "../lib/api";
66 import { usePresence } from "../lib/presence";
77 import { useSettings } from "../lib/settings";
88 import { useTheme } from "../lib/theme";
9-import { useVoice, type VoiceState } from "../lib/voice";
9+import { beep, useVoice, type VoiceState } from "../lib/voice";
1010 import type { Channel, Message, Reaction, User, UserSummary, VoiceOccupant } from "../lib/types";
11+import CallView from "../components/CallView";
1112 import Composer from "../components/Composer";
1213 import MessageActions from "../components/MessageActions";
1314 import MessageList, { hasImage } from "../components/MessageList";
@@ -41,9 +42,12 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
4142 const [voiceRooms, setVoiceRooms] = useState<Record<string, VoiceOccupant[]>>({});
4243 const [view, setView] = useState<"channel" | "settings">("channel");
4344 const [openShare, setOpenShare] = useState<string | null>(null);
45+ const [ringing, setRinging] = useState<number | null>(null);
4446 const desktop = useRef(matchMedia("(min-width: 768px)").matches);
4547 const [sidebarOpen, setSidebarOpen] = useState(desktop.current);
4648 const selectedId = useRef<number | null>(null);
49+ // The channel of the DM call we started, so we can tell the peer when it ends.
50+ const rangRef = useRef<number | null>(null);
4751 // The server drops our voice state when the socket dies, so keep the last
4852 // thing we told it and replay that on every (re)connect.
4953 const voiceState = useRef<VoiceState>({ channelId: 0, muted: false, deafened: false });
@@ -72,6 +76,23 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
7276 setChannels((cs) => (cs.some((c) => c.id === ev.channel!.id) ? cs : [...cs, ev.channel!]));
7377 } else if (ev.type === "channel_delete" && ev.channelId) {
7478 setChannels((cs) => cs.filter((c) => c.id !== ev.channelId));
79+ } else if (ev.type === "ring" && ev.channelId) {
80+ // Answering sends its own ring; ignore it when already in that call.
81+ if (voice.channel?.id === ev.channelId) return;
82+ setRinging(ev.channelId);
83+ // Answering means opening the DM, so surface it for the callee.
84+ const dm = channels.find((c) => c.id === ev.channelId);
85+ if (dm) open(dm);
86+ else
87+ api<Channel[]>("/channels")
88+ .then((cs) => {
89+ setChannels(cs);
90+ const c = cs.find((x) => x.id === ev.channelId);
91+ if (c) open(c);
92+ })
93+ .catch(() => {});
94+ } else if (ev.type === "ring_end" && ev.channelId) {
95+ setRinging((r) => (r === ev.channelId ? null : r));
7596 } else if (ev.message) {
7697 if (ev.type === "message") {
7798 setChannels((cs) => {
@@ -107,6 +128,31 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
107128 });
108129 const theme = useTheme();
109130
131+ // Ring while a peer calls us. The slow beep repeats (when enabled) until we
132+ // answer, they hang up, or the call times out as missed.
133+ useEffect(() => {
134+ if (ringing === null) return;
135+ const timeout = setTimeout(() => setRinging(null), 30_000);
136+ const sound = settings.ringSound ? (beep(880), setInterval(() => beep(880), 2000)) : 0;
137+ return () => {
138+ clearTimeout(timeout);
139+ clearInterval(sound);
140+ };
141+ }, [ringing, settings.ringSound]);
142+
143+ // Joining the call answers the ring.
144+ useEffect(() => {
145+ if (ringing !== null && voice.channel?.id === ringing) setRinging(null);
146+ }, [ringing, voice.channel]);
147+
148+ // Tell the peer when a DM call we started is over.
149+ useEffect(() => {
150+ const started = rangRef.current;
151+ if (started === null || voice.channel?.id === started) return;
152+ rangRef.current = null;
153+ api(`/channels/${started}/ring`, { method: "DELETE" }).catch(() => {});
154+ }, [voice.channel]);
155+
110156 // The watched share lives in the LiveKit room, so it vanishes when the sharer
111157 // stops; the pane then falls back to the selected channel.
112158 const share = voice.shares.find((s) => s.id === openShare) ?? null;
@@ -177,6 +223,14 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
177223 if (!desktop.current) setSidebarOpen(false);
178224 }
179225
226+ // startCall opens a DM video call and rings the peer. Joining leaves any
227+ // voice channel first, since only one call can use the mic and camera.
228+ function startCall(c: Channel) {
229+ rangRef.current = c.id;
230+ voice.join(c);
231+ api(`/channels/${c.id}/ring`, { method: "POST" }).catch(() => {});
232+ }
233+
180234 async function selectUser(u: UserSummary) {
181235 try {
182236 const dm = await api<Channel>(`/dms/${u.id}`, { method: "POST" });
@@ -254,6 +308,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
254308
255309 const chat = selected?.type === "text" || selected?.type === "dm";
256310 const showingShare = view === "channel" && share !== null;
311+ const inCall = selected?.type === "dm" && voice.channel?.id === selected.id;
257312
258313 return (
259314 <div className="flex h-dvh overflow-hidden">
@@ -339,6 +394,16 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
339394 <Hash size={16} className="text-faint" />
340395 ))}
341396 <span className="truncate text-sm font-medium">{selected?.name}</span>
397+ {selected?.type === "dm" && (
398+ <IconButton
399+ label={inCall ? "leave call" : ringing === selected.id ? "answer call" : "start video call"}
400+ variant={inCall ? "danger" : ringing === selected.id ? "primary" : "ghost"}
401+ size="sm"
402+ onClick={() => (inCall ? voice.leave() : startCall(selected))}
403+ >
404+ {inCall ? <PhoneOff /> : <Video />}
405+ </IconButton>
406+ )}
342407 {selected?.type === "text" && !selected.permanent && (
343408 <MenuPrimitive
344409 className="ml-auto"
@@ -368,37 +433,40 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
368433 <ScreenShareView track={share.track} />
369434 ) : chat && selected ? (
370435 <>
371- <MessageList
372- channelId={selected.id}
373- messages={messages}
374- username={user.username}
375- collapsed={collapsed}
376- editing={editing}
377- activeId={active?.id ?? null}
378- onSelect={(m) => setActive((a) => (a?.id === m.id ? null : m))}
379- onToggleImages={toggleImages}
380- onEditBody={(body) => setEditing((e) => (e ? { ...e, body } : e))}
381- onCancelEdit={() => setEditing(null)}
382- onSaveEdit={saveEdit}
383- />
384- {active && !editing && (
385- <MessageActions
386- message={active}
387- own={active.username === user.username}
388- onReact={(r) => react(active, r)}
389- onEdit={() => startEdit(active)}
390- onDelete={() => deleteMessage(active)}
391- onClose={() => setActive(null)}
436+ {inCall && <CallView voice={voice} className="flex-[3]" />}
437+ <div className={cn("flex min-h-0 flex-1 flex-col", inCall && "flex-[1] border-t border-line")}>
438+ <MessageList
439+ channelId={selected.id}
440+ messages={messages}
441+ username={user.username}
442+ collapsed={collapsed}
443+ editing={editing}
444+ activeId={active?.id ?? null}
445+ onSelect={(m) => setActive((a) => (a?.id === m.id ? null : m))}
446+ onToggleImages={toggleImages}
447+ onEditBody={(body) => setEditing((e) => (e ? { ...e, body } : e))}
448+ onCancelEdit={() => setEditing(null)}
449+ onSaveEdit={saveEdit}
392450 />
393- )}
394- <Composer
395- channel={selected}
396- users={users}
397- onEditLast={startEditLast}
398- onCollapseImages={() =>
399- setCollapsed(new Set(messages.filter((m) => hasImage(m.body)).map((m) => m.id)))
400- }
401- />
451+ {active && !editing && (
452+ <MessageActions
453+ message={active}
454+ own={active.username === user.username}
455+ onReact={(r) => react(active, r)}
456+ onEdit={() => startEdit(active)}
457+ onDelete={() => deleteMessage(active)}
458+ onClose={() => setActive(null)}
459+ />
460+ )}
461+ <Composer
462+ channel={selected}
463+ users={users}
464+ onEditLast={startEditLast}
465+ onCollapseImages={() =>
466+ setCollapsed(new Set(messages.filter((m) => hasImage(m.body)).map((m) => m.id)))
467+ }
468+ />
469+ </div>
402470 </>
403471 ) : (
404472 <Centered>select a channel</Centered>