From dc29ca531c1a200d1d979d3d33bd293bf622c2d1 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Mon, 7 Mar 2022 16:20:06 -0800 Subject: [PATCH] Conditionally use SSG depending on public vs private instance --- web/hooks/use-propz.ts | 8 ++++++-- web/pages/[username]/[contractSlug].tsx | 25 +++++++++++++------------ web/pages/home.tsx | 14 ++++++++------ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/web/hooks/use-propz.ts b/web/hooks/use-propz.ts index 3687c725..60b6b142 100644 --- a/web/hooks/use-propz.ts +++ b/web/hooks/use-propz.ts @@ -1,6 +1,7 @@ import _ from 'lodash' import { useRouter } from 'next/router' import { useState, useEffect } from 'react' +import { IS_PRIVATE_MANIFOLD } from '../lib/firebase/init' type PropzProps = { params: any @@ -10,7 +11,7 @@ type PropzProps = { // 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/ export function usePropz( - getStaticPropz: (props?: PropzProps) => Promise, + getStaticPropz: (props: PropzProps) => Promise, // Dynamic routes will need the query params from the router needParams?: boolean ) { @@ -23,8 +24,11 @@ export function usePropz( if (needParams && _.isEmpty(params)) { return } - // @ts-ignore getStaticPropz({ params }).then((result) => setPropz(result.props)) }, [params]) return propz } + +export function fromPropz(getStaticPropz: (props: PropzProps) => Promise) { + return IS_PRIVATE_MANIFOLD ? async () => {} : getStaticPropz +} diff --git a/web/pages/[username]/[contractSlug].tsx b/web/pages/[username]/[contractSlug].tsx index c7baff55..59d68899 100644 --- a/web/pages/[username]/[contractSlug].tsx +++ b/web/pages/[username]/[contractSlug].tsx @@ -29,8 +29,9 @@ import { useFoldsWithTags } from '../../hooks/use-fold' import { listAllAnswers } from '../../lib/firebase/answers' import { Answer } from '../../../common/answer' 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: { 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' } } @@ -78,16 +79,16 @@ export default function ContractPage(props: { slug: string folds: Fold[] }) { - // @ts-ignore - props = usePropz(getStaticPropz, true) ?? { - contract: null, - username: '', - comments: [], - answers: [], - bets: [], - slug: '', - folds: [], - } + props = props ?? + usePropz(getStaticPropz, true) ?? { + contract: null, + username: '', + comments: [], + answers: [], + bets: [], + slug: '', + folds: [], + } const user = useUser() const contract = useContractWithPreload(props.slug, props.contract) diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 6ebe500b..0cec2756 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -22,12 +22,13 @@ import { useFilterYourContracts, useFindActiveContracts, } 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 { useGetRecentBets, useRecentBets } from '../hooks/use-bets' import { useActiveContracts } from '../hooks/use-contracts' import { useRecentComments } from '../hooks/use-comments' +export const getStaticProps = fromPropz(getStaticPropz) export async function getStaticPropz() { const contractInfo = await getAllContractInfo() @@ -42,11 +43,12 @@ const Home = (props: { folds: Fold[] recentComments: Comment[] }) => { - props = usePropz(getStaticPropz) ?? { - contracts: [], - folds: [], - recentComments: [], - } + props = props ?? + usePropz(getStaticPropz) ?? { + contracts: [], + folds: [], + recentComments: [], + } const { folds } = props const user = useUser()