-
-
Notifications
You must be signed in to change notification settings - Fork 10
perf(replies): prefer cached board preview replies #1101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,13 @@ import useQuotedByMap from '../../hooks/use-quoted-by-map'; | |
| import useProgressiveRender from '../../hooks/use-progressive-render'; | ||
| import useFreshReplies from '../../hooks/use-fresh-replies'; | ||
| import { BOARD_REPLIES_PREVIEW_FETCH_SIZE, BOARD_REPLIES_PREVIEW_VISIBLE_COUNT, REPLIES_PER_PAGE } from '../../lib/constants'; | ||
| import { computeOmittedCount, filterRepliesForDisplay, getPreviewDisplayReplies, getTotalReplyCount } from '../../lib/utils/replies-preview-utils'; | ||
| import { | ||
| computeOmittedCount, | ||
| filterRepliesForDisplay, | ||
| getPreviewDisplayReplies, | ||
| getTotalReplyCount, | ||
| hasEnoughPreviewReplies, | ||
| } from '../../lib/utils/replies-preview-utils'; | ||
| import { isCommentArchived } from '../../lib/utils/comment-moderation-utils'; | ||
| import { getThreadTopNavigationState, scrollThreadContainerToTop } from '../../lib/utils/thread-scroll-utils'; | ||
| import useDeleteFailedPost from '../../hooks/use-delete-failed-post'; | ||
|
|
@@ -821,11 +827,27 @@ const PostDesktop = ({ | |
|
|
||
| const { showOmittedReplies, setShowOmittedReplies } = useShowOmittedReplies(); | ||
|
|
||
| const shouldFetchPreview = showReplies && !isModQueue && !showAllReplies && showOmittedReplies[cid] !== true; | ||
| const shouldUsePreview = showReplies && !isModQueue && !showAllReplies; | ||
| const shouldFetchFull = showReplies && !isModQueue && (showAllReplies || showOmittedReplies[cid]); | ||
|
|
||
| const cachedPreviewRepliesResult = useReplies({ | ||
| comment: shouldUsePreview ? resolvedPost : undefined, | ||
| onlyIfCached: true, | ||
| sortType: 'new', | ||
| flat: true, | ||
| repliesPerPage: BOARD_REPLIES_PREVIEW_FETCH_SIZE, | ||
| accountComments: { newerThan: Infinity, append: true }, | ||
| }); | ||
| const cachedPreviewReplies = (cachedPreviewRepliesResult as { updatedReplies?: Comment[] }).updatedReplies?.length | ||
| ? (cachedPreviewRepliesResult as { updatedReplies?: Comment[] }).updatedReplies! | ||
| : cachedPreviewRepliesResult.replies || []; | ||
| const hasEnoughCachedPreview = hasEnoughPreviewReplies({ | ||
| replyCount: resolvedPost?.replyCount, | ||
| loadedCount: cachedPreviewReplies.length, | ||
| visibleCount: BOARD_REPLIES_PREVIEW_VISIBLE_COUNT, | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Desktop cached preview count includes deleted repliesMedium Severity The desktop Additional Locations (1) |
||
| const previewRepliesResult = useReplies({ | ||
| comment: shouldFetchPreview ? resolvedPost : undefined, | ||
| comment: shouldUsePreview && !showOmittedReplies[cid] && !hasEnoughCachedPreview ? resolvedPost : undefined, | ||
| sortType: 'new', | ||
| flat: true, | ||
| repliesPerPage: BOARD_REPLIES_PREVIEW_FETCH_SIZE, | ||
|
|
@@ -839,9 +861,10 @@ const PostDesktop = ({ | |
| accountComments: { newerThan: Infinity, append: true }, | ||
| }); | ||
|
|
||
| const previewReplies = (previewRepliesResult as { updatedReplies?: Comment[] }).updatedReplies?.length | ||
| const livePreviewReplies = (previewRepliesResult as { updatedReplies?: Comment[] }).updatedReplies?.length | ||
| ? (previewRepliesResult as { updatedReplies?: Comment[] }).updatedReplies! | ||
| : previewRepliesResult.replies || []; | ||
| const previewReplies = hasEnoughCachedPreview ? cachedPreviewReplies : livePreviewReplies; | ||
| const fullReplies = (fullRepliesResult as { updatedReplies?: Comment[] }).updatedReplies?.length | ||
| ? (fullRepliesResult as { updatedReplies?: Comment[] }).updatedReplies! | ||
| : fullRepliesResult.replies || []; | ||
|
|
@@ -875,7 +898,7 @@ const PostDesktop = ({ | |
| const totalReplyCount = getTotalReplyCount({ | ||
| replyCount: resolvedPost?.replyCount, | ||
| fullLoadedCount: fullReplies.length, | ||
| previewLoadedCount: previewReplies.length, | ||
| previewLoadedCount: Math.max(cachedPreviewReplies.length, livePreviewReplies.length), | ||
| }); | ||
| const repliesCount = computeOmittedCount({ | ||
| totalReplyCount, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | |
| import { copyToClipboard } from '../clipboard-utils'; | ||
| import { hashStringToColor, getTextColorForBackground, removeMarkdown } from '../post-utils'; | ||
| import { preloadReplyModal, preloadThemeAssets, resolveAssetUrl } from '../preload-utils'; | ||
| import { computeOmittedCount, filterRepliesForDisplay, getPreviewDisplayReplies, getTotalReplyCount } from '../replies-preview-utils'; | ||
| import { computeOmittedCount, filterRepliesForDisplay, getPreviewDisplayReplies, getTotalReplyCount, hasEnoughPreviewReplies } from '../replies-preview-utils'; | ||
| import { getQuotedCidsFromContent, mergeQuotedCids } from '../reply-quote-utils'; | ||
| import { formatUserIDForDisplay, truncateWithEllipsisInMiddle } from '../string-utils'; | ||
| import { getFormattedDate, getFormattedTimeAgo, isChristmas } from '../time-utils'; | ||
|
|
@@ -186,6 +186,10 @@ describe('misc utils', () => { | |
|
|
||
| expect(computeOmittedCount({ totalReplyCount: 2, visibleCount: 5 })).toBe(0); | ||
| expect(computeOmittedCount({ totalReplyCount: 9, visibleCount: 5 })).toBe(4); | ||
| expect(hasEnoughPreviewReplies({ replyCount: 2, loadedCount: 2, visibleCount: 5 })).toBe(true); | ||
| expect(hasEnoughPreviewReplies({ replyCount: 9, loadedCount: 4, visibleCount: 5 })).toBe(false); | ||
| expect(hasEnoughPreviewReplies({ replyCount: undefined, loadedCount: 5, visibleCount: 5 })).toBe(true); | ||
| expect(hasEnoughPreviewReplies({ replyCount: undefined, loadedCount: 4, visibleCount: 5 })).toBe(false); | ||
|
Comment on lines
+189
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add one component regression test for the cache-hit/cache-miss branches. These assertions cover the helper math, but the fix in this PR is the conditional As per coding guidelines, "Add or update tests for bug fixes and non-trivial logic changes when the code is reasonably testable." 🤖 Prompt for AI Agents |
||
| expect(getTotalReplyCount({ replyCount: undefined, fullLoadedCount: 7, previewLoadedCount: 5 })).toBe(7); | ||
| expect(getTotalReplyCount({ replyCount: 12, fullLoadedCount: 7, previewLoadedCount: 5 })).toBe(12); | ||
| }); | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mirror production semantics in the
hasEnoughPreviewRepliestest mock.This mock currently skips the production helper’s numeric/non-negative guard for
replyCount, so edge inputs can behave differently in tests vs runtime.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents