2022-07-22 22:28:53 +00:00
|
|
|
import { debounce, sortBy, take } from 'lodash'
|
2022-07-19 23:15:55 +00:00
|
|
|
import PlusSmIcon from '@heroicons/react/solid/PlusSmIcon'
|
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'
|
|
|
|
import { listAllBets } from 'web/lib/firebase/bets'
|
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-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'
|
|
|
|
import { UserLink } from 'web/components/user-page'
|
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'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-07-15 12:52:08 +00:00
|
|
|
import { listMembers, useGroup, useMembers } from 'web/hooks/use-group'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { scoreCreators, scoreTraders } from 'common/scoring'
|
|
|
|
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'
|
2022-07-19 23:15:55 +00:00
|
|
|
import { CreateQuestionButton } from 'web/components/create-question-button'
|
2022-07-21 19:43:10 +00:00
|
|
|
import React, { useState } from 'react'
|
2022-06-24 17:16:37 +00:00
|
|
|
import { GroupChat } from 'web/components/groups/group-chat'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import { Modal } from 'web/components/layout/modal'
|
2022-07-15 21:16:00 +00:00
|
|
|
import { getSavedSort } from 'web/hooks/use-sort-and-query-params'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
|
|
|
import { toast } from 'react-hot-toast'
|
2022-06-24 17:16:37 +00:00
|
|
|
import { useCommentsOnGroup } from 'web/hooks/use-comments'
|
2022-07-01 13:47:19 +00:00
|
|
|
import { REFERRAL_AMOUNT } from 'common/user'
|
2022-07-01 22:37:30 +00:00
|
|
|
import { ContractSearch } from 'web/components/contract-search'
|
2022-07-06 23:24:53 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import { FollowList } from 'web/components/follow-list'
|
2022-07-07 13:05:12 +00:00
|
|
|
import { SearchIcon } from '@heroicons/react/outline'
|
2022-07-07 23:23:13 +00:00
|
|
|
import { useTipTxns } from 'web/hooks/use-tip-txns'
|
2022-07-13 21:11:22 +00:00
|
|
|
import { JoinOrLeaveGroupButton } from 'web/components/groups/groups-button'
|
2022-07-15 21:16:00 +00:00
|
|
|
import { searchInAny } from 'common/util/parse'
|
2022-07-17 20:56:39 +00:00
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
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-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-07-14 15:09:12 +00:00
|
|
|
const members = group && (await listMembers(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-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const bets = await Promise.all(
|
|
|
|
contracts.map((contract: Contract) => listAllBets(contract.id))
|
|
|
|
)
|
|
|
|
|
|
|
|
const creatorScores = scoreCreators(contracts)
|
|
|
|
const traderScores = scoreTraders(contracts, bets)
|
2022-07-14 15:09:12 +00:00
|
|
|
const [topCreators, topTraders] =
|
|
|
|
(members && [
|
|
|
|
toTopUsers(creatorScores, members),
|
|
|
|
toTopUsers(traderScores, members),
|
|
|
|
]) ??
|
|
|
|
[]
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const creator = await creatorPromise
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
group,
|
2022-06-24 23:38:39 +00:00
|
|
|
members,
|
2022-06-22 16:35:50 +00:00
|
|
|
creator,
|
|
|
|
traderScores,
|
|
|
|
topTraders,
|
|
|
|
creatorScores,
|
|
|
|
topCreators,
|
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:09:12 +00:00
|
|
|
function toTopUsers(userScores: { [userId: string]: number }, users: User[]) {
|
2022-06-22 16:35:50 +00:00
|
|
|
const topUserPairs = take(
|
|
|
|
sortBy(Object.entries(userScores), ([_, score]) => -1 * score),
|
|
|
|
10
|
|
|
|
).filter(([_, score]) => score >= 0.5)
|
|
|
|
|
2022-07-14 15:09:12 +00:00
|
|
|
const topUsers = topUserPairs.map(
|
|
|
|
([userId]) => users.filter((user) => user.id === userId)[0]
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
|
|
|
return topUsers.filter((user) => user)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-24 23:38:39 +00:00
|
|
|
members: User[]
|
2022-06-22 16:35:50 +00:00
|
|
|
creator: User
|
|
|
|
traderScores: { [userId: string]: number }
|
|
|
|
topTraders: User[]
|
|
|
|
creatorScores: { [userId: string]: number }
|
|
|
|
topCreators: User[]
|
|
|
|
}) {
|
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
group: null,
|
2022-06-24 23:38:39 +00:00
|
|
|
members: [],
|
2022-06-22 16:35:50 +00:00
|
|
|
creator: null,
|
|
|
|
traderScores: {},
|
|
|
|
topTraders: [],
|
|
|
|
creatorScores: {},
|
|
|
|
topCreators: [],
|
|
|
|
}
|
2022-06-24 23:38:39 +00:00
|
|
|
const {
|
|
|
|
creator,
|
|
|
|
members,
|
|
|
|
traderScores,
|
|
|
|
topTraders,
|
|
|
|
creatorScores,
|
|
|
|
topCreators,
|
|
|
|
} = 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-07-07 23:23:13 +00:00
|
|
|
const tips = useTipTxns({ groupId: group?.id })
|
2022-06-24 17:32:59 +00:00
|
|
|
|
2022-06-24 17:16:37 +00:00
|
|
|
const messages = useCommentsOnGroup(group?.id)
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
2022-07-21 19:43:10 +00:00
|
|
|
|
|
|
|
useSaveReferral(user, {
|
|
|
|
defaultReferrer: creator.username,
|
|
|
|
groupId: group?.id,
|
|
|
|
})
|
2022-07-01 13:47:19 +00:00
|
|
|
|
2022-07-17 20:56:39 +00:00
|
|
|
const { width } = useWindowSize()
|
2022-07-18 17:55:31 +00:00
|
|
|
const chatDisabled = !group || group.chatDisabled
|
|
|
|
const showChatSidebar = !chatDisabled && (width ?? 1280) >= 1280
|
|
|
|
const showChatTab = !chatDisabled && !showChatSidebar
|
2022-07-17 20:56:39 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
if (group === null || !groupSubpages.includes(page) || slugs[2]) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
|
|
|
const { memberIds } = group
|
|
|
|
const isCreator = user && group && user.id === group.creatorId
|
|
|
|
const isMember = user && memberIds.includes(user.id)
|
|
|
|
|
2022-07-06 23:24:53 +00:00
|
|
|
const leaderboard = (
|
2022-06-24 21:02:05 +00:00
|
|
|
<Col>
|
2022-06-24 23:38:39 +00:00
|
|
|
<GroupLeaderboards
|
|
|
|
traderScores={traderScores}
|
|
|
|
creatorScores={creatorScores}
|
|
|
|
topTraders={topTraders}
|
|
|
|
topCreators={topCreators}
|
|
|
|
members={members}
|
|
|
|
user={user}
|
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
|
|
|
)
|
2022-07-06 23:24:53 +00:00
|
|
|
|
|
|
|
const aboutTab = (
|
|
|
|
<Col>
|
|
|
|
<GroupOverview
|
|
|
|
group={group}
|
|
|
|
creator={creator}
|
|
|
|
isCreator={!!isCreator}
|
|
|
|
user={user}
|
2022-07-14 15:09:12 +00:00
|
|
|
members={members}
|
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 chatTab = (
|
2022-07-17 20:56:39 +00:00
|
|
|
<Col className="">
|
|
|
|
{messages ? (
|
|
|
|
<GroupChat messages={messages} user={user} group={group} tips={tips} />
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator />
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
|
2022-07-18 17:55:31 +00:00
|
|
|
const questionsTab = (
|
|
|
|
<ContractSearch
|
|
|
|
querySortOptions={{
|
|
|
|
shouldLoadFromStorage: true,
|
|
|
|
defaultSort: getSavedSort() ?? 'newest',
|
|
|
|
defaultFilter: 'open',
|
|
|
|
}}
|
|
|
|
additionalFilter={{ groupSlug: group.slug }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
const tabs = [
|
2022-07-18 17:55:31 +00:00
|
|
|
...(!showChatTab
|
2022-07-13 21:11:22 +00:00
|
|
|
? []
|
|
|
|
: [
|
|
|
|
{
|
|
|
|
title: 'Chat',
|
2022-07-17 20:56:39 +00:00
|
|
|
content: chatTab,
|
2022-07-15 12:52:08 +00:00
|
|
|
href: groupPath(group.slug, GROUP_CHAT_SLUG),
|
2022-07-13 21:11:22 +00:00
|
|
|
},
|
|
|
|
]),
|
|
|
|
{
|
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-07-15 12:52:08 +00:00
|
|
|
const tabIndex = tabs.map((t) => t.title).indexOf(page ?? GROUP_CHAT_SLUG)
|
2022-07-17 20:56:39 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
2022-07-17 20:56:39 +00:00
|
|
|
<Page
|
|
|
|
rightSidebar={showChatSidebar ? chatTab : undefined}
|
2022-07-18 17:55:31 +00:00
|
|
|
rightSidebarClassName={showChatSidebar ? '!top-0' : ''}
|
2022-07-20 21:06:24 +00:00
|
|
|
className={showChatSidebar ? '!max-w-7xl !pb-0' : ''}
|
2022-07-17 20:56:39 +00:00
|
|
|
>
|
2022-06-22 16:35:50 +00:00
|
|
|
<SEO
|
|
|
|
title={group.name}
|
|
|
|
description={`Created by ${creator.name}. ${group.about}`}
|
|
|
|
url={groupPath(group.slug)}
|
|
|
|
/>
|
2022-07-11 23:40:25 +00:00
|
|
|
<Col className="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-07-13 21:11:22 +00:00
|
|
|
className={'mb-0 sm:mb-2'}
|
|
|
|
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-07-14 15:09:12 +00:00
|
|
|
members: User[]
|
2022-06-22 16:35:50 +00:00
|
|
|
}) {
|
2022-07-14 15:09:12 +00:00
|
|
|
const { group, creator, isCreator, user, members } = 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-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} />
|
|
|
|
) : (
|
|
|
|
user &&
|
|
|
|
group.memberIds.includes(user?.id) && (
|
|
|
|
<Row>
|
|
|
|
<JoinOrLeaveGroupButton group={group} />
|
|
|
|
</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'}>
|
|
|
|
{anyoneCanJoin ? 'Open' : 'Closed'}
|
|
|
|
</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-07-20 16:45:53 +00:00
|
|
|
|
2022-07-06 23:24:53 +00:00
|
|
|
<Col className={'mt-2'}>
|
2022-07-20 16:45:53 +00:00
|
|
|
<div className="mb-2 text-lg">Members</div>
|
2022-07-15 12:52:08 +00:00
|
|
|
<GroupMemberSearch members={members} group={group} />
|
2022-07-06 23:24:53 +00:00
|
|
|
</Col>
|
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-07-06 23:24:53 +00:00
|
|
|
function SearchBar(props: { setQuery: (query: string) => void }) {
|
|
|
|
const { setQuery } = props
|
|
|
|
const debouncedQuery = debounce(setQuery, 50)
|
|
|
|
return (
|
|
|
|
<div className={'relative'}>
|
|
|
|
<SearchIcon className={'absolute left-5 top-3.5 h-5 w-5 text-gray-500'} />
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
onChange={(e) => debouncedQuery(e.target.value)}
|
|
|
|
placeholder="Find a member"
|
|
|
|
className="input input-bordered mb-4 w-full pl-12"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-15 12:52:08 +00:00
|
|
|
function GroupMemberSearch(props: { members: User[]; group: Group }) {
|
2022-07-06 23:24:53 +00:00
|
|
|
const [query, setQuery] = useState('')
|
2022-07-15 12:52:08 +00:00
|
|
|
const { group } = props
|
|
|
|
let { members } = props
|
|
|
|
|
|
|
|
// Use static members on load, but also listen to member changes:
|
|
|
|
const listenToMembers = useMembers(group)
|
|
|
|
if (listenToMembers) {
|
|
|
|
members = listenToMembers
|
|
|
|
}
|
2022-07-06 23:24:53 +00:00
|
|
|
|
|
|
|
// TODO use find-active-contracts to sort by?
|
2022-07-15 21:16:00 +00:00
|
|
|
const matches = sortBy(members, [(member) => member.name]).filter((m) =>
|
|
|
|
searchInAny(query, m.name, m.username)
|
2022-07-06 23:24:53 +00:00
|
|
|
)
|
2022-07-13 21:11:22 +00:00
|
|
|
const matchLimit = 25
|
|
|
|
|
2022-07-06 23:24:53 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<SearchBar setQuery={setQuery} />
|
|
|
|
<Col className={'gap-2'}>
|
|
|
|
{matches.length > 0 && (
|
2022-07-13 21:11:22 +00:00
|
|
|
<FollowList userIds={matches.slice(0, matchLimit).map((m) => m.id)} />
|
|
|
|
)}
|
|
|
|
{matches.length > 25 && (
|
|
|
|
<div className={'text-center'}>
|
|
|
|
And {matches.length - matchLimit} more...
|
|
|
|
</div>
|
2022-07-06 23:24:53 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-24 23:38:39 +00:00
|
|
|
function SortedLeaderboard(props: {
|
|
|
|
users: User[]
|
|
|
|
scoreFunction: (user: User) => number
|
|
|
|
title: string
|
|
|
|
header: string
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow?: number
|
2022-06-24 23:38:39 +00:00
|
|
|
}) {
|
2022-07-13 21:11:22 +00:00
|
|
|
const { users, scoreFunction, title, header, maxToShow } = props
|
2022-06-24 23:38:39 +00:00
|
|
|
const sortedUsers = users.sort((a, b) => scoreFunction(b) - scoreFunction(a))
|
|
|
|
return (
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
|
|
|
users={sortedUsers}
|
|
|
|
title={title}
|
|
|
|
columns={[
|
|
|
|
{ header, renderCell: (user) => formatMoney(scoreFunction(user)) },
|
|
|
|
]}
|
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 GroupLeaderboards(props: {
|
|
|
|
traderScores: { [userId: string]: number }
|
|
|
|
creatorScores: { [userId: string]: number }
|
|
|
|
topTraders: User[]
|
|
|
|
topCreators: User[]
|
2022-06-24 23:38:39 +00:00
|
|
|
members: User[]
|
2022-06-22 16:35:50 +00:00
|
|
|
user: User | null | undefined
|
|
|
|
}) {
|
2022-06-24 23:38:39 +00:00
|
|
|
const { traderScores, creatorScores, members, topTraders, topCreators } =
|
|
|
|
props
|
2022-07-13 21:11:22 +00:00
|
|
|
const maxToShow = 50
|
2022-06-24 23:38:39 +00:00
|
|
|
// Consider hiding M$0
|
2022-07-06 23:24:53 +00:00
|
|
|
// If it's just one member (curator), show all bettors, otherwise just show members
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
2022-06-24 23:38:39 +00:00
|
|
|
<Col>
|
|
|
|
<div className="mt-4 flex flex-col gap-8 px-4 md:flex-row">
|
2022-07-06 23:24:53 +00:00
|
|
|
{members.length > 1 ? (
|
2022-06-24 23:38:39 +00:00
|
|
|
<>
|
|
|
|
<SortedLeaderboard
|
|
|
|
users={members}
|
|
|
|
scoreFunction={(user) => traderScores[user.id] ?? 0}
|
2022-07-22 17:03:33 +00:00
|
|
|
title="🏅 Top traders"
|
2022-06-24 23:38:39 +00:00
|
|
|
header="Profit"
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow={maxToShow}
|
2022-06-24 23:38:39 +00:00
|
|
|
/>
|
|
|
|
<SortedLeaderboard
|
|
|
|
users={members}
|
|
|
|
scoreFunction={(user) => creatorScores[user.id] ?? 0}
|
2022-07-20 16:15:55 +00:00
|
|
|
title="🏅 Top creators"
|
2022-06-24 23:38:39 +00:00
|
|
|
header="Market volume"
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow={maxToShow}
|
2022-06-24 23:38:39 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
2022-07-22 17:03:33 +00:00
|
|
|
title="🏅 Top traders"
|
2022-06-24 23:38:39 +00:00
|
|
|
users={topTraders}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Profit',
|
|
|
|
renderCell: (user) => formatMoney(traderScores[user.id] ?? 0),
|
|
|
|
},
|
|
|
|
]}
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow={maxToShow}
|
2022-06-24 23:38:39 +00:00
|
|
|
/>
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
|
|
|
title="🏅 Top creators"
|
|
|
|
users={topCreators}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Market volume',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(creatorScores[user.id] ?? 0),
|
|
|
|
},
|
|
|
|
]}
|
2022-07-13 21:11:22 +00:00
|
|
|
maxToShow={maxToShow}
|
2022-06-24 23:38:39 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
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-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'}>
|
|
|
|
<button
|
|
|
|
className={clsx('btn btn-sm btn-outline')}
|
|
|
|
onClick={() => setOpen(true)}
|
|
|
|
>
|
|
|
|
<PlusSmIcon className="h-6 w-6" aria-hidden="true" /> question
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
2022-07-26 01:27:43 +00:00
|
|
|
<Modal open={open} setOpen={setOpen} className={'sm:p-0'} size={'lg'}>
|
|
|
|
<Col className={' w-full gap-4 rounded-md bg-white'}>
|
2022-07-19 23:15:55 +00:00
|
|
|
<Col className="p-8 pb-0">
|
|
|
|
<div className={'text-xl text-indigo-700'}>
|
|
|
|
Add a question to your group
|
|
|
|
</div>
|
|
|
|
|
2022-07-26 01:27:43 +00:00
|
|
|
{contracts.length === 0 ? (
|
|
|
|
<Col className="items-center justify-center">
|
|
|
|
<CreateQuestionButton
|
|
|
|
user={user}
|
|
|
|
overrideText={'New question'}
|
|
|
|
className={'w-48 flex-shrink-0 '}
|
|
|
|
query={`?groupId=${group.id}`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className={'mt-1 text-lg text-gray-600'}>
|
|
|
|
(or select old questions)
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
<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
|
|
|
|
hideOrderSelector={true}
|
|
|
|
onContractClick={addContractToCurrentGroup}
|
2022-07-26 01:27:43 +00:00
|
|
|
overrideGridClassName={
|
|
|
|
'flex grid grid-cols-1 sm:grid-cols-2 flex-col gap-3 p-1'
|
|
|
|
}
|
2022-07-01 22:37:30 +00:00
|
|
|
showPlaceHolder={true}
|
2022-07-26 01:27:43 +00:00
|
|
|
cardHideOptions={{ hideGroupLink: true, hideQuickBet: true }}
|
2022-07-01 22:37:30 +00:00
|
|
|
additionalFilter={{ excludeContractIds: group.contractIds }}
|
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-07-14 17:09:28 +00:00
|
|
|
function addUserToGroup() {
|
2022-06-22 16:35:50 +00:00
|
|
|
if (user && !group.memberIds.includes(user.id)) {
|
2022-07-14 17:09:28 +00:00
|
|
|
toast.promise(joinGroup(group, user.id), {
|
2022-07-01 22:37:30 +00:00
|
|
|
loading: 'Joining group...',
|
|
|
|
success: 'Joined group!',
|
2022-07-14 17:09:28 +00:00
|
|
|
error: "Couldn't join group, try again?",
|
2022-07-01 22:37:30 +00:00
|
|
|
})
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div>
|
2022-06-23 17:40:32 +00:00
|
|
|
<button
|
2022-07-14 17:09:28 +00:00
|
|
|
onClick={user ? addUserToGroup : firebaseLogin}
|
2022-06-23 17:40:32 +00:00
|
|
|
className={'btn-md btn-outline btn whitespace-nowrap normal-case'}
|
|
|
|
>
|
|
|
|
{user ? 'Join group' : 'Login to join group'}
|
2022-06-22 16:35:50 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|