2022-05-17 15:55:26 +00:00
|
|
|
import { Answer } from 'common/answer'
|
2022-08-30 09:41:47 +00:00
|
|
|
import { FreeResponseContract } from 'common/contract'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from 'common/comment'
|
2022-09-22 19:58:40 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2022-10-05 06:16:56 +00:00
|
|
|
import { sum } from 'lodash'
|
2022-05-17 15:55:26 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Avatar } from 'web/components/avatar'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import clsx from 'clsx'
|
2022-05-18 14:42:13 +00:00
|
|
|
import {
|
2022-09-07 22:09:20 +00:00
|
|
|
ContractCommentInput,
|
2022-08-30 09:41:47 +00:00
|
|
|
FeedComment,
|
2022-09-22 19:40:44 +00:00
|
|
|
ReplyTo,
|
2022-05-18 14:42:13 +00:00
|
|
|
} from 'web/components/feed/feed-comments'
|
2022-05-17 15:55:26 +00:00
|
|
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-10-05 06:16:56 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { useEvent } from 'web/hooks/use-event'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { CommentTipMap } from 'web/hooks/use-tip-txns'
|
2022-08-30 15:38:59 +00:00
|
|
|
import { UserLink } from 'web/components/user-link'
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
export function FeedAnswerCommentGroup(props: {
|
2022-08-30 09:41:47 +00:00
|
|
|
contract: FreeResponseContract
|
2022-05-17 15:55:26 +00:00
|
|
|
answer: Answer
|
2022-08-30 09:41:47 +00:00
|
|
|
answerComments: ContractComment[]
|
2022-06-18 03:28:16 +00:00
|
|
|
tips: CommentTipMap
|
2022-05-17 15:55:26 +00:00
|
|
|
}) {
|
2022-09-20 22:25:58 +00:00
|
|
|
const { answer, contract, answerComments, tips } = props
|
2022-05-17 15:55:26 +00:00
|
|
|
const { username, avatarUrl, name, text } = answer
|
2022-06-08 13:24:12 +00:00
|
|
|
|
2022-09-22 19:40:44 +00:00
|
|
|
const [replyTo, setReplyTo] = useState<ReplyTo>()
|
2022-10-05 06:16:56 +00:00
|
|
|
const user = useUser()
|
2022-06-08 13:24:12 +00:00
|
|
|
const router = useRouter()
|
2022-05-17 15:55:26 +00:00
|
|
|
const answerElementId = `answer-${answer.id}`
|
2022-09-22 19:58:40 +00:00
|
|
|
const highlighted = router.asPath.endsWith(`#${answerElementId}`)
|
|
|
|
const answerRef = useRef<HTMLDivElement>(null)
|
2022-06-08 23:09:49 +00:00
|
|
|
|
2022-10-05 06:16:56 +00:00
|
|
|
const onSubmitComment = useEvent(() => setReplyTo(undefined))
|
|
|
|
const onReplyClick = useEvent((comment: ContractComment) => {
|
|
|
|
setReplyTo({ id: comment.id, username: comment.userUsername })
|
|
|
|
})
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
useEffect(() => {
|
2022-09-22 19:58:40 +00:00
|
|
|
if (highlighted && answerRef.current != null) {
|
|
|
|
answerRef.current.scrollIntoView(true)
|
2022-05-17 15:55:26 +00:00
|
|
|
}
|
2022-09-22 19:58:40 +00:00
|
|
|
}, [highlighted])
|
2022-05-17 15:55:26 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-20 21:03:33 +00:00
|
|
|
<Col className="relative flex-1 items-stretch gap-3">
|
2022-05-17 15:55:26 +00:00
|
|
|
<Row
|
|
|
|
className={clsx(
|
2022-08-30 09:41:47 +00:00
|
|
|
'gap-3 space-x-3 pt-4 transition-all duration-1000',
|
2022-05-17 15:55:26 +00:00
|
|
|
highlighted ? `-m-2 my-3 rounded bg-indigo-500/[0.2] p-2` : ''
|
|
|
|
)}
|
2022-09-22 19:58:40 +00:00
|
|
|
ref={answerRef}
|
2022-05-17 15:55:26 +00:00
|
|
|
id={answerElementId}
|
|
|
|
>
|
2022-07-21 03:13:37 +00:00
|
|
|
<Avatar username={username} avatarUrl={avatarUrl} />
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
<Col className="min-w-0 flex-1 lg:gap-1">
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
<UserLink username={username} name={name} /> answered
|
|
|
|
<CopyLinkDateTimeComponent
|
2022-06-22 16:35:50 +00:00
|
|
|
prefix={contract.creatorUsername}
|
|
|
|
slug={contract.slug}
|
2022-05-17 15:55:26 +00:00
|
|
|
createdTime={answer.createdTime}
|
|
|
|
elementId={answerElementId}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-07-21 03:13:37 +00:00
|
|
|
<Col className="align-items justify-between gap-2 sm:flex-row">
|
2022-05-17 15:55:26 +00:00
|
|
|
<span className="whitespace-pre-line text-lg">
|
|
|
|
<Linkify text={text} />
|
|
|
|
</span>
|
2022-09-20 21:11:26 +00:00
|
|
|
<div className="sm:hidden">
|
2022-05-17 15:55:26 +00:00
|
|
|
<button
|
2022-09-20 21:03:33 +00:00
|
|
|
className="text-xs font-bold text-gray-500 hover:underline"
|
2022-09-22 19:40:44 +00:00
|
|
|
onClick={() =>
|
|
|
|
setReplyTo({ id: answer.id, username: answer.username })
|
|
|
|
}
|
2022-05-17 15:55:26 +00:00
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-09-20 21:11:26 +00:00
|
|
|
</Col>
|
|
|
|
<div className="justify-initial hidden sm:block">
|
|
|
|
<button
|
|
|
|
className="text-xs font-bold text-gray-500 hover:underline"
|
2022-09-22 19:40:44 +00:00
|
|
|
onClick={() =>
|
|
|
|
setReplyTo({ id: answer.id, username: answer.username })
|
|
|
|
}
|
2022-09-20 21:11:26 +00:00
|
|
|
>
|
|
|
|
Reply
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-05-17 15:55:26 +00:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-08-30 09:41:47 +00:00
|
|
|
<Col className="gap-3 pl-1">
|
|
|
|
{answerComments.map((comment) => (
|
|
|
|
<FeedComment
|
|
|
|
key={comment.id}
|
|
|
|
indent={true}
|
|
|
|
contract={contract}
|
|
|
|
comment={comment}
|
2022-10-05 06:16:56 +00:00
|
|
|
myTip={user ? tips[comment.id]?.[user.id] : undefined}
|
|
|
|
totalTip={sum(Object.values(tips[comment.id] ?? {}))}
|
|
|
|
showTip={true}
|
|
|
|
onReplyClick={onReplyClick}
|
2022-08-30 09:41:47 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
2022-09-22 19:40:44 +00:00
|
|
|
{replyTo && (
|
2022-09-20 21:03:33 +00:00
|
|
|
<div className="relative ml-7">
|
2022-06-08 13:24:12 +00:00
|
|
|
<span
|
2022-08-30 09:41:47 +00:00
|
|
|
className="absolute -left-1 -ml-[1px] mt-[1.25rem] h-2 w-0.5 rotate-90 bg-gray-200"
|
2022-06-08 13:24:12 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-09-07 22:09:20 +00:00
|
|
|
<ContractCommentInput
|
2022-05-17 15:55:26 +00:00
|
|
|
contract={contract}
|
2022-06-08 13:24:12 +00:00
|
|
|
parentAnswerOutcome={answer.number.toString()}
|
2022-09-22 19:40:44 +00:00
|
|
|
replyTo={replyTo}
|
2022-10-05 06:16:56 +00:00
|
|
|
onSubmitComment={onSubmitComment}
|
2022-05-17 15:55:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|