get chats count

This commit is contained in:
Pritimay Sarkar
2024-02-11 14:14:26 +05:30
parent ff8c62ef46
commit 47579b0796

View File

@@ -1,5 +1,5 @@
import React, { useState, useEffect, useRef } from 'react';
import { query, collection, onSnapshot, addDoc, where, getDocs, orderBy } from 'firebase/firestore';
import { query, collection, onSnapshot, addDoc, where, getDocs, orderBy, getCountFromServer } from 'firebase/firestore';
import Markdown from 'react-markdown';
import MathJax from 'react-mathjax';
import RemarkMathPlugin from 'remark-math';
@@ -20,6 +20,7 @@ function Gemini(props) {
const [isTyping, setIsTyping] = useState(false);
const [discussionId, setDiscussionId] = useState(uuidv4());
const [isLoading, setIsLoading] = useState(false);
const [chatCount, setChatCount] = useState(0);
const scrollRef = useRef(null);
@@ -53,6 +54,17 @@ function Gemini(props) {
)
};
const getDocumentCount = async (uid) => {
const collectionPath = `users/uid/discussions`;
try {
const querySnapshot = await getCountFromServer(collection(db, collectionPath));
// const documentCount = querySnapshot.size;
// setChatCount(querySnapshot);
console.log(`Document count in the collection "${collectionPath}": ${JSON.stringify(querySnapshot._data.count.integerValue)}`);
} catch (error) {
console.error("Error getting document count:", error);
}
};
useEffect(() => {
const loadChatHistory = async () => {
@@ -60,6 +72,8 @@ function Gemini(props) {
// const discussionId = uuidv4();
console.log(discussionId)
getDocumentCount(uid);
const collectionPath = `users/${uid}/discussions/${discussionId}/messages`;
const q = query(collection(db, collectionPath), orderBy('createTime'));
@@ -171,6 +185,7 @@ function Gemini(props) {
return (
<div>
{/* <div>Chats: {chatCount}</div> */}
<div style={{ height: '500px', overflowY: 'scroll', border: '1px solid #ccc' }} ref={scrollRef}>
{messages.map((message, index) => (
<div key={index} className={message.sender === 'user' ? 'user-message' : 'other-message'}>