From 708ea9555c535dcd206c2e22a245a58e9697d110 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Tue, 30 Aug 2022 15:01:03 -0700 Subject: [PATCH] Order comments in Firestore instead of on client --- web/lib/firebase/comments.ts | 30 +++++++++++-------------- web/pages/[username]/[contractSlug].tsx | 3 --- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/web/lib/firebase/comments.ts b/web/lib/firebase/comments.ts index 70785858..aab4de85 100644 --- a/web/lib/firebase/comments.ts +++ b/web/lib/firebase/comments.ts @@ -92,17 +92,15 @@ function getCommentsOnGroupCollection(groupId: string) { } export async function listAllComments(contractId: string) { - const comments = await getValues(getCommentsCollection(contractId)) - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - return comments + return await getValues( + query(getCommentsCollection(contractId), orderBy('createdTime', 'desc')) + ) } export async function listAllCommentsOnGroup(groupId: string) { - const comments = await getValues( - getCommentsOnGroupCollection(groupId) + return await getValues( + query(getCommentsOnGroupCollection(groupId), orderBy('createdTime', 'desc')) ) - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - return comments } export function listenForCommentsOnContract( @@ -110,23 +108,21 @@ export function listenForCommentsOnContract( setComments: (comments: ContractComment[]) => void ) { return listenForValues( - getCommentsCollection(contractId), - (comments) => { - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - setComments(comments) - } + query(getCommentsCollection(contractId), orderBy('createdTime', 'desc')), + setComments ) } + export function listenForCommentsOnGroup( groupId: string, setComments: (comments: GroupComment[]) => void ) { return listenForValues( - getCommentsOnGroupCollection(groupId), - (comments) => { - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - setComments(comments) - } + query( + getCommentsOnGroupCollection(groupId), + orderBy('createdTime', 'desc') + ), + setComments ) } diff --git a/web/pages/[username]/[contractSlug].tsx b/web/pages/[username]/[contractSlug].tsx index f7a5c5c5..f3c48a68 100644 --- a/web/pages/[username]/[contractSlug].tsx +++ b/web/pages/[username]/[contractSlug].tsx @@ -168,9 +168,6 @@ export function ContractPageContent( [bets] ) - // Sort for now to see if bug is fixed. - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - const tips = useTipTxns({ contractId: contract.id }) const [showConfetti, setShowConfetti] = useState(false)