* enhance: FaviconProvider, keep track of new comment IDs to change favicon, remove new comment IDs per outline removal * don't track oneself comments * enhance: auto-show new comments, idempotency by ignoring already injected comments, preserveScroll utility * fadeIn animation on comment injection; cleanup: remove unused counts and thread handling; non-critical fix: always give rootLastCommentAt a value * reliably preserve scroll position by tracking a reference found at the center of the viewport; cleanup: add more comments, add cleanup function * mitigate fractional scrolling subtle layout shifts by rounding the new reference element position * enhanced outlining system, favicon context keeps track of new comments presence - de-outlining now happens only for outlined comments - enhanced outlining: add outline only if isNewComment - de-outlining will remove the new comments favicon - on unmount remove the new comments favicon * remove the new comments favicon on new comments injection * track only deduplicated new comments * fix typo * clearer unsetOutline conditions, fix typo in live comments hook * backport: remove the injectedComment class from injected comments after animation ends * set the new comments favicon on any new outlined comment * enhance: directly inject new comments; cleanup: dismantle ShowNewComments, remove newComments field * tweaks: slower injection animation, clear favicon on Comment section unmount * change nDirectComments bug strategy to avoiding updates on comment edit * cleanup: better naming, re-instate injected comments outline * injection: major cache utilities refactor, don't preserve scroll if no comments have been injected - don't preserve scroll if after deduplication we don't inject any comments - use manual read/write cache updates to control the flow -- allows to check if we are really injecting or not - reduce polling to 5 seconds instead of 10 - light cleanup -- removed update cache functions -- added 'injected' to typeDefs (gql consistency) * cleanup: detailed comments, refactor, remove clutter Refactor: + clearer variables + depth calculation utility function + use destructured Apollo cache + extract item object from item query + skip ignored comment instead of ending the loop CSS: + from-to fadeIn animation keyframes - floatingComments unused class Favicon: + provider exported by default * fix wrong merge * split: remove favicon context * split: remove favicon pngs * regression: revert to updateQuery for multiple comment fragments handling * reverse multiple reads for deduplication on comment injection * fix regression on apollo manipulations via fn; cleanup: remove wrong deps from outlining
116 lines
3.7 KiB
JavaScript
116 lines
3.7 KiB
JavaScript
import { Fragment, useMemo } from 'react'
|
|
import Comment, { CommentSkeleton } from './comment'
|
|
import styles from './header.module.css'
|
|
import Nav from 'react-bootstrap/Nav'
|
|
import Navbar from 'react-bootstrap/Navbar'
|
|
import { numWithUnits } from '@/lib/format'
|
|
import { defaultCommentSort } from '@/lib/item'
|
|
import { useRouter } from 'next/router'
|
|
import MoreFooter from './more-footer'
|
|
import { FULL_COMMENTS_THRESHOLD } from '@/lib/constants'
|
|
import useLiveComments from './use-live-comments'
|
|
|
|
export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, commentSats }) {
|
|
const router = useRouter()
|
|
const sort = router.query.sort || defaultCommentSort(pinned, bio, parentCreatedAt)
|
|
|
|
const getHandleClick = sort => {
|
|
return () => {
|
|
handleSort(sort)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Navbar className='pt-1 pb-0 px-3'>
|
|
<Nav
|
|
className={styles.navbarNav}
|
|
activeKey={sort}
|
|
>
|
|
<Nav.Item className='text-muted'>
|
|
{numWithUnits(commentSats)}
|
|
</Nav.Item>
|
|
<div className='ms-auto d-flex'>
|
|
<Nav.Item>
|
|
<Nav.Link
|
|
eventKey='hot'
|
|
className={styles.navLink}
|
|
onClick={getHandleClick('hot')}
|
|
>
|
|
hot
|
|
</Nav.Link>
|
|
</Nav.Item>
|
|
<Nav.Item>
|
|
<Nav.Link
|
|
eventKey='recent'
|
|
className={styles.navLink}
|
|
onClick={getHandleClick('recent')}
|
|
>
|
|
recent
|
|
</Nav.Link>
|
|
</Nav.Item>
|
|
<Nav.Item>
|
|
<Nav.Link
|
|
eventKey='top'
|
|
className={styles.navLink}
|
|
onClick={getHandleClick('top')}
|
|
>
|
|
top
|
|
</Nav.Link>
|
|
</Nav.Item>
|
|
</div>
|
|
</Nav>
|
|
</Navbar>
|
|
)
|
|
}
|
|
|
|
export default function Comments ({
|
|
parentId, pinned, bio, parentCreatedAt,
|
|
commentSats, comments, commentsCursor, fetchMoreComments, ncomments, lastCommentAt, item, ...props
|
|
}) {
|
|
const router = useRouter()
|
|
|
|
// fetch new comments that arrived after the lastCommentAt, and update the item.comments field in cache
|
|
useLiveComments(parentId, lastCommentAt || parentCreatedAt, router.query.sort)
|
|
|
|
const pins = useMemo(() => comments?.filter(({ position }) => !!position).sort((a, b) => a.position - b.position), [comments])
|
|
|
|
return (
|
|
<>
|
|
{comments?.length > 0
|
|
? <CommentsHeader
|
|
commentSats={commentSats} parentCreatedAt={parentCreatedAt}
|
|
pinned={pinned} bio={bio} handleSort={sort => {
|
|
const { commentsViewedAt, commentId, ...query } = router.query
|
|
delete query.nodata
|
|
router.push({
|
|
pathname: router.pathname,
|
|
query: { ...query, commentsViewedAt, sort }
|
|
}, {
|
|
pathname: `/items/${parentId}`,
|
|
query: sort === defaultCommentSort(pinned, bio, parentCreatedAt) ? undefined : { sort }
|
|
}, { scroll: false })
|
|
}}
|
|
/>
|
|
: null}
|
|
{pins.map(item => (
|
|
<Fragment key={item.id}>
|
|
<Comment depth={1} item={item} rootLastCommentAt={lastCommentAt || parentCreatedAt} {...props} pin />
|
|
</Fragment>
|
|
))}
|
|
{comments.filter(({ position }) => !position).map(item => (
|
|
<Comment depth={1} key={item.id} item={item} rootLastCommentAt={lastCommentAt || parentCreatedAt} {...props} />
|
|
))}
|
|
{ncomments > FULL_COMMENTS_THRESHOLD &&
|
|
<MoreFooter
|
|
cursor={commentsCursor} fetchMore={fetchMoreComments} noMoreText=' '
|
|
count={comments?.length}
|
|
Skeleton={CommentsSkeleton}
|
|
/>}
|
|
</>
|
|
)
|
|
}
|
|
|
|
export function CommentsSkeleton () {
|
|
return <CommentSkeleton skeletonChildren={7} />
|
|
}
|