Skip to content

Commit 5f0ca6c

Browse files
committed
refactor: merge ShowAllNewComments with ShowNewComments, better usage of props
1 parent a702b7b commit 5f0ca6c

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

components/comment.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import LinkToContext from './link-to-context'
2828
import Boost from './boost-button'
2929
import { gql, useApolloClient } from '@apollo/client'
3030
import classNames from 'classnames'
31-
import { ShowAllNewComments, ShowNewComments } from './show-new-comments'
31+
import { ShowNewComments } from './show-new-comments'
3232

3333
function Parent ({ item, rootText }) {
3434
const root = useRoot()
@@ -262,9 +262,7 @@ export default function Comment ({
262262
<Reply depth={depth + 1} item={item} replyOpen={replyOpen} onCancelQuote={cancelQuote} onQuoteReply={quoteReply} quote={quote}>
263263
{root.bounty && !bountyPaid && <PayBounty item={item} />}
264264
<div className='ms-auto'>
265-
{item.path.split('.').length === 2
266-
? <ShowAllNewComments item={item} setHasNewComments={setHasNewComments} />
267-
: <ShowNewComments comments={item.comments.comments} newComments={item.newComments} itemId={item.id} setHasNewComments={setHasNewComments} />}
265+
<ShowNewComments comments={item.comments.comments} newComments={item.newComments} itemId={item.id} item={item} setHasNewComments={setHasNewComments} />
268266
</div>
269267
</Reply>}
270268
{children}
@@ -273,7 +271,7 @@ export default function Comment ({
273271
? (
274272
<>
275273
{item.comments.comments.map((item) => (
276-
<Comment depth={depth + 1} key={item.id} item={item} />
274+
<Comment depth={depth + 1} key={item.id} item={item} setHasNewComments={setHasNewComments} />
277275
))}
278276
{item.comments.comments.length < item.nDirectComments && <ViewAllReplies id={item.id} nhas={item.ncomments} />}
279277
</>

components/comments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default function Comments ({
101101
/>
102102
: null}
103103
{newComments?.length > 0 && (
104-
<ShowNewComments topLevel comments={comments} newComments={newComments} itemId={parentId} sort={router.query.sort} setHasNewComments={setHasNewComments} />
104+
<ShowNewComments comments={comments} newComments={newComments} itemId={parentId} sort={router.query.sort} setHasNewComments={setHasNewComments} />
105105
)}
106106
{pins.map(item => (
107107
<Fragment key={item.id}>

components/show-new-comments.js

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,52 +76,36 @@ function collectAllNewComments (item) {
7676
return allNewComments
7777
}
7878

79-
// TODO: merge this with ShowNewComments
80-
export function ShowAllNewComments ({ item, setHasNewComments }) {
79+
export function ShowNewComments ({ sort, comments, newComments = [], itemId, item, setHasNewComments }) {
8180
const client = useApolloClient()
8281

83-
const newComments = useMemo(() => collectAllNewComments(item), [item])
84-
85-
const showNewComments = useCallback(() => {
86-
showAllNewCommentsRecursively(client, item)
87-
setHasNewComments(false)
88-
}, [client, item])
89-
90-
if (newComments.length === 0) {
91-
return null
92-
}
93-
94-
return (
95-
<div
96-
onClick={showNewComments}
97-
className='d-flex align-items-center gap-2 px-3 pointer'
98-
>
99-
{newComments.length > 1
100-
? `show all ${newComments.length} new comments`
101-
: 'show new comment'}
102-
<div className={styles.newCommentDot} />
103-
</div>
104-
)
105-
}
106-
107-
// ShowNewComments is a component that dedupes, refreshes and injects newComments into the comments field
108-
export function ShowNewComments ({ topLevel = false, comments, newComments = [], itemId, sort, setHasNewComments }) {
109-
const client = useApolloClient()
110-
const dedupedNewComments = useMemo(() => dedupeNewComments(newComments, comments), [newComments, comments])
82+
const topLevel = !!sort
83+
// if item is provided, we're showing all new comments for a thread,
84+
// otherwise we're showing new comments for a comment
85+
const isThread = !!item
86+
const allNewComments = useMemo(() => {
87+
if (isThread) {
88+
return collectAllNewComments(item)
89+
}
90+
return dedupeNewComments(newComments, comments)
91+
}, [isThread, item, newComments, comments])
11192

11293
const showNewComments = useCallback(() => {
113-
// fetch the latest version of the comments from the cache by their ids
114-
const payload = prepareComments(client, dedupedNewComments)
115-
116-
if (topLevel) {
117-
itemUpdateQuery(client, itemId, sort, payload)
94+
if (isThread) {
95+
showAllNewCommentsRecursively(client, item)
11896
} else {
119-
commentUpdateFragment(client, itemId, payload)
97+
// fetch the latest version of the comments from the cache by their ids
98+
const payload = prepareComments(client, allNewComments)
99+
if (topLevel) {
100+
itemUpdateQuery(client, itemId, sort, payload)
101+
} else {
102+
commentUpdateFragment(client, itemId, payload)
103+
}
120104
}
121105
setHasNewComments(false)
122-
}, [client, itemId, dedupedNewComments, topLevel, sort])
106+
}, [client, itemId, allNewComments, topLevel, sort])
123107

124-
if (dedupedNewComments.length === 0) {
108+
if (allNewComments.length === 0) {
125109
return null
126110
}
127111

@@ -130,8 +114,8 @@ export function ShowNewComments ({ topLevel = false, comments, newComments = [],
130114
onClick={showNewComments}
131115
className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
132116
>
133-
{dedupedNewComments.length > 1
134-
? `${dedupedNewComments.length} new comments`
117+
{allNewComments.length > 1
118+
? `${isThread ? 'show all ' : ''}${allNewComments.length} new comments`
135119
: 'show new comment'}
136120
<div className={styles.newCommentDot} />
137121
</div>

0 commit comments

Comments
 (0)