From d35f7e7bf0b9b625cd76fee75879bab07a997452 Mon Sep 17 00:00:00 2001 From: Christopher Gurnee Date: Mon, 16 May 2022 20:53:14 +0000 Subject: [PATCH] Fix some minor (mostly display) context bugs --- src/pages/thread/CommentSection.js | 9 +++++---- src/pages/thread/index.js | 16 +++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/pages/thread/CommentSection.js b/src/pages/thread/CommentSection.js index efb9f70..c2c1d80 100644 --- a/src/pages/thread/CommentSection.js +++ b/src/pages/thread/CommentSection.js @@ -142,10 +142,11 @@ const commentSection = (props) => { } } - lastTotal = total - lastRoot = root - lastFilter = commentFilter - lastSort = commentSort + lastTotal = total + lastRoot = root + lastContext = context + lastFilter = commentFilter + lastSort = commentSort console.timeEnd('Build comment tree') props.setMoreContextAvail(commentTree.length > 0 && commentTree[0].parent_id != commentTree[0].link_id) diff --git a/src/pages/thread/index.js b/src/pages/thread/index.js index 8d66aae..9d71236 100644 --- a/src/pages/thread/index.js +++ b/src/pages/thread/index.js @@ -371,7 +371,7 @@ class Thread extends React.Component { // Check if the context query parameter has changed if (commentID) { - const context = parseInt((new URLSearchParams(this.props.location.search)).get('context')) || 0 + const context = Math.max(parseInt((new URLSearchParams(this.props.location.search)).get('context')) || 0, 0) if (context > this.state.context) { this.setState({reloadingComments: true}) this.props.global.setLoading('Loading comments...') @@ -383,9 +383,11 @@ class Thread extends React.Component { this.props.global.setSuccess() this.setState({loadingComments: false, reloadingComments: false}) }) - } else if (context >= 0 && context != this.state.context) + } else if (context != this.state.context) this.setState({ context }) - } + } else if (0 != this.state.context) + this.setState({ context: 0 }) + } // end of "If we're not already downloading comments, check to see if we need to start" // Handle any requested scrolling @@ -575,6 +577,7 @@ class Thread extends React.Component { // Makes a best-effort attempt to retrieve context# ancestors of the current commentID. // Returns a Promise which resolves with the number retrieved, or rejects with undefined. + // (Each code path below must setState({ context }) to avoid an infinite loop.) getContext (context) { const { params } = this.props.match const { pushshiftCommentLookup } = this.state @@ -585,12 +588,15 @@ class Thread extends React.Component { .then(pushshiftComments => { if (ids.length) console.log('Pushshift:', pushshiftComments.length, 'comments') - this.setState({ context }) + this.setState({ context }) // Displays the retrieved context pushshiftComments.forEach(comment => pushshiftCommentLookup.set(comment.id, comment)) return this.compareAndUpdateComments(redditComments) }) }) - .catch(e => console.error(e)) + .catch(e => { + console.error(e) + this.setState({ context }) + }) } componentWillUnmount () {