2022-05-16 02:57:50 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-04-11 21:13:26 +00:00
|
|
|
import Router, { useRouter } from 'next/router'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { ActivityFeed } from 'web/components/feed/activity-feed'
|
|
|
|
import FeedCreate from 'web/components/feed-create'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import { useAlgoFeed } from 'web/hooks/use-algo-feed'
|
2022-04-11 21:13:26 +00:00
|
|
|
import { ContractPageContent } from './[username]/[contractSlug]'
|
2022-05-12 15:07:10 +00:00
|
|
|
import { CategorySelector } from '../components/feed/category-selector'
|
2022-01-23 00:16:23 +00:00
|
|
|
|
2022-04-09 23:10:58 +00:00
|
|
|
const Home = () => {
|
2022-01-23 00:16:23 +00:00
|
|
|
const user = useUser()
|
2022-05-16 02:57:50 +00:00
|
|
|
const [category, setCategory] = useState<string>('all')
|
2022-01-23 00:16:23 +00:00
|
|
|
|
2022-05-16 02:57:50 +00:00
|
|
|
const feed = useAlgoFeed(user, category)
|
2022-02-25 07:59:53 +00:00
|
|
|
|
2022-04-11 21:13:26 +00:00
|
|
|
const router = useRouter()
|
2022-04-13 04:11:10 +00:00
|
|
|
const { u: username, s: slug } = router.query
|
2022-05-01 16:36:54 +00:00
|
|
|
const contract = feed?.find(
|
|
|
|
({ contract }) => contract.slug === slug
|
|
|
|
)?.contract
|
2022-04-11 21:13:26 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-04-13 04:11:10 +00:00
|
|
|
// 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
|
|
|
}
|
2022-04-13 04:11:10 +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}>
|
2022-05-13 23:47:50 +00:00
|
|
|
<Col className="mx-auto w-full max-w-[700px]">
|
|
|
|
<FeedCreate user={user ?? undefined} />
|
|
|
|
<Spacer h={2} />
|
2022-05-16 02:57:50 +00:00
|
|
|
<CategorySelector
|
|
|
|
user={user}
|
|
|
|
category={category}
|
|
|
|
setCategory={setCategory}
|
|
|
|
/>
|
2022-05-13 23:47:50 +00:00
|
|
|
<Spacer h={1} />
|
|
|
|
{feed ? (
|
|
|
|
<ActivityFeed
|
|
|
|
feed={feed}
|
|
|
|
mode="only-recent"
|
|
|
|
getContractPath={(c) => `home?u=${c.creatorUsername}&s=${c.slug}`}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator className="mt-4" />
|
|
|
|
)}
|
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
|