diff --git a/components/Dashboard/TeamVoiceLine.tsx b/components/Dashboard/TeamVoiceLine.tsx index 368ff97..eeca5ed 100644 --- a/components/Dashboard/TeamVoiceLine.tsx +++ b/components/Dashboard/TeamVoiceLine.tsx @@ -20,52 +20,11 @@ import { Collections } from "../../services/collections"; import { KeyCode } from "../../globals/keycode"; import { Tooltip } from "antd"; import { useAudioContext } from "../../contexts/audioContext"; +import UserStatusBubble, { UserPulse } from "../UserStatusBubble"; const userService = new UserService(); const db = getFirestore(); -function statusBubble(status: UserStatus) { - switch (status) { - case UserStatus.online: - return ( - - ); - case UserStatus.busy: - return ( - - ); - case UserStatus.offline: - return ( - - ); - default: - return ( - - ); - } -} - -function renderPulse(status: UserStatus) { - switch (status) { - case UserStatus.online: - return ( - - ); - case UserStatus.busy: - return ( - - ); - case UserStatus.offline: - return ( - - ); - default: - return ( - - ); - } -} - const maxNumberOfKeyboardMappings: number = 9; export default function TeamVoiceLine() { @@ -177,7 +136,7 @@ export default function TeamVoiceLine() { - {statusBubble(tmember.userStatus)} + ) : ( - <>{renderPulse(tmember.userStatus)} + )} ); diff --git a/components/UserStatusBubble.tsx b/components/UserStatusBubble.tsx new file mode 100644 index 0000000..dfdac15 --- /dev/null +++ b/components/UserStatusBubble.tsx @@ -0,0 +1,49 @@ +import { IoPulseOutline, IoRemoveOutline } from "react-icons/io5"; +import { UserStatus } from "../models/user"; + +interface UserStatusPropsInterface { + status: UserStatus; +} + +export default function UserStatusBubble(props: UserStatusPropsInterface) { + switch (props.status) { + case UserStatus.online: + return ( + + ); + case UserStatus.busy: + return ( + + ); + case UserStatus.offline: + return ( + + ); + default: + return ( + + ); + } +} + +// same as status pretty much but prolly less needed for all components +export function UserPulse(props: UserStatusPropsInterface) { + switch (props.status) { + case UserStatus.online: + return ( + + ); + case UserStatus.busy: + return ( + + ); + case UserStatus.offline: + return ( + + ); + default: + return ( + + ); + } +} diff --git a/models/user.ts b/models/user.ts index 839bdf1..b79b053 100644 --- a/models/user.ts +++ b/models/user.ts @@ -36,6 +36,5 @@ export class User { export enum UserStatus { online = "online", offline = "offline", - inCall = "in call", busy = "busy", } diff --git a/pages/teams/[teamid].tsx b/pages/teams/[teamid].tsx index 298b10c..429e1c5 100644 --- a/pages/teams/[teamid].tsx +++ b/pages/teams/[teamid].tsx @@ -13,7 +13,9 @@ import { } from "../../contexts/teamDashboardContext"; import { Team } from "../../models/team"; import { TeamMemberStatus } from "../../models/teamMember"; +import { UserStatus } from "../../models/user"; import TeamService from "../../services/teamService"; +import UserService from "../../services/userService"; // User has switched back to the tab const onFocus = () => { @@ -25,30 +27,49 @@ const onBlur = () => { console.log("Tab is blurred"); }; -const onClose = (ev) => { +const alertUserAboutClosing = (ev) => { // change user status to offline ev.preventDefault(); return (ev.returnValue = "are you sure?"); }; +const userService = new UserService(); + function TeamDashboard() { const { currUser } = useAuth(); const router = useRouter(); const teamDashboardContext = useTeamDashboardContext(); + // user goes offline + const handleTabClosing = () => { + // change status of user + userService.updateUserStatus(currUser.uid, UserStatus.offline); + }; + + // shows browser alert to warn user of exiting + const alertUserAboutClosing = (event: any) => { + event.preventDefault(); + event.returnValue = ""; + }; + useEffect(() => { window.addEventListener("focus", onFocus); window.addEventListener("blur", onBlur); // Calls onFocus when the window first loads onFocus(); - // Specify how to clean up after this effect: - window.addEventListener("beforeunload", onClose); + // set user to online until he closes tab/window + userService.updateUserStatus(currUser.uid, UserStatus.online); + + window.addEventListener("beforeunload", alertUserAboutClosing); + window.addEventListener("unload", handleTabClosing); return () => { + // Specify how to clean up after this effect: window.removeEventListener("focus", onFocus); window.removeEventListener("blur", onBlur); - window.removeEventListener("beforeunload", onClose); + window.removeEventListener("beforeunload", alertUserAboutClosing); + window.removeEventListener("unload", handleTabClosing); }; }, []); diff --git a/services/userService.tsx b/services/userService.tsx index 47d8995..dd69436 100644 --- a/services/userService.tsx +++ b/services/userService.tsx @@ -11,7 +11,7 @@ import { Unsubscribe, DocumentSnapshot, } from "firebase/firestore"; -import { User } from "../models/user"; +import { User, UserStatus } from "../models/user"; import { Collections } from "./collections"; export default class UserService { @@ -65,4 +65,13 @@ export default class UserService { { merge: true } ); } + + async updateUserStatus(userId: string, newStatus: UserStatus) { + const docRef = doc(this.db, Collections.users, userId); + await setDoc( + docRef, + { userStatus: newStatus, lastUpdatedDate: serverTimestamp() }, + { merge: true } + ); + } }