import { Avatar, Tooltip } from "antd"; import toast from "react-hot-toast"; import { FaPhoneSlash, FaPlus, FaWalking } from "react-icons/fa"; import { HiSpeakerphone } from "react-icons/hi"; import { useAuth } from "../contexts/authContext"; import { useTeamDashboardContext } from "../contexts/teamDashboardContext"; import OfficeRoom, { OfficeRoomState } from "../models/officeRoom"; import { User } from "../models/user"; import OfficeRoomService from "../services/officeRoomService"; import { VscDebugDisconnect } from "react-icons/vsc"; import AgoraService from "../services/agoraService"; interface IOfficeCard { officeRoom: OfficeRoom; handleJoinChannel: Function; handleLeaveChannel: Function; } const officeRoomService = new OfficeRoomService(); const agoraService = new AgoraService(); export default function OfficeCard(props: IOfficeCard) { const { currUser } = useAuth(); const { teamUsersMap, user } = useTeamDashboardContext(); var allMembersInRoom = props.officeRoom.members?.reduce((results, userId) => { if (userId in teamUsersMap) { results.push(teamUsersMap[userId]); } if (userId == currUser.uid) { results.push(user); } return results; }, [] as User[]); async function handleJoinOfficeRoom() { toast.loading("Connecting"); // make sure user is not in another room // get agora token from cloud function // join channel for agora if (props.officeRoom.members.includes(currUser.uid)) { toast.error("You are already in this office room!"); return; } try { // agora token from CF const agoraToken = await agoraService.getAgoraToken(props.officeRoom.id); // // function to do it all // await officeRoomService.joinOfficeRoom(props.officeRoom, currUser.uid); // handle joining agora channel await props.handleJoinChannel(props.officeRoom.id, agoraToken); // update firestore to add ourselves in the office room const newMembers = [...props.officeRoom.members, currUser.uid]; await officeRoomService.updateMembersInOfficeRoom( props.officeRoom.id, newMembers ); } catch (error) { console.error(error); toast.error("problem joining office room"); } toast.dismiss(); toast.success("joined office room"); } async function handleLeaveOfficeRoom() { toast.loading("leaving"); if (!props.officeRoom.members.includes(currUser.uid)) { toast.error("You are not in this office room!"); return; } try { // leave agora channel await props.handleLeaveChannel(); // leave from firestore database const newMembersInRoom = props.officeRoom.members.filter( (memberId) => memberId != currUser.uid ); await officeRoomService.updateMembersInOfficeRoom( props.officeRoom.id, newMembersInRoom ); } catch (error) { console.error(error); toast.error("problem leaving office room"); } toast.dismiss(); toast.success("left office room"); } // check if user is in the office room var isUserInRoom: boolean = false; if (props.officeRoom.members?.includes(currUser.uid)) { isUserInRoom = true; } return ( {renderOfficePulse(props.officeRoom.state)} {/* office location name */} {props.officeRoom.name} {/* all members in room */} {allMembersInRoom.map((member) => { return ( {member.nickName[0]} ); })} {isUserInRoom ? ( ) : ( )} ); } function renderOfficePulse(officeRoomState: OfficeRoomState) { switch (officeRoomState) { case OfficeRoomState.active: return ( ); case OfficeRoomState.idle: return ( ); default: return ( ); } }