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-06-13 04:42:41 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
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-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-06-08 13:24:12 +00:00
|
|
|
import { User } from 'common/user'
|
2022-06-08 23:09:49 +00:00
|
|
|
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-08-06 20:39:52 +00:00
|
|
|
const [replyToUser, setReplyToUser] =
|
|
|
|
useState<Pick<User, 'id' | 'username'>>()
|
2022-06-08 13:24:12 +00:00
|
|
|
const [showReply, setShowReply] = useState(false)
|
|
|
|
const [highlighted, setHighlighted] = useState(false)
|
|
|
|
const router = useRouter()
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
const answerElementId = `answer-${answer.id}`
|
2022-06-08 23:09:49 +00:00
|
|
|
|
|
|
|
const scrollAndOpenReplyInput = useEvent(
|
2022-08-19 08:06:40 +00:00
|
|
|
(comment?: ContractComment, answer?: Answer) => {
|
2022-08-06 20:39:52 +00:00
|
|
|
setReplyToUser(
|
|
|
|
comment
|
|
|
|
? { id: comment.userId, username: comment.userUsername }
|
|
|
|
: answer
|
|
|
|
? { id: answer.userId, username: answer.username }
|
|
|
|
: undefined
|
|
|
|
)
|
2022-06-08 23:09:49 +00:00
|
|
|
setShowReply(true)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-17 15:55:26 +00:00
|
|
|
useEffect(() => {
|
2022-05-17 16:13:29 +00:00
|
|
|
if (router.asPath.endsWith(`#${answerElementId}`)) {
|
2022-05-17 15:55:26 +00:00
|
|
|
setHighlighted(true)
|
|
|
|
}
|
2022-05-21 21:48:15 +00:00
|
|
|
}, [answerElementId, router.asPath])
|
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` : ''
|
|
|
|
)}
|
|
|
|
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-06-08 13:24:12 +00:00
|
|
|
onClick={() => scrollAndOpenReplyInput(undefined, answer)}
|
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"
|
|
|
|
onClick={() => scrollAndOpenReplyInput(undefined, answer)}
|
|
|
|
>
|
|
|
|
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-09-20 22:58:47 +00:00
|
|
|
tips={tips[comment.id] ?? {}}
|
2022-08-30 09:41:47 +00:00
|
|
|
onReplyClick={scrollAndOpenReplyInput}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
2022-05-17 15:55:26 +00:00
|
|
|
{showReply && (
|
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-08-06 20:39:52 +00:00
|
|
|
replyToUser={replyToUser}
|
|
|
|
onSubmitComment={() => setShowReply(false)}
|
2022-05-17 15:55:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|