* check new comments every 10 seconds * enhance: clear newComments on child comments when we show a topLevel new comment; cleanup: resolvers, logs * handle comments of comments, new structure to clear newComments on childs * use original recursive comments data structure * correct comment structure after deduplication * faster newComments query deduplication, don't need to know how many comments are there * cleanup: comments on newComments fetches and dedupes * cleanup, use correct function declarations * stop polling after 30 minutes, pause polling if user is not on the page * ActionTooltip indicating that the user is in a live comment section * handleVisibilityChange to control polling by visibility * paused polling styling, check activity on 1 minute intervals and visibility change, light cleanup * user can resume polling without refreshing the page * better naming, straightforward dedupeComment on newComment arrival * cleanup: better naming, get latest comment creation, correct order of comment injection * cleanup: refactor live comments related functions to use-live-comments.js * refactor: clearer naming, optimized polling and date retrieval logic, use of constants, general cleanup * ui: place ShowNewComments in the bottom-right corner of nested comments * fix: make updateQuery sort-aware to correctly inject the comment in the correct Item query * cleanup: better naming; fix: usecallback on live comments component; fix leak on useEffect because of missing sort atomic apollo cache manipulations; manage top sort not being present in item query cache queue nested comments without a parent, retry on the next poll fix commit messages * fix: don't show unpaid comments; cleanup: compact cache merge/dedupe, queue comments via state * fix: read new comments fragments to inject fresh new comments, fixing dropped comments; ui: show amount of new comments refactor: correct function positioning; cleanup: useless logs * enhance: queuedComments Ref, cache-and-network fetch policy; freshNewComments readFragment fallback to received comment * cleanup: detailed comments and better ShowNewComment text * fix: while showing new comments, also update ncomments for UI and pagination * refactor: ShowNewComments is its own component; cleanup: proven useless dedupe on ShowNewComments, count nested ncomments from fresh new comments * enhance: direct latest comment createdAt calc with reduce * cleanup queue on unmount * feat: live comments indicator for bottomed-out replies, ncomments updates; fix: nested comment structures - new comments indicator for bottomed-out replies - ncomments sync for parent and its ancestors - limited comments fragment for comments that don't have CommentsRecursive - reduce cache complexity by removing useless roundtrips ux: live comments indicator on bottomedOut replies fix: dedupe newComments before displaying ShowNewComments to avoid false positives enhance: store ids of new comments in the cache, instead of carrying full comments that would get discarded anyway hotfix: newComments deduplication ID mismatch, filter null comments from freshNewComments fix: ncomments not updating for all comment levels; refactor: share Reply update ancestors' ncomments function with ShowNewComments cleanup: better naming to indicate the total number of comments including nested comments fix: increment parent comment ncomments cleanup: Items that will have comments will always have a structure where item.comments is true cleanup: reduce code complexity checking the nested comment update result instead of preventively reading the fragment cleanup: avoid double-updating ncomments on parent fix: don't use CommentsRecursive for bottomed-out comments cleanup: better fragment naming; add TODO for absolute bottom comments * backport live comments logic enhancements use-live-comments: - remove useless dedupe against already present comments - check newComments.comments length to tell if there are new comments - code reordering show-new-comments: - show all new comments recursively for nested comments - get always the newest comments to inject also their own child new comments - update local storage commentsViewedAt on comment injection - respect depth on comment injection comments.js - apollo cache manipulations now live here * hotfix: handle undefined item.comments.comments on dedupe * hotfix: limited fragment for recursive comment collection; protect from null fragments; add missing deps to memoization * docs: clarify ncomments updates * cleanup: remove unused export * count and show only the direct new comments and recursively their children enhance: dedupe against existing comments only in the component enhance: recursive count/injection share the same logic * fix regression on top level counting * hotfix: introduce readNestedCommentsFragment in lib/comments.js * fix: count also existing comments of a new comment; cleanup: use readCommentFragment also for prepareComments; reduce freshNewComments usage * add support for comments at the deepest level fixes: - client-side navigation re-fetched all new comments because 'after' was cached, now the latest new comment time persists in sessionStorage enhancements: - use CommentWithNewMinimal fragment fallback for comments at the deepest level - tweak ReplyOnAnotherPage to show also how many direct new comments are there cleanup: - queue management is not needed anymore, therefore it has been removed * cleanup: remove logs * revert counting on ReplyOnAnotherPage, TODO for enhancements PR * move ShowNewComments to CommentsHeader for top level comments * fix: update commentsViewedAfterComment to support ncomments * fix typo, lint * cleanup: remove old CSS * enhance: inject topLevel and its children new comments, simplify injection logic - top-level and nested comment handling share the same recursion logic - ShowNewComments references the item object for every type of comments — note: item from item-full.js is passed to comments.js - depth now starts at 0 to support top level comments - injection and counting now reach the deepest level, updating also the deepest comment * cleanup: remove unused topLevel prop * fix: deepest comments don't have CommentsRecursive structure, don't access it on injection * move top level ShowNewComments above CommentsHeader; preserve space to avoid vertical layout shifting * cleanup: remove unused item on CommentsHeader --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
348 lines
13 KiB
JavaScript
348 lines
13 KiB
JavaScript
import itemStyles from './item.module.css'
|
|
import styles from './comment.module.css'
|
|
import Text, { SearchText } from './text'
|
|
import Link from 'next/link'
|
|
import Reply from './reply'
|
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
import UpVote from './upvote'
|
|
import Eye from '@/svgs/eye-fill.svg'
|
|
import EyeClose from '@/svgs/eye-close-line.svg'
|
|
import { useRouter } from 'next/router'
|
|
import CommentEdit from './comment-edit'
|
|
import { USER_ID, COMMENT_DEPTH_LIMIT, UNKNOWN_LINK_REL } from '@/lib/constants'
|
|
import PayBounty from './pay-bounty'
|
|
import BountyIcon from '@/svgs/bounty-bag.svg'
|
|
import ActionTooltip from './action-tooltip'
|
|
import { numWithUnits } from '@/lib/format'
|
|
import Share from './share'
|
|
import ItemInfo from './item-info'
|
|
import Badge from 'react-bootstrap/Badge'
|
|
import { RootProvider, useRoot } from './root'
|
|
import { useMe } from './me'
|
|
import { useQuoteReply } from './use-quote-reply'
|
|
import { DownZap } from './dont-link-this'
|
|
import Skull from '@/svgs/death-skull.svg'
|
|
import { commentSubTreeRootId } from '@/lib/item'
|
|
import Pin from '@/svgs/pushpin-fill.svg'
|
|
import LinkToContext from './link-to-context'
|
|
import Boost from './boost-button'
|
|
import { gql, useApolloClient } from '@apollo/client'
|
|
import classNames from 'classnames'
|
|
import { ShowNewComments } from './show-new-comments'
|
|
|
|
function Parent ({ item, rootText }) {
|
|
const root = useRoot()
|
|
|
|
const ParentFrag = () => (
|
|
<>
|
|
<span> \ </span>
|
|
<Link href={`/items/${item.parentId}`} className='text-reset'>
|
|
parent
|
|
</Link>
|
|
</>
|
|
)
|
|
|
|
return (
|
|
<>
|
|
{Number(root.id) !== Number(item.parentId) && <ParentFrag />}
|
|
<span> \ </span>
|
|
<Link href={`/items/${root.id}`} className='text-reset'>
|
|
{rootText || 'on:'} {root?.title}
|
|
</Link>
|
|
{root.subName &&
|
|
<Link href={`/~${root.subName}`}>
|
|
{' '}<Badge className={itemStyles.newComment} bg={null}>{root.subName}</Badge>
|
|
</Link>}
|
|
</>
|
|
)
|
|
}
|
|
|
|
const truncateString = (string = '', maxLength = 140) =>
|
|
string.length > maxLength
|
|
? `${string.substring(0, maxLength)} […]`
|
|
: string
|
|
|
|
export function CommentFlat ({ item, rank, siblingComments, ...props }) {
|
|
const router = useRouter()
|
|
const [href, as] = useMemo(() => {
|
|
const rootId = commentSubTreeRootId(item)
|
|
return [{
|
|
pathname: '/items/[id]',
|
|
query: { id: rootId, commentId: item.id }
|
|
}, `/items/${rootId}`]
|
|
}, [item?.id])
|
|
|
|
return (
|
|
<>
|
|
{rank
|
|
? (
|
|
<div className={`${itemStyles.rank} pt-2 align-self-start`}>
|
|
{rank}
|
|
</div>)
|
|
: <div />}
|
|
<LinkToContext
|
|
className='py-2'
|
|
onClick={e => {
|
|
e.preventDefault()
|
|
router.push(href, as)
|
|
}}
|
|
href={href}
|
|
>
|
|
<RootProvider root={item.root}>
|
|
<Comment item={item} {...props} />
|
|
</RootProvider>
|
|
</LinkToContext>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default function Comment ({
|
|
item, children, replyOpen, includeParent, topLevel,
|
|
rootText, noComments, noReply, truncate, depth, pin, setDisableRetry, disableRetry
|
|
}) {
|
|
const [edit, setEdit] = useState()
|
|
const { me } = useMe()
|
|
const isHiddenFreebie = me?.privates?.satsFilter !== 0 && !item.mine && item.freebie && !item.freedFreebie
|
|
const isDeletedChildless = item?.ncomments === 0 && item?.deletedAt
|
|
const [collapse, setCollapse] = useState(
|
|
(isHiddenFreebie || isDeletedChildless || item?.user?.meMute || (item?.outlawed && !me?.privates?.wildWestMode)) && !includeParent
|
|
? 'yep'
|
|
: 'nope')
|
|
const ref = useRef(null)
|
|
const router = useRouter()
|
|
const root = useRoot()
|
|
const { ref: textRef, quote, quoteReply, cancelQuote } = useQuoteReply({ text: item.text })
|
|
|
|
const { cache } = useApolloClient()
|
|
|
|
useEffect(() => {
|
|
const comment = cache.readFragment({
|
|
id: `Item:${router.query.commentId}`,
|
|
fragment: gql`
|
|
fragment CommentPath on Item {
|
|
path
|
|
}`
|
|
})
|
|
if (comment?.path.split('.').includes(item.id)) {
|
|
window.localStorage.setItem(`commentCollapse:${item.id}`, 'nope')
|
|
}
|
|
setCollapse(window.localStorage.getItem(`commentCollapse:${item.id}`) || collapse)
|
|
if (Number(router.query.commentId) === Number(item.id)) {
|
|
// HACK wait for other comments to uncollapse if they're collapsed
|
|
setTimeout(() => {
|
|
ref.current.scrollIntoView({ behavior: 'instant', block: 'start' })
|
|
// make sure we can outline a comment again if it was already outlined before
|
|
ref.current.addEventListener('animationend', () => {
|
|
ref.current.classList.remove('outline-it')
|
|
}, { once: true })
|
|
ref.current.classList.add('outline-it')
|
|
}, 100)
|
|
}
|
|
}, [item.id, cache, router.query.commentId])
|
|
|
|
useEffect(() => {
|
|
if (router.query.commentsViewedAt &&
|
|
me?.id !== item.user?.id &&
|
|
new Date(item.createdAt).getTime() > router.query.commentsViewedAt) {
|
|
ref.current.classList.add('outline-new-comment')
|
|
}
|
|
}, [item.id])
|
|
|
|
const bottomedOut = depth === COMMENT_DEPTH_LIMIT || (item.comments?.comments.length === 0 && item.nDirectComments > 0)
|
|
// Don't show OP badge when anon user comments on anon user posts
|
|
const op = root.user.name === item.user.name && Number(item.user.id) !== USER_ID.anon
|
|
? 'OP'
|
|
: root.forwards?.some(f => f.user.name === item.user.name) && Number(item.user.id) !== USER_ID.anon
|
|
? 'fwd'
|
|
: null
|
|
const bountyPaid = root.bountyPaidTo?.includes(Number(item.id))
|
|
|
|
return (
|
|
<div
|
|
ref={ref} className={includeParent ? '' : `${styles.comment} ${collapse === 'yep' ? styles.collapsed : ''}`}
|
|
onMouseEnter={() => ref.current.classList.add('outline-new-comment-unset')}
|
|
onTouchStart={() => ref.current.classList.add('outline-new-comment-unset')}
|
|
>
|
|
<div className={`${itemStyles.item} ${styles.item}`}>
|
|
{item.outlawed && !me?.privates?.wildWestMode
|
|
? <Skull className={styles.dontLike} width={24} height={24} />
|
|
: pin
|
|
? <Pin width={22} height={22} className={styles.pin} />
|
|
: item.mine
|
|
? <Boost item={item} className={styles.upvote} />
|
|
: item.meDontLikeSats > item.meSats
|
|
? <DownZap width={24} height={24} className={styles.dontLike} item={item} />
|
|
: <UpVote item={item} className={styles.upvote} collapsed={collapse === 'yep'} />}
|
|
<div className={`${itemStyles.hunk} ${styles.hunk}`}>
|
|
<div className='d-flex align-items-center'>
|
|
{item.user?.meMute && !includeParent && collapse === 'yep'
|
|
? (
|
|
<span
|
|
className={`${itemStyles.other} ${styles.other} pointer`} onClick={() => {
|
|
setCollapse('nope')
|
|
window.localStorage.setItem(`commentCollapse:${item.id}`, 'nope')
|
|
}}
|
|
>reply from someone you muted
|
|
</span>)
|
|
: <ItemInfo
|
|
full={topLevel}
|
|
item={item}
|
|
commentsText='replies'
|
|
commentTextSingular='reply'
|
|
className={`${itemStyles.other} ${styles.other}`}
|
|
embellishUser={op && <><span> </span><Badge bg={op === 'fwd' ? 'secondary' : 'boost'} className={`${styles.op} bg-opacity-75`}>{op}</Badge></>}
|
|
onQuoteReply={quoteReply}
|
|
nested={!includeParent}
|
|
setDisableRetry={setDisableRetry}
|
|
disableRetry={disableRetry}
|
|
extraInfo={
|
|
<>
|
|
{includeParent && <Parent item={item} rootText={rootText} />}
|
|
{bountyPaid &&
|
|
<ActionTooltip notForm overlayText={`${numWithUnits(root.bounty)} paid`}>
|
|
<BountyIcon className={`${styles.bountyIcon} ${'fill-success vertical-align-middle'}`} height={16} width={16} />
|
|
</ActionTooltip>}
|
|
</>
|
|
}
|
|
edit={edit}
|
|
toggleEdit={e => { setEdit(!edit) }}
|
|
editText={edit ? 'cancel' : 'edit'}
|
|
/>}
|
|
|
|
{!includeParent && (collapse === 'yep'
|
|
? <Eye
|
|
className={styles.collapser} height={10} width={10} onClick={() => {
|
|
setCollapse('nope')
|
|
window.localStorage.setItem(`commentCollapse:${item.id}`, 'nope')
|
|
}}
|
|
/>
|
|
: <EyeClose
|
|
className={styles.collapser} height={10} width={10} onClick={() => {
|
|
setCollapse('yep')
|
|
window.localStorage.setItem(`commentCollapse:${item.id}`, 'yep')
|
|
}}
|
|
/>)}
|
|
{topLevel && (
|
|
<span className='d-flex ms-auto align-items-center'>
|
|
<Share title={item?.title} path={`/items/${item?.id}`} />
|
|
</span>
|
|
)}
|
|
</div>
|
|
{edit
|
|
? (
|
|
<CommentEdit
|
|
comment={item}
|
|
onSuccess={() => {
|
|
setEdit(!edit)
|
|
}}
|
|
/>
|
|
)
|
|
: (
|
|
<div className={styles.text} ref={textRef}>
|
|
{item.searchText
|
|
? <SearchText text={item.searchText} />
|
|
: (
|
|
<Text itemId={item.id} topLevel={topLevel} rel={item.rel ?? UNKNOWN_LINK_REL} outlawed={item.outlawed} imgproxyUrls={item.imgproxyUrls}>
|
|
{item.outlawed && !me?.privates?.wildWestMode
|
|
? '*stackers have outlawed this. turn on wild west mode in your [settings](/settings) to see outlawed content.*'
|
|
: truncate ? truncateString(item.text) : item.text}
|
|
</Text>)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
{collapse !== 'yep' && (
|
|
bottomedOut
|
|
? <div className={styles.children}><div className={classNames(styles.comment, 'mt-3')}><ReplyOnAnotherPage item={item} /></div></div>
|
|
: (
|
|
<div className={styles.children}>
|
|
{item.outlawed && !me?.privates?.wildWestMode
|
|
? <div className='py-2' />
|
|
: !noReply &&
|
|
<Reply depth={depth + 1} item={item} replyOpen={replyOpen} onCancelQuote={cancelQuote} onQuoteReply={quoteReply} quote={quote}>
|
|
{root.bounty && !bountyPaid && <PayBounty item={item} />}
|
|
{item.newComments?.length > 0 && (
|
|
<div className='ms-auto'>
|
|
<ShowNewComments item={item} depth={depth} />
|
|
</div>
|
|
)}
|
|
</Reply>}
|
|
{children}
|
|
<div className={styles.comments}>
|
|
{!noComments && item.comments?.comments
|
|
? (
|
|
<>
|
|
{item.comments.comments.map((item) => (
|
|
<Comment depth={depth + 1} key={item.id} item={item} />
|
|
))}
|
|
{item.comments.comments.length < item.nDirectComments && <ViewAllReplies id={item.id} nhas={item.ncomments} />}
|
|
</>
|
|
)
|
|
: null}
|
|
{/* TODO: add link to more comments if they're limited */}
|
|
</div>
|
|
</div>
|
|
)
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function ViewAllReplies ({ id, nshown, nhas }) {
|
|
const text = `view all ${nhas} replies`
|
|
|
|
return (
|
|
<div className={`d-block fw-bold ${styles.comment} pb-2 ps-3`}>
|
|
<Link href={`/items/${id}`} as={`/items/${id}`} className='text-muted'>
|
|
{text}
|
|
</Link>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ReplyOnAnotherPage ({ item }) {
|
|
const root = useRoot()
|
|
const rootId = commentSubTreeRootId(item, root)
|
|
|
|
let text = 'reply on another page'
|
|
if (item.ncomments > 0) {
|
|
text = `view all ${item.ncomments} replies`
|
|
}
|
|
|
|
return (
|
|
<Link href={`/items/${rootId}?commentId=${item.id}`} as={`/items/${rootId}`} className='pb-2 fw-bold d-flex align-items-center gap-2 text-muted'>
|
|
{text}
|
|
{item.newComments?.length > 0 && <div className={styles.newCommentDot} />}
|
|
</Link>
|
|
)
|
|
}
|
|
|
|
export function CommentSkeleton ({ skeletonChildren }) {
|
|
return (
|
|
<div className={styles.comment}>
|
|
<div className={`${itemStyles.item} ${itemStyles.skeleton} ${styles.item} ${styles.skeleton}`}>
|
|
<UpVote className={styles.upvote} />
|
|
<div className={`${itemStyles.hunk} ${styles.hunk}`}>
|
|
<div className={itemStyles.other}>
|
|
<span className={`${itemStyles.otherItem} clouds`} />
|
|
<span className={`${itemStyles.otherItem} clouds`} />
|
|
<span className={`${itemStyles.otherItem} clouds`} />
|
|
<span className={`${itemStyles.otherItem} ${itemStyles.otherItemLonger} clouds`} />
|
|
</div>
|
|
<div className={`${styles.text} clouds`} />
|
|
</div>
|
|
</div>
|
|
<div className={`${itemStyles.children} ${styles.children} ${styles.skeleton}`}>
|
|
<div className={styles.replyPadder}>
|
|
<div className={`${itemStyles.other} ${styles.reply} clouds`} />
|
|
</div>
|
|
<div className={`${styles.comments} ms-sm-1 ms-md-3`}>
|
|
{skeletonChildren
|
|
? <CommentSkeleton skeletonChildren={skeletonChildren - 1} />
|
|
: null}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|