From 536bccba2a86cc5b49ac69a063fffd2a589bdcbd Mon Sep 17 00:00:00 2001 From: Christopher Gurnee Date: Fri, 15 Apr 2022 00:11:01 +0000 Subject: [PATCH] Fix visits to full posts but with a bookmark/hash Beforehand, only initial visits to single comments (permalinks) and internal links correctly scrolled to the bookmark/hash. --- src/pages/thread/index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/pages/thread/index.js b/src/pages/thread/index.js index c55a88d..699aeff 100644 --- a/src/pages/thread/index.js +++ b/src/pages/thread/index.js @@ -230,8 +230,10 @@ class Thread extends React.Component { // Set the scroll location to just below the post if not already set (only with permalinks) if (!this.props.location.hash) this.props.location.hash = '#comment-info' - this.props.location.state = {scrollBehavior: 'smooth'} } + + if (this.props.location.hash) + this.props.location.state = {scrollBehavior: 'smooth'} } // Updates this.curContigIdx based on URL's commentID if it's already downloaded. @@ -337,12 +339,13 @@ class Thread extends React.Component { } } - if (!loadingComments && !this.props.global.isErrored() && this.props.location.state?.scrollBehavior) { - const { location } = this.props - const id = location.hash.substring(1) - if (id) - document.getElementById(id)?.scrollIntoView({behavior: location.state.scrollBehavior}) - delete location.state + const { location } = this.props + if (location.state?.scrollBehavior && location.hash.length > 1 && !this.props.global.isErrored()) { + const hashElem = document.getElementById(location.hash.substring(1)) + if (hashElem) { + hashElem.scrollIntoView({behavior: location.state.scrollBehavior}) + delete location.state + } } if (this.nextAllCommentsFiltered != this.state.allCommentsFiltered)