import { useEffect, useRef } from 'react'; import { useSetAtom } from 'jotai'; import { CallEmbed } from '../plugins/call'; import { useCallHangupEvent } from './useCallEmbed'; import { toastQueueAtom } from '../state/toast'; import { describeCallEnd } from '../utils/callSummary'; // The fork sends io.lotus.call_summary on SFU disconnect, which normally lands // before Element Call's hangup echo; give a late one this long before toasting. const SUMMARY_GRACE_MS = 400; /** * [Gitea #143] "Call ended · 41 min · connection was good" in the existing * toast style when a call you were in ends. Duration comes from our own * join clock; the quality readout from the fork, if it arrived. */ export function useCallEndedToast(embed: CallEmbed): void { const setToast = useSetAtom(toastQueueAtom); const fired = useRef(false); useEffect(() => { fired.current = false; }, [embed]); useCallHangupEvent(embed, () => { if (fired.current || !embed.joined || embed.joinedAt === undefined) return; fired.current = true; const durationMs = Date.now() - embed.joinedAt; const toast = () => setToast({ id: `call-ended-${Date.now()}`, displayName: 'Lotus Chat', body: describeCallEnd(durationMs, embed.lastSummary), roomName: embed.room.name ?? 'Voice call', roomId: embed.roomId, }); if (embed.lastSummary) toast(); else setTimeout(toast, SUMMARY_GRACE_MS); }); }