Make homepage load user via SSR, pass it to contract stuff (#729)

This commit is contained in:
Marshall Polaris 2022-08-09 15:28:52 -07:00 committed by GitHub
parent 847d3d0f27
commit c07daafb8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 13 deletions

View File

@ -2,6 +2,7 @@
import algoliasearch from 'algoliasearch/lite' import algoliasearch from 'algoliasearch/lite'
import { Contract } from 'common/contract' import { Contract } from 'common/contract'
import { User } from 'common/user'
import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params' import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
import { import {
ContractHighlightOptions, ContractHighlightOptions,
@ -11,7 +12,6 @@ import { Row } from './layout/row'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { Spacer } from './layout/spacer' import { Spacer } from './layout/spacer'
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants' import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
import { useUser } from 'web/hooks/use-user'
import { useFollows } from 'web/hooks/use-follows' import { useFollows } from 'web/hooks/use-follows'
import { track, trackCallback } from 'web/lib/service/analytics' import { track, trackCallback } from 'web/lib/service/analytics'
import ContractSearchFirestore from 'web/pages/contract-search-firestore' import ContractSearchFirestore from 'web/pages/contract-search-firestore'
@ -45,6 +45,7 @@ export const DEFAULT_SORT = 'score'
type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all' type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all'
export function ContractSearch(props: { export function ContractSearch(props: {
user: User | null | undefined
querySortOptions?: { querySortOptions?: {
defaultSort: Sort defaultSort: Sort
defaultFilter?: filter defaultFilter?: filter
@ -67,6 +68,7 @@ export function ContractSearch(props: {
} }
}) { }) {
const { const {
user,
querySortOptions, querySortOptions,
additionalFilter, additionalFilter,
onContractClick, onContractClick,
@ -77,7 +79,6 @@ export function ContractSearch(props: {
highlightOptions, highlightOptions,
} = props } = props
const user = useUser()
const memberGroups = (useMemberGroups(user?.id) ?? []).filter( const memberGroups = (useMemberGroups(user?.id) ?? []).filter(
(group) => !NEW_USER_GROUP_SLUGS.includes(group.slug) (group) => !NEW_USER_GROUP_SLUGS.includes(group.slug)
) )

View File

@ -97,11 +97,15 @@ export function ContractsGrid(props: {
) )
} }
export function CreatorContractsList(props: { creator: User }) { export function CreatorContractsList(props: {
const { creator } = props user: User | null | undefined
creator: User
}) {
const { user, creator } = props
return ( return (
<ContractSearch <ContractSearch
user={user}
querySortOptions={{ querySortOptions={{
defaultSort: 'newest', defaultSort: 'newest',
defaultFilter: 'all', defaultFilter: 'all',

View File

@ -300,7 +300,9 @@ export function UserPage(props: { user: User; currentUser?: User }) {
tabs={[ tabs={[
{ {
title: 'Markets', title: 'Markets',
content: <CreatorContractsList creator={user} />, content: (
<CreatorContractsList user={currentUser} creator={user} />
),
tabIcon: ( tabIcon: (
<span className="px-0.5 font-bold"> <span className="px-0.5 font-bold">
{usersContracts.length} {usersContracts.length}

View File

@ -92,6 +92,7 @@ export default function ContractPage(props: {
slug: '', slug: '',
} }
const user = useUser()
const inIframe = useIsIframe() const inIframe = useIsIframe()
if (inIframe) { if (inIframe) {
return <ContractEmbedPage {...props} /> return <ContractEmbedPage {...props} />
@ -103,7 +104,7 @@ export default function ContractPage(props: {
return <Custom404 /> return <Custom404 />
} }
return <ContractPageContent {...{ ...props, contract }} /> return <ContractPageContent {...{ ...props, contract, user }} />
} }
export function ContractPageSidebar(props: { export function ContractPageSidebar(props: {
@ -144,9 +145,12 @@ export function ContractPageSidebar(props: {
} }
export function ContractPageContent( export function ContractPageContent(
props: Parameters<typeof ContractPage>[0] & { contract: Contract } props: Parameters<typeof ContractPage>[0] & {
contract: Contract
user?: User | null
}
) { ) {
const { backToHome, comments } = props const { backToHome, comments, user } = props
const contract = useContractWithPreload(props.contract) ?? props.contract const contract = useContractWithPreload(props.contract) ?? props.contract
@ -164,8 +168,6 @@ export function ContractPageContent(
const tips = useTipTxns({ contractId: contract.id }) const tips = useTipTxns({ contractId: contract.id })
const user = useUser()
const { width, height } = useWindowSize() const { width, height } = useWindowSize()
const [showConfetti, setShowConfetti] = useState(false) const [showConfetti, setShowConfetti] = useState(false)

View File

@ -201,6 +201,7 @@ export default function GroupPage(props: {
const questionsTab = ( const questionsTab = (
<ContractSearch <ContractSearch
user={user}
querySortOptions={{ querySortOptions={{
shouldLoadFromStorage: true, shouldLoadFromStorage: true,
defaultSort: getSavedSort() ?? 'newest', defaultSort: getSavedSort() ?? 'newest',
@ -614,6 +615,7 @@ function AddContractButton(props: { group: Group; user: User }) {
<div className={'overflow-y-scroll sm:px-8'}> <div className={'overflow-y-scroll sm:px-8'}>
<ContractSearch <ContractSearch
user={user}
hideOrderSelector={true} hideOrderSelector={true}
onContractClick={addContractToCurrentGroup} onContractClick={addContractToCurrentGroup}
overrideGridClassName={ overrideGridClassName={

View File

@ -7,16 +7,22 @@ import { Col } from 'web/components/layout/col'
import { getSavedSort } from 'web/hooks/use-sort-and-query-params' import { getSavedSort } from 'web/hooks/use-sort-and-query-params'
import { ContractSearch, DEFAULT_SORT } from 'web/components/contract-search' import { ContractSearch, DEFAULT_SORT } from 'web/components/contract-search'
import { Contract } from 'common/contract' import { Contract } from 'common/contract'
import { User } from 'common/user'
import { ContractPageContent } from './[username]/[contractSlug]' import { ContractPageContent } from './[username]/[contractSlug]'
import { getContractFromSlug } from 'web/lib/firebase/contracts' import { getContractFromSlug } from 'web/lib/firebase/contracts'
import { getUser } from 'web/lib/firebase/users'
import { useTracking } from 'web/hooks/use-tracking' import { useTracking } from 'web/hooks/use-tracking'
import { track } from 'web/lib/service/analytics' import { track } from 'web/lib/service/analytics'
import { redirectIfLoggedOut } from 'web/lib/firebase/server-auth' import { redirectIfLoggedOut } from 'web/lib/firebase/server-auth'
import { useSaveReferral } from 'web/hooks/use-save-referral' import { useSaveReferral } from 'web/hooks/use-save-referral'
export const getServerSideProps = redirectIfLoggedOut('/') export const getServerSideProps = redirectIfLoggedOut('/', async (_, creds) => {
const user = await getUser(creds.user.uid)
return { props: { user } }
})
const Home = () => { const Home = (props: { user: User }) => {
const { user } = props
const [contract, setContract] = useContractPage() const [contract, setContract] = useContractPage()
const router = useRouter() const router = useRouter()
@ -29,6 +35,7 @@ const Home = () => {
<Page suspend={!!contract}> <Page suspend={!!contract}>
<Col className="mx-auto w-full p-2"> <Col className="mx-auto w-full p-2">
<ContractSearch <ContractSearch
user={user}
querySortOptions={{ querySortOptions={{
shouldLoadFromStorage: true, shouldLoadFromStorage: true,
defaultSort: getSavedSort() ?? DEFAULT_SORT, defaultSort: getSavedSort() ?? DEFAULT_SORT,
@ -56,6 +63,7 @@ const Home = () => {
{contract && ( {contract && (
<ContractPageContent <ContractPageContent
contract={contract} contract={contract}
user={user}
username={contract.creatorUsername} username={contract.creatorUsername}
slug={contract.slug} slug={contract.slug}
bets={[]} bets={[]}

View File

@ -1,9 +1,11 @@
import { useUser } from 'web/hooks/use-user'
import { ContractSearch } from '../components/contract-search' import { ContractSearch } from '../components/contract-search'
import { Page } from '../components/page' import { Page } from '../components/page'
import { SEO } from '../components/SEO' import { SEO } from '../components/SEO'
// TODO: Rename endpoint to "Explore" // TODO: Rename endpoint to "Explore"
export default function Markets() { export default function Markets() {
const user = useUser()
return ( return (
<Page> <Page>
<SEO <SEO
@ -11,7 +13,7 @@ export default function Markets() {
description="Discover what's new, trending, or soon-to-close. Or search thousands of prediction markets." description="Discover what's new, trending, or soon-to-close. Or search thousands of prediction markets."
url="/markets" url="/markets"
/> />
<ContractSearch /> <ContractSearch user={user} />
</Page> </Page>
) )
} }

View File

@ -1,10 +1,12 @@
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useUser } from 'web/hooks/use-user'
import { ContractSearch } from '../../components/contract-search' import { ContractSearch } from '../../components/contract-search'
import { Page } from '../../components/page' import { Page } from '../../components/page'
import { Title } from '../../components/title' import { Title } from '../../components/title'
export default function TagPage() { export default function TagPage() {
const router = useRouter() const router = useRouter()
const user = useUser()
const { tag } = router.query as { tag: string } const { tag } = router.query as { tag: string }
if (!router.isReady) return <div /> if (!router.isReady) return <div />
@ -12,6 +14,7 @@ export default function TagPage() {
<Page> <Page>
<Title text={`#${tag}`} /> <Title text={`#${tag}`} />
<ContractSearch <ContractSearch
user={user}
querySortOptions={{ querySortOptions={{
defaultSort: 'newest', defaultSort: 'newest',
defaultFilter: 'all', defaultFilter: 'all',