2022-08-28 21:26:29 +00:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { toast } from 'react-hot-toast'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-07-15 12:52:08 +00:00
|
|
|
import { Group, GROUP_CHAT_SLUG } from 'common/group'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Page } from 'web/components/page'
|
2022-07-14 15:09:12 +00:00
|
|
|
import { Contract, listContractsByGroupSlug } from 'web/lib/firebase/contracts'
|
2022-06-22 16:35:50 +00:00
|
|
|
import {
|
2022-07-22 22:28:53 +00:00
|
|
|
addContractToGroup,
|
2022-06-22 16:35:50 +00:00
|
|
|
getGroupBySlug,
|
2022-07-22 22:28:53 +00:00
|
|
|
groupPath,
|
2022-07-14 17:09:28 +00:00
|
|
|
joinGroup,
|
2022-09-12 19:54:11 +00:00
|
|
|
listMemberIds,
|
2022-07-22 22:28:53 +00:00
|
|
|
updateGroup,
|
2022-06-22 16:35:50 +00:00
|
|
|
} from 'web/lib/firebase/groups'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
2022-07-21 19:43:10 +00:00
|
|
|
import { firebaseLogin, getUser, User } from 'web/lib/firebase/users'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
2022-08-10 17:27:59 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-09-12 19:54:11 +00:00
|
|
|
import {
|
|
|
|
useGroup,
|
|
|
|
useGroupContractIds,
|
|
|
|
useMemberIds,
|
|
|
|
} from 'web/hooks/use-group'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Leaderboard } from 'web/components/leaderboard'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { EditGroupButton } from 'web/components/groups/edit-group-button'
|
|
|
|
import Custom404 from '../../404'
|
|
|
|
import { SEO } from 'web/components/SEO'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
|
|
|
import { Tabs } from 'web/components/layout/tabs'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import { Modal } from 'web/components/layout/modal'
|
|
|
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
2022-07-01 22:37:30 +00:00
|
|
|
import { ContractSearch } from 'web/components/contract-search'
|
2022-07-13 21:11:22 +00:00
|
|
|
import { JoinOrLeaveGroupButton } from 'web/components/groups/groups-button'
|
2022-07-20 16:45:53 +00:00
|
|
|
import { CopyLinkButton } from 'web/components/copy-link-button'
|
|
|
|
import { ENV_CONFIG } from 'common/envs/constants'
|
2022-07-21 19:43:10 +00:00
|
|
|
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
2022-07-26 01:27:43 +00:00
|
|
|
import { Button } from 'web/components/button'
|
2022-08-03 21:38:35 +00:00
|
|
|
import { listAllCommentsOnGroup } from 'web/lib/firebase/comments'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { GroupComment } from 'common/comment'
|
2022-08-22 05:37:26 +00:00
|
|
|
import { REFERRAL_AMOUNT } from 'common/economy'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { UserLink } from 'web/components/user-link'
|
2022-08-30 12:39:10 +00:00
|
|
|
import { GroupAboutPost } from 'web/components/groups/group-about-post'
|
|
|
|
import { getPost } from 'web/lib/firebase/posts'
|
|
|
|
import { Post } from 'common/post'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { usePost } from 'web/hooks/use-post'
|
2022-08-31 18:29:49 +00:00
|
|
|
import { useAdmin } from 'web/hooks/use-admin'
|
2022-09-05 21:51:09 +00:00
|
|
|
import { track } from '@amplitude/analytics-browser'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz(props: { params: { slugs: string[] } }) {
|
|
|
|
const { slugs } = props.params
|
|
|
|
|
|
|
|
const group = await getGroupBySlug(slugs[0])
|
2022-09-12 19:54:11 +00:00
|
|
|
const memberIds = group && (await listMemberIds(group))
|
2022-06-22 16:35:50 +00:00
|
|
|
const creatorPromise = group ? getUser(group.creatorId) : null
|
|
|
|
|
2022-07-13 22:20:56 +00:00
|
|
|
const contracts =
|
2022-07-14 15:09:12 +00:00
|
|
|
(group && (await listContractsByGroupSlug(group.slug))) ?? []
|
2022-09-01 15:11:14 +00:00
|
|
|
const now = Date.now()
|
|
|
|
const suggestedFilter =
|
|
|
|
contracts.filter((c) => (c.closeTime ?? 0) > now).length < 5
|
|
|
|
? 'all'
|
|
|
|
: 'open'
|
2022-08-30 12:39:10 +00:00
|
|
|
const aboutPost =
|
|
|
|
group && group.aboutPostId != null && (await getPost(group.aboutPostId))
|
2022-08-03 21:38:35 +00:00
|
|
|
const messages = group && (await listAllCommentsOnGroup(group.id))
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-09-12 19:54:11 +00:00
|
|
|
const cachedTopTraderIds =
|
|
|
|
(group && group.cachedLeaderboard?.topTraders) ?? []
|
|
|
|
const cachedTopCreatorIds =
|
|
|
|
(group && group.cachedLeaderboard?.topCreators) ?? []
|
|
|
|
const topTraders = await toTopUsers(cachedTopTraderIds)
|
|
|
|
|
|
|
|
const topCreators = await toTopUsers(cachedTopCreatorIds)
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const creator = await creatorPromise
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
group,
|
2022-09-12 19:54:11 +00:00
|
|
|
memberIds,
|
2022-06-22 16:35:50 +00:00
|
|
|
creator,
|
|
|
|
topTraders,
|
|
|
|
topCreators,
|
2022-08-03 21:38:35 +00:00
|
|
|
messages,
|
2022-08-30 12:39:10 +00:00
|
|
|
aboutPost,
|
2022-09-01 15:11:14 +00:00
|
|
|
suggestedFilter,
|
2022-06-22 16:35:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
return { paths: [], fallback: 'blocking' }
|
|
|
|
}
|
2022-07-06 23:24:53 +00:00
|
|
|
const groupSubpages = [
|
|
|
|
undefined,
|
2022-07-15 12:52:08 +00:00
|
|
|
GROUP_CHAT_SLUG,
|
2022-07-21 23:17:02 +00:00
|
|
|
'markets',
|
2022-07-20 16:15:55 +00:00
|
|
|
'leaderboards',
|
2022-07-06 23:24:53 +00:00
|
|
|
'about',
|
|
|
|
] as const
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export default function GroupPage(props: {
|
|
|
|
group: Group | null
|
2022-09-12 19:54:11 +00:00
|
|
|
memberIds: string[]
|
2022-06-22 16:35:50 +00:00
|
|
|
creator: User
|
2022-09-12 19:54:11 +00:00
|
|
|
topTraders: { user: User; score: number }[]
|
|
|
|
topCreators: { user: User; score: number }[]
|
2022-08-19 08:06:40 +00:00
|
|
|
messages: GroupComment[]
|
2022-08-30 12:39:10 +00:00
|
|
|
aboutPost: Post
|
2022-09-01 15:11:14 +00:00
|
|
|
suggestedFilter: 'open' | 'all'
|
2022-06-22 16:35:50 +00:00
|
|
|
}) {
|
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
group: null,
|
2022-09-12 19:54:11 +00:00
|
|
|
memberIds: [],
|
2022-06-22 16:35:50 +00:00
|
|
|
creator: null,
|
|
|
|
topTraders: [],
|
|
|
|
topCreators: [],
|
2022-08-03 21:38:35 +00:00
|
|
|
messages: [],
|
2022-09-01 15:11:14 +00:00
|
|
|
suggestedFilter: 'open',
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
2022-09-13 13:53:01 +00:00
|
|
|
const { creator, topTraders, topCreators, suggestedFilter } = props
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const { slugs } = router.query as { slugs: string[] }
|
2022-07-13 21:11:22 +00:00
|
|
|
const page = slugs?.[1] as typeof groupSubpages[number]
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const group = useGroup(props.group?.id) ?? props.group
|
2022-08-30 12:39:10 +00:00
|
|
|
const aboutPost = usePost(props.aboutPost?.id) ?? props.aboutPost
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
2022-08-31 18:29:49 +00:00
|
|
|
const isAdmin = useAdmin()
|
2022-09-12 19:54:11 +00:00
|
|
|
const memberIds = useMemberIds(group?.id ?? null) ?? props.memberIds
|
2022-07-21 19:43:10 +00:00
|
|
|
|
|
|
|
useSaveReferral(user, {
|
2022-08-04 21:27:02 +00:00
|
|
|
defaultReferrerUsername: creator.username,
|
2022-07-21 19:43:10 +00:00
|
|
|
groupId: group?.id,
|
|
|
|
})
|
2022-07-01 13:47:19 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
if (group === null || !groupSubpages.includes(page) || slugs[2]) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
|
|
|
const isCreator = user && group && user.id === group.creatorId
|
2022-09-12 19:54:11 +00:00
|
|
|
const isMember = user && memberIds.includes(user.id)
|
|
|
|
const maxLeaderboardSize = 50
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-07-06 23:24:53 +00:00
|
|
|
const leaderboard = (
|
2022-06-24 21:02:05 +00:00
|
|
|
<Col>
|
2022-09-12 19:54:11 +00:00
|
|
|
<div className="mt-4 flex flex-col gap-8 px-4 md:flex-row">
|
|
|
|
<GroupLeaderboard
|
|
|
|
topUsers={topTraders}
|
|
|
|
title="🏅 Top traders"
|
|
|
|
header="Profit"
|
|
|
|
maxToShow={maxLeaderboardSize}
|
|
|
|
/>
|
|
|
|
<GroupLeaderboard
|
|
|
|
topUsers={topCreators}
|
|
|
|
title="🏅 Top creators"
|
|
|
|
header="Market volume"
|
|
|
|
maxToShow={maxLeaderboardSize}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
|
|
|
)
|
2022-07-06 23:24:53 +00:00
|
|
|
|
|
|
|
const aboutTab = (
|
|
|
|
<Col>
|
2022-08-31 18:29:49 +00:00
|
|
|
{(group.aboutPostId != null || isCreator || isAdmin) && (
|
2022-08-30 12:39:10 +00:00
|
|
|
<GroupAboutPost
|
|
|
|
group={group}
|
2022-08-31 18:29:49 +00:00
|
|
|
isEditable={!!isCreator || isAdmin}
|
2022-08-30 12:39:10 +00:00
|
|
|
post={aboutPost}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Spacer h={3} />
|
2022-07-06 23:24:53 +00:00
|
|
|
<GroupOverview
|
|
|
|
group={group}
|
|
|
|
creator={creator}
|
|
|
|
isCreator={!!isCreator}
|
|
|
|
user={user}
|
2022-09-12 19:54:11 +00:00
|
|
|
memberIds={memberIds}
|
2022-07-06 23:24:53 +00:00
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
2022-07-13 21:11:22 +00:00
|
|
|
|
2022-07-18 17:55:31 +00:00
|
|
|
const questionsTab = (
|
|
|
|
<ContractSearch
|
2022-08-09 22:28:52 +00:00
|
|
|
user={user}
|
2022-08-15 05:09:25 +00:00
|
|
|
defaultSort={'newest'}
|
2022-09-01 15:11:14 +00:00
|
|
|
defaultFilter={suggestedFilter}
|
2022-07-18 17:55:31 +00:00
|
|
|
additionalFilter={{ groupSlug: group.slug }}
|
2022-09-08 02:33:33 +00:00
|
|
|
persistPrefix={`group-${group.slug}`}
|
2022-07-18 17:55:31 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
const tabs = [
|
|
|
|
{
|
2022-07-21 23:17:02 +00:00
|
|
|
title: 'Markets',
|
2022-07-18 17:55:31 +00:00
|
|
|
content: questionsTab,
|
2022-07-21 23:17:02 +00:00
|
|
|
href: groupPath(group.slug, 'markets'),
|
2022-07-13 21:11:22 +00:00
|
|
|
},
|
|
|
|
{
|
2022-07-20 16:15:55 +00:00
|
|
|
title: 'Leaderboards',
|
2022-07-13 21:11:22 +00:00
|
|
|
content: leaderboard,
|
2022-07-20 16:15:55 +00:00
|
|
|
href: groupPath(group.slug, 'leaderboards'),
|
2022-07-13 21:11:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'About',
|
|
|
|
content: aboutTab,
|
|
|
|
href: groupPath(group.slug, 'about'),
|
|
|
|
},
|
|
|
|
]
|
2022-07-30 07:50:03 +00:00
|
|
|
|
2022-08-10 17:27:59 +00:00
|
|
|
const tabIndex = tabs
|
|
|
|
.map((t) => t.title.toLowerCase())
|
|
|
|
.indexOf(page ?? 'markets')
|
2022-07-17 20:56:39 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
2022-08-03 21:38:35 +00:00
|
|
|
<Page>
|
2022-06-22 16:35:50 +00:00
|
|
|
<SEO
|
|
|
|
title={group.name}
|
|
|
|
description={`Created by ${creator.name}. ${group.about}`}
|
|
|
|
url={groupPath(group.slug)}
|
|
|
|
/>
|
2022-08-03 21:38:35 +00:00
|
|
|
<Col className="relative px-3">
|
2022-06-24 21:02:05 +00:00
|
|
|
<Row className={'items-center justify-between gap-4'}>
|
2022-07-07 12:53:14 +00:00
|
|
|
<div className={'sm:mb-1'}>
|
|
|
|
<div
|
2022-07-19 23:15:55 +00:00
|
|
|
className={'line-clamp-1 my-2 text-2xl text-indigo-700 sm:my-3'}
|
2022-07-07 12:53:14 +00:00
|
|
|
>
|
|
|
|
{group.name}
|
|
|
|
</div>
|
|
|
|
<div className={'hidden sm:block'}>
|
|
|
|
<Linkify text={group.about} />
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</div>
|
2022-07-19 23:15:55 +00:00
|
|
|
<div className="mt-2">
|
2022-07-15 14:45:52 +00:00
|
|
|
<JoinOrAddQuestionsButtons
|
2022-06-24 21:02:05 +00:00
|
|
|
group={group}
|
2022-06-22 16:35:50 +00:00
|
|
|
user={user}
|
2022-06-24 21:02:05 +00:00
|
|
|
isMember={!!isMember}
|
2022-06-22 16:35:50 +00:00
|
|
|
/>
|
2022-06-24 21:02:05 +00:00
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Row>
|
2022-06-24 21:02:05 +00:00
|
|
|
</Col>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Tabs
|
2022-07-07 21:24:13 +00:00
|
|
|
currentPageForAnalytics={groupPath(group.slug)}
|
2022-08-13 18:50:26 +00:00
|
|
|
className={'mx-2 mb-0 sm:mb-2'}
|
2022-07-13 21:11:22 +00:00
|
|
|
defaultIndex={tabIndex > 0 ? tabIndex : 0}
|
|
|
|
tabs={tabs}
|
2022-06-22 16:35:50 +00:00
|
|
|
/>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:45:52 +00:00
|
|
|
function JoinOrAddQuestionsButtons(props: {
|
2022-06-24 21:02:05 +00:00
|
|
|
group: Group
|
|
|
|
user: User | null | undefined
|
|
|
|
isMember: boolean
|
|
|
|
}) {
|
|
|
|
const { group, user, isMember } = props
|
2022-07-06 23:24:53 +00:00
|
|
|
return user && isMember ? (
|
2022-07-19 23:15:55 +00:00
|
|
|
<Row className={'mt-0 justify-end'}>
|
2022-07-06 23:24:53 +00:00
|
|
|
<AddContractButton group={group} user={user} />
|
|
|
|
</Row>
|
2022-06-24 21:02:05 +00:00
|
|
|
) : group.anyoneCanJoin ? (
|
|
|
|
<JoinGroupButton group={group} user={user} />
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function GroupOverview(props: {
|
|
|
|
group: Group
|
|
|
|
creator: User
|
|
|
|
user: User | null | undefined
|
|
|
|
isCreator: boolean
|
2022-09-12 19:54:11 +00:00
|
|
|
memberIds: string[]
|
2022-06-22 16:35:50 +00:00
|
|
|
}) {
|
2022-09-12 19:54:11 +00:00
|
|
|
const { group, creator, isCreator, user, memberIds } = props
|
2022-06-22 16:35:50 +00:00
|
|
|
const anyoneCanJoinChoices: { [key: string]: string } = {
|
|
|
|
Closed: 'false',
|
|
|
|
Open: 'true',
|
|
|
|
}
|
|
|
|
const [anyoneCanJoin, setAnyoneCanJoin] = useState(group.anyoneCanJoin)
|
|
|
|
function updateAnyoneCanJoin(newVal: boolean) {
|
|
|
|
if (group.anyoneCanJoin == newVal || !isCreator) return
|
|
|
|
setAnyoneCanJoin(newVal)
|
|
|
|
toast.promise(updateGroup(group, { ...group, anyoneCanJoin: newVal }), {
|
|
|
|
loading: 'Updating group...',
|
|
|
|
success: 'Updated group!',
|
|
|
|
error: "Couldn't update group",
|
|
|
|
})
|
|
|
|
}
|
2022-07-20 16:45:53 +00:00
|
|
|
const postFix = user ? '?referrer=' + user.username : ''
|
|
|
|
const shareUrl = `https://${ENV_CONFIG.domain}${groupPath(
|
|
|
|
group.slug
|
|
|
|
)}${postFix}`
|
2022-09-12 19:54:11 +00:00
|
|
|
const isMember = user ? memberIds.includes(user.id) : false
|
2022-07-20 16:45:53 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
2022-07-07 12:53:14 +00:00
|
|
|
<>
|
|
|
|
<Col className="gap-2 rounded-b bg-white p-2">
|
2022-07-01 13:47:19 +00:00
|
|
|
<Row className={'flex-wrap justify-between'}>
|
|
|
|
<div className={'inline-flex items-center'}>
|
|
|
|
<div className="mr-1 text-gray-500">Created by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={creator.name}
|
|
|
|
username={creator.username}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-07-13 21:11:22 +00:00
|
|
|
{isCreator ? (
|
|
|
|
<EditGroupButton className={'ml-1'} group={group} />
|
|
|
|
) : (
|
2022-09-03 00:06:48 +00:00
|
|
|
user && (
|
2022-07-13 21:11:22 +00:00
|
|
|
<Row>
|
2022-09-03 01:36:49 +00:00
|
|
|
<JoinOrLeaveGroupButton
|
|
|
|
group={group}
|
|
|
|
user={user}
|
|
|
|
isMember={isMember}
|
|
|
|
/>
|
2022-07-13 21:11:22 +00:00
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
)}
|
2022-06-22 16:35:50 +00:00
|
|
|
</Row>
|
2022-07-07 12:53:14 +00:00
|
|
|
<div className={'block sm:hidden'}>
|
|
|
|
<Linkify text={group.about} />
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Row className={'items-center gap-1'}>
|
|
|
|
<span className={'text-gray-500'}>Membership</span>
|
|
|
|
{user && user.id === creator.id ? (
|
|
|
|
<ChoicesToggleGroup
|
|
|
|
currentChoice={anyoneCanJoin.toString()}
|
|
|
|
choicesMap={anyoneCanJoinChoices}
|
|
|
|
setChoice={(choice) =>
|
|
|
|
updateAnyoneCanJoin(choice.toString() === 'true')
|
|
|
|
}
|
|
|
|
toggleClassName={'h-10'}
|
|
|
|
className={'ml-2'}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<span className={'text-gray-700'}>
|
2022-08-30 23:28:49 +00:00
|
|
|
{anyoneCanJoin ? 'Open to all' : 'Closed (by invite only)'}
|
2022-06-22 16:35:50 +00:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Row>
|
2022-07-20 16:45:53 +00:00
|
|
|
|
2022-07-01 13:47:19 +00:00
|
|
|
{anyoneCanJoin && user && (
|
2022-07-20 16:45:53 +00:00
|
|
|
<Col className="my-4 px-2">
|
|
|
|
<div className="text-lg">Invite</div>
|
|
|
|
<div className={'mb-2 text-gray-500'}>
|
|
|
|
Invite a friend to this group and get M${REFERRAL_AMOUNT} if they
|
|
|
|
sign up!
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<CopyLinkButton
|
|
|
|
url={shareUrl}
|
|
|
|
tracking="copy group share link"
|
|
|
|
buttonClassName="btn-md rounded-l-none"
|
|
|
|
toastClassName={'-left-28 mt-1'}
|
|
|
|
/>
|
|
|
|
</Col>
|
2022-07-01 13:47:19 +00:00
|
|
|
)}
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
2022-07-07 12:53:14 +00:00
|
|
|
</>
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-12 19:54:11 +00:00
|
|
|
function GroupLeaderboard(props: {
|
|
|
|
topUsers: { user: User; score: number }[]
|
2022-06-24 23:38:39 +00:00
|
|
|
title: string
|
2022-09-12 19:54:11 +00:00
|
|
|
maxToShow: number
|
2022-06-24 23:38:39 +00:00
|
|
|
header: string
|
|
|
|
}) {
|
2022-09-12 19:54:11 +00:00
|
|
|
const { topUsers, title, maxToShow, header } = props
|
|
|
|
|
|
|
|
const scoresByUser = topUsers.reduce((acc, { user, score }) => {
|
|
|
|
acc[user.id] = score
|
|
|
|
return acc
|
|
|
|
}, {} as { [key: string]: number })
|
|
|
|
|
2022-06-24 23:38:39 +00:00
|
|
|
return (
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
2022-09-12 19:54:11 +00:00
|
|
|
users={topUsers.map((t) => t.user)}
|
2022-06-24 23:38:39 +00:00
|
|
|
title={title}
|
|
|
|
columns={[
|
2022-09-12 19:54:11 +00:00
|
|
|
{ header, renderCell: (user) => formatMoney(scoresByUser[user.id]) },
|
2022-06-24 23:38:39 +00:00
|
|
|
]}
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow={maxToShow}
|
2022-06-24 23:38:39 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function AddContractButton(props: { group: Group; user: User }) {
|
2022-07-19 23:15:55 +00:00
|
|
|
const { group, user } = props
|
2022-06-22 16:35:50 +00:00
|
|
|
const [open, setOpen] = useState(false)
|
2022-07-26 01:27:43 +00:00
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
const [loading, setLoading] = useState(false)
|
2022-09-03 00:06:48 +00:00
|
|
|
const groupContractIds = useGroupContractIds(group.id)
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-07-01 22:37:30 +00:00
|
|
|
async function addContractToCurrentGroup(contract: Contract) {
|
2022-07-26 01:27:43 +00:00
|
|
|
if (contracts.map((c) => c.id).includes(contract.id)) {
|
|
|
|
setContracts(contracts.filter((c) => c.id !== contract.id))
|
|
|
|
} else setContracts([...contracts, contract])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doneAddingContracts() {
|
|
|
|
Promise.all(
|
|
|
|
contracts.map(async (contract) => {
|
|
|
|
setLoading(true)
|
|
|
|
await addContractToGroup(group, contract, user.id)
|
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
setLoading(false)
|
|
|
|
setOpen(false)
|
|
|
|
setContracts([])
|
|
|
|
})
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-07-19 23:15:55 +00:00
|
|
|
<div className={'flex justify-center'}>
|
2022-08-20 20:19:04 +00:00
|
|
|
<Button
|
|
|
|
className="whitespace-nowrap"
|
2022-08-28 21:26:29 +00:00
|
|
|
size="md"
|
|
|
|
color="indigo"
|
2022-08-20 20:19:04 +00:00
|
|
|
onClick={() => setOpen(true)}
|
|
|
|
>
|
2022-08-13 18:49:25 +00:00
|
|
|
Add market
|
|
|
|
</Button>
|
2022-07-19 23:15:55 +00:00
|
|
|
</div>
|
|
|
|
|
2022-08-26 23:17:15 +00:00
|
|
|
<Modal
|
|
|
|
open={open}
|
|
|
|
setOpen={setOpen}
|
|
|
|
className={'max-w-4xl sm:p-0'}
|
|
|
|
size={'xl'}
|
|
|
|
>
|
2022-08-27 05:59:00 +00:00
|
|
|
<Col
|
|
|
|
className={'min-h-screen w-full max-w-4xl gap-4 rounded-md bg-white'}
|
|
|
|
>
|
2022-07-19 23:15:55 +00:00
|
|
|
<Col className="p-8 pb-0">
|
2022-08-28 21:26:29 +00:00
|
|
|
<div className={'text-xl text-indigo-700'}>Add markets</div>
|
|
|
|
|
|
|
|
<div className={'text-md my-4 text-gray-600'}>
|
|
|
|
Add pre-existing markets to this group, or{' '}
|
|
|
|
<Link href={`/create?groupId=${group.id}`}>
|
|
|
|
<span className="cursor-pointer font-semibold underline">
|
|
|
|
create a new one
|
|
|
|
</span>
|
|
|
|
</Link>
|
|
|
|
.
|
2022-07-19 23:15:55 +00:00
|
|
|
</div>
|
|
|
|
|
2022-08-28 21:26:29 +00:00
|
|
|
{contracts.length > 0 && (
|
2022-07-26 01:27:43 +00:00
|
|
|
<Col className={'w-full '}>
|
|
|
|
{!loading ? (
|
|
|
|
<Row className={'justify-end gap-4'}>
|
|
|
|
<Button onClick={doneAddingContracts} color={'indigo'}>
|
|
|
|
Add {contracts.length} question
|
|
|
|
{contracts.length > 1 && 's'}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
setContracts([])
|
|
|
|
}}
|
|
|
|
color={'gray'}
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
) : (
|
|
|
|
<Row className={'justify-center'}>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</Row>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)}
|
2022-07-19 23:15:55 +00:00
|
|
|
</Col>
|
|
|
|
|
|
|
|
<div className={'overflow-y-scroll sm:px-8'}>
|
2022-07-01 22:37:30 +00:00
|
|
|
<ContractSearch
|
2022-08-09 22:28:52 +00:00
|
|
|
user={user}
|
2022-07-01 22:37:30 +00:00
|
|
|
hideOrderSelector={true}
|
|
|
|
onContractClick={addContractToCurrentGroup}
|
2022-07-26 01:27:43 +00:00
|
|
|
cardHideOptions={{ hideGroupLink: true, hideQuickBet: true }}
|
2022-09-03 00:06:48 +00:00
|
|
|
additionalFilter={{
|
|
|
|
excludeContractIds: groupContractIds,
|
|
|
|
}}
|
2022-07-26 01:27:43 +00:00
|
|
|
highlightOptions={{
|
|
|
|
contractIds: contracts.map((c) => c.id),
|
|
|
|
highlightClassName: '!bg-indigo-100 border-indigo-100 border-2',
|
|
|
|
}}
|
2022-07-01 22:37:30 +00:00
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Modal>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function JoinGroupButton(props: {
|
|
|
|
group: Group
|
|
|
|
user: User | null | undefined
|
|
|
|
}) {
|
|
|
|
const { group, user } = props
|
2022-09-05 21:51:09 +00:00
|
|
|
|
|
|
|
const follow = async () => {
|
|
|
|
track('join group')
|
|
|
|
const userId = user ? user.id : (await firebaseLogin()).user.uid
|
|
|
|
|
|
|
|
toast.promise(joinGroup(group, userId), {
|
|
|
|
loading: 'Following group...',
|
|
|
|
success: 'Followed',
|
|
|
|
error: "Couldn't follow group, try again?",
|
|
|
|
})
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
2022-09-05 21:51:09 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2022-06-23 17:40:32 +00:00
|
|
|
<button
|
2022-09-05 21:51:09 +00:00
|
|
|
onClick={follow}
|
2022-06-23 17:40:32 +00:00
|
|
|
className={'btn-md btn-outline btn whitespace-nowrap normal-case'}
|
|
|
|
>
|
2022-09-05 21:51:09 +00:00
|
|
|
Follow
|
2022-06-22 16:35:50 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-09-12 19:54:11 +00:00
|
|
|
|
|
|
|
const toTopUsers = async (
|
|
|
|
cachedUserIds: { userId: string; score: number }[]
|
|
|
|
): Promise<{ user: User; score: number }[]> =>
|
|
|
|
(
|
|
|
|
await Promise.all(
|
|
|
|
cachedUserIds.map(async (e) => {
|
|
|
|
const user = await getUser(e.userId)
|
|
|
|
return { user, score: e.score ?? 0 }
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).filter((e) => e.user != null)
|