From dce106a6c8548a3ebb68afa2bb72ad1bd293161e Mon Sep 17 00:00:00 2001 From: Jesper Wrang Date: Mon, 19 Feb 2018 01:01:59 +0100 Subject: [PATCH] More effcient replacement of the comment scores from reddit to pushshift --- .eslintrc | 1 - src/js/components/CommentSection.js | 4 ++-- src/js/pages/Subreddit.js | 20 +++++++------------- src/js/pages/Thread.js | 15 ++++++++------- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/.eslintrc b/.eslintrc index db78a15..6111fdd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -46,7 +46,6 @@ ], "no-use-before-define": "off", "import/first": "off", - "prefer-destructuring": "off", "jsx-a11y/click-events-have-key-events": "off", "jsx-a11y/no-static-element-interactions": "off" } diff --git a/src/js/components/CommentSection.js b/src/js/components/CommentSection.js index 5c833c4..6531934 100644 --- a/src/js/components/CommentSection.js +++ b/src/js/components/CommentSection.js @@ -92,6 +92,7 @@ const filterCommentTree = (comments, filterFunction) => { } const commentSection = (props) => { + console.time('render comment section') const commentTree = unflatten(props.comments, props.root, props.removed, props.deleted) if (props.show === SHOW_REMOVED_DELETED) { @@ -111,8 +112,7 @@ const commentSection = (props) => { } else if (props.sort === SORT_OLD) { sortCommentTree(commentTree, oldSort) } - - console.log('COMMENT SECTION RENDERED') + console.timeEnd('render comment section') return (
diff --git a/src/js/pages/Subreddit.js b/src/js/pages/Subreddit.js index a5ece53..a2edf89 100644 --- a/src/js/pages/Subreddit.js +++ b/src/js/pages/Subreddit.js @@ -3,16 +3,8 @@ import { getRemovedThreadIDs } from 'api/removeddit' import { getThreads } from 'api/reddit' import Post from 'components/Post' -const displaySubreddit = props => { - const subreddit = props.match.params.subreddit - if (subreddit === undefined) { - return 'all' - } - return subreddit -} - const getSubredditForAPI = props => { - const subreddit = props.match.params.subreddit + const { subreddit } = props.match.params if (subreddit === undefined) { return '' } else if (subreddit.toLowerCase() === 'all') { @@ -45,16 +37,18 @@ export default class Subreddit extends React.Component { } render() { - const subreddit = `/r/${displaySubreddit(this.props)}` + const { subreddit = 'all' } = this.props.match.params + console.log(subreddit) + const subredditLink = `/r/${subreddit}` return (
- {subreddit} + {subredditLink} - reddit + reddit - ceddit + ceddit
{ this.state.threads.map(thread => ( diff --git a/src/js/pages/Thread.js b/src/js/pages/Thread.js index 9b2b76e..357022a 100644 --- a/src/js/pages/Thread.js +++ b/src/js/pages/Thread.js @@ -18,7 +18,6 @@ export default class Thread extends React.Component { this.state = { post: {}, pushshiftComments: [], - redditComments: [], removed: [], deleted: [], loadingComments: true, @@ -27,7 +26,6 @@ export default class Thread extends React.Component { componentDidMount() { const { subreddit, threadID } = this.props.match.params - console.timeEnd('scripts loaded') Promise.all([ // Get thread from reddit @@ -49,18 +47,22 @@ export default class Thread extends React.Component { .then(results => { const pushshiftComments = results[1] - // Extract ids from pushshift response const ids = pushshiftComments.map(comment => comment.id) - console.log('pushshift:', ids.length) + console.log('Number of comments from Pushshift:', ids.length) // Get all the comments from reddit return ( getRedditComments(ids) .then(redditComments => { + const redditCommentLookup = {} + redditComments.forEach(comment => { + redditCommentLookup[comment.id] = comment + }) + pushshiftComments.forEach(comment => { // Replace pushshift score with reddit (its usually more accurate) - const redditComment = redditComments.find(rComment => rComment.id === comment.id) + const redditComment = redditCommentLookup[comment.id] if (redditComment !== undefined) { comment.score = redditComment.score } @@ -72,7 +74,7 @@ export default class Thread extends React.Component { ) }) .then(redditComments => { - console.log('reddit:', redditComments.length) + console.log('Number of comments from Reddit:', redditComments.length) const removed = [] const deleted = [] @@ -88,7 +90,6 @@ export default class Thread extends React.Component { this.setState({ removed, deleted, - redditComments, loadingComments: false, }) })