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.
This commit is contained in:
Christopher Gurnee 2022-04-15 00:11:01 +00:00
parent 1e21ba474a
commit 536bccba2a

View file

@ -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) // Set the scroll location to just below the post if not already set (only with permalinks)
if (!this.props.location.hash) if (!this.props.location.hash)
this.props.location.hash = '#comment-info' 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. // 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 { location } = this.props if (location.state?.scrollBehavior && location.hash.length > 1 && !this.props.global.isErrored()) {
const id = location.hash.substring(1) const hashElem = document.getElementById(location.hash.substring(1))
if (id) if (hashElem) {
document.getElementById(id)?.scrollIntoView({behavior: location.state.scrollBehavior}) hashElem.scrollIntoView({behavior: location.state.scrollBehavior})
delete location.state delete location.state
}
} }
if (this.nextAllCommentsFiltered != this.state.allCommentsFiltered) if (this.nextAllCommentsFiltered != this.state.allCommentsFiltered)