contract tabs: separate market timeline from your trades (#76)

* contract tabs: separate market timeline from your trades

* contract tabs: use pre-existing tab component

* tab styling
This commit is contained in:
mantikoros 2022-04-08 16:13:10 -05:00 committed by GitHub
parent 108f0afb95
commit ab41404d07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 65 deletions

View File

@ -83,15 +83,6 @@ export const ContractOverview = (props: {
{children} {children}
<Spacer h={4} /> <Spacer h={4} />
<ContractActivity
contract={contract}
bets={bets}
comments={comments}
user={user}
mode="all"
betRowClassName="!mt-0 xl:hidden"
/>
</Col> </Col>
) )
} }

View File

@ -0,0 +1,57 @@
import { useState } from 'react'
import clsx from 'clsx'
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'
import { Comment } from '../../lib/firebase/comments'
import { User } from '../../../common/user'
import { useBets } from '../../hooks/use-bets'
import { ContractActivity } from '../feed/contract-activity'
import { ContractBetsTable, MyBetsSummary } from '../bets-list'
import { Spacer } from '../layout/spacer'
import { Tabs } from '../layout/tabs'
export function ContractTabs(props: {
contract: Contract
user: User | null | undefined
bets: Bet[]
comments: Comment[]
}) {
const { contract, user, comments } = props
const bets = useBets(contract.id) ?? props.bets
// Decending creation time.
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
const userBets = user && bets.filter((bet) => bet.userId === user.id)
const activity = (
<ContractActivity
contract={contract}
bets={bets}
comments={comments}
user={user}
mode="all"
betRowClassName="!mt-0 xl:hidden"
/>
)
if (!user || !userBets?.length) return activity
const yourTrades = (
<div>
<MyBetsSummary className="px-2" contract={contract} bets={userBets} />
<Spacer h={6} />
<ContractBetsTable contract={contract} bets={userBets} />
<Spacer h={12} />
</div>
)
return (
<Tabs
tabs={[
{ title: 'Timeline', content: activity },
{ title: 'Your trades', content: yourTrades },
]}
/>
)
}

View File

@ -17,31 +17,35 @@ export function Tabs(props: { tabs: Tab[]; defaultIndex?: number }) {
return ( return (
<div> <div>
<nav className="flex space-x-4" aria-label="Tabs"> <div className="border-b border-gray-200">
<nav className="-mb-px flex space-x-8" aria-label="Tabs">
{tabs.map((tab, i) => ( {tabs.map((tab, i) => (
<Link href={tab.href ?? '#'} key={tab.title} shallow={!!tab.href}> <Link href={tab.href ?? '#'} key={tab.title} shallow={!!tab.href}>
<a <a
key={tab.title} key={tab.title}
className={clsx(
tab.title === activeTab.title
? 'bg-gray-200 text-gray-700'
: 'text-gray-500 hover:bg-gray-100 hover:text-gray-700',
'rounded-md px-3 py-2 text-sm font-medium'
)}
aria-current={tab.title === activeTab.title ? 'page' : undefined}
onClick={(e) => { onClick={(e) => {
if (!tab.href) { if (!tab.href) {
e.preventDefault() e.preventDefault()
} }
setActiveIndex(i) setActiveIndex(i)
}} }}
className={clsx(
activeIndex === i
? 'border-indigo-500 text-indigo-600'
: 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700',
'cursor-pointer whitespace-nowrap border-b-2 py-4 px-1 text-sm font-medium'
)}
aria-current={activeIndex === i ? 'page' : undefined}
> >
{tab.tabIcon ? <span className="mr-2">{tab.tabIcon}</span> : null} {tab.tabIcon ? (
<span className="mr-2">{tab.tabIcon}</span>
) : null}
{tab.title} {tab.title}
</a> </a>
</Link> </Link>
))} ))}
</nav> </nav>
</div>
<div className="mt-4">{activeTab.content}</div> <div className="mt-4">{activeTab.content}</div>
</div> </div>

View File

@ -6,8 +6,6 @@ import { BetPanel } from '../../components/bet-panel'
import { Col } from '../../components/layout/col' import { Col } from '../../components/layout/col'
import { useUser } from '../../hooks/use-user' import { useUser } from '../../hooks/use-user'
import { ResolutionPanel } from '../../components/resolution-panel' import { ResolutionPanel } from '../../components/resolution-panel'
import { ContractBetsTable, MyBetsSummary } from '../../components/bets-list'
import { useBets } from '../../hooks/use-bets'
import { Title } from '../../components/title' import { Title } from '../../components/title'
import { Spacer } from '../../components/layout/spacer' import { Spacer } from '../../components/layout/spacer'
import { listUsers, User } from '../../lib/firebase/users' import { listUsers, User } from '../../lib/firebase/users'
@ -25,17 +23,17 @@ import { Comment, listAllComments } from '../../lib/firebase/comments'
import Custom404 from '../404' import Custom404 from '../404'
import { getFoldsByTags } from '../../lib/firebase/folds' import { getFoldsByTags } from '../../lib/firebase/folds'
import { Fold } from '../../../common/fold' import { Fold } from '../../../common/fold'
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 { fromPropz, usePropz } from '../../hooks/use-propz' import { fromPropz, usePropz } from '../../hooks/use-propz'
import { Leaderboard } from '../../components/leaderboard' import { Leaderboard } from '../../components/leaderboard'
import _ from 'lodash' import _ from 'lodash'
import { calculatePayout, resolvedPayout } from '../../../common/calculate' import { resolvedPayout } from '../../../common/calculate'
import { formatMoney } from '../../../common/util/format' import { formatMoney } from '../../../common/util/format'
import { FeedBet, FeedComment } from '../../components/feed/feed-items' import { FeedBet, FeedComment } from '../../components/feed/feed-items'
import { useUserById } from '../../hooks/use-users' import { useUserById } from '../../hooks/use-users'
import { ContractTabs } from '../../components/contract/contract-tabs'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { export async function getStaticPropz(props: {
@ -168,38 +166,18 @@ export default function ContractPage(props: {
<Spacer h={12} /> <Spacer h={12} />
</> </>
)} )}
<BetsSection contract={contract} user={user ?? null} bets={bets} />
<ContractTabs
contract={contract}
user={user}
bets={bets}
comments={comments}
/>
</Col> </Col>
</Page> </Page>
) )
} }
function BetsSection(props: {
contract: Contract
user: User | null
bets: Bet[]
}) {
const { contract, user } = props
const bets = useBets(contract.id) ?? props.bets
// Decending creation time.
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
const userBets = user && bets.filter((bet) => bet.userId === user.id)
if (!userBets || userBets.length === 0) return <></>
return (
<div>
<Title className="px-2" text="Your trades" />
<MyBetsSummary className="px-2" contract={contract} bets={userBets} />
<Spacer h={6} />
<ContractBetsTable contract={contract} bets={userBets} />
<Spacer h={12} />
</div>
)
}
function ContractLeaderboard(props: { contract: Contract; bets: Bet[] }) { function ContractLeaderboard(props: { contract: Contract; bets: Bet[] }) {
const { contract, bets } = props const { contract, bets } = props
const [users, setUsers] = useState<User[]>() const [users, setUsers] = useState<User[]>()