2022-08-13 03:42:58 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
2022-08-19 08:06:40 +00:00
|
|
|
import { Comment, ContractComment } from 'common/comment'
|
2022-08-16 05:43:46 +00:00
|
|
|
import { groupConsecutive } from 'common/util/array'
|
2022-08-13 03:42:58 +00:00
|
|
|
import { getUsersComments } from 'web/lib/firebase/comments'
|
2022-05-05 22:30:30 +00:00
|
|
|
import { SiteLink } from './site-link'
|
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { Avatar } from './avatar'
|
|
|
|
import { RelativeTimestamp } from './relative-timestamp'
|
|
|
|
import { UserLink } from './user-page'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { User } from 'common/user'
|
2022-05-05 22:30:30 +00:00
|
|
|
import { Col } from './layout/col'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { Content } from './editor'
|
2022-08-13 03:42:58 +00:00
|
|
|
import { Pagination } from './pagination'
|
|
|
|
import { LoadingIndicator } from './loading-indicator'
|
|
|
|
|
|
|
|
const COMMENTS_PER_PAGE = 50
|
|
|
|
|
2022-08-16 05:43:46 +00:00
|
|
|
function contractPath(slug: string) {
|
|
|
|
// by convention this includes the contract creator username, but we don't
|
|
|
|
// have that handy, so we just put /market/
|
|
|
|
return `/market/${slug}`
|
|
|
|
}
|
2022-05-05 22:30:30 +00:00
|
|
|
|
2022-08-13 03:42:58 +00:00
|
|
|
export function UserCommentsList(props: { user: User }) {
|
|
|
|
const { user } = props
|
|
|
|
const [comments, setComments] = useState<ContractComment[] | undefined>()
|
|
|
|
const [page, setPage] = useState(0)
|
|
|
|
const start = page * COMMENTS_PER_PAGE
|
|
|
|
const end = start + COMMENTS_PER_PAGE
|
2022-07-11 00:19:35 +00:00
|
|
|
|
2022-08-13 03:42:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
getUsersComments(user.id).then((cs) => {
|
|
|
|
// we don't show comments in groups here atm, just comments on contracts
|
2022-08-19 08:06:40 +00:00
|
|
|
setComments(
|
|
|
|
cs.filter((c) => c.commentType == 'contract') as ContractComment[]
|
|
|
|
)
|
2022-08-13 03:42:58 +00:00
|
|
|
})
|
|
|
|
}, [user.id])
|
2022-05-05 22:30:30 +00:00
|
|
|
|
2022-08-16 05:43:46 +00:00
|
|
|
if (comments == null) {
|
2022-08-13 03:42:58 +00:00
|
|
|
return <LoadingIndicator />
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:43:46 +00:00
|
|
|
const pageComments = groupConsecutive(comments.slice(start, end), (c) => {
|
|
|
|
return { question: c.contractQuestion, slug: c.contractSlug }
|
|
|
|
})
|
2022-05-05 22:30:30 +00:00
|
|
|
return (
|
|
|
|
<Col className={'bg-white'}>
|
2022-08-13 03:42:58 +00:00
|
|
|
{pageComments.map(({ key, items }, i) => {
|
2022-07-26 07:10:11 +00:00
|
|
|
return (
|
2022-08-13 03:42:58 +00:00
|
|
|
<div key={start + i} className="border-b p-5">
|
2022-07-26 07:10:11 +00:00
|
|
|
<SiteLink
|
2022-08-12 19:10:45 +00:00
|
|
|
className="mb-2 block pb-2 font-medium text-indigo-700"
|
2022-08-16 05:43:46 +00:00
|
|
|
href={contractPath(key.slug)}
|
2022-07-26 07:10:11 +00:00
|
|
|
>
|
2022-08-16 05:43:46 +00:00
|
|
|
{key.question}
|
2022-05-05 22:30:30 +00:00
|
|
|
</SiteLink>
|
2022-08-12 19:10:45 +00:00
|
|
|
<Col className="gap-6">
|
2022-08-13 03:42:58 +00:00
|
|
|
{items.map((comment) => (
|
2022-08-12 19:10:45 +00:00
|
|
|
<ProfileComment
|
|
|
|
key={comment.id}
|
|
|
|
comment={comment}
|
|
|
|
className="relative flex items-start space-x-3"
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
2022-05-05 22:30:30 +00:00
|
|
|
</div>
|
2022-07-26 07:10:11 +00:00
|
|
|
)
|
|
|
|
})}
|
2022-08-13 03:42:58 +00:00
|
|
|
<Pagination
|
|
|
|
page={page}
|
|
|
|
itemsPerPage={COMMENTS_PER_PAGE}
|
|
|
|
totalItems={comments.length}
|
|
|
|
setPage={setPage}
|
|
|
|
/>
|
2022-05-05 22:30:30 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-26 07:10:11 +00:00
|
|
|
function ProfileComment(props: { comment: Comment; className?: string }) {
|
|
|
|
const { comment, className } = props
|
2022-08-06 20:39:52 +00:00
|
|
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
|
|
|
comment
|
2022-05-05 22:30:30 +00:00
|
|
|
// TODO: find and attach relevant bets by comment betId at some point
|
|
|
|
return (
|
2022-07-26 07:10:11 +00:00
|
|
|
<Row className={className}>
|
|
|
|
<Avatar username={userUsername} avatarUrl={userAvatarUrl} />
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
<p className="mt-0.5 text-sm text-gray-500">
|
|
|
|
<UserLink
|
|
|
|
className="text-gray-500"
|
|
|
|
username={userUsername}
|
|
|
|
name={userName}
|
|
|
|
/>{' '}
|
|
|
|
<RelativeTimestamp time={createdTime} />
|
|
|
|
</p>
|
2022-08-10 02:04:55 +00:00
|
|
|
<Content content={content || text} smallImage />
|
2022-07-26 07:10:11 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
2022-05-05 22:30:30 +00:00
|
|
|
)
|
|
|
|
}
|