2022-09-12 05:39:04 +00:00
|
|
|
import React from 'react'
|
2022-08-31 04:45:55 +00:00
|
|
|
import Router from 'next/router'
|
2022-09-08 06:36:34 +00:00
|
|
|
import {
|
2022-09-13 22:47:29 +00:00
|
|
|
AdjustmentsIcon,
|
2022-09-08 06:36:34 +00:00
|
|
|
PlusSmIcon,
|
|
|
|
ArrowSmRightIcon,
|
|
|
|
} from '@heroicons/react/solid'
|
|
|
|
import clsx from 'clsx'
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { ContractSearch, SORTS } from 'web/components/contract-search'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { useTracking } from 'web/hooks/use-tracking'
|
|
|
|
import { track } from 'web/lib/service/analytics'
|
|
|
|
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
|
|
|
import { Sort } from 'web/components/contract-search'
|
|
|
|
import { Group } from 'common/group'
|
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { useMemberGroups } from 'web/hooks/use-group'
|
|
|
|
import { Button } from 'web/components/button'
|
2022-09-08 06:36:34 +00:00
|
|
|
import { getHomeItems } from '../../../components/arrange-home'
|
2022-08-31 04:45:55 +00:00
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
2022-09-05 23:09:01 +00:00
|
|
|
import { ProbChangeTable } from 'web/components/contract/prob-change-table'
|
2022-09-08 06:36:34 +00:00
|
|
|
import { groupPath } from 'web/lib/firebase/groups'
|
|
|
|
import { usePortfolioHistory } from 'web/hooks/use-portfolio-history'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-09-12 05:39:04 +00:00
|
|
|
import { useProbChanges } from 'web/hooks/use-prob-changes'
|
2022-09-13 22:47:29 +00:00
|
|
|
import { ProfitBadge } from 'web/components/bets-list'
|
|
|
|
import { calculatePortfolioProfit } from 'common/calculate-metrics'
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-13 22:47:29 +00:00
|
|
|
export default function Home() {
|
2022-09-08 06:36:34 +00:00
|
|
|
const user = useUser()
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
useTracking('view home')
|
|
|
|
|
|
|
|
useSaveReferral()
|
|
|
|
|
2022-08-31 05:30:31 +00:00
|
|
|
const groups = useMemberGroups(user?.id) ?? []
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
const { sections } = getHomeItems(groups, user?.homeSections ?? [])
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
2022-09-08 06:36:34 +00:00
|
|
|
<Col className="pm:mx-10 gap-4 px-4 pb-12">
|
2022-09-13 22:47:29 +00:00
|
|
|
<Row className={'mt-4 w-full items-start justify-between'}>
|
|
|
|
<Row className="items-end gap-4">
|
|
|
|
<Title className="!mb-1 !mt-0" text="Home" />
|
|
|
|
<EditButton />
|
|
|
|
</Row>
|
|
|
|
<DailyProfitAndBalance className="" user={user} />
|
2022-08-31 04:45:55 +00:00
|
|
|
</Row>
|
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
{sections.map((item) => {
|
2022-09-08 06:36:34 +00:00
|
|
|
const { id } = item
|
2022-09-12 05:39:04 +00:00
|
|
|
if (id === 'daily-movers') {
|
|
|
|
return <DailyMoversSection key={id} userId={user?.id} />
|
2022-09-08 06:36:34 +00:00
|
|
|
}
|
|
|
|
const sort = SORTS.find((sort) => sort.value === id)
|
|
|
|
if (sort)
|
|
|
|
return (
|
|
|
|
<SearchSection
|
|
|
|
key={id}
|
2022-09-12 05:39:04 +00:00
|
|
|
label={sort.value === 'newest' ? 'New for you' : sort.label}
|
2022-09-08 06:36:34 +00:00
|
|
|
sort={sort.value}
|
2022-09-12 05:39:04 +00:00
|
|
|
followed={sort.value === 'newest'}
|
2022-09-08 06:36:34 +00:00
|
|
|
user={user}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
const group = groups.find((g) => g.id === id)
|
|
|
|
if (group) return <GroupSection key={id} group={group} user={user} />
|
|
|
|
|
|
|
|
return null
|
|
|
|
})}
|
2022-08-31 04:45:55 +00:00
|
|
|
</Col>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="fixed bottom-[70px] right-3 z-20 inline-flex items-center rounded-full border border-transparent bg-indigo-600 p-3 text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 lg:hidden"
|
|
|
|
onClick={() => {
|
|
|
|
Router.push('/create')
|
|
|
|
track('mobile create button')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PlusSmIcon className="h-8 w-8" aria-hidden="true" />
|
|
|
|
</button>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function SearchSection(props: {
|
|
|
|
label: string
|
2022-09-12 05:39:04 +00:00
|
|
|
user: User | null | undefined | undefined
|
2022-08-31 04:45:55 +00:00
|
|
|
sort: Sort
|
2022-08-31 05:30:31 +00:00
|
|
|
yourBets?: boolean
|
2022-09-12 05:39:04 +00:00
|
|
|
followed?: boolean
|
2022-08-31 04:45:55 +00:00
|
|
|
}) {
|
2022-09-12 05:39:04 +00:00
|
|
|
const { label, user, sort, yourBets, followed } = props
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col>
|
2022-09-13 22:47:29 +00:00
|
|
|
<SectionHeader label={label} href={`/home?s=${sort}`} />
|
2022-08-31 04:45:55 +00:00
|
|
|
<ContractSearch
|
|
|
|
user={user}
|
|
|
|
defaultSort={sort}
|
2022-09-12 05:39:04 +00:00
|
|
|
additionalFilter={
|
|
|
|
yourBets
|
|
|
|
? { yourBets: true }
|
|
|
|
: followed
|
|
|
|
? { followed: true }
|
|
|
|
: undefined
|
|
|
|
}
|
2022-08-31 04:45:55 +00:00
|
|
|
noControls
|
2022-09-08 06:36:34 +00:00
|
|
|
maxResults={6}
|
|
|
|
persistPrefix={`experimental-home-${sort}`}
|
2022-08-31 04:45:55 +00:00
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
function GroupSection(props: {
|
|
|
|
group: Group
|
|
|
|
user: User | null | undefined | undefined
|
|
|
|
}) {
|
2022-08-31 04:45:55 +00:00
|
|
|
const { group, user } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col>
|
2022-09-13 22:47:29 +00:00
|
|
|
<SectionHeader label={group.name} href={groupPath(group.slug)} />
|
2022-08-31 04:45:55 +00:00
|
|
|
<ContractSearch
|
|
|
|
user={user}
|
|
|
|
defaultSort={'score'}
|
|
|
|
additionalFilter={{ groupSlug: group.slug }}
|
|
|
|
noControls
|
2022-09-08 06:36:34 +00:00
|
|
|
maxResults={6}
|
|
|
|
persistPrefix={`experimental-home-${group.slug}`}
|
2022-08-31 04:45:55 +00:00
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
function DailyMoversSection(props: { userId: string | null | undefined }) {
|
|
|
|
const { userId } = props
|
|
|
|
const changes = useProbChanges(userId ?? '')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className="gap-2">
|
2022-09-13 22:47:29 +00:00
|
|
|
<SectionHeader label="Daily movers" href="daily-movers" />
|
|
|
|
<ProbChangeTable changes={changes} />
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function SectionHeader(props: { label: string; href: string }) {
|
|
|
|
const { label, href } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row className="mb-3 items-center justify-between">
|
|
|
|
<SiteLink className="text-xl" href={href}>
|
|
|
|
{label}{' '}
|
2022-09-12 05:39:04 +00:00
|
|
|
<ArrowSmRightIcon
|
|
|
|
className="mb-0.5 inline h-6 w-6 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</SiteLink>
|
2022-09-13 22:47:29 +00:00
|
|
|
</Row>
|
2022-09-12 05:39:04 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-08 06:36:34 +00:00
|
|
|
function EditButton(props: { className?: string }) {
|
|
|
|
const { className } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SiteLink href="/experimental/home/edit">
|
2022-09-13 22:47:29 +00:00
|
|
|
<Button size="sm" color="gray-white" className={clsx(className, 'flex')}>
|
|
|
|
<AdjustmentsIcon className={clsx('h-[24px] w-5')} aria-hidden="true" />
|
2022-09-08 06:36:34 +00:00
|
|
|
</Button>
|
|
|
|
</SiteLink>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function DailyProfitAndBalance(props: {
|
2022-09-13 22:47:29 +00:00
|
|
|
user: User | null | undefined
|
2022-08-31 04:45:55 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-09-13 22:47:29 +00:00
|
|
|
const { user, className } = props
|
|
|
|
const metrics = usePortfolioHistory(user?.id ?? '', 'daily') ?? []
|
2022-09-08 06:36:34 +00:00
|
|
|
const [first, last] = [metrics[0], metrics[metrics.length - 1]]
|
|
|
|
|
|
|
|
if (first === undefined || last === undefined) return null
|
|
|
|
|
|
|
|
const profit =
|
|
|
|
calculatePortfolioProfit(last) - calculatePortfolioProfit(first)
|
2022-09-13 22:47:29 +00:00
|
|
|
const profitPercent = profit / first.investmentValue
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-13 22:47:29 +00:00
|
|
|
<Row className={'gap-4'}>
|
|
|
|
<Col>
|
|
|
|
<div className="text-gray-500">Daily profit</div>
|
|
|
|
<Row className={clsx(className, 'items-center text-lg')}>
|
|
|
|
<span>{formatMoney(profit)}</span>{' '}
|
|
|
|
<ProfitBadge profitPercent={profitPercent * 100} />
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<div className="text-gray-500">Streak</div>
|
|
|
|
<Row className={clsx(className, 'items-center text-lg')}>
|
|
|
|
<span>🔥 {user?.currentBettingStreak ?? 0}</span>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-08-31 04:45:55 +00:00
|
|
|
)
|
|
|
|
}
|