From 7dddff52b86f7c3fc7bddb554ea93d45b44c0829 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Tue, 30 Aug 2022 20:28:30 -0700 Subject: [PATCH] Tidying some feed code up (#818) * Clean up some markup & dead code * Order comments in Firestore instead of on client * Order bets in Firestore instead of on client * Make indexes file up to date with production --- firestore.indexes.json | 150 ++++++++++++++++++ web/components/contract/contract-overview.tsx | 2 +- web/components/feed/feed-liquidity.tsx | 41 +++-- web/components/user-link.tsx | 4 +- web/lib/firebase/bets.ts | 30 ++-- web/lib/firebase/comments.ts | 30 ++-- web/pages/[username]/[contractSlug].tsx | 3 - web/pages/embed/[username]/[contractSlug].tsx | 2 - 8 files changed, 198 insertions(+), 64 deletions(-) diff --git a/firestore.indexes.json b/firestore.indexes.json index 80b08996..bcee41d5 100644 --- a/firestore.indexes.json +++ b/firestore.indexes.json @@ -26,9 +26,55 @@ "collectionGroup": "bets", "queryScope": "COLLECTION_GROUP", "fields": [ + { + "fieldPath": "isFilled", + "order": "ASCENDING" + }, { "fieldPath": "userId", "order": "ASCENDING" + } + ] + }, + { + "collectionGroup": "bets", + "queryScope": "COLLECTION_GROUP", + "fields": [ + { + "fieldPath": "userId", + "order": "ASCENDING" + }, + { + "fieldPath": "createdTime", + "order": "DESCENDING" + } + ] + }, + { + "collectionGroup": "bets", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "isCancelled", + "order": "ASCENDING" + }, + { + "fieldPath": "isFilled", + "order": "ASCENDING" + }, + { + "fieldPath": "createdTime", + "order": "DESCENDING" + } + ] + }, + { + "collectionGroup": "challenges", + "queryScope": "COLLECTION_GROUP", + "fields": [ + { + "fieldPath": "creatorId", + "order": "ASCENDING" }, { "fieldPath": "createdTime", @@ -54,6 +100,34 @@ } ] }, + { + "collectionGroup": "comments", + "queryScope": "COLLECTION_GROUP", + "fields": [ + { + "fieldPath": "userId", + "order": "ASCENDING" + }, + { + "fieldPath": "createdTime", + "order": "ASCENDING" + } + ] + }, + { + "collectionGroup": "comments", + "queryScope": "COLLECTION_GROUP", + "fields": [ + { + "fieldPath": "userId", + "order": "ASCENDING" + }, + { + "fieldPath": "createdTime", + "order": "DESCENDING" + } + ] + }, { "collectionGroup": "contracts", "queryScope": "COLLECTION", @@ -82,6 +156,42 @@ } ] }, + { + "collectionGroup": "contracts", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "creatorId", + "order": "ASCENDING" + }, + { + "fieldPath": "isResolved", + "order": "ASCENDING" + }, + { + "fieldPath": "popularityScore", + "order": "DESCENDING" + } + ] + }, + { + "collectionGroup": "contracts", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "groupSlugs", + "arrayConfig": "CONTAINS" + }, + { + "fieldPath": "isResolved", + "order": "ASCENDING" + }, + { + "fieldPath": "popularityScore", + "order": "DESCENDING" + } + ] + }, { "collectionGroup": "contracts", "queryScope": "COLLECTION", @@ -128,6 +238,46 @@ } ] }, + { + "collectionGroup": "contracts", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "isResolved", + "order": "ASCENDING" + }, + { + "fieldPath": "visibility", + "order": "ASCENDING" + }, + { + "fieldPath": "closeTime", + "order": "ASCENDING" + }, + { + "fieldPath": "popularityScore", + "order": "DESCENDING" + } + ] + }, + { + "collectionGroup": "contracts", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "isResolved", + "order": "ASCENDING" + }, + { + "fieldPath": "visibility", + "order": "ASCENDING" + }, + { + "fieldPath": "popularityScore", + "order": "DESCENDING" + } + ] + }, { "collectionGroup": "contracts", "queryScope": "COLLECTION", diff --git a/web/components/contract/contract-overview.tsx b/web/components/contract/contract-overview.tsx index bf62f77e..a7d3102f 100644 --- a/web/components/contract/contract-overview.tsx +++ b/web/components/contract/contract-overview.tsx @@ -126,7 +126,7 @@ export const ContractOverview = (props: {
{(isBinary || isPseudoNumeric) && ( - + )}{' '} {(outcomeType === 'FREE_RESPONSE' || outcomeType === 'MULTIPLE_CHOICE') && ( diff --git a/web/components/feed/feed-liquidity.tsx b/web/components/feed/feed-liquidity.tsx index ee2e34e5..e2a80624 100644 --- a/web/components/feed/feed-liquidity.tsx +++ b/web/components/feed/feed-liquidity.tsx @@ -1,3 +1,4 @@ +import clsx from 'clsx' import dayjs from 'dayjs' import { User } from 'common/user' import { useUser, useUserById } from 'web/hooks/use-user' @@ -24,26 +25,23 @@ export function FeedLiquidity(props: { const isSelf = user?.id === userId return ( - <> - - {isSelf ? ( - - ) : bettor ? ( - - ) : ( -
- -
- )} -
- + + {isSelf ? ( + + ) : bettor ? ( + + ) : ( +
+
-
- + )} + + ) } @@ -51,8 +49,9 @@ export function LiquidityStatusText(props: { liquidity: LiquidityProvision isSelf: boolean bettor?: User + className?: string }) { - const { liquidity, bettor, isSelf } = props + const { liquidity, bettor, isSelf, className } = props const { amount, createdTime } = liquidity // TODO: Withdrawn liquidity will never be shown, since liquidity amounts currently are zeroed out upon withdrawal. @@ -60,7 +59,7 @@ export function LiquidityStatusText(props: { const money = formatMoney(Math.abs(amount)) return ( -
+
{bettor ? ( ) : ( diff --git a/web/components/user-link.tsx b/web/components/user-link.tsx index 796bb367..a3bd9e9f 100644 --- a/web/components/user-link.tsx +++ b/web/components/user-link.tsx @@ -24,11 +24,10 @@ function shortenName(name: string) { export function UserLink(props: { name: string username: string - showUsername?: boolean className?: string short?: boolean }) { - const { name, username, showUsername, className, short } = props + const { name, username, className, short } = props const shortName = short ? shortenName(name) : name return ( {shortName} - {showUsername && ` (@${username})`} ) } diff --git a/web/lib/firebase/bets.ts b/web/lib/firebase/bets.ts index 2a095d32..7f44786a 100644 --- a/web/lib/firebase/bets.ts +++ b/web/lib/firebase/bets.ts @@ -28,9 +28,9 @@ function getBetsCollection(contractId: string) { } export async function listAllBets(contractId: string) { - const bets = await getValues(getBetsCollection(contractId)) - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - return bets + return await getValues( + query(getBetsCollection(contractId), orderBy('createdTime', 'desc')) + ) } const DAY_IN_MS = 24 * 60 * 60 * 1000 @@ -64,10 +64,10 @@ export function listenForBets( contractId: string, setBets: (bets: Bet[]) => void ) { - return listenForValues(getBetsCollection(contractId), (bets) => { - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - setBets(bets) - }) + return listenForValues( + query(getBetsCollection(contractId), orderBy('createdTime', 'desc')), + setBets + ) } export async function getUserBets( @@ -147,12 +147,10 @@ export function listenForUserContractBets( ) { const betsQuery = query( collection(db, 'contracts', contractId, 'bets'), - where('userId', '==', userId) + where('userId', '==', userId), + orderBy('createdTime', 'desc') ) - return listenForValues(betsQuery, (bets) => { - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - setBets(bets) - }) + return listenForValues(betsQuery, setBets) } export function listenForUnfilledBets( @@ -162,12 +160,10 @@ export function listenForUnfilledBets( const betsQuery = query( collection(db, 'contracts', contractId, 'bets'), where('isFilled', '==', false), - where('isCancelled', '==', false) + where('isCancelled', '==', false), + orderBy('createdTime', 'desc') ) - return listenForValues(betsQuery, (bets) => { - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - setBets(bets) - }) + return listenForValues(betsQuery, setBets) } export function withoutAnteBets(contract: Contract, bets?: Bet[]) { 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) diff --git a/web/pages/embed/[username]/[contractSlug].tsx b/web/pages/embed/[username]/[contractSlug].tsx index 8044ec6e..3f91baf7 100644 --- a/web/pages/embed/[username]/[contractSlug].tsx +++ b/web/pages/embed/[username]/[contractSlug].tsx @@ -71,8 +71,6 @@ export default function ContractEmbedPage(props: { const contract = useContractWithPreload(props.contract) const { bets } = props - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - if (!contract) { return }