From f8b8f1386256af554a99cf7bbc87596ca3e79279 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 28 May 2022 16:10:37 -0500 Subject: [PATCH] adding more logic client side for selection and creating and such --- packages/desktop/src/components/Terminal.tsx | 34 ++++++++++++++++---- packages/desktop/src/firebase/firestore.ts | 19 ++++++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/packages/desktop/src/components/Terminal.tsx b/packages/desktop/src/components/Terminal.tsx index 4e5a165..206d18f 100644 --- a/packages/desktop/src/components/Terminal.tsx +++ b/packages/desktop/src/components/Terminal.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useState, useCallback, useMemo } from 'react'; +import React, { useContext, useState, useCallback, useMemo, useEffect } from 'react'; import { Container } from '@mui/system'; import { Avatar, Box, Fab, Grid, IconButton, Paper, Stack, Typography } from '@mui/material'; @@ -10,10 +10,15 @@ import Conversations from './Conversations'; import Navbar from './Navbar'; import Conversation from '@nirvana/core/src/models/conversation.model'; import useAuth from '../providers/AuthProvider'; -import { createOneOnOneConversation } from '../firebase/firestore'; +import { + createOneOnOneConversation, + getConversationsQueryLIVE, + useGetConversationsQueryLIVE, +} from '../firebase/firestore'; import { Link, AudioClip, Image } from '@nirvana/core/src/models/content.model'; import { useImmer } from 'use-immer'; import { User } from '@nirvana/core/src/models/user.model'; +import { onSnapshot } from 'firebase/firestore'; type ConversationMap = { [conversationId: string]: Conversation; @@ -71,6 +76,19 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) { const [userMap, updateUserMap] = useImmer({}); const [contentMap, updatecontentMap] = useImmer({}); + const queryLiveConversations = useGetConversationsQueryLIVE(); + + useEffect(() => { + const unsub = onSnapshot(queryLiveConversations, (querySnapshot) => { + const convos = querySnapshot.docs.map((doc) => doc.data()); + + enqueueSnackbar('got conversations...logging', { variant: 'success' }); + console.log(convos); + }); + + return () => unsub(); + }, [queryLiveConversations]); + // cache of selected conversation const selectedConversation: Conversation | undefined = useMemo(() => { // select if we do have it @@ -100,9 +118,9 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) { // if there is already a convo with exactly me and him, then select it const findExistingConversation = Object.values(conversationMap).find((convo) => { if ( - convo.membersInRoom?.length === 2 && - convo.membersInRoom.includes(user.uid) && - convo.membersInRoom.includes(otherUserId) + convo.membersList?.length === 2 && + convo.membersList.includes(user.uid) && + convo.membersList.includes(otherUserId) ) { return true; } @@ -115,7 +133,11 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) { } // create conversation in this case - await createOneOnOneConversation(otherUserId, user.uid); + const newConversationId = await createOneOnOneConversation(otherUserId, user.uid); + + enqueueSnackbar('started conversation!', { variant: 'success' }); + + setSelectedConversationId(newConversationId); } catch (error) { enqueueSnackbar('Something went wrong, please try again', { variant: 'error' }); } diff --git a/packages/desktop/src/firebase/firestore.ts b/packages/desktop/src/firebase/firestore.ts index ecf1321..9965b03 100644 --- a/packages/desktop/src/firebase/firestore.ts +++ b/packages/desktop/src/firebase/firestore.ts @@ -23,6 +23,7 @@ import { User as FirebaseUser } from 'firebase/auth'; import { firestoreDb } from './connect'; import { User } from '@nirvana/core/src/models/user.model'; import Conversation from '@nirvana/core/src/models/conversation.model'; +import useAuth from '../providers/AuthProvider'; interface Document { id: string; @@ -127,17 +128,27 @@ export const getUserById = async (userId: string): Promise => }; // get the conversations for particular user -export const getConversationsQueryLIVE = async (userId: string) => - query(db.conversations, where('membersList', 'array-contains', userId)); +export const useGetConversationsQueryLIVE = () => { + const { user } = useAuth(); + return query(db.conversations, where('membersList', 'array-contains', user.uid)); +}; + +/** + * + * @param otherUserId + * @param myUserId + * @returns id of new conversation + */ export const createOneOnOneConversation = async ( otherUserId: string, myUserId: string, -): Promise => { +): Promise => { const newConversation = new Conversation(myUserId, [myUserId, otherUserId]); try { - await addDoc(db.conversations, newConversation); + const newDoc = await addDoc(db.conversations, newConversation); + return newDoc.id; } catch (e) { console.error('Error creating user: ', e);