import clsx from 'clsx' import { User } from 'web/lib/firebase/users' import { CreatorContractsList } from './contract/contracts-list' 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 { LinkIcon } from '@heroicons/react/solid' import { genHash } from 'common/util/random' import { PencilIcon } from '@heroicons/react/outline' import { Tabs } from './layout/tabs' import { UserCommentsList } from './comments-list' import { useEffect, useState } from 'react' 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 { BetsList } from './bets-list' import { Bet } from 'common/bet' import { getUserBets } from 'web/lib/firebase/bets' import { uniq } from 'lodash' export function UserLink(props: { name: string username: string showUsername?: boolean className?: string }) { const { name, username, showUsername, className } = props return ( {name} {showUsername && ` (@${username})`} ) } export const TAB_IDS = ['markets', 'comments', 'bets'] export function UserPage(props: { user: User currentUser?: User defaultTabTitle?: 'markets' | 'comments' | 'bets' }) { const { user, currentUser, defaultTabTitle } = props const isCurrentUser = user.id === currentUser?.id const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id) const [usersComments, setUsersComments] = useState([] as Comment[]) const [usersContracts, setUsersContracts] = useState( 'loading' ) const [usersBets, setUsersBets] = useState('loading') const [commentsByContract, setCommentsByContract] = useState< Map | 'loading' >('loading') useEffect(() => { if (!user) return getUsersComments(user.id).then(setUsersComments) listContracts(user.id).then(setUsersContracts) getUserBets(user.id, { includeRedemptions: false }).then(setUsersBets) }, [user]) useEffect(() => { const uniqueContractIds = uniq( usersComments.map((comment) => comment.contractId) ) Promise.all( uniqueContractIds.map((contractId) => getContractFromId(contractId)) ).then((contracts) => { const commentsByContract = new Map() contracts.forEach((contract) => { if (!contract) return commentsByContract.set( contract, usersComments.filter((comment) => comment.contractId === contract.id) ) }) setCommentsByContract(commentsByContract) }) }, [usersComments]) return ( {/* Banner image up top, with an circle avatar overlaid */}
{/* Top right buttons (e.g. edit, follow) */}
{isCurrentUser && ( {' '}
Edit
)}
{/* Profile details: name, username, bio, and link to twitter/discord */} {user.name} @{user.username} {user.bio && ( <>
)} {user.website && ( {user.website} )} {user.twitterHandle && ( Twitter {user.twitterHandle} )} {user.discordHandle && ( Discord {user.discordHandle} )} {usersContracts !== 'loading' && commentsByContract != 'loading' ? ( { const tabId = tabName.toLowerCase() const subpath = tabId === 'markets' ? '' : '/' + tabId // 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` window.history.replaceState('', '', `/${user.username}${subpath}`) }} tabs={[ { title: 'Markets', content: , tabIcon: (
{usersContracts.length}
), }, { title: 'Comments', content: ( ), tabIcon: (
{usersComments.length}
), }, { title: 'Bets', content: (
{isCurrentUser && }
), tabIcon: (
{usersBets.length}
), }, ]} /> ) : ( )}
) } // 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] } import { ExclamationIcon } from '@heroicons/react/solid' function AlertBox(props: { title: string; text: string }) { const { title, text } = props return (

{title}

) }