From 80776186aa6c022511f00916cb78dff774278853 Mon Sep 17 00:00:00 2001 From: mantikoros <95266179+mantikoros@users.noreply.github.com> Date: Sat, 22 Jan 2022 18:16:23 -0600 Subject: [PATCH] /home for signed in users (#35) --- web/components/feed-create.tsx | 9 ++- web/components/manifold-logo.tsx | 6 +- web/pages/home.tsx | 104 +++++++++++++++++++++++++++++++ web/pages/index.tsx | 96 +++++----------------------- 4 files changed, 129 insertions(+), 86 deletions(-) create mode 100644 web/pages/home.tsx diff --git a/web/components/feed-create.tsx b/web/components/feed-create.tsx index 061ac5ea..3e36794c 100644 --- a/web/components/feed-create.tsx +++ b/web/components/feed-create.tsx @@ -49,7 +49,7 @@ export function FeedPromo(props: { hotContracts: Contract[] }) { ) } -export default function FeedCreate(props: { user: User }) { +export default function FeedCreate(props: { user?: User }) { const { user } = props const [question, setQuestion] = useState('') @@ -70,9 +70,10 @@ export default function FeedCreate(props: { user: User }) {
+
{/* TODO: Show focus, for accessibility */}
@@ -88,10 +89,12 @@ export default function FeedCreate(props: { user: User }) {
+ {/* Hide component instead of deleting, so edits to NewContract don't get lost */}
+ {/* Show a fake "Create Market" button, which gets replaced with the NewContract one*/} {!question && (
diff --git a/web/components/manifold-logo.tsx b/web/components/manifold-logo.tsx index db37c918..65fee46b 100644 --- a/web/components/manifold-logo.tsx +++ b/web/components/manifold-logo.tsx @@ -2,14 +2,18 @@ import Link from 'next/link' import Image from 'next/image' import clsx from 'clsx' +import { useUser } from '../hooks/use-user' + export function ManifoldLogo(props: { className?: string darkBackground?: boolean }) { const { darkBackground, className } = props + const user = useUser() + return ( - + []), + getRecentComments().catch(() => []), + getHotContracts().catch(() => []), + getClosingSoonContracts().catch(() => []), + ]) + + const activeContracts = findActiveContracts(contracts, recentComments) + const activeContractBets = await Promise.all( + activeContracts.map((contract) => listAllBets(contract.id).catch((_) => [])) + ) + const activeContractComments = await Promise.all( + activeContracts.map((contract) => + listAllComments(contract.id).catch((_) => []) + ) + ) + + return { + props: { + activeContracts, + activeContractBets, + activeContractComments, + hotContracts, + closingSoonContracts, + }, + + revalidate: 60, // regenerate after a minute + } +} + +const Home = (props: { + activeContracts: Contract[] + activeContractBets: Bet[][] + activeContractComments: Comment[][] + hotContracts: Contract[] + closingSoonContracts: Contract[] +}) => { + const { + activeContracts, + activeContractBets, + activeContractComments, + hotContracts, + closingSoonContracts, + } = props + + const user = useUser() + + if (user === null) { + Router.replace('/') + return <> + } + + return ( + + + +
+ + + + + + + + + + +
+ + +
+ ) +} + +export default Home diff --git a/web/pages/index.tsx b/web/pages/index.tsx index 5b4048d8..aeb7a502 100644 --- a/web/pages/index.tsx +++ b/web/pages/index.tsx @@ -1,105 +1,37 @@ import React from 'react' -import _ from 'lodash' -import { - Contract, - getClosingSoonContracts, - getHotContracts, - listAllContracts, -} from '../lib/firebase/contracts' +import Router from 'next/router' + +import { Contract, getHotContracts } from '../lib/firebase/contracts' import { Page } from '../components/page' -import { ActivityFeed, findActiveContracts } from './activity' -import { - getRecentComments, - Comment, - listAllComments, -} from '../lib/firebase/comments' -import { Bet, listAllBets } from '../lib/firebase/bets' -import { useContracts } from '../hooks/use-contracts' -import { useRecentComments } from '../hooks/use-comments' -import FeedCreate, { FeedPromo } from '../components/feed-create' -import { Spacer } from '../components/layout/spacer' +import { FeedPromo } from '../components/feed-create' import { Col } from '../components/layout/col' import { useUser } from '../hooks/use-user' -import { ClosingSoonMarkets, HotMarkets } from './markets' export async function getStaticProps() { - const [contracts, recentComments, hotContracts, closingSoonContracts] = - await Promise.all([ - listAllContracts().catch((_) => []), - getRecentComments().catch(() => []), - getHotContracts().catch(() => []), - getClosingSoonContracts().catch(() => []), - ]) - - const activeContracts = findActiveContracts(contracts, recentComments) - const activeContractBets = await Promise.all( - activeContracts.map((contract) => listAllBets(contract.id).catch((_) => [])) - ) - const activeContractComments = await Promise.all( - activeContracts.map((contract) => - listAllComments(contract.id).catch((_) => []) - ) - ) + const hotContracts = (await getHotContracts().catch(() => [])) ?? [] return { - props: { - activeContracts, - activeContractBets, - activeContractComments, - hotContracts, - closingSoonContracts, - }, - + props: { hotContracts }, revalidate: 60, // regenerate after a minute } } -const Home = (props: { - activeContracts: Contract[] - activeContractBets: Bet[][] - activeContractComments: Comment[][] - hotContracts: Contract[] - closingSoonContracts: Contract[] -}) => { - const { - activeContractBets, - activeContractComments, - hotContracts, - closingSoonContracts, - } = props - - const contracts = useContracts() ?? props.activeContracts - const recentComments = useRecentComments() - const activeContracts = recentComments - ? findActiveContracts(contracts, recentComments) - : props.activeContracts +const Home = (props: { hotContracts: Contract[] }) => { + const { hotContracts } = props const user = useUser() + if (user) { + Router.replace('/home') + return <> + } + return (
- {user ? ( - <> - - - - - - - - - ) : ( - <> - - - )} +