adding in content listeners
This commit is contained in:
@@ -53,6 +53,7 @@ import Conversation, { ConversationMember } from '@nirvana/core/src/models/conve
|
|||||||
import useAuth from '../providers/AuthProvider';
|
import useAuth from '../providers/AuthProvider';
|
||||||
import {
|
import {
|
||||||
createOneOnOneConversation,
|
createOneOnOneConversation,
|
||||||
|
getConversationContentQueryLIVE,
|
||||||
getConversationsQueryLIVE,
|
getConversationsQueryLIVE,
|
||||||
getUserById,
|
getUserById,
|
||||||
joinConversation,
|
joinConversation,
|
||||||
@@ -62,7 +63,7 @@ import {
|
|||||||
} from '../firebase/firestore';
|
} from '../firebase/firestore';
|
||||||
import { useImmer } from 'use-immer';
|
import { useImmer } from 'use-immer';
|
||||||
import { User } from '@nirvana/core/src/models/user.model';
|
import { User } from '@nirvana/core/src/models/user.model';
|
||||||
import { onSnapshot } from 'firebase/firestore';
|
import { onSnapshot, Unsubscribe } from 'firebase/firestore';
|
||||||
|
|
||||||
import NirvanaAvatar from './NirvanaAvatar';
|
import NirvanaAvatar from './NirvanaAvatar';
|
||||||
import { useDebounce, useEffectOnce, useKeyPressEvent } from 'react-use';
|
import { useDebounce, useEffectOnce, useKeyPressEvent } from 'react-use';
|
||||||
@@ -175,10 +176,58 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
|
|||||||
return () => unsub();
|
return () => unsub();
|
||||||
}, [user, updateConversationMap, enqueueSnackbar]);
|
}, [user, updateConversationMap, enqueueSnackbar]);
|
||||||
|
|
||||||
// cached listeners for a
|
const [contentListeners, setContentListeners] = useImmer<{
|
||||||
// useEffect(() => {
|
[conversationId: string]: Unsubscribe;
|
||||||
|
}>({});
|
||||||
|
// cached listeners for all content blocks of specific conversations?
|
||||||
|
useEffect(() => {
|
||||||
|
// go through all conversations
|
||||||
|
Object.keys(conversationMap).forEach((conversationId) => {
|
||||||
|
setContentListeners((draftListeners) => {
|
||||||
|
// if we have a listener for conversation already, then just move on
|
||||||
|
if (draftListeners[conversationId]) return;
|
||||||
|
|
||||||
// }, [])
|
// if we don't, then create one for this conversation
|
||||||
|
const contentListener = onSnapshot(
|
||||||
|
getConversationContentQueryLIVE(conversationId),
|
||||||
|
(querySnapshot) => {
|
||||||
|
querySnapshot.docChanges().forEach((docChange) => {
|
||||||
|
const currentContentBlock = docChange.doc.data();
|
||||||
|
|
||||||
|
if (docChange.type === 'added') {
|
||||||
|
// TODO: add to audio queue from here if there was an addition?
|
||||||
|
enqueueSnackbar('new content received!', { variant: 'default' });
|
||||||
|
updatecontentMap((draftContent) => {
|
||||||
|
if (draftContent[conversationId]) {
|
||||||
|
draftContent[conversationId].push(currentContentBlock);
|
||||||
|
} else {
|
||||||
|
draftContent[conversationId] = [currentContentBlock];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (docChange.type === 'modified') {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
if (docChange.type === 'removed') {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
//add to map of listeners
|
||||||
|
|
||||||
|
draftListeners[conversationId] = contentListener;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ?optimization
|
||||||
|
// ? only start listening to conversation content where it is in my priority box?
|
||||||
|
// ? all other conversations, just fetch periodically?
|
||||||
|
|
||||||
|
// TODO: future work/optimization
|
||||||
|
// if we removed a conversation, unsubscribe from content listener as well
|
||||||
|
}, [conversationMap, updatecontentMap, enqueueSnackbar, setContentListeners]);
|
||||||
|
|
||||||
// cache of selected conversation
|
// cache of selected conversation
|
||||||
const selectedConversation: Conversation | undefined = useMemo(() => {
|
const selectedConversation: Conversation | undefined = useMemo(() => {
|
||||||
|
|||||||
@@ -144,6 +144,10 @@ export const getUserById = async (userId: string): Promise<User | undefined> =>
|
|||||||
export const getConversationsQueryLIVE = (userId: string) =>
|
export const getConversationsQueryLIVE = (userId: string) =>
|
||||||
query(db.conversations, where(`members.${userId}.isActive`, '==', true));
|
query(db.conversations, where(`members.${userId}.isActive`, '==', true));
|
||||||
|
|
||||||
|
// get content blocks for particular conversation
|
||||||
|
export const getConversationContentQueryLIVE = (conversationId: string) =>
|
||||||
|
query(db.conversationContent(conversationId));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param otherUserId
|
* @param otherUserId
|
||||||
|
|||||||
Reference in New Issue
Block a user