Add '?max_comments=n' query parameter

This increases the initial max-comments-to-download above the user's
preference without changing their saved setting. This can be useful when
linking to a specific comment which is new enough that the default max-
comments setting (of 800) is too low to download the linked comment
tree, causing most users to see a confusing 'No comments found' message.
This commit is contained in:
Christopher Gurnee 2022-02-25 11:08:33 -05:00
parent cab177c021
commit d6a55ee31c
3 changed files with 18 additions and 6 deletions

View file

@ -1,8 +1,9 @@
import React, { useState } from 'react'
import {connect, sort, filter, saveMaxComments, minCommentsLimit, maxCommentsLimit} from '../../state'
import {connect, sort, filter, getMaxComments, saveMaxComments, minCommentsLimit, maxCommentsLimit} from '../../state'
const sortBy = props => {
const [maxCommentsField, setMaxCommentsField] = useState(props.global.state.maxComments)
const initSavedMaxComments = getMaxComments()
const isFirefox = typeof InstallTrigger !== 'undefined'
let usedMouse;
@ -43,7 +44,7 @@ const sortBy = props => {
onBlur= {e => e.target.value = saveMaxComments(e.target.value)}
{ ...(isFirefox ? {
onClick: e => e.target.focus() } : {}) }
defaultValue={props.global.state.maxComments} type='number' maxLength='5' required
defaultValue={initSavedMaxComments} type='number' maxLength='5' required
min={minCommentsLimit} max={maxCommentsLimit} step={minCommentsLimit} />
</span>
{ maxCommentsField > props.global.state.maxComments && props.global.state.maxComments < maxCommentsLimit && !props.reloadingComments &&

View file

@ -9,7 +9,7 @@ import {
getComments as getPushshiftComments
} from '../../api/pushshift'
import { isDeleted, isRemoved } from '../../utils'
import { connect } from '../../state'
import { connect, constrainMaxComments } from '../../state'
import Post from '../common/Post'
import CommentSection from './CommentSection'
import SortBy from './SortBy'
@ -29,7 +29,12 @@ class Thread extends React.Component {
componentDidMount () {
const { subreddit, threadID } = this.props.match.params
this.curMaxComments = this.props.global.state.maxComments
let maxCommentsQuery = parseInt((new URLSearchParams(this.props.location.search)).get('max_comments'))
if (maxCommentsQuery > this.props.global.state.maxComments)
// Directly mutating the state is not recommended, but it's only done once and is probably OK here
this.props.global.state.maxComments = this.curMaxComments = constrainMaxComments(maxCommentsQuery)
else
this.curMaxComments = this.props.global.state.maxComments
this.props.global.setLoading('Loading post...')
// Get post from reddit

View file

@ -23,11 +23,17 @@ export const maxCommentsDefault = 800
export const minCommentsLimit = chunkSize
export const maxCommentsLimit = 20000
// Contrains, saves, and returns it, but does not change the state (does not load more comments)
export const saveMaxComments = maxComments => {
// Constrains input to between minCommentsLimit and maxCommentsLimit
export const constrainMaxComments = maxComments => {
maxComments = Math.min(Math.round(maxComments), maxCommentsLimit)
if (!(maxComments >= minCommentsLimit)) // also true when maxComments isn't a number
maxComments = minCommentsLimit
return maxComments
}
// Contrains, saves, and returns it, but does not change the state (does not load more comments)
export const saveMaxComments = maxComments => {
maxComments = constrainMaxComments(maxComments)
put(maxCommentsKey, maxComments)
return maxComments
}