Load all fold comments server-side to serve better feed
This commit is contained in:
parent
3686697237
commit
f952ea109a
|
@ -5,7 +5,6 @@ import {
|
||||||
setDoc,
|
setDoc,
|
||||||
query,
|
query,
|
||||||
collectionGroup,
|
collectionGroup,
|
||||||
getDocs,
|
|
||||||
where,
|
where,
|
||||||
orderBy,
|
orderBy,
|
||||||
} from 'firebase/firestore'
|
} from 'firebase/firestore'
|
||||||
|
|
|
@ -2,8 +2,6 @@ import _ from 'lodash'
|
||||||
import { ContractFeed } from '../components/contract-feed'
|
import { ContractFeed } from '../components/contract-feed'
|
||||||
import { Page } from '../components/page'
|
import { Page } from '../components/page'
|
||||||
import { Title } from '../components/title'
|
import { Title } from '../components/title'
|
||||||
import { useRecentComments } from '../hooks/use-comments'
|
|
||||||
import { useContracts } from '../hooks/use-contracts'
|
|
||||||
import { Contract } from '../lib/firebase/contracts'
|
import { Contract } from '../lib/firebase/contracts'
|
||||||
import { Comment } from '../lib/firebase/comments'
|
import { Comment } from '../lib/firebase/comments'
|
||||||
import { Col } from '../components/layout/col'
|
import { Col } from '../components/layout/col'
|
||||||
|
@ -68,22 +66,15 @@ export function ActivityFeed(props: {
|
||||||
contracts: Contract[]
|
contracts: Contract[]
|
||||||
contractBets: Bet[][]
|
contractBets: Bet[][]
|
||||||
contractComments: Comment[][]
|
contractComments: Comment[][]
|
||||||
listenForChanges?: boolean
|
|
||||||
}) {
|
}) {
|
||||||
const { contractBets, contractComments, listenForChanges } = props
|
const { contracts, contractBets, contractComments } = props
|
||||||
const contracts = useContracts() ?? props.contracts
|
|
||||||
const recentComments = useRecentComments()
|
|
||||||
const activeContracts =
|
|
||||||
listenForChanges && recentComments
|
|
||||||
? findActiveContracts(contracts, recentComments)
|
|
||||||
: props.contracts
|
|
||||||
|
|
||||||
return activeContracts.length > 0 ? (
|
return contracts.length > 0 ? (
|
||||||
<Col className="items-center">
|
<Col className="items-center">
|
||||||
<Col className="w-full max-w-3xl">
|
<Col className="w-full max-w-3xl">
|
||||||
<Title text="Recent Activity" />
|
<Title text="Recent Activity" />
|
||||||
<Col className="w-full bg-white self-center divide-gray-300 divide-y">
|
<Col className="w-full bg-white self-center divide-gray-300 divide-y">
|
||||||
{activeContracts.map((contract, i) => (
|
{contracts.map((contract, i) => (
|
||||||
<div key={contract.id} className="py-6 px-2 sm:px-4">
|
<div key={contract.id} className="py-6 px-2 sm:px-4">
|
||||||
<ContractFeed
|
<ContractFeed
|
||||||
contract={contract}
|
contract={contract}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
|
import _ from 'lodash'
|
||||||
import { Fold } from '../../../common/fold'
|
import { Fold } from '../../../common/fold'
|
||||||
import { Comment } from '../../../common/comment'
|
import { Comment } from '../../../common/comment'
|
||||||
import { Page } from '../../components/page'
|
import { Page } from '../../components/page'
|
||||||
import { Title } from '../../components/title'
|
import { Title } from '../../components/title'
|
||||||
import { Bet, listAllBets } from '../../lib/firebase/bets'
|
import { Bet, listAllBets } from '../../lib/firebase/bets'
|
||||||
import { getRecentComments, listAllComments } from '../../lib/firebase/comments'
|
import { listAllComments } from '../../lib/firebase/comments'
|
||||||
import { Contract } from '../../lib/firebase/contracts'
|
import { Contract } from '../../lib/firebase/contracts'
|
||||||
import { getFoldBySlug, getFoldContracts } from '../../lib/firebase/folds'
|
import { getFoldBySlug, getFoldContracts } from '../../lib/firebase/folds'
|
||||||
import { ActivityFeed, findActiveContracts } from '../activity'
|
import { ActivityFeed, findActiveContracts } from '../activity'
|
||||||
|
@ -12,18 +13,22 @@ import { TagsList } from '../../components/tags-list'
|
||||||
export async function getStaticProps(props: { params: { foldSlug: string } }) {
|
export async function getStaticProps(props: { params: { foldSlug: string } }) {
|
||||||
const { foldSlug } = props.params
|
const { foldSlug } = props.params
|
||||||
|
|
||||||
const recentCommentsPromise = getRecentComments().catch(() => [])
|
|
||||||
|
|
||||||
const fold = await getFoldBySlug(foldSlug)
|
const fold = await getFoldBySlug(foldSlug)
|
||||||
const contracts = fold ? await getFoldContracts(fold) : []
|
const contracts = fold ? await getFoldContracts(fold) : []
|
||||||
|
const contractComments = await Promise.all(
|
||||||
|
contracts.map((contract) => listAllComments(contract.id))
|
||||||
|
)
|
||||||
|
|
||||||
const recentComments = await recentCommentsPromise
|
const activeContracts = findActiveContracts(
|
||||||
const activeContracts = findActiveContracts(contracts, recentComments)
|
contracts,
|
||||||
|
_.flatten(contractComments)
|
||||||
|
)
|
||||||
const activeContractBets = await Promise.all(
|
const activeContractBets = await Promise.all(
|
||||||
activeContracts.map((contract) => listAllBets(contract.id))
|
activeContracts.map((contract) => listAllBets(contract.id))
|
||||||
)
|
)
|
||||||
const activeContractComments = await Promise.all(
|
const activeContractComments = activeContracts.map(
|
||||||
activeContracts.map((contract) => listAllComments(contract.id))
|
(contract) =>
|
||||||
|
contractComments[contracts.findIndex((c) => c.id === contract.id)]
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -17,6 +17,8 @@ import {
|
||||||
} from '../lib/firebase/comments'
|
} from '../lib/firebase/comments'
|
||||||
import { Bet, listAllBets } from '../lib/firebase/bets'
|
import { Bet, listAllBets } from '../lib/firebase/bets'
|
||||||
import { ContractsGrid } from '../components/contracts-list'
|
import { ContractsGrid } from '../components/contracts-list'
|
||||||
|
import { useContracts } from '../hooks/use-contracts'
|
||||||
|
import { useRecentComments } from '../hooks/use-comments'
|
||||||
|
|
||||||
export async function getStaticProps() {
|
export async function getStaticProps() {
|
||||||
const [contracts, hotContracts, closingSoonContracts, recentComments] =
|
const [contracts, hotContracts, closingSoonContracts, recentComments] =
|
||||||
|
@ -56,13 +58,18 @@ const Home = (props: {
|
||||||
closingSoonContracts: Contract[]
|
closingSoonContracts: Contract[]
|
||||||
}) => {
|
}) => {
|
||||||
const {
|
const {
|
||||||
activeContracts,
|
|
||||||
activeContractBets,
|
activeContractBets,
|
||||||
activeContractComments,
|
activeContractComments,
|
||||||
hotContracts,
|
hotContracts,
|
||||||
closingSoonContracts,
|
closingSoonContracts,
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
|
const contracts = useContracts() ?? props.activeContracts
|
||||||
|
const recentComments = useRecentComments()
|
||||||
|
const activeContracts = recentComments
|
||||||
|
? findActiveContracts(contracts, recentComments)
|
||||||
|
: props.activeContracts
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Page>
|
<Page>
|
||||||
<HotMarkets contracts={hotContracts} />
|
<HotMarkets contracts={hotContracts} />
|
||||||
|
@ -73,7 +80,6 @@ const Home = (props: {
|
||||||
contracts={activeContracts}
|
contracts={activeContracts}
|
||||||
contractBets={activeContractBets}
|
contractBets={activeContractBets}
|
||||||
contractComments={activeContractComments}
|
contractComments={activeContractComments}
|
||||||
listenForChanges
|
|
||||||
/>
|
/>
|
||||||
</Page>
|
</Page>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user