import clsx from 'clsx' import { Dictionary, keyBy, uniq } from 'lodash' import { useEffect, useState } from 'react' import { useRouter } from 'next/router' import { LinkIcon } from '@heroicons/react/solid' import { PencilIcon } from '@heroicons/react/outline' import { User } from 'web/lib/firebase/users' import { CreatorContractsList } from './contract/contracts-grid' import { SEO } from './SEO' import { Page } from './page' import { SiteLink } from './site-link' import { Avatar } from './avatar' import { Col } from './layout/col' import { Linkify } from './linkify' import { Spacer } from './layout/spacer' import { Row } from './layout/row' import { genHash } from 'common/util/random' import { QueryUncontrolledTabs } from './layout/tabs' import { UserCommentsList } from './comments-list' import { Comment, getUsersComments } from 'web/lib/firebase/comments' import { Contract } from 'common/contract' import { getContractFromId, listContracts } from 'web/lib/firebase/contracts' import { LoadingIndicator } from './loading-indicator' import { FullscreenConfetti } from 'web/components/fullscreen-confetti' import { BetsList } from './bets-list' import { FollowersButton, FollowingButton } from './following-button' import { UserFollowButton } from './follow-button' import { GroupsButton } from 'web/components/groups/groups-button' import { PortfolioValueSection } from './portfolio/portfolio-value-section' import { filterDefined } from 'common/util/array' import { useUserBets } from 'web/hooks/use-user-bets' import { ReferralsButton } from 'web/components/referrals-button' import { formatMoney } from 'common/util/format' import { ShareIconButton } from 'web/components/share-icon-button' import { ENV_CONFIG } from 'common/envs/constants' export function UserLink(props: { name: string username: string showUsername?: boolean className?: string justFirstName?: boolean }) { const { name, username, showUsername, className, justFirstName } = props return ( {justFirstName ? name.split(' ')[0] : name} {showUsername && ` (@${username})`} ) } export const TAB_IDS = ['markets', 'comments', 'bets', 'groups'] const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf() export function UserPage(props: { user: User; currentUser?: User }) { const { user, currentUser } = props const router = useRouter() const isCurrentUser = user.id === currentUser?.id const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id) const [usersComments, setUsersComments] = useState() const [usersContracts, setUsersContracts] = useState( 'loading' ) const userBets = useUserBets(user.id, { includeRedemptions: true }) const betCount = userBets === undefined ? 0 : userBets.filter((bet) => !bet.isRedemption && bet.amount !== 0).length const [contractsById, setContractsById] = useState< Dictionary | undefined >() const [showConfetti, setShowConfetti] = useState(false) useEffect(() => { const claimedMana = router.query['claimed-mana'] === 'yes' setShowConfetti(claimedMana) }, [router]) useEffect(() => { if (!user) return getUsersComments(user.id).then(setUsersComments) listContracts(user.id).then(setUsersContracts) }, [user]) // TODO: display comments on groups useEffect(() => { if (usersComments && userBets) { const uniqueContractIds = uniq([ ...usersComments.map((comment) => comment.contractId), ...(userBets?.map((bet) => bet.contractId) ?? []), ]) Promise.all( uniqueContractIds.map((contractId) => contractId ? getContractFromId(contractId) : undefined ) ).then((contracts) => { const contractsById = keyBy(filterDefined(contracts), 'id') setContractsById(contractsById) }) } }, [userBets, usersComments]) const profit = user.profitCached.allTime return ( {showConfetti && ( )} {/* Banner image up top, with an circle avatar overlaid */}
{/* Top right buttons (e.g. edit, follow) */}
{!isCurrentUser && } {isCurrentUser && ( {' '}
Edit
)}
{/* Profile details: name, username, bio, and link to twitter/discord */} {user.name} @{user.username} = 0 ? 'text-green-600' : 'text-red-400' )} > {formatMoney(profit)} {' '} profit {user.bio && ( <>
)} {currentUser && ['ian', 'Austin', 'SG', 'JamesGrugett'].includes( currentUser.username ) && } {user.website && ( {user.website} )} {user.twitterHandle && ( Twitter {user.twitterHandle} )} {user.discordHandle && ( Discord {user.discordHandle} )} {currentUser?.id === user.id && ( Refer a friend and earn {formatMoney(500)} when they sign up! {' '} You have )} {usersContracts !== 'loading' && contractsById && usersComments ? ( ), tabIcon: ( {usersContracts.length} ), }, { title: 'Comments', content: ( ), tabIcon: ( {usersComments.length} ), }, { title: 'Bets', content: (
), tabIcon: {betCount}, }, ]} /> ) : ( )}
) } // 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] }