Fix up comment permalink stuff (#915)

* Eliminate needless state/effects to highlight comments

* Scroll to comment on render if highlighted
This commit is contained in:
Marshall Polaris 2022-09-22 12:58:40 -07:00 committed by GitHub
parent 7704de6904
commit a1c3d0a2dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -1,7 +1,7 @@
import { Answer } from 'common/answer'
import { FreeResponseContract } from 'common/contract'
import { ContractComment } from 'common/comment'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row'
import { Avatar } from 'web/components/avatar'
@ -27,15 +27,16 @@ export function FeedAnswerCommentGroup(props: {
const { username, avatarUrl, name, text } = answer
const [replyTo, setReplyTo] = useState<ReplyTo>()
const [highlighted, setHighlighted] = useState(false)
const router = useRouter()
const answerElementId = `answer-${answer.id}`
const highlighted = router.asPath.endsWith(`#${answerElementId}`)
const answerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (router.asPath.endsWith(`#${answerElementId}`)) {
setHighlighted(true)
if (highlighted && answerRef.current != null) {
answerRef.current.scrollIntoView(true)
}
}, [answerElementId, router.asPath])
}, [highlighted])
return (
<Col className="relative flex-1 items-stretch gap-3">
@ -44,6 +45,7 @@ export function FeedAnswerCommentGroup(props: {
'gap-3 space-x-3 pt-4 transition-all duration-1000',
highlighted ? `-m-2 my-3 rounded bg-indigo-500/[0.2] p-2` : ''
)}
ref={answerRef}
id={answerElementId}
>
<Avatar username={username} avatarUrl={avatarUrl} />

View File

@ -1,6 +1,6 @@
import { ContractComment } from 'common/comment'
import { Contract } from 'common/contract'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { useUser } from 'web/hooks/use-user'
import { formatMoney } from 'common/util/format'
import { useRouter } from 'next/router'
@ -94,16 +94,19 @@ export function FeedComment(props: {
money = formatMoney(Math.abs(comment.betAmount))
}
const [highlighted, setHighlighted] = useState(false)
const router = useRouter()
const highlighted = router.asPath.endsWith(`#${comment.id}`)
const commentRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (router.asPath.endsWith(`#${comment.id}`)) {
setHighlighted(true)
if (highlighted && commentRef.current != null) {
commentRef.current.scrollIntoView(true)
}
}, [comment.id, router.asPath])
}, [highlighted])
return (
<Row
ref={commentRef}
id={comment.id}
className={clsx(
'relative',