2022-01-15 23:28:32 +00:00
|
|
|
import clsx from 'clsx'
|
2022-06-14 02:08:56 +00:00
|
|
|
import { uniq } from 'lodash'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-06-14 02:08:56 +00:00
|
|
|
import { LinkIcon } from '@heroicons/react/solid'
|
|
|
|
import { PencilIcon } from '@heroicons/react/outline'
|
2022-06-23 08:07:52 +00:00
|
|
|
import Confetti from 'react-confetti'
|
2022-06-14 02:08:56 +00:00
|
|
|
|
2022-06-24 17:14:20 +00:00
|
|
|
import {
|
|
|
|
follow,
|
|
|
|
unfollow,
|
|
|
|
User,
|
|
|
|
getPortfolioHistory,
|
|
|
|
} from 'web/lib/firebase/users'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { CreatorContractsList } from './contract/contracts-list'
|
2021-12-16 21:17:32 +00:00
|
|
|
import { SEO } from './SEO'
|
2021-12-20 04:06:30 +00:00
|
|
|
import { Page } from './page'
|
2021-12-31 19:17:32 +00:00
|
|
|
import { SiteLink } from './site-link'
|
2022-02-18 01:16:58 +00:00
|
|
|
import { Avatar } from './avatar'
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Linkify } from './linkify'
|
|
|
|
import { Spacer } from './layout/spacer'
|
|
|
|
import { Row } from './layout/row'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { genHash } from 'common/util/random'
|
2022-05-05 22:30:30 +00:00
|
|
|
import { Tabs } from './layout/tabs'
|
|
|
|
import { UserCommentsList } from './comments-list'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Comment, getUsersComments } from 'web/lib/firebase/comments'
|
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { getContractFromId, listContracts } from 'web/lib/firebase/contracts'
|
2022-05-05 22:30:30 +00:00
|
|
|
import { LoadingIndicator } from './loading-indicator'
|
2022-05-18 15:52:12 +00:00
|
|
|
import { BetsList } from './bets-list'
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { getUserBets } from 'web/lib/firebase/bets'
|
2022-06-08 03:24:18 +00:00
|
|
|
import { FollowersButton, FollowingButton } from './following-button'
|
2022-06-14 02:08:56 +00:00
|
|
|
import { useFollows } from 'web/hooks/use-follows'
|
|
|
|
import { FollowButton } from './follow-button'
|
2022-06-24 17:14:20 +00:00
|
|
|
import { PortfolioMetrics } from 'common/user'
|
2022-07-01 13:47:19 +00:00
|
|
|
import { ReferralsButton } from 'web/components/referrals-button'
|
2022-06-29 16:00:43 +00:00
|
|
|
import { GroupsButton } from 'web/components/groups/groups-button'
|
2021-12-16 02:11:29 +00:00
|
|
|
|
2022-01-13 21:16:47 +00:00
|
|
|
export function UserLink(props: {
|
|
|
|
name: string
|
|
|
|
username: string
|
|
|
|
showUsername?: boolean
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { name, username, showUsername, className } = props
|
2021-12-16 02:11:29 +00:00
|
|
|
|
|
|
|
return (
|
2022-05-18 21:08:36 +00:00
|
|
|
<SiteLink
|
|
|
|
href={`/${username}`}
|
|
|
|
className={clsx('z-10 truncate', className)}
|
|
|
|
>
|
2022-01-13 21:16:47 +00:00
|
|
|
{name}
|
|
|
|
{showUsername && ` (@${username})`}
|
2021-12-30 20:03:32 +00:00
|
|
|
</SiteLink>
|
2021-12-16 02:11:29 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export const TAB_IDS = ['markets', 'comments', 'bets', 'groups']
|
2022-06-01 01:06:27 +00:00
|
|
|
const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf()
|
2022-05-18 15:52:12 +00:00
|
|
|
|
2022-05-05 22:30:30 +00:00
|
|
|
export function UserPage(props: {
|
|
|
|
user: User
|
|
|
|
currentUser?: User
|
2022-06-22 16:35:50 +00:00
|
|
|
defaultTabTitle?: string | undefined
|
2022-05-05 22:30:30 +00:00
|
|
|
}) {
|
|
|
|
const { user, currentUser, defaultTabTitle } = props
|
2022-06-23 08:07:52 +00:00
|
|
|
const router = useRouter()
|
2021-12-16 21:17:32 +00:00
|
|
|
const isCurrentUser = user.id === currentUser?.id
|
2022-02-18 01:16:58 +00:00
|
|
|
const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id)
|
2022-05-05 22:30:30 +00:00
|
|
|
const [usersComments, setUsersComments] = useState<Comment[]>([] as Comment[])
|
|
|
|
const [usersContracts, setUsersContracts] = useState<Contract[] | 'loading'>(
|
|
|
|
'loading'
|
|
|
|
)
|
2022-05-18 15:52:12 +00:00
|
|
|
const [usersBets, setUsersBets] = useState<Bet[] | 'loading'>('loading')
|
2022-06-24 17:14:20 +00:00
|
|
|
const [, setUsersPortfolioHistory] = useState<PortfolioMetrics[]>([])
|
2022-05-05 22:30:30 +00:00
|
|
|
const [commentsByContract, setCommentsByContract] = useState<
|
|
|
|
Map<Contract, Comment[]> | 'loading'
|
|
|
|
>('loading')
|
2022-06-23 08:07:52 +00:00
|
|
|
const [showConfetti, setShowConfetti] = useState(false)
|
|
|
|
const { width, height } = useWindowSize()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const claimedMana = router.query['claimed-mana'] === 'yes'
|
|
|
|
setShowConfetti(claimedMana)
|
|
|
|
}, [router])
|
2022-05-05 22:30:30 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!user) return
|
|
|
|
getUsersComments(user.id).then(setUsersComments)
|
|
|
|
listContracts(user.id).then(setUsersContracts)
|
2022-05-19 16:03:37 +00:00
|
|
|
getUserBets(user.id, { includeRedemptions: false }).then(setUsersBets)
|
2022-06-24 17:14:20 +00:00
|
|
|
getPortfolioHistory(user.id).then(setUsersPortfolioHistory)
|
2022-05-05 22:30:30 +00:00
|
|
|
}, [user])
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
// TODO: display comments on groups
|
2022-05-05 22:30:30 +00:00
|
|
|
useEffect(() => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const uniqueContractIds = uniq(
|
2022-05-05 22:30:30 +00:00
|
|
|
usersComments.map((comment) => comment.contractId)
|
|
|
|
)
|
|
|
|
Promise.all(
|
2022-06-22 16:35:50 +00:00
|
|
|
uniqueContractIds.map(
|
|
|
|
(contractId) => contractId && getContractFromId(contractId)
|
|
|
|
)
|
2022-05-05 22:30:30 +00:00
|
|
|
).then((contracts) => {
|
|
|
|
const commentsByContract = new Map<Contract, Comment[]>()
|
|
|
|
contracts.forEach((contract) => {
|
|
|
|
if (!contract) return
|
|
|
|
commentsByContract.set(
|
|
|
|
contract,
|
|
|
|
usersComments.filter((comment) => comment.contractId === contract.id)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
setCommentsByContract(commentsByContract)
|
|
|
|
})
|
|
|
|
}, [usersComments])
|
2021-12-16 21:17:32 +00:00
|
|
|
|
2022-06-03 04:52:14 +00:00
|
|
|
const yourFollows = useFollows(currentUser?.id)
|
|
|
|
const isFollowing = yourFollows?.includes(user.id)
|
|
|
|
|
|
|
|
const onFollow = () => {
|
|
|
|
if (!currentUser) return
|
|
|
|
follow(currentUser.id, user.id)
|
|
|
|
}
|
|
|
|
const onUnfollow = () => {
|
|
|
|
if (!currentUser) return
|
|
|
|
unfollow(currentUser.id, user.id)
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:11:29 +00:00
|
|
|
return (
|
2022-06-08 20:51:51 +00:00
|
|
|
<Page key={user.id}>
|
2021-12-16 21:17:32 +00:00
|
|
|
<SEO
|
2022-02-18 01:16:58 +00:00
|
|
|
title={`${user.name} (@${user.username})`}
|
2022-03-17 17:03:15 +00:00
|
|
|
description={user.bio ?? ''}
|
2022-02-18 01:16:58 +00:00
|
|
|
url={`/${user.username}`}
|
2021-12-16 21:17:32 +00:00
|
|
|
/>
|
2022-06-23 08:07:52 +00:00
|
|
|
{showConfetti && (
|
|
|
|
<Confetti
|
|
|
|
width={width ? width : 500}
|
|
|
|
height={height ? height : 500}
|
|
|
|
recycle={false}
|
|
|
|
numberOfPieces={300}
|
|
|
|
/>
|
|
|
|
)}
|
2022-02-18 01:16:58 +00:00
|
|
|
{/* Banner image up top, with an circle avatar overlaid */}
|
|
|
|
<div
|
|
|
|
className="h-32 w-full bg-cover bg-center sm:h-40"
|
|
|
|
style={{
|
|
|
|
backgroundImage: `url(${bannerUrl})`,
|
|
|
|
}}
|
|
|
|
></div>
|
|
|
|
<div className="relative mb-20">
|
|
|
|
<div className="absolute -top-10 left-4">
|
|
|
|
<Avatar
|
|
|
|
username={user.username}
|
|
|
|
avatarUrl={user.avatarUrl}
|
|
|
|
size={20}
|
|
|
|
className="bg-white ring-4 ring-white"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Top right buttons (e.g. edit, follow) */}
|
|
|
|
<div className="absolute right-0 top-0 mt-4 mr-4">
|
2022-06-03 04:52:14 +00:00
|
|
|
{!isCurrentUser && (
|
|
|
|
<FollowButton
|
|
|
|
isFollowing={isFollowing}
|
|
|
|
onFollow={onFollow}
|
|
|
|
onUnfollow={onUnfollow}
|
|
|
|
/>
|
|
|
|
)}
|
2022-02-18 01:16:58 +00:00
|
|
|
{isCurrentUser && (
|
|
|
|
<SiteLink className="btn" href="/profile">
|
|
|
|
<PencilIcon className="h-5 w-5" />{' '}
|
|
|
|
<div className="ml-2">Edit</div>
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Profile details: name, username, bio, and link to twitter/discord */}
|
|
|
|
<Col className="mx-4 -mt-6">
|
|
|
|
<span className="text-2xl font-bold">{user.name}</span>
|
|
|
|
<span className="text-gray-500">@{user.username}</span>
|
|
|
|
|
2022-06-08 03:24:18 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-03-17 17:03:15 +00:00
|
|
|
{user.bio && (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<Linkify text={user.bio}></Linkify>
|
|
|
|
</div>
|
|
|
|
<Spacer h={4} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-02-18 01:16:58 +00:00
|
|
|
|
2022-07-01 13:47:19 +00:00
|
|
|
<Col className="flex-wrap gap-2 sm:flex-row sm:items-center sm:gap-4">
|
2022-06-10 16:11:27 +00:00
|
|
|
<Row className="gap-4">
|
|
|
|
<FollowingButton user={user} />
|
|
|
|
<FollowersButton user={user} />
|
2022-07-01 13:47:19 +00:00
|
|
|
<ReferralsButton user={user} />
|
2022-06-29 16:00:43 +00:00
|
|
|
<GroupsButton user={user} />
|
2022-06-10 16:11:27 +00:00
|
|
|
</Row>
|
2022-06-08 03:24:18 +00:00
|
|
|
|
2022-02-18 01:16:58 +00:00
|
|
|
{user.website && (
|
2022-04-20 15:47:14 +00:00
|
|
|
<SiteLink
|
2022-04-20 16:13:17 +00:00
|
|
|
href={
|
|
|
|
'https://' +
|
|
|
|
user.website.replace('http://', '').replace('https://', '')
|
|
|
|
}
|
2022-04-20 15:47:14 +00:00
|
|
|
>
|
2022-02-18 01:16:58 +00:00
|
|
|
<Row className="items-center gap-1">
|
|
|
|
<LinkIcon className="h-4 w-4" />
|
|
|
|
<span className="text-sm text-gray-500">{user.website}</span>
|
|
|
|
</Row>
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
2021-12-16 02:11:29 +00:00
|
|
|
|
2022-02-18 01:16:58 +00:00
|
|
|
{user.twitterHandle && (
|
2022-04-20 15:47:14 +00:00
|
|
|
<SiteLink
|
|
|
|
href={`https://twitter.com/${user.twitterHandle
|
|
|
|
.replace('https://www.twitter.com/', '')
|
|
|
|
.replace('https://twitter.com/', '')
|
|
|
|
.replace('www.twitter.com/', '')
|
|
|
|
.replace('twitter.com/', '')}`}
|
|
|
|
>
|
2022-02-18 01:16:58 +00:00
|
|
|
<Row className="items-center gap-1">
|
|
|
|
<img
|
|
|
|
src="/twitter-logo.svg"
|
|
|
|
className="h-4 w-4"
|
|
|
|
alt="Twitter"
|
|
|
|
/>
|
|
|
|
<span className="text-sm text-gray-500">
|
|
|
|
{user.twitterHandle}
|
|
|
|
</span>
|
|
|
|
</Row>
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{user.discordHandle && (
|
|
|
|
<SiteLink href="https://discord.com/invite/eHQBNBqXuh">
|
|
|
|
<Row className="items-center gap-1">
|
|
|
|
<img
|
|
|
|
src="/discord-logo.svg"
|
|
|
|
className="h-4 w-4"
|
|
|
|
alt="Discord"
|
|
|
|
/>
|
|
|
|
<span className="text-sm text-gray-500">
|
|
|
|
{user.discordHandle}
|
|
|
|
</span>
|
|
|
|
</Row>
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
|
|
|
|
<Spacer h={10} />
|
2022-06-24 17:14:20 +00:00
|
|
|
|
2022-05-05 22:30:30 +00:00
|
|
|
{usersContracts !== 'loading' && commentsByContract != 'loading' ? (
|
|
|
|
<Tabs
|
2022-07-01 22:37:30 +00:00
|
|
|
labelClassName={'pb-2 pt-1 '}
|
2022-06-22 16:35:50 +00:00
|
|
|
defaultIndex={
|
|
|
|
defaultTabTitle ? TAB_IDS.indexOf(defaultTabTitle) : 0
|
|
|
|
}
|
2022-05-18 15:52:12 +00:00
|
|
|
onClick={(tabName) => {
|
|
|
|
const tabId = tabName.toLowerCase()
|
2022-06-22 16:35:50 +00:00
|
|
|
const subpath = tabId === 'markets' ? '' : '?tab=' + tabId
|
2022-05-18 15:52:12 +00:00
|
|
|
// BUG: if you start on `/Bob/bets`, then click on Markets, use-query-and-sort-params
|
|
|
|
// rewrites the url incorrectly to `/Bob/bets` instead of `/Bob`
|
2022-06-22 16:35:50 +00:00
|
|
|
router.push(`/${user.username}${subpath}`, undefined, {
|
|
|
|
shallow: true,
|
|
|
|
})
|
2022-05-18 15:52:12 +00:00
|
|
|
}}
|
2022-05-05 22:30:30 +00:00
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
title: 'Markets',
|
2022-05-09 17:38:33 +00:00
|
|
|
content: <CreatorContractsList creator={user} />,
|
2022-05-05 22:30:30 +00:00
|
|
|
tabIcon: (
|
2022-05-09 02:53:02 +00:00
|
|
|
<div className="px-0.5 font-bold">
|
2022-05-05 22:30:30 +00:00
|
|
|
{usersContracts.length}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Comments',
|
|
|
|
content: (
|
|
|
|
<UserCommentsList
|
|
|
|
user={user}
|
|
|
|
commentsByUniqueContracts={commentsByContract}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
tabIcon: (
|
2022-05-09 02:53:02 +00:00
|
|
|
<div className="px-0.5 font-bold">{usersComments.length}</div>
|
2022-05-05 22:30:30 +00:00
|
|
|
),
|
|
|
|
},
|
2022-05-18 15:52:12 +00:00
|
|
|
{
|
|
|
|
title: 'Bets',
|
|
|
|
content: (
|
|
|
|
<div>
|
2022-06-24 17:14:20 +00:00
|
|
|
{
|
|
|
|
// TODO: add portfolio-value-section here
|
|
|
|
}
|
2022-06-01 01:06:27 +00:00
|
|
|
<BetsList
|
|
|
|
user={user}
|
|
|
|
hideBetsBefore={isCurrentUser ? 0 : JUNE_1_2022}
|
2022-05-18 15:52:12 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
tabIcon: (
|
|
|
|
<div className="px-0.5 font-bold">{usersBets.length}</div>
|
|
|
|
),
|
|
|
|
},
|
2022-05-05 22:30:30 +00:00
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator />
|
|
|
|
)}
|
2022-02-18 01:16:58 +00:00
|
|
|
</Col>
|
2021-12-20 04:06:30 +00:00
|
|
|
</Page>
|
2021-12-16 02:11:29 +00:00
|
|
|
)
|
|
|
|
}
|
2022-02-18 01:16:58 +00:00
|
|
|
|
|
|
|
// Assign each user to a random default banner based on the hash of userId
|
|
|
|
// TODO: Consider handling banner uploads using our own storage bucket, like user avatars.
|
|
|
|
export function defaultBannerUrl(userId: string) {
|
|
|
|
const defaultBanner = [
|
|
|
|
'https://images.unsplash.com/photo-1501523460185-2aa5d2a0f981?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2131&q=80',
|
|
|
|
'https://images.unsplash.com/photo-1458682625221-3a45f8a844c7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1974&q=80',
|
|
|
|
'https://images.unsplash.com/photo-1558517259-165ae4b10f7f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2080&q=80',
|
|
|
|
'https://images.unsplash.com/photo-1563260797-cb5cd70254c8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2069&q=80',
|
|
|
|
'https://images.unsplash.com/photo-1603399587513-136aa9398f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1467&q=80',
|
|
|
|
]
|
|
|
|
return defaultBanner[genHash(userId)() % defaultBanner.length]
|
|
|
|
}
|