Fix count of viewed comments of a post (#2297)

* fix: parse existingRootComments as Number to correctly add new comments to the count

* return commentsViewedAt and commentsViewedNum parsed as Number

* hotfix: commentsViewedNum accepts itemId directly

* consistently receive itemId on new-comments functions that only uses IDs
This commit is contained in:
soxa 2025-07-18 20:02:39 +02:00 committed by GitHub
parent 96f149fa79
commit d4efacadc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 10 deletions

View File

@ -110,7 +110,7 @@ export default function ItemInfo ({
</>} </>}
<Link <Link
href={`/items/${item.id}`} onClick={(e) => { href={`/items/${item.id}`} onClick={(e) => {
const viewedAt = commentsViewedAt(item) const viewedAt = commentsViewedAt(item.id)
if (viewedAt) { if (viewedAt) {
e.preventDefault() e.preventDefault()
router.push( router.push(

View File

@ -29,7 +29,7 @@ import { useShowModal } from './modal'
import { BoostHelp } from './adv-post-form' import { BoostHelp } from './adv-post-form'
function onItemClick (e, router, item) { function onItemClick (e, router, item) {
const viewedAt = commentsViewedAt(item) const viewedAt = commentsViewedAt(item.id)
if (viewedAt) { if (viewedAt) {
e.preventDefault() e.preventDefault()
if (e.ctrlKey || e.metaKey) { if (e.ctrlKey || e.metaKey) {

View File

@ -8,20 +8,23 @@ export function commentsViewed (item) {
} }
} }
export function commentsViewedAfterComment (rootId, createdAt) { export function commentsViewedAt (itemId) {
window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime()) return Number(window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${itemId}`))
const existingRootComments = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${rootId}`) || 0
window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, existingRootComments + 1)
} }
export function commentsViewedAt (item) { export function commentsViewedNum (itemId) {
return window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${item.id}`) return Number(window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${itemId}`))
}
export function commentsViewedAfterComment (rootId, createdAt) {
window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime())
window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, commentsViewedNum(rootId) + 1)
} }
export function newComments (item) { export function newComments (item) {
if (!item.parentId) { if (!item.parentId) {
const viewedAt = commentsViewedAt(item) const viewedAt = commentsViewedAt(item.id)
const viewNum = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${item.id}`) const viewNum = commentsViewedNum(item.id)
if (viewedAt && viewNum) { if (viewedAt && viewNum) {
return viewedAt < new Date(item.lastCommentAt).getTime() || viewNum < item.ncomments return viewedAt < new Date(item.lastCommentAt).getTime() || viewNum < item.ncomments