diff --git a/components/item-info.js b/components/item-info.js index ec0a0958..63129fcd 100644 --- a/components/item-info.js +++ b/components/item-info.js @@ -110,7 +110,7 @@ export default function ItemInfo ({ } { - const viewedAt = commentsViewedAt(item) + const viewedAt = commentsViewedAt(item.id) if (viewedAt) { e.preventDefault() router.push( diff --git a/components/item.js b/components/item.js index 0ae1d0af..c1892ba4 100644 --- a/components/item.js +++ b/components/item.js @@ -29,7 +29,7 @@ import { useShowModal } from './modal' import { BoostHelp } from './adv-post-form' function onItemClick (e, router, item) { - const viewedAt = commentsViewedAt(item) + const viewedAt = commentsViewedAt(item.id) if (viewedAt) { e.preventDefault() if (e.ctrlKey || e.metaKey) { diff --git a/lib/new-comments.js b/lib/new-comments.js index 20ae8dd4..7578e806 100644 --- a/lib/new-comments.js +++ b/lib/new-comments.js @@ -8,20 +8,23 @@ export function commentsViewed (item) { } } -export function commentsViewedAfterComment (rootId, createdAt) { - window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime()) - const existingRootComments = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${rootId}`) || 0 - window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, existingRootComments + 1) +export function commentsViewedAt (itemId) { + return Number(window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${itemId}`)) } -export function commentsViewedAt (item) { - return window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${item.id}`) +export function commentsViewedNum (itemId) { + 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) { if (!item.parentId) { - const viewedAt = commentsViewedAt(item) - const viewNum = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${item.id}`) + const viewedAt = commentsViewedAt(item.id) + const viewNum = commentsViewedNum(item.id) if (viewedAt && viewNum) { return viewedAt < new Date(item.lastCommentAt).getTime() || viewNum < item.ncomments