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:
parent
7704de6904
commit
a1c3d0a2dd
|
@ -1,7 +1,7 @@
|
||||||
import { Answer } from 'common/answer'
|
import { Answer } from 'common/answer'
|
||||||
import { FreeResponseContract } from 'common/contract'
|
import { FreeResponseContract } from 'common/contract'
|
||||||
import { ContractComment } from 'common/comment'
|
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 { Col } from 'web/components/layout/col'
|
||||||
import { Row } from 'web/components/layout/row'
|
import { Row } from 'web/components/layout/row'
|
||||||
import { Avatar } from 'web/components/avatar'
|
import { Avatar } from 'web/components/avatar'
|
||||||
|
@ -27,15 +27,16 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
const { username, avatarUrl, name, text } = answer
|
const { username, avatarUrl, name, text } = answer
|
||||||
|
|
||||||
const [replyTo, setReplyTo] = useState<ReplyTo>()
|
const [replyTo, setReplyTo] = useState<ReplyTo>()
|
||||||
const [highlighted, setHighlighted] = useState(false)
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const answerElementId = `answer-${answer.id}`
|
const answerElementId = `answer-${answer.id}`
|
||||||
|
const highlighted = router.asPath.endsWith(`#${answerElementId}`)
|
||||||
|
const answerRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (router.asPath.endsWith(`#${answerElementId}`)) {
|
if (highlighted && answerRef.current != null) {
|
||||||
setHighlighted(true)
|
answerRef.current.scrollIntoView(true)
|
||||||
}
|
}
|
||||||
}, [answerElementId, router.asPath])
|
}, [highlighted])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className="relative flex-1 items-stretch gap-3">
|
<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',
|
'gap-3 space-x-3 pt-4 transition-all duration-1000',
|
||||||
highlighted ? `-m-2 my-3 rounded bg-indigo-500/[0.2] p-2` : ''
|
highlighted ? `-m-2 my-3 rounded bg-indigo-500/[0.2] p-2` : ''
|
||||||
)}
|
)}
|
||||||
|
ref={answerRef}
|
||||||
id={answerElementId}
|
id={answerElementId}
|
||||||
>
|
>
|
||||||
<Avatar username={username} avatarUrl={avatarUrl} />
|
<Avatar username={username} avatarUrl={avatarUrl} />
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { ContractComment } from 'common/comment'
|
import { ContractComment } from 'common/comment'
|
||||||
import { Contract } from 'common/contract'
|
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 { useUser } from 'web/hooks/use-user'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
|
@ -94,16 +94,19 @@ export function FeedComment(props: {
|
||||||
money = formatMoney(Math.abs(comment.betAmount))
|
money = formatMoney(Math.abs(comment.betAmount))
|
||||||
}
|
}
|
||||||
|
|
||||||
const [highlighted, setHighlighted] = useState(false)
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const highlighted = router.asPath.endsWith(`#${comment.id}`)
|
||||||
|
const commentRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (router.asPath.endsWith(`#${comment.id}`)) {
|
if (highlighted && commentRef.current != null) {
|
||||||
setHighlighted(true)
|
commentRef.current.scrollIntoView(true)
|
||||||
}
|
}
|
||||||
}, [comment.id, router.asPath])
|
}, [highlighted])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row
|
<Row
|
||||||
|
ref={commentRef}
|
||||||
id={comment.id}
|
id={comment.id}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'relative',
|
'relative',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user