manifold/web/pages/home.tsx

76 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-04-11 21:13:26 +00:00
import React, { useEffect } from 'react'
import Router, { useRouter } from 'next/router'
import _ from 'lodash'
2022-01-23 00:16:23 +00:00
import { Page } from '../components/page'
import { ActivityFeed } from '../components/feed/activity-feed'
2022-01-23 00:16:23 +00:00
import FeedCreate from '../components/feed-create'
import { Spacer } from '../components/layout/spacer'
import { Col } from '../components/layout/col'
import { useUser } from '../hooks/use-user'
2022-02-03 23:12:09 +00:00
import { LoadingIndicator } from '../components/loading-indicator'
import { useAlgoFeed } from '../hooks/use-algo-feed'
2022-04-11 21:13:26 +00:00
import { ContractPageContent } from './[username]/[contractSlug]'
2022-01-23 00:16:23 +00:00
const Home = () => {
2022-01-23 00:16:23 +00:00
const user = useUser()
2022-04-28 17:28:52 +00:00
const feed = useAlgoFeed()
2022-04-11 21:13:26 +00:00
const router = useRouter()
const { u: username, s: slug } = router.query
2022-04-28 17:28:52 +00:00
const contract = feed?.find(
({ contract }) => contract.slug === slug
)?.contract
2022-04-11 21:13:26 +00:00
useEffect(() => {
// If the page initially loads with query params, redirect to the contract page.
if (router.isReady && slug && username) {
Router.replace(`/${username}/${slug}`)
2022-04-11 21:13:26 +00:00
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router.isReady])
2022-04-11 21:13:26 +00:00
2022-01-23 00:16:23 +00:00
if (user === null) {
Router.replace('/')
return <></>
}
return (
2022-04-11 21:13:26 +00:00
<>
<Page assertUser="signed-in" suspend={!!contract}>
<Col className="items-center">
<Col className="w-full max-w-[700px]">
<FeedCreate user={user ?? undefined} />
<Spacer h={10} />
2022-04-28 17:28:52 +00:00
{feed ? (
2022-04-11 21:13:26 +00:00
<ActivityFeed
2022-04-28 17:28:52 +00:00
feed={feed}
2022-04-11 21:13:26 +00:00
mode="only-recent"
getContractPath={(c) =>
`home?u=${c.creatorUsername}&s=${c.slug}`
}
2022-04-11 21:13:26 +00:00
/>
) : (
<LoadingIndicator className="mt-4" />
)}
</Col>
2022-01-23 00:16:23 +00:00
</Col>
2022-04-11 21:13:26 +00:00
</Page>
{contract && (
<ContractPageContent
contract={contract}
username={contract.creatorUsername}
slug={contract.slug}
bets={[]}
comments={[]}
backToHome={router.back}
/>
)}
</>
2022-01-23 00:16:23 +00:00
)
}
export default Home