2022-10-13 02:15:40 +00:00
|
|
|
import React, { ReactNode, useEffect } from 'react'
|
2022-09-16 21:12:24 +00:00
|
|
|
import Router from 'next/router'
|
|
|
|
import {
|
|
|
|
AdjustmentsIcon,
|
|
|
|
PencilAltIcon,
|
|
|
|
ArrowSmRightIcon,
|
|
|
|
} from '@heroicons/react/solid'
|
2022-09-18 00:25:17 +00:00
|
|
|
import { PlusCircleIcon, XCircleIcon } from '@heroicons/react/outline'
|
2022-09-16 21:12:24 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import { toast, Toaster } from 'react-hot-toast'
|
2022-09-30 17:00:14 +00:00
|
|
|
import { Dictionary, sortBy, sum } from 'lodash'
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
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'
|
2022-10-11 05:32:55 +00:00
|
|
|
import {
|
|
|
|
usePrivateUser,
|
|
|
|
useUser,
|
|
|
|
useUserContractMetricsByProfit,
|
|
|
|
} from 'web/hooks/use-user'
|
2022-09-16 21:12:24 +00:00
|
|
|
import {
|
2022-09-17 23:30:27 +00:00
|
|
|
useMemberGroupsSubscription,
|
2022-09-16 21:12:24 +00:00
|
|
|
useTrendingGroups,
|
|
|
|
} from 'web/hooks/use-group'
|
|
|
|
import { Button } from 'web/components/button'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
2022-10-11 05:32:55 +00:00
|
|
|
import { ProfitChangeTable } from 'web/components/contract/prob-change-table'
|
2022-09-17 23:30:27 +00:00
|
|
|
import { groupPath, joinGroup, leaveGroup } from 'web/lib/firebase/groups'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { usePortfolioHistory } from 'web/hooks/use-portfolio-history'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-10-11 05:32:55 +00:00
|
|
|
import { ContractMetrics } from 'common/calculate-metrics'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { hasCompletedStreakToday } from 'web/components/profile/betting-streak-modal'
|
|
|
|
import { ContractsGrid } from 'web/components/contract/contracts-grid'
|
|
|
|
import { PillButton } from 'web/components/buttons/pill-button'
|
|
|
|
import { filterDefined } from 'common/util/array'
|
|
|
|
import { updateUser } from 'web/lib/firebase/users'
|
|
|
|
import { isArray, keyBy } from 'lodash'
|
|
|
|
import { usePrefetch } from 'web/hooks/use-prefetch'
|
2022-10-12 14:04:39 +00:00
|
|
|
import { Contract, CPMMBinaryContract } from 'common/contract'
|
2022-10-01 01:07:49 +00:00
|
|
|
import {
|
|
|
|
useContractsByDailyScoreNotBetOn,
|
|
|
|
useContractsByDailyScoreGroups,
|
|
|
|
useTrendingContracts,
|
|
|
|
useNewContracts,
|
|
|
|
} from 'web/hooks/use-contracts'
|
2022-09-27 17:09:54 +00:00
|
|
|
import { ProfitBadge } from 'web/components/profit-badge'
|
2022-09-23 20:43:23 +00:00
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
2022-10-10 02:37:24 +00:00
|
|
|
import { Input } from 'web/components/input'
|
2022-10-12 14:04:39 +00:00
|
|
|
import { PinnedItems } from 'web/components/groups/group-overview'
|
|
|
|
import { updateGlobalConfig } from 'web/lib/firebase/globalConfig'
|
|
|
|
import { getPost } from 'web/lib/firebase/posts'
|
|
|
|
import { PostCard } from 'web/components/post-card'
|
|
|
|
import { getContractFromId } from 'web/lib/firebase/contracts'
|
|
|
|
import { ContractCard } from 'web/components/contract/contract-card'
|
|
|
|
import { Post } from 'common/post'
|
|
|
|
import { useAllPosts } from 'web/hooks/use-post'
|
|
|
|
import { useGlobalConfig } from 'web/hooks/use-global-config'
|
2022-10-12 14:38:06 +00:00
|
|
|
import { useAdmin } from 'web/hooks/use-admin'
|
2022-10-12 15:02:57 +00:00
|
|
|
import { GlobalConfig } from 'common/globalConfig'
|
2022-10-13 02:15:40 +00:00
|
|
|
import {
|
|
|
|
inMemoryStore,
|
|
|
|
usePersistentState,
|
|
|
|
} from 'web/hooks/use-persistent-state'
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
export default function Home() {
|
|
|
|
const user = useUser()
|
2022-10-12 14:38:06 +00:00
|
|
|
const isAdmin = useAdmin()
|
2022-10-12 15:02:57 +00:00
|
|
|
const globalConfig = useGlobalConfig()
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
useTracking('view home')
|
|
|
|
|
|
|
|
useSaveReferral()
|
|
|
|
usePrefetch(user?.id)
|
|
|
|
|
2022-09-26 21:04:08 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (user === null) {
|
|
|
|
// Go to landing page if not logged in.
|
|
|
|
Router.push('/')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-09-30 17:00:14 +00:00
|
|
|
const { sections } = getHomeItems(user?.homeSections ?? [])
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-09-18 04:58:18 +00:00
|
|
|
useEffect(() => {
|
2022-09-30 17:00:14 +00:00
|
|
|
if (user && !user.homeSections && sections.length > 0) {
|
2022-09-18 04:58:18 +00:00
|
|
|
// Save initial home sections.
|
|
|
|
updateUser(user.id, { homeSections: sections.map((s) => s.id) })
|
|
|
|
}
|
2022-09-30 17:00:14 +00:00
|
|
|
}, [user, sections])
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
const contractMetricsByProfit = useUserContractMetricsByProfit(
|
2022-10-13 18:49:43 +00:00
|
|
|
user?.id ?? '_'
|
2022-10-11 05:32:55 +00:00
|
|
|
)
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
const trendingContracts = useTrendingContracts(6)
|
|
|
|
const newContracts = useNewContracts(6)
|
|
|
|
const dailyTrendingContracts = useContractsByDailyScoreNotBetOn(user?.id, 6)
|
2022-09-18 04:58:18 +00:00
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
const groups = useMemberGroupsSubscription(user)
|
2022-10-05 05:54:38 +00:00
|
|
|
const trendingGroups = useTrendingGroups()
|
2022-09-22 21:57:48 +00:00
|
|
|
const groupContracts = useContractsByDailyScoreGroups(
|
|
|
|
groups?.map((g) => g.slug)
|
|
|
|
)
|
|
|
|
|
2022-10-13 02:15:40 +00:00
|
|
|
const [pinned, setPinned] = usePersistentState<JSX.Element[] | null>(null, {
|
|
|
|
store: inMemoryStore(),
|
|
|
|
key: 'home-pinned',
|
|
|
|
})
|
2022-10-12 15:02:57 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const pinnedItems = globalConfig?.pinnedItems
|
|
|
|
|
|
|
|
async function getPinned() {
|
|
|
|
if (pinnedItems == null) {
|
|
|
|
if (globalConfig != null) {
|
|
|
|
updateGlobalConfig(globalConfig, { pinnedItems: [] })
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const itemComponents = await Promise.all(
|
|
|
|
pinnedItems.map(async (element) => {
|
|
|
|
if (element.type === 'post') {
|
|
|
|
const post = await getPost(element.itemId)
|
|
|
|
if (post) {
|
|
|
|
return <PostCard post={post as Post} />
|
|
|
|
}
|
|
|
|
} else if (element.type === 'contract') {
|
|
|
|
const contract = await getContractFromId(element.itemId)
|
|
|
|
if (contract) {
|
|
|
|
return <ContractCard contract={contract as Contract} />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
setPinned(
|
|
|
|
itemComponents.filter(
|
|
|
|
(element) => element != undefined
|
|
|
|
) as JSX.Element[]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getPinned()
|
2022-10-13 02:15:40 +00:00
|
|
|
}, [globalConfig, setPinned])
|
2022-10-12 15:02:57 +00:00
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
const isLoading =
|
2022-10-02 19:59:02 +00:00
|
|
|
!user ||
|
2022-10-11 05:32:55 +00:00
|
|
|
!contractMetricsByProfit ||
|
2022-10-02 19:59:02 +00:00
|
|
|
!trendingContracts ||
|
|
|
|
!newContracts ||
|
2022-10-12 15:02:57 +00:00
|
|
|
!dailyTrendingContracts ||
|
|
|
|
!globalConfig ||
|
|
|
|
!pinned
|
2022-10-01 01:07:49 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<Toaster />
|
|
|
|
|
2022-10-02 19:41:44 +00:00
|
|
|
<Col className="pm:mx-10 gap-4 px-4 pb-8 pt-4 sm:pt-0">
|
2022-10-02 22:04:04 +00:00
|
|
|
<Row
|
|
|
|
className={'mb-2 w-full items-center justify-between gap-4 sm:gap-8'}
|
|
|
|
>
|
2022-10-10 02:37:24 +00:00
|
|
|
<Input
|
2022-10-02 19:41:44 +00:00
|
|
|
type="text"
|
|
|
|
placeholder={'Search'}
|
2022-10-10 02:37:24 +00:00
|
|
|
className="w-full"
|
2022-10-02 19:41:44 +00:00
|
|
|
onClick={() => Router.push('/search')}
|
|
|
|
/>
|
|
|
|
<CustomizeButton justIcon />
|
2022-09-16 22:09:12 +00:00
|
|
|
<DailyStats user={user} />
|
2022-09-16 21:12:24 +00:00
|
|
|
</Row>
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
{isLoading ? (
|
2022-09-23 20:43:23 +00:00
|
|
|
<LoadingIndicator />
|
|
|
|
) : (
|
|
|
|
<>
|
2022-10-12 14:38:06 +00:00
|
|
|
{renderSections(
|
|
|
|
sections,
|
|
|
|
{
|
|
|
|
score: trendingContracts,
|
|
|
|
newest: newContracts,
|
|
|
|
'daily-trending': dailyTrendingContracts,
|
|
|
|
'daily-movers': contractMetricsByProfit,
|
|
|
|
},
|
2022-10-12 15:02:57 +00:00
|
|
|
isAdmin,
|
|
|
|
globalConfig,
|
|
|
|
pinned
|
2022-10-12 14:38:06 +00:00
|
|
|
)}
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-10-05 05:54:38 +00:00
|
|
|
{groups && groupContracts && trendingGroups.length > 0 ? (
|
|
|
|
<>
|
|
|
|
<TrendingGroupsSection
|
|
|
|
className="mb-4"
|
|
|
|
user={user}
|
|
|
|
myGroups={groups}
|
|
|
|
trendingGroups={trendingGroups}
|
|
|
|
/>
|
|
|
|
{renderGroupSections(user, groups, groupContracts)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator />
|
|
|
|
)}
|
2022-09-23 20:43:23 +00:00
|
|
|
</>
|
|
|
|
)}
|
2022-09-16 21:12:24 +00:00
|
|
|
</Col>
|
2022-09-30 17:00:14 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="fixed bottom-[70px] right-3 z-20 inline-flex items-center rounded-full border border-transparent bg-indigo-600 p-4 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')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PencilAltIcon className="h-7 w-7" aria-hidden="true" />
|
|
|
|
</button>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const HOME_SECTIONS = [
|
2022-10-12 14:04:39 +00:00
|
|
|
{ label: 'Featured', id: 'featured' },
|
2022-09-23 19:33:48 +00:00
|
|
|
{ label: 'Daily trending', id: 'daily-trending' },
|
2022-10-01 01:07:49 +00:00
|
|
|
{ label: 'Daily movers', id: 'daily-movers' },
|
2022-09-16 21:12:24 +00:00
|
|
|
{ label: 'Trending', id: 'score' },
|
2022-09-17 20:15:37 +00:00
|
|
|
{ label: 'New', id: 'newest' },
|
2022-10-12 14:04:39 +00:00
|
|
|
] as const
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-09-30 17:00:14 +00:00
|
|
|
export const getHomeItems = (sections: string[]) => {
|
2022-09-16 21:12:24 +00:00
|
|
|
// Accommodate old home sections.
|
|
|
|
if (!isArray(sections)) sections = []
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
const itemsById = keyBy(HOME_SECTIONS, 'id')
|
2022-09-16 21:12:24 +00:00
|
|
|
const sectionItems = filterDefined(sections.map((id) => itemsById[id]))
|
|
|
|
|
2022-09-23 20:39:17 +00:00
|
|
|
// Add new home section items to the top.
|
|
|
|
sectionItems.unshift(
|
|
|
|
...HOME_SECTIONS.filter((item) => !sectionItems.includes(item))
|
|
|
|
)
|
2022-09-16 21:12:24 +00:00
|
|
|
// Add unmentioned items to the end.
|
2022-10-01 01:07:49 +00:00
|
|
|
sectionItems.push(
|
|
|
|
...HOME_SECTIONS.filter((item) => !sectionItems.includes(item))
|
|
|
|
)
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
sections: sectionItems,
|
|
|
|
itemsById,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
function renderSections(
|
|
|
|
sections: { id: string; label: string }[],
|
|
|
|
sectionContracts: {
|
2022-10-11 05:32:55 +00:00
|
|
|
'daily-movers': {
|
|
|
|
contracts: CPMMBinaryContract[]
|
|
|
|
metrics: ContractMetrics[]
|
|
|
|
}
|
2022-10-01 01:07:49 +00:00
|
|
|
'daily-trending': CPMMBinaryContract[]
|
|
|
|
newest: CPMMBinaryContract[]
|
|
|
|
score: CPMMBinaryContract[]
|
2022-10-12 14:38:06 +00:00
|
|
|
},
|
2022-10-12 15:02:57 +00:00
|
|
|
isAdmin: boolean,
|
|
|
|
globalConfig: GlobalConfig,
|
|
|
|
pinned: JSX.Element[]
|
2022-10-01 01:07:49 +00:00
|
|
|
) {
|
2022-10-12 14:04:39 +00:00
|
|
|
type sectionTypes = typeof HOME_SECTIONS[number]['id']
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{sections.map((s) => {
|
2022-10-11 05:32:55 +00:00
|
|
|
const { id, label } = s as {
|
2022-10-12 14:04:39 +00:00
|
|
|
id: sectionTypes
|
2022-10-11 05:32:55 +00:00
|
|
|
label: string
|
|
|
|
}
|
2022-10-01 01:07:49 +00:00
|
|
|
if (id === 'daily-movers') {
|
2022-10-11 05:32:55 +00:00
|
|
|
return <DailyMoversSection key={id} {...sectionContracts[id]} />
|
2022-10-01 01:07:49 +00:00
|
|
|
}
|
2022-10-11 05:32:55 +00:00
|
|
|
|
2022-10-12 14:04:39 +00:00
|
|
|
if (id === 'featured') {
|
2022-10-12 16:44:43 +00:00
|
|
|
return (
|
|
|
|
<FeaturedSection
|
2022-10-12 22:29:56 +00:00
|
|
|
key={id}
|
2022-10-12 16:44:43 +00:00
|
|
|
globalConfig={globalConfig}
|
|
|
|
pinned={pinned}
|
|
|
|
isAdmin={isAdmin}
|
|
|
|
/>
|
|
|
|
)
|
2022-10-12 14:04:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
const contracts = sectionContracts[id]
|
|
|
|
|
2022-10-01 01:07:49 +00:00
|
|
|
if (id === 'daily-trending') {
|
|
|
|
return (
|
2022-10-05 05:54:38 +00:00
|
|
|
<SearchSection
|
2022-10-01 01:07:49 +00:00
|
|
|
key={id}
|
|
|
|
label={label}
|
2022-10-02 19:59:02 +00:00
|
|
|
contracts={contracts}
|
2022-10-01 01:07:49 +00:00
|
|
|
sort="daily-score"
|
|
|
|
showProbChange
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return (
|
2022-10-05 05:54:38 +00:00
|
|
|
<SearchSection
|
2022-10-01 01:07:49 +00:00
|
|
|
key={id}
|
|
|
|
label={label}
|
|
|
|
contracts={contracts}
|
2022-10-05 05:54:38 +00:00
|
|
|
sort={id as Sort}
|
2022-10-01 01:07:49 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
)
|
2022-09-30 17:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderGroupSections(
|
|
|
|
user: User,
|
2022-10-05 05:54:38 +00:00
|
|
|
groups: Group[],
|
|
|
|
groupContracts: Dictionary<CPMMBinaryContract[]>
|
2022-09-30 17:00:14 +00:00
|
|
|
) {
|
|
|
|
const filteredGroups = groups.filter((g) => groupContracts[g.slug])
|
|
|
|
const orderedGroups = sortBy(filteredGroups, (g) =>
|
|
|
|
// Sort by sum of top two daily scores.
|
|
|
|
sum(
|
|
|
|
sortBy(groupContracts[g.slug].map((c) => c.dailyScore))
|
|
|
|
.reverse()
|
|
|
|
.slice(0, 2)
|
|
|
|
)
|
|
|
|
).reverse()
|
|
|
|
|
2022-10-03 23:28:41 +00:00
|
|
|
const previouslySeenContracts = new Set<string>()
|
|
|
|
|
2022-09-30 17:00:14 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{orderedGroups.map((group) => {
|
|
|
|
const contracts = groupContracts[group.slug].filter(
|
2022-10-03 23:28:41 +00:00
|
|
|
(c) =>
|
|
|
|
Math.abs(c.probChanges.day) >= 0.01 &&
|
|
|
|
!previouslySeenContracts.has(c.id)
|
2022-09-30 17:00:14 +00:00
|
|
|
)
|
|
|
|
if (contracts.length === 0) return null
|
|
|
|
|
2022-10-03 23:28:41 +00:00
|
|
|
contracts.forEach((c) => previouslySeenContracts.add(c.id))
|
|
|
|
|
2022-09-30 17:00:14 +00:00
|
|
|
return (
|
|
|
|
<GroupSection
|
|
|
|
key={group.id}
|
|
|
|
group={group}
|
|
|
|
user={user}
|
|
|
|
contracts={contracts}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
)
|
2022-09-16 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function SectionHeader(props: {
|
|
|
|
label: string
|
|
|
|
href: string
|
|
|
|
children?: ReactNode
|
|
|
|
}) {
|
|
|
|
const { label, href, children } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row className="mb-3 items-center justify-between">
|
2022-09-16 21:21:13 +00:00
|
|
|
<SiteLink
|
|
|
|
className="text-xl"
|
|
|
|
href={href}
|
|
|
|
onClick={() => track('home click section header', { section: href })}
|
|
|
|
>
|
2022-09-16 21:12:24 +00:00
|
|
|
{label}{' '}
|
|
|
|
<ArrowSmRightIcon
|
|
|
|
className="mb-0.5 inline h-6 w-6 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</SiteLink>
|
|
|
|
{children}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:54:38 +00:00
|
|
|
function SearchSection(props: {
|
2022-09-16 21:12:24 +00:00
|
|
|
label: string
|
2022-10-01 01:07:49 +00:00
|
|
|
contracts: CPMMBinaryContract[]
|
2022-09-16 21:12:24 +00:00
|
|
|
sort: Sort
|
|
|
|
pill?: string
|
2022-10-01 01:07:49 +00:00
|
|
|
showProbChange?: boolean
|
2022-09-16 21:12:24 +00:00
|
|
|
}) {
|
2022-10-01 01:07:49 +00:00
|
|
|
const { label, contracts, sort, pill, showProbChange } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col>
|
|
|
|
<SectionHeader
|
|
|
|
label={label}
|
|
|
|
href={`/search?s=${sort}${pill ? `&p=${pill}` : ''}`}
|
|
|
|
/>
|
2022-10-01 01:07:49 +00:00
|
|
|
<ContractsGrid contracts={contracts} cardUIOptions={{ showProbChange }} />
|
2022-09-16 21:12:24 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:02:57 +00:00
|
|
|
function FeaturedSection(props: {
|
|
|
|
globalConfig: GlobalConfig
|
|
|
|
pinned: JSX.Element[]
|
2022-10-12 16:44:43 +00:00
|
|
|
isAdmin: boolean
|
2022-10-12 15:02:57 +00:00
|
|
|
}) {
|
2022-10-12 16:44:43 +00:00
|
|
|
const { globalConfig, pinned, isAdmin } = props
|
2022-10-12 14:04:39 +00:00
|
|
|
const posts = useAllPosts()
|
|
|
|
|
|
|
|
async function onSubmit(selectedItems: { itemId: string; type: string }[]) {
|
|
|
|
if (globalConfig == null) return
|
|
|
|
await updateGlobalConfig(globalConfig, {
|
|
|
|
pinnedItems: [
|
|
|
|
...(globalConfig?.pinnedItems ?? []),
|
|
|
|
...(selectedItems as { itemId: string; type: 'contract' | 'post' }[]),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function onDeleteClicked(index: number) {
|
|
|
|
if (globalConfig == null) return
|
|
|
|
const newPinned = globalConfig.pinnedItems.filter((item) => {
|
|
|
|
return item.itemId !== globalConfig.pinnedItems[index].itemId
|
|
|
|
})
|
|
|
|
updateGlobalConfig(globalConfig, { pinnedItems: newPinned })
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col>
|
|
|
|
<PinnedItems
|
|
|
|
posts={posts}
|
2022-10-12 16:44:43 +00:00
|
|
|
isEditable={isAdmin}
|
2022-10-12 14:04:39 +00:00
|
|
|
pinned={pinned}
|
|
|
|
onDeleteClicked={onDeleteClicked}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
modalMessage={'Pin posts or markets to the overview of this group.'}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
function GroupSection(props: {
|
|
|
|
group: Group
|
2022-09-23 20:43:23 +00:00
|
|
|
user: User
|
2022-09-22 21:57:48 +00:00
|
|
|
contracts: CPMMBinaryContract[]
|
2022-09-16 21:12:24 +00:00
|
|
|
}) {
|
2022-09-22 21:57:48 +00:00
|
|
|
const { group, user, contracts } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col>
|
|
|
|
<SectionHeader label={group.name} href={groupPath(group.slug)}>
|
|
|
|
<Button
|
|
|
|
color="gray-white"
|
|
|
|
onClick={() => {
|
2022-09-23 20:43:23 +00:00
|
|
|
const homeSections = (user.homeSections ?? []).filter(
|
|
|
|
(id) => id !== group.id
|
|
|
|
)
|
|
|
|
updateUser(user.id, { homeSections })
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-09-23 20:43:23 +00:00
|
|
|
toast.promise(leaveGroup(group, user.id), {
|
|
|
|
loading: 'Unfollowing group...',
|
|
|
|
success: `Unfollowed ${group.name}`,
|
|
|
|
error: "Couldn't unfollow group, try again?",
|
|
|
|
})
|
2022-09-16 21:12:24 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-09-18 00:25:17 +00:00
|
|
|
<XCircleIcon className={'h-5 w-5 flex-shrink-0'} aria-hidden="true" />
|
2022-09-16 21:12:24 +00:00
|
|
|
</Button>
|
|
|
|
</SectionHeader>
|
2022-09-22 21:57:48 +00:00
|
|
|
<ContractsGrid
|
|
|
|
contracts={contracts.slice(0, 4)}
|
|
|
|
cardUIOptions={{ showProbChange: true }}
|
|
|
|
/>
|
2022-09-16 21:12:24 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
function DailyMoversSection(props: {
|
|
|
|
contracts: CPMMBinaryContract[]
|
|
|
|
metrics: ContractMetrics[]
|
|
|
|
}) {
|
|
|
|
const { contracts, metrics } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
if (contracts.length === 0) {
|
2022-09-22 21:57:48 +00:00
|
|
|
return null
|
2022-09-18 00:47:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
return (
|
|
|
|
<Col className="gap-2">
|
|
|
|
<SectionHeader label="Daily movers" href="/daily-movers" />
|
2022-10-13 18:49:43 +00:00
|
|
|
<ProfitChangeTable contracts={contracts} metrics={metrics} maxRows={3} />
|
2022-09-16 21:12:24 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
function DailyStats(props: { user: User | null | undefined }) {
|
|
|
|
const { user } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
const privateUser = usePrivateUser()
|
2022-09-18 21:05:13 +00:00
|
|
|
const streaks = privateUser?.notificationPreferences?.betting_streaks ?? []
|
|
|
|
const streaksHidden = streaks.length === 0
|
2022-09-16 21:12:24 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-02 19:41:44 +00:00
|
|
|
<Row className={'flex-shrink-0 gap-4'}>
|
2022-10-11 05:32:55 +00:00
|
|
|
<DailyProfit user={user} />
|
2022-09-16 21:12:24 +00:00
|
|
|
{!streaksHidden && (
|
|
|
|
<Col>
|
|
|
|
<div className="text-gray-500">Streak</div>
|
|
|
|
<Row
|
|
|
|
className={clsx(
|
|
|
|
'items-center text-lg',
|
|
|
|
user && !hasCompletedStreakToday(user) && 'grayscale'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<span>🔥 {user?.currentBettingStreak ?? 0}</span>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
export function DailyProfit(props: { user: User | null | undefined }) {
|
|
|
|
const { user } = props
|
|
|
|
|
|
|
|
const contractMetricsByProfit = useUserContractMetricsByProfit(
|
2022-10-13 18:49:43 +00:00
|
|
|
user?.id ?? '_'
|
2022-10-11 05:32:55 +00:00
|
|
|
)
|
|
|
|
const profit = sum(
|
|
|
|
contractMetricsByProfit?.metrics.map((m) =>
|
|
|
|
m.from ? m.from.day.profit : 0
|
|
|
|
) ?? []
|
|
|
|
)
|
|
|
|
|
|
|
|
const metrics = usePortfolioHistory(user?.id ?? '', 'daily') ?? []
|
|
|
|
const [first, last] = [metrics[0], metrics[metrics.length - 1]]
|
|
|
|
|
|
|
|
let profitPercent = 0
|
|
|
|
if (first && last) {
|
|
|
|
// profit = calculatePortfolioProfit(last) - calculatePortfolioProfit(first)
|
|
|
|
profitPercent = profit / first.investmentValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SiteLink className="flex flex-col" href="/daily-movers">
|
|
|
|
<div className="text-gray-500">Daily profit</div>
|
|
|
|
<Row className="items-center text-lg">
|
|
|
|
<span>{formatMoney(profit)}</span>{' '}
|
|
|
|
<ProfitBadge profitPercent={profitPercent * 100} />
|
|
|
|
</Row>
|
|
|
|
</SiteLink>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-17 23:30:27 +00:00
|
|
|
export function TrendingGroupsSection(props: {
|
2022-10-05 05:54:38 +00:00
|
|
|
user: User
|
|
|
|
myGroups: Group[]
|
|
|
|
trendingGroups: Group[]
|
2022-09-17 23:30:27 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-10-05 05:54:38 +00:00
|
|
|
const { user, myGroups, trendingGroups, className } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
|
2022-10-05 05:54:38 +00:00
|
|
|
const myGroupIds = new Set(myGroups.map((g) => g.id))
|
|
|
|
|
|
|
|
const groups = trendingGroups.filter((g) => !myGroupIds.has(g.id))
|
2022-10-03 23:22:18 +00:00
|
|
|
const count = 20
|
2022-09-16 21:12:24 +00:00
|
|
|
const chosenGroups = groups.slice(0, count)
|
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
if (chosenGroups.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
return (
|
2022-09-17 23:30:27 +00:00
|
|
|
<Col className={className}>
|
2022-09-30 17:00:14 +00:00
|
|
|
<SectionHeader label="Trending groups" href="/explore-groups" />
|
2022-10-03 23:22:18 +00:00
|
|
|
<div className="mb-4 text-gray-500">
|
|
|
|
Follow groups you are interested in.
|
|
|
|
</div>
|
2022-09-16 21:12:24 +00:00
|
|
|
<Row className="flex-wrap gap-2">
|
|
|
|
{chosenGroups.map((g) => (
|
|
|
|
<PillButton
|
2022-09-18 00:25:17 +00:00
|
|
|
className="flex flex-row items-center gap-1"
|
2022-09-16 21:12:24 +00:00
|
|
|
key={g.id}
|
2022-10-05 05:54:38 +00:00
|
|
|
selected={myGroupIds.has(g.id)}
|
2022-09-16 21:12:24 +00:00
|
|
|
onSelect={() => {
|
2022-10-05 05:54:38 +00:00
|
|
|
if (myGroupIds.has(g.id)) leaveGroup(g, user.id)
|
2022-09-16 21:12:24 +00:00
|
|
|
else {
|
|
|
|
const homeSections = (user.homeSections ?? [])
|
|
|
|
.filter((id) => id !== g.id)
|
|
|
|
.concat(g.id)
|
|
|
|
updateUser(user.id, { homeSections })
|
|
|
|
|
|
|
|
toast.promise(joinGroup(g, user.id), {
|
|
|
|
loading: 'Following group...',
|
|
|
|
success: `Followed ${g.name}`,
|
|
|
|
error: "Couldn't follow group, try again?",
|
|
|
|
})
|
2022-09-16 21:21:13 +00:00
|
|
|
|
|
|
|
track('home follow group', { group: g.slug })
|
2022-09-16 21:12:24 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2022-09-18 00:25:17 +00:00
|
|
|
<PlusCircleIcon
|
|
|
|
className={'h-5 w-5 flex-shrink-0 text-gray-500'}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
{g.name}
|
|
|
|
</PillButton>
|
|
|
|
))}
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-17 23:30:27 +00:00
|
|
|
function CustomizeButton(props: { justIcon?: boolean; className?: string }) {
|
|
|
|
const { justIcon, className } = props
|
2022-09-16 21:12:24 +00:00
|
|
|
return (
|
|
|
|
<SiteLink
|
2022-09-17 23:30:27 +00:00
|
|
|
className={clsx(
|
|
|
|
className,
|
|
|
|
'flex flex-row items-center text-xl hover:no-underline'
|
|
|
|
)}
|
2022-09-16 21:12:24 +00:00
|
|
|
href="/home/edit"
|
|
|
|
>
|
|
|
|
<Button size="lg" color="gray" className={clsx('flex gap-2')}>
|
|
|
|
<AdjustmentsIcon
|
|
|
|
className={clsx('h-[24px] w-5 text-gray-500')}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-09-17 23:30:27 +00:00
|
|
|
{!justIcon && 'Customize'}
|
2022-09-16 21:12:24 +00:00
|
|
|
</Button>
|
|
|
|
</SiteLink>
|
|
|
|
)
|
|
|
|
}
|