Skip to content

Commit

Permalink
removed: deprecated or no more necessary code has been removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoehabb committed Dec 25, 2024
1 parent 38b0496 commit 689498c
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 774 deletions.
19 changes: 11 additions & 8 deletions apps/nova/src/pages/Call.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import cn from "classnames";
// import Messages from "@/components/Chat/Messages";
import { orNull, orUndefined } from "@litespace/sol/utils";
import {
useSessionMembers,
// useCall,
//useSessionMembers,
//usePeerIds,
useSessionV2,
useFindSessionRoomById,
useFullScreen,
usePeerIds,
} from "@litespace/headless/sessions";
import { useDisplayRecorder } from "@litespace/headless/recorder";
import { isGhost } from "@/lib/ghost";
Expand Down Expand Up @@ -56,8 +55,8 @@ const Call: React.FC = () => {
return callRoom.data.members.find((member) => member.id !== user?.id);
}, [callRoom.data, user?.id]);

const members = useSessionMembers(sessionId);
useEffect(() => console.log("In App members: ", members), [members]);
//const members = useSessionMembers(sessionId);
//useEffect(() => console.log("In App members: ", members), [members]);

// const messages = useMemo(
// () =>
Expand Down Expand Up @@ -118,6 +117,7 @@ const Call: React.FC = () => {

const fullScreen = useFullScreen<HTMLDivElement>();

/*
const peers = usePeerIds(
useMemo(
() => ({
Expand All @@ -130,6 +130,7 @@ const Call: React.FC = () => {
[sessionId, mateInfo?.id, user?.role]
)
);
*/

const onCloseSession = useCallback(() => {
// peers.ghost.refetch();
Expand All @@ -150,12 +151,12 @@ const Call: React.FC = () => {
useMemo(
() => ({
isGhost,
ghostPeerId: orNull(peers.ghost.data),
tutorPeerId: orNull(peers.tutor.data),
ghostPeerId: orNull("ghost"), // TODO: pass peers.ghost.data
tutorPeerId: orNull("ghost"), // TODO: pass peers.tutor.data
userId: orNull(user?.id),
onCloseSession,
}),
[onCloseSession, peers.ghost.data, peers.tutor.data, user?.id]
[onCloseSession, user?.id]
)
);

Expand Down Expand Up @@ -302,12 +303,14 @@ const Call: React.FC = () => {
/*
* TODO: replace this by members views
*/
/*
<div>
<h1>Joined members</h1>
{members.map((m) => (
<label className="mx-1">-{m}-</label>
))}
</div>
*/
}

{/* <Button
Expand Down
6 changes: 0 additions & 6 deletions packages/atlas/src/atlas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { User } from "@/user";
import { Auth } from "@/auth";
import { Backend } from "@litespace/types";
import { Session } from "@/session";
import { Plan } from "@/plan";
import { Coupon } from "@/coupon";
import { Invite } from "@/invite";
Expand All @@ -18,12 +17,10 @@ import { Invoice } from "@/invoice";
import { Topic } from "@/topic";
import { AuthToken } from "@/client";
import { Cache } from "@/cache";
import { Peer } from "@/peer";

export class Atlas {
public readonly user: User;
public readonly auth: Auth;
public readonly session: Session;
public readonly plan: Plan;
public readonly coupon: Coupon;
public readonly invite: Invite;
Expand All @@ -39,12 +36,10 @@ export class Atlas {
public readonly invoice: Invoice;
public readonly cache: Cache;
public readonly topic: Topic;
public readonly peer: Peer;

constructor(backend: Backend, token: AuthToken | null) {
this.user = new User(backend, token);
this.auth = new Auth(backend, token);
this.session = new Session(backend, token);
this.plan = new Plan(backend, token);
this.coupon = new Coupon(backend, token);
this.invite = new Invite(backend, token);
Expand All @@ -60,6 +55,5 @@ export class Atlas {
this.invoice = new Invoice(backend, token);
this.cache = new Cache(backend, token);
this.topic = new Topic(backend, token);
this.peer = new Peer(backend, token);
}
}
24 changes: 0 additions & 24 deletions packages/atlas/src/peer.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/atlas/src/session.ts

This file was deleted.

149 changes: 2 additions & 147 deletions packages/headless/src/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISession, IPeer, IRoom, IUser, Void, Wss } from "@litespace/types";
import { ISession, IRoom, Void, Wss } from "@litespace/types";
import { useQuery, UseQueryResult } from "@tanstack/react-query";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useAtlas } from "@/atlas/index";
Expand All @@ -7,9 +7,7 @@ import { useSocket } from "@/socket";
import { usePeer } from "@/peer";
import { MediaConnection } from "peerjs";
import { orUndefined } from "@litespace/sol/utils";
import { QueryKey } from "@/constants";
import zod from "zod";
import { ServerEvent, ServerEventsMap } from "@litespace/types/dist/esm/wss";

declare module "peerjs" {
export interface CallOption {
Expand Down Expand Up @@ -364,9 +362,7 @@ export function useSession({
// call.on("stream", setMateMediaStream);
call.on("stream", (stream: MediaStream) => addStream(call, stream));
call.on("close", () => onMediaConnectionClose(call));
}, 3000);
},
[
}, 3000); }, [
onMediaConnectionClose,
addMediaConnection,
addStream,
Expand Down Expand Up @@ -680,62 +676,6 @@ export function useCallEvents(
};
}

// todo: move to peer.ts
export function useFindPeerId(payload?: IPeer.FindPeerIdApiQuery) {
const atlas = useAtlas();

const findPeerId = useCallback(async () => {
if (!payload) return null;
const result = await atlas.peer.findPeerId(payload);
if (result.peer === null) throw new Error("Peer not found");
return result.peer;
}, [atlas.peer, payload]);

return useQuery({
queryFn: findPeerId,
queryKey: [QueryKey.FindPeerId, payload],
retryDelay: (attempt) => {
if (attempt <= 10) return 2_000;
return Math.min(1_000 * 2 ** attempt, 30_000);
},
enabled: !!payload,
});
}

export function usePeerIds({
isGhost,
sessionId,
role,
mateUserId,
disableGhost = false,
}: {
isGhost: boolean;
sessionId: string | null;
role: IUser.Role | null;
mateUserId: number | null;
disableGhost?: boolean;
}) {
const findGhostPeerIdQuery = useMemo(():
| IPeer.FindPeerIdApiQuery
| undefined => {
if (isGhost || !sessionId || disableGhost) return;
return { type: IPeer.PeerType.Ghost, session: sessionId };
}, [sessionId, disableGhost, isGhost]);

const findTutorPeerIdQuery = useMemo(():
| IPeer.FindPeerIdApiQuery
| undefined => {
const allowed =
role === IUser.Role.Student || role === IUser.Role.TutorManager;
if (isGhost || !mateUserId || !allowed) return;
return { type: IPeer.PeerType.Tutor, tutor: mateUserId };
}, [isGhost, mateUserId, role]);

const ghostPeerId = useFindPeerId(findGhostPeerIdQuery);
const tutorPeerId = useFindPeerId(findTutorPeerIdQuery);
return { ghost: ghostPeerId, tutor: tutorPeerId };
}

const ghostCallMetadata = zod.object({
userId: zod.number().positive().int(),
screen: zod.boolean(),
Expand Down Expand Up @@ -1007,88 +947,3 @@ export function useFullScreen<T extends Element>() {
exit,
};
}

/*
* Send an HTTP request to retrieve the current members of a call
*/
export function useFindSessionMembers(
sessionId: string | null
): UseQueryResult<ISession.FindSessionMembersApiResponse | null, Error> {
const atlas = useAtlas();

const findCallMembers = useCallback(async () => {
if (!sessionId) return null;
return await atlas.session.findSessionMembers(sessionId);
}, [atlas.session, sessionId]);

return useQuery({
queryFn: findCallMembers,
queryKey: [QueryKey.FindCallMembers],
});
}

// a shorthand for some ServerEvent Callback type
type CallEventsCallback = ServerEventsMap[ServerEvent.MemberJoinedSession];

/*
* Real-time call members state.
*/
export function useSessionMembers(
sessionId: ISession.Id | null
): number[] {
const [members, setMembers] = useState<number[]>([]);

const socket = useSocket();

// get the initial list by an http request
const query = useFindSessionMembers(sessionId);

useEffect(() => {
if (query.data && query.isFetching && query.isError) setMembers(query.data);
}, [query.data, query.isError, query.isFetching]);

// define WSS events callbacks
const onMemberJoinWssCallback: CallEventsCallback = useCallback((payload) => {
setMembers((prev) => [...prev, payload.userId]);
}, []);

const onMemberLeaveWssCallback: CallEventsCallback = useCallback(
(payload) => {
setMembers((prev) => prev.filter((id) => id !== payload.userId));
},
[]
);

// define react callbacks
const joinSession = useCallback(() => {
if (!socket || !sessionId) return;
socket.emit(Wss.ClientEvent.JoinSession, { sessionId });
}, [socket, sessionId]);

const leaveSession = useCallback(() => {
if (!socket || !sessionId) return;
socket.emit(Wss.ClientEvent.LeaveSession, { sessionId });
}, [socket, sessionId]);

useEffect(() => {
// listen to wss events to modify members list accordingly
if (!socket) return;
socket.on(Wss.ServerEvent.MemberJoinedSession, onMemberJoinWssCallback);
socket.on(Wss.ServerEvent.MemberLeftSession, onMemberLeaveWssCallback);
joinSession();
return () => {
socket.off(Wss.ServerEvent.MemberJoinedSession);
socket.off(Wss.ServerEvent.MemberLeftSession);
leaveSession();
};
}, [
sessionId,
socket,
joinSession,
leaveSession,
onMemberLeaveWssCallback,
onMemberJoinWssCallback,
]);

return members;
}
Loading

0 comments on commit 689498c

Please sign in to comment.