cleanup to make it more dynamic and use the lifecycle of react components instead of messy cache and listener management that would be hard to manage with manual code

This commit is contained in:
talksik
2022-06-19 08:22:31 -05:00
parent 33c2872d81
commit 7fd251e37e
@@ -322,40 +322,73 @@ export function ConversationProvider({ children }: { children: React.ReactNode }
[selectConversation], [selectConversation],
); );
const masterConversations = useMemo(() => { const userTunedInConversations = useMemo(() => {
const priorityConversations: MasterConversation[] = []; const tunedConversations: MasterConversation[] = [];
const inboxConversations: MasterConversation[] = [];
Object.values(conversationMap).forEach((currentMasterConversation) => { Object.values(conversationMap).forEach((currentMasterConversation) => {
const personalConvoMember = currentMasterConversation.members.find( if (currentMasterConversation.tunedInUsers.includes(user._id.toString())) {
(mem) => mem._id.toString() === user._id.toString(), tunedConversations.push(currentMasterConversation);
);
if (personalConvoMember && personalConvoMember.memberState === 'priority') {
priorityConversations.push(currentMasterConversation);
return;
} }
inboxConversations.push(currentMasterConversation);
}); });
return [priorityConversations, inboxConversations]; return tunedConversations;
}, [conversationMap, user]); }, [conversationMap, user]);
if (fetchState.error) {
return (
<Typography variant={'h6'} color={'danger'}>
Something went terribly wrong
</Typography>
);
}
return (
<ConversationContext.Provider
value={{
conversationMap,
handleStartConversation,
selectedConversation,
selectConversation,
}}
>
{children}
{/* with id specified, shouldn't unmount and mount unexpectedly */}
{userTunedInConversations.map((tunedConversation) => (
<Room
key={`streamRoom-${tunedConversation._id.toString()}`}
conversation={tunedConversation}
/>
))}
</ConversationContext.Provider>
);
}
export default function useConversations() {
return React.useContext(ConversationContext);
}
// renderless component to manage a room and send data upward for other components
// - decoupled from sockets and other data, render if you want to join x room
// - unmount if you want to leave room
// - have event listeners specific to this room
// - take in new users that come into the room
function Room({ conversation }: { conversation: MasterConversation }) {
// have internal state to manage details of this "room"
// ============== STREAMING =============== // ============== STREAMING ===============
// handle incoming calls and accept calls and create objects for them // handle incoming calls and accept calls and create objects for them
useEffect(() => { useEffect(() => {
// //
}, []); }, []);
const handleJoinRoom = useCallback( const handleJoinRoom = useCallback((roomId: string) => {
(roomId: string) => { // call up folks and then have listeners for the peer connection objects that we created
// call up folks and then have listeners for the peer connection objects that we created // such as when peer connection closes, we want to remove from our list of peers for such conversation
// such as when peer connection closes, we want to remove from our list of peers for such conversation // error handler as well for the peer connection
// error handler as well for the peer connection // rest endpoint to get all of the folks in a certain room
// rest endpoint to get all of the folks in a certain room }, []);
},
[setConversationMap, $ws],
);
const handleLeaveRoom = useCallback(() => { const handleLeaveRoom = useCallback(() => {
// untune from line // untune from line
@@ -383,28 +416,15 @@ export function ConversationProvider({ children }: { children: React.ReactNode }
// ============= END OF STREAMING MODULE ======== // ============= END OF STREAMING MODULE ========
if (fetchState.error) { useEffect(() => {
return ( // call all of the tuned in folks that are already here
<Typography variant={'h6'} color={'danger'}> console.log(conversation.tunedInUsers);
Something went terribly wrong
</Typography>
);
}
return ( return () => {
<ConversationContext.Provider // close peer connections here
value={{ // update the conversation map as well with proper states and data
conversationMap, };
handleStartConversation, }, []);
selectedConversation,
selectConversation,
}}
>
{children}
</ConversationContext.Provider>
);
}
export default function useConversations() { return null;
return React.useContext(ConversationContext);
} }