Skip reloading existing comments from Pushshift

This commit is contained in:
Christopher Gurnee 2022-02-19 13:27:40 -05:00
parent 37d43fe60b
commit 07c72a2e41
6 changed files with 40 additions and 27 deletions

2
dist/main.css vendored

File diff suppressed because one or more lines are too long

View file

@ -67,10 +67,8 @@ export const getPost = async threadID => {
} }
} }
export const getComments = async (threadID, maxComments) => { export const getComments = async (allComments, threadID, maxComments, after) => {
let chunks = Math.ceil(maxComments / chunkSize) let chunks = Math.ceil(maxComments / chunkSize), comments
let after = 0, comments
const allComments = new Map()
while (true) { while (true) {
let delay = 0 let delay = 0
@ -97,8 +95,8 @@ export const getComments = async (threadID, maxComments) => {
})) }))
if (comments.length < chunkSize/2 || chunks <= 1) if (comments.length < chunkSize/2 || chunks <= 1)
break break
chunks -= 1 chunks--
after = Math.max(comments[comments.length - 1].created_utc - 1, after + 1) after = Math.max(comments[comments.length - 1].created_utc - 1, after + 1)
} }
return allComments return comments[comments.length - 1].created_utc
} }

View file

@ -42,7 +42,7 @@ const sortBy = props => {
{ ...(isFirefox ? { { ...(isFirefox ? {
onClick: e => e.target.focus() } : {}) } onClick: e => e.target.focus() } : {}) }
defaultValue={props.global.state.maxComments} type='number' maxLength='5' required min='100' max={maxCommentsLimit} step='100' /> defaultValue={props.global.state.maxComments} type='number' maxLength='5' required min='100' max={maxCommentsLimit} step='100' />
{reloadVisible && <> {reloadVisible && !props.reloadingComments && <>
<span className='space' /> <span className='space' />
<input onClick={() => {props.global.loadMaxComments(); setReloadVisible(false)}} type='button' value='Reload' /> <input onClick={() => {props.global.loadMaxComments(); setReloadVisible(false)}} type='button' value='Reload' />
</>} </>}

View file

@ -21,7 +21,8 @@ class Thread extends React.Component {
pushshiftCommentLookup: new Map(), pushshiftCommentLookup: new Map(),
removed: [], removed: [],
deleted: [], deleted: [],
loadingComments: true loadingComments: true,
reloadingComments: false
} }
componentDidMount () { componentDidMount () {
@ -67,32 +68,30 @@ class Thread extends React.Component {
this.props.global.setLoading('Loading comments from Pushshift...') this.props.global.setLoading('Loading comments from Pushshift...')
}) })
this.getComments() this.getComments(this.props.global.state.maxComments, 0)
} }
componentDidUpdate () { componentDidUpdate () {
if (this.props.global.state.maxComments > this.curMaxComments) { const newCommentCount = this.props.global.state.maxComments - this.curMaxComments
if (newCommentCount > 0) {
this.curMaxComments = this.props.global.state.maxComments this.curMaxComments = this.props.global.state.maxComments
this.setState({ this.setState({reloadingComments: true})
pushshiftCommentLookup: new Map(), this.props.global.setLoading('Loading more comments from Pushshift...')
removed: [], this.getComments(newCommentCount, this.lastCreatedUtc - 1)
deleted: [],
loadingComments: true
})
this.props.global.setLoading('Loading comments from Pushshift...')
this.getComments()
} }
} }
getComments () { getComments (newCommentCount, after) {
const { threadID } = this.props.match.params const { threadID } = this.props.match.params
const pushshiftCommentLookup = this.state.pushshiftCommentLookup
// Get comment ids from pushshift // Get comment ids from pushshift
getPushshiftComments(threadID, this.props.global.state.maxComments) getPushshiftComments(pushshiftCommentLookup, threadID, newCommentCount, after)
.then(pushshiftCommentLookup => { .then(lastCreatedUtc => {
console.log(`Pushshift: ${pushshiftCommentLookup.size} comments`) console.log(`Pushshift: ${pushshiftCommentLookup.size} comments`)
const ids = [] const ids = []
const missingIds = new Set() const missingIds = new Set()
this.lastCreatedUtc = lastCreatedUtc
// Extract ids from pushshift response // Extract ids from pushshift response
pushshiftCommentLookup.forEach(comment => { pushshiftCommentLookup.forEach(comment => {
@ -152,7 +151,8 @@ class Thread extends React.Component {
pushshiftCommentLookup, pushshiftCommentLookup,
removed, removed,
deleted, deleted,
loadingComments: false loadingComments: false,
reloadingComments: false
}) })
}) })
.catch(e => { .catch(e => {
@ -196,7 +196,9 @@ class Thread extends React.Component {
removed={this.state.removed.length} removed={this.state.removed.length}
deleted={this.state.deleted.length} deleted={this.state.deleted.length}
/> />
<SortBy /> <SortBy
reloadingComments={this.state.reloadingComments}
/>
{isSingleComment && {isSingleComment &&
<div className='view-rest-of-comment'> <div className='view-rest-of-comment'>
<div>you are viewing a single comment's thread.</div> <div>you are viewing a single comment's thread.</div>

View file

@ -4,6 +4,9 @@ html, body
background-color: $background background-color: $background
font-family: verdana, arial, helvetica, sans-serif font-family: verdana, arial, helvetica, sans-serif
.wait
cursor: wait
.main .main
margin: 15px margin: 15px
color: white color: white

View file

@ -56,14 +56,24 @@ class GlobalState extends Container {
loadMaxComments = () => this.setState({maxComments: get(maxCommentsKey, maxCommentsDefault)}) loadMaxComments = () => this.setState({maxComments: get(maxCommentsKey, maxCommentsDefault)})
setSuccess = () => this.setState({statusText: '', statusHelpUrl: undefined, statusImage: '/images/success.png'}) setSuccess = () => {
setLoading = (text) => this.setState({statusText: text, statusImage: '/images/loading.gif'}) this.setState({statusText: '', statusHelpUrl: undefined, statusImage: '/images/success.png'})
document.body.classList.remove('wait')
}
setLoading = text => {
this.setState({statusText: text, statusImage: '/images/loading.gif'})
document.body.classList.add('wait')
}
setError = (error, helpUrl = undefined) => { setError = (error, helpUrl = undefined) => {
this.setState({statusText: error.message, statusImage: '/images/error.png'}) this.setState({statusText: error.message, statusImage: '/images/error.png'})
if (helpUrl) if (helpUrl)
this.setState({statusHelpUrl: helpUrl}) this.setState({statusHelpUrl: helpUrl})
document.body.classList.remove('wait')
}
clearStatus = () => {
this.setState({statusText: '', statusHelpUrl: undefined, statusImage: undefined})
document.body.classList.remove('wait')
} }
clearStatus = () => this.setState({statusText: '', statusHelpUrl: undefined, statusImage: undefined})
} }
// A redux-like connect function for Unstated // A redux-like connect function for Unstated