-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from m1her/backup
kinda finished
- Loading branch information
Showing
2 changed files
with
75 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { messagesType } from "@/components/ChatBox"; | ||
import { DocumentData, Query, getDocs } from "firebase/firestore"; | ||
|
||
type setMessagesFunctionProps = { | ||
messagesQuery: Query<DocumentData, DocumentData>; | ||
setLastStanpshot: React.Dispatch<any>; | ||
setHasMore: React.Dispatch<React.SetStateAction<boolean>>; | ||
setMessages: React.Dispatch<React.SetStateAction<messagesType>>; | ||
next?: boolean; | ||
}; | ||
|
||
export const setMessagesFunction = ({ | ||
messagesQuery, | ||
setHasMore, | ||
setLastStanpshot, | ||
setMessages, | ||
next, | ||
}: setMessagesFunctionProps) => { | ||
getDocs(messagesQuery) | ||
.then((querySnapshot) => { | ||
const latestMessages: React.SetStateAction<messagesType> = []; | ||
const first = querySnapshot; | ||
querySnapshot.forEach((doc) => { | ||
latestMessages.push({ | ||
id: doc.id, | ||
message: doc.data().message, | ||
sender: doc.data().sender, | ||
time: doc.data().time, | ||
}); | ||
}); | ||
if (first.empty) { | ||
setHasMore(false); | ||
} else { | ||
setHasMore(true); | ||
if (next) { | ||
setMessages((prev) => [...prev, ...latestMessages]); | ||
} else { | ||
setMessages(latestMessages); | ||
} | ||
const lastVisible = first.docs[first.docs.length - 1]; | ||
setLastStanpshot(lastVisible); | ||
} | ||
}) | ||
.catch((error) => { | ||
setHasMore(false); | ||
console.error("Error getting documents:", error); | ||
}); | ||
}; |