From 0ca5630ffef4c71d7521a15f6e38c9f625f597f4 Mon Sep 17 00:00:00 2001 From: Christopher Gurnee Date: Fri, 29 Apr 2022 17:05:15 +0000 Subject: [PATCH] Refactor counting of removed/deleted comments This fixes a small bug, and avoids unnecessary setState() calls. --- src/pages/thread/index.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/pages/thread/index.js b/src/pages/thread/index.js index b027bd6..b25175c 100644 --- a/src/pages/thread/index.js +++ b/src/pages/thread/index.js @@ -87,6 +87,19 @@ class Thread extends React.Component { return comment } + // Can be called when a comment is missing from Pushshift; + // the comment's ids must have already been updated by redditIdsToPushshift() + useRedditComment (comment) { + if (isRemoved(comment.body)) { + this.state.removed++ + commentHint.removed = true + } else if (isDeleted(comment.body)) { + this.state.deleted++ + commentHint.deleted = true + } + this.state.pushshiftCommentLookup.set(comment.id, comment) + } + commentIdAttempts = new Set() // keeps track of attempts to load permalinks to avoid reattempts componentDidMount () { @@ -315,7 +328,7 @@ class Thread extends React.Component { } else { const { pushshiftCommentLookup } = this.state this.redditIdsToPushshift(comment) - pushshiftCommentLookup.set(comment.id, comment) // so use the Reddit comment instead + this.useRedditComment(comment) // so use the Reddit comment instead this.setCurContig(insertBefore - 1) // (this was the failed earlier attempt) console.timeEnd('Load comments') this.props.global.setSuccess() @@ -397,6 +410,7 @@ class Thread extends React.Component { // Download a list of comments by id from Reddit, and process them doRedditComments = ids => redditPromises.push(getRedditComments(ids) .then(comments => { + let removed = 0, deleted = 0 comments.forEach(comment => { let pushshiftComment = pushshiftCommentLookup.get(comment.id) if (pushshiftComment === undefined) { @@ -409,7 +423,6 @@ class Thread extends React.Component { } // Check what is removed / deleted according to reddit - let removed = 0, deleted = 0 if (isRemoved(comment.body)) { removed++ pushshiftComment.removed = true @@ -426,8 +439,8 @@ class Thread extends React.Component { pushshiftComment.edited = comment.edited } } - this.setState({ removed: this.state.removed + removed, deleted: this.state.deleted + deleted }) }) + this.setState({ removed: this.state.removed + removed, deleted: this.state.deleted + deleted }) return comments.length }) .catch(error => { @@ -468,14 +481,7 @@ class Thread extends React.Component { commentHint.created_utc >= this.curContig().firstCreated && ( commentHint.created_utc < this.curContig().lastCreated || curContigLoadedAll )) { - if (isRemoved(commentHint.body)) { - this.state.removed++ - commentHint.removed = true - } else if (isDeleted(commentHint.body)) { - this.state.deleted++ - commentHint.deleted = true - } - pushshiftCommentLookup.set(commentHint.id, commentHint) + this.useRedditComment(commentHint) commentHint = undefined }