import { useEffect, useState } from 'react' import { Dictionary, keyBy, uniq } from 'lodash' import { Comment } from 'common/comment' import { Contract } from 'common/contract' import { filterDefined, groupConsecutive } from 'common/util/array' import { contractPath } from 'web/lib/firebase/contracts' import { getUsersComments } from 'web/lib/firebase/comments' import { getContractFromId } from 'web/lib/firebase/contracts' import { SiteLink } from './site-link' import { Row } from './layout/row' import { Avatar } from './avatar' import { RelativeTimestamp } from './relative-timestamp' import { UserLink } from './user-page' import { User } from 'common/user' import { Col } from './layout/col' import { Content } from './editor' import { Pagination } from './pagination' import { LoadingIndicator } from './loading-indicator' const COMMENTS_PER_PAGE = 50 type ContractComment = Comment & { contractId: string } export function UserCommentsList(props: { user: User }) { const { user } = props const [comments, setComments] = useState() const [contracts, setContracts] = useState | undefined>() const [page, setPage] = useState(0) const start = page * COMMENTS_PER_PAGE const end = start + COMMENTS_PER_PAGE useEffect(() => { getUsersComments(user.id).then((cs) => { // we don't show comments in groups here atm, just comments on contracts setComments(cs.filter((c) => c.contractId) as ContractComment[]) }) }, [user.id]) useEffect(() => { if (comments) { const contractIds = uniq(comments.map((c) => c.contractId)) Promise.all(contractIds.map(getContractFromId)).then((contracts) => { setContracts(keyBy(filterDefined(contracts), 'id')) }) } }, [comments]) if (comments == null || contracts == null) { return } const pageComments = groupConsecutive( comments.slice(start, end), (c) => c.contractId ) return ( {pageComments.map(({ key, items }, i) => { const contract = contracts[key] return (
{contract.question} {items.map((comment) => ( ))}
) })} ) } function ProfileComment(props: { comment: Comment; className?: string }) { const { comment, className } = props const { text, content, userUsername, userName, userAvatarUrl, createdTime } = comment // TODO: find and attach relevant bets by comment betId at some point return (

{' '}

) }