Load comments via static props

This commit is contained in:
James Grugett 2022-09-28 13:11:26 -04:00
parent eb762d9b9e
commit d55cedb36c
3 changed files with 29 additions and 9 deletions

View File

@ -23,13 +23,15 @@ import {
HOUSE_LIQUIDITY_PROVIDER_ID, HOUSE_LIQUIDITY_PROVIDER_ID,
} from 'common/antes' } from 'common/antes'
import { buildArray } from 'common/util/array' import { buildArray } from 'common/util/array'
import { ContractComment } from 'common/comment'
export function ContractTabs(props: { export function ContractTabs(props: {
contract: Contract contract: Contract
bets: Bet[] bets: Bet[]
userBets: Bet[] userBets: Bet[]
comments: ContractComment[]
}) { }) {
const { contract, bets, userBets } = props const { contract, bets, userBets, comments } = props
const yourTrades = ( const yourTrades = (
<div> <div>
@ -42,7 +44,7 @@ export function ContractTabs(props: {
const tabs = buildArray( const tabs = buildArray(
{ {
title: 'Comments', title: 'Comments',
content: <CommentsTabContent contract={contract} />, content: <CommentsTabContent contract={contract} comments={comments} />,
}, },
{ {
title: capitalize(PAST_BETS), title: capitalize(PAST_BETS),
@ -61,10 +63,11 @@ export function ContractTabs(props: {
const CommentsTabContent = memo(function CommentsTabContent(props: { const CommentsTabContent = memo(function CommentsTabContent(props: {
contract: Contract contract: Contract
comments: ContractComment[]
}) { }) {
const { contract } = props const { contract } = props
const tips = useTipTxns({ contractId: contract.id }) const tips = useTipTxns({ contractId: contract.id })
const comments = useComments(contract.id) const comments = useComments(contract.id) ?? props.comments
if (comments == null) { if (comments == null) {
return <LoadingIndicator /> return <LoadingIndicator />
} }

View File

@ -131,7 +131,7 @@ function getCommentsOnPostCollection(postId: string) {
} }
export async function listAllComments(contractId: string) { export async function listAllComments(contractId: string) {
return await getValues<Comment>( return await getValues<ContractComment>(
query(getCommentsCollection(contractId), orderBy('createdTime', 'desc')) query(getCommentsCollection(contractId), orderBy('createdTime', 'desc'))
) )
} }

View File

@ -46,6 +46,8 @@ import { BetSignUpPrompt } from 'web/components/sign-up-prompt'
import { PlayMoneyDisclaimer } from 'web/components/play-money-disclaimer' import { PlayMoneyDisclaimer } from 'web/components/play-money-disclaimer'
import BetButton from 'web/components/bet-button' import BetButton from 'web/components/bet-button'
import { BetsSummary } from 'web/components/bet-summary' import { BetsSummary } from 'web/components/bet-summary'
import { listAllComments } from 'web/lib/firebase/comments'
import { ContractComment } from 'common/comment'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { export async function getStaticPropz(props: {
@ -55,10 +57,15 @@ export async function getStaticPropz(props: {
const contract = (await getContractFromSlug(contractSlug)) || null const contract = (await getContractFromSlug(contractSlug)) || null
const contractId = contract?.id const contractId = contract?.id
const bets = contractId ? await listAllBets(contractId) : [] const bets = contractId ? await listAllBets(contractId) : []
const comments = contractId ? await listAllComments(contractId) : []
return { return {
// Limit the data sent to the client. Client will still load all bets directly. props: {
props: { contract, bets: bets.slice(0, 5000) }, contract,
// Limit the data sent to the client. Client will still load all bets/comments directly.
bets: bets.slice(0, 5000),
comments: comments.slice(0, 1000),
},
revalidate: 5, // regenerate after five seconds revalidate: 5, // regenerate after five seconds
} }
} }
@ -70,9 +77,14 @@ export async function getStaticPaths() {
export default function ContractPage(props: { export default function ContractPage(props: {
contract: Contract | null contract: Contract | null
bets: Bet[] bets: Bet[]
comments: ContractComment[]
backToHome?: () => void backToHome?: () => void
}) { }) {
props = usePropz(props, getStaticPropz) ?? { contract: null, bets: [] } props = usePropz(props, getStaticPropz) ?? {
contract: null,
bets: [],
comments: [],
}
const inIframe = useIsIframe() const inIframe = useIsIframe()
if (inIframe) { if (inIframe) {
@ -147,7 +159,7 @@ export function ContractPageContent(
contract: Contract contract: Contract
} }
) { ) {
const { backToHome } = props const { backToHome, comments } = props
const contract = useContractWithPreload(props.contract) ?? props.contract const contract = useContractWithPreload(props.contract) ?? props.contract
const user = useUser() const user = useUser()
usePrefetch(user?.id) usePrefetch(user?.id)
@ -258,7 +270,12 @@ export function ContractPageContent(
userBets={userBets} userBets={userBets}
/> />
<ContractTabs contract={contract} bets={bets} userBets={userBets} /> <ContractTabs
contract={contract}
bets={bets}
userBets={userBets}
comments={comments}
/>
{!user ? ( {!user ? (
<Col className="mt-4 max-w-sm items-center xl:hidden"> <Col className="mt-4 max-w-sm items-center xl:hidden">