"use client"; import { useCallback, useEffect, useRef, useState, RefObject } from "react"; import { Box, Button, Text, VStack, HStack, Icon } from "@chakra-ui/react"; import { toaster } from "../../components/ui/toaster"; import { useRouter } from "next/navigation"; import useSessionStatus from "../../lib/useSessionStatus"; import { useRecordingConsent } from "../../recordingConsentContext"; import useApi from "../../lib/useApi"; import { FaBars } from "react-icons/fa6"; interface Meeting { id: string; room_url: string; host_room_url?: string; recording_type: string; platform?: string; } interface WherebyRoomProps { meeting: Meeting; } // Focus management for Whereby embed and consent dialog const useConsentWherebyFocusManagement = ( acceptButtonRef: RefObject, wherebyRef: RefObject, ) => { const currentFocusRef = useRef(null); useEffect(() => { if (acceptButtonRef.current) { acceptButtonRef.current.focus(); } else { console.error( "accept button ref not available yet for focus management - seems to be illegal state", ); } const handleWherebyReady = () => { console.log("whereby ready - refocusing consent button"); currentFocusRef.current = document.activeElement as HTMLElement; if (acceptButtonRef.current) { acceptButtonRef.current.focus(); } }; if (wherebyRef.current) { wherebyRef.current.addEventListener("ready", handleWherebyReady); } else { console.warn( "whereby ref not available yet for focus management - seems to be illegal state. not waiting, focus management off.", ); } return () => { wherebyRef.current?.removeEventListener("ready", handleWherebyReady); currentFocusRef.current?.focus(); }; }, []); }; const useConsentDialog = ( meetingId: string, wherebyRef: RefObject, ) => { const { state: consentState, touch, hasConsent } = useRecordingConsent(); const [consentLoading, setConsentLoading] = useState(false); const [modalOpen, setModalOpen] = useState(false); const api = useApi(); const handleConsent = useCallback( async (meetingId: string, given: boolean) => { if (!api) return; setConsentLoading(true); try { await api.v1MeetingAudioConsent({ meetingId, requestBody: { consent_given: given }, }); touch(meetingId); } catch (error) { console.error("Error submitting consent:", error); } finally { setConsentLoading(false); } }, [api, touch], ); const showConsentModal = useCallback(() => { if (modalOpen) return; setModalOpen(true); const toastId = toaster.create({ placement: "top", duration: null, render: ({ dismiss }) => { const AcceptButton = () => { const buttonRef = useRef(null); useConsentWherebyFocusManagement(buttonRef, wherebyRef); return ( ); }; return ( Can we have your permission to store this meeting's audio recording on our servers? ); }, }); // Set modal state when toast is dismissed toastId.then((id) => { const checkToastStatus = setInterval(() => { if (!toaster.isActive(id)) { setModalOpen(false); clearInterval(checkToastStatus); } }, 100); }); // Handle escape key to close the toast const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") { toastId.then((id) => toaster.dismiss(id)); } }; document.addEventListener("keydown", handleKeyDown); const cleanup = () => { toastId.then((id) => toaster.dismiss(id)); document.removeEventListener("keydown", handleKeyDown); }; return cleanup; }, [meetingId, handleConsent, wherebyRef, modalOpen]); return { showConsentModal, consentState, hasConsent, consentLoading }; }; function ConsentDialogButton({ meetingId, wherebyRef, }: { meetingId: string; wherebyRef: React.RefObject; }) { const { showConsentModal, consentState, hasConsent, consentLoading } = useConsentDialog(meetingId, wherebyRef); if (!consentState.ready || hasConsent(meetingId) || consentLoading) { return null; } return ( ); } const recordingTypeRequiresConsent = (recordingType: string) => { return recordingType === "cloud"; }; // Whereby SDK loading hook const useWhereby = () => { const [wherebyLoaded, setWherebyLoaded] = useState(false); useEffect(() => { if (typeof window !== "undefined") { import("@whereby.com/browser-sdk/embed") .then(() => { setWherebyLoaded(true); }) .catch(console.error.bind(console)); } }, []); return wherebyLoaded; }; export default function WherebyRoom({ meeting }: WherebyRoomProps) { const wherebyLoaded = useWhereby(); const wherebyRef = useRef(null); const router = useRouter(); const { isLoading, isAuthenticated } = useSessionStatus(); const roomUrl = meeting?.host_room_url ? meeting?.host_room_url : meeting?.room_url; const handleLeave = useCallback(() => { router.push("/browse"); }, [router]); useEffect(() => { if (isLoading || !isAuthenticated || !roomUrl || !wherebyLoaded) return; wherebyRef.current?.addEventListener("leave", handleLeave); return () => { wherebyRef.current?.removeEventListener("leave", handleLeave); }; }, [handleLeave, roomUrl, isLoading, isAuthenticated, wherebyLoaded]); if (!roomUrl || !wherebyLoaded) { return null; } return ( <> {recordingTypeRequiresConsent(meeting.recording_type) && ( )} ); }