Conditionally use SSG depending on public vs private instance

This commit is contained in:
Austin Chen 2022-03-07 16:20:06 -08:00
parent ae63f70a30
commit dc29ca531c
3 changed files with 27 additions and 20 deletions

View File

@ -1,6 +1,7 @@
import _ from 'lodash' import _ from 'lodash'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { IS_PRIVATE_MANIFOLD } from '../lib/firebase/init'
type PropzProps = { type PropzProps = {
params: any params: any
@ -10,7 +11,7 @@ type PropzProps = {
// This allows us to client-side render the page for authenticated users. // This allows us to client-side render the page for authenticated users.
// TODO: Could cache the result using stale-while-revalidate: https://swr.vercel.app/ // TODO: Could cache the result using stale-while-revalidate: https://swr.vercel.app/
export function usePropz( export function usePropz(
getStaticPropz: (props?: PropzProps) => Promise<any>, getStaticPropz: (props: PropzProps) => Promise<any>,
// Dynamic routes will need the query params from the router // Dynamic routes will need the query params from the router
needParams?: boolean needParams?: boolean
) { ) {
@ -23,8 +24,11 @@ export function usePropz(
if (needParams && _.isEmpty(params)) { if (needParams && _.isEmpty(params)) {
return return
} }
// @ts-ignore
getStaticPropz({ params }).then((result) => setPropz(result.props)) getStaticPropz({ params }).then((result) => setPropz(result.props))
}, [params]) }, [params])
return propz return propz
} }
export function fromPropz(getStaticPropz: (props: PropzProps) => Promise<any>) {
return IS_PRIVATE_MANIFOLD ? async () => {} : getStaticPropz
}

View File

@ -29,8 +29,9 @@ import { useFoldsWithTags } from '../../hooks/use-fold'
import { listAllAnswers } from '../../lib/firebase/answers' import { listAllAnswers } from '../../lib/firebase/answers'
import { Answer } from '../../../common/answer' import { Answer } from '../../../common/answer'
import { AnswersPanel } from '../../components/answers/answers-panel' import { AnswersPanel } from '../../components/answers/answers-panel'
import { usePropz } from '../../hooks/use-propz' import { fromPropz, usePropz } from '../../hooks/use-propz'
export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { export async function getStaticPropz(props: {
params: { username: string; contractSlug: string } params: { username: string; contractSlug: string }
}) { }) {
@ -65,7 +66,7 @@ export async function getStaticPropz(props: {
} }
} }
export async function getStaticPathz() { export async function getStaticPaths() {
return { paths: [], fallback: 'blocking' } return { paths: [], fallback: 'blocking' }
} }
@ -78,16 +79,16 @@ export default function ContractPage(props: {
slug: string slug: string
folds: Fold[] folds: Fold[]
}) { }) {
// @ts-ignore props = props ??
props = usePropz(getStaticPropz, true) ?? { usePropz(getStaticPropz, true) ?? {
contract: null, contract: null,
username: '', username: '',
comments: [], comments: [],
answers: [], answers: [],
bets: [], bets: [],
slug: '', slug: '',
folds: [], folds: [],
} }
const user = useUser() const user = useUser()
const contract = useContractWithPreload(props.slug, props.contract) const contract = useContractWithPreload(props.slug, props.contract)

View File

@ -22,12 +22,13 @@ import {
useFilterYourContracts, useFilterYourContracts,
useFindActiveContracts, useFindActiveContracts,
} from '../hooks/use-find-active-contracts' } from '../hooks/use-find-active-contracts'
import { usePropz } from '../hooks/use-propz' import { fromPropz, usePropz } from '../hooks/use-propz'
import { IS_PRIVATE_MANIFOLD } from '../lib/firebase/init' import { IS_PRIVATE_MANIFOLD } from '../lib/firebase/init'
import { useGetRecentBets, useRecentBets } from '../hooks/use-bets' import { useGetRecentBets, useRecentBets } from '../hooks/use-bets'
import { useActiveContracts } from '../hooks/use-contracts' import { useActiveContracts } from '../hooks/use-contracts'
import { useRecentComments } from '../hooks/use-comments' import { useRecentComments } from '../hooks/use-comments'
export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz() { export async function getStaticPropz() {
const contractInfo = await getAllContractInfo() const contractInfo = await getAllContractInfo()
@ -42,11 +43,12 @@ const Home = (props: {
folds: Fold[] folds: Fold[]
recentComments: Comment[] recentComments: Comment[]
}) => { }) => {
props = usePropz(getStaticPropz) ?? { props = props ??
contracts: [], usePropz(getStaticPropz) ?? {
folds: [], contracts: [],
recentComments: [], folds: [],
} recentComments: [],
}
const { folds } = props const { folds } = props
const user = useUser() const user = useUser()