2021-11-30 19:29:44 +00:00
import React , { useState } from 'react'
Improve permalinks and add parent link to comments
Beforehand, visiting a permalink would cause Unddit to begin loading
from the Post's first comment. If the max-to-download setting was too
small, and the permalinked comment wasn't yet encountered, no comment
tree would be displayed.
Now, only comments that occur at the same time as the permalinked
comment or later (chronologically) are downloaded.
This also adds a Parent link to each comment. When viewing a permalinked
comment, clicking the top-level Parent link will download only new
comments between the parent and child comments. Likewise, clicking the
'view rest of comments' link will only download new comments from the
first comment up until the previously viewed comment.
In general, Unddit will not re-download comments that it knows it has
previously downloaded, nor download more than max-to-download comments
per link clicked. This can leave 'gaps' in the list of downloaded
comments. If the 'load more comments' link is clicked, Unddit will only
download comments that can fill such gaps between the currently-linked
permalink (or if not viewing one, the first comment) and the last, up to
the max-to-download setting.
2022-03-18 18:10:20 +00:00
import { Link , NavLink } from 'react-router-dom'
2021-12-12 18:20:08 +00:00
import { prettyScore , prettyDate , prettyTimeDiff , exactDateTime , parse , isRemoved } from '../../utils'
2018-01-14 10:12:41 +00:00
2018-02-04 16:44:04 +00:00
const Comment = ( props ) => {
2018-01-14 19:21:53 +00:00
let commentStyle = 'comment '
2018-01-14 10:12:41 +00:00
2018-01-14 13:41:00 +00:00
if ( props . removed ) {
commentStyle += 'removed'
} else if ( props . deleted ) {
commentStyle += 'deleted'
} else {
2018-01-14 19:21:53 +00:00
commentStyle += props . depth % 2 === 0 ? 'comment-even' : 'comment-odd'
2018-01-14 13:41:00 +00:00
}
2021-12-08 21:10:20 +00:00
let innerHTML , editedInnerHTML ;
2022-04-14 22:25:17 +00:00
if ( props . removed && isRemoved ( props . body ) ) {
2021-10-23 16:58:06 +00:00
if ( ! props . hasOwnProperty ( 'retrieved_utc' ) && ! props . hasOwnProperty ( 'retrieved_on' ) || ! props . hasOwnProperty ( 'created_utc' ) ) {
innerHTML = '<p>[removed too quickly to be archived]</p>'
} else if ( props . created _utc < 1627776000 ) { // Aug 1 2021
const retrieved = props . hasOwnProperty ( 'retrieved_utc' ) ? props . retrieved _utc : props . retrieved _on ;
2021-10-27 18:14:59 +00:00
innerHTML = ` <p>[removed within ${ prettyTimeDiff ( retrieved - props . created _utc ) } ]</p> `
2021-10-23 16:58:06 +00:00
}
// After around Aug 1 2021, Pushshift began updating comments from Reddit after around
// 24-48 hours, including removing(?) comments that were removed from Reddit. The presence
// of either retrieved_utc or retrieved_on can currently be used to test for this behaviour.
else if ( props . hasOwnProperty ( 'retrieved_utc' ) ) {
innerHTML = ` <p>[removed within ${ prettyTimeDiff ( props . retrieved _utc - props . created _utc ) } ]</p> `
} else {
2021-11-01 20:14:05 +00:00
innerHTML = ` <p>[either removed too quickly, or <a href='https://www.reddit.com/r/pushshift/comments/pgzdav/the_api_now_appears_to_rewrite_nearly_all/'>removed(?) from archive</a> after ${ prettyTimeDiff ( props . retrieved _on - props . created _utc , true ) } ]</p> `
2021-10-23 16:58:06 +00:00
}
} else {
innerHTML = parse ( props . body )
2021-12-08 21:10:20 +00:00
if ( props . hasOwnProperty ( 'edited_body' ) )
editedInnerHTML = parse ( props . edited _body )
2021-10-23 16:58:06 +00:00
}
2021-11-30 19:29:44 +00:00
const [ collapsed , setCollapsed ] = useState ( false )
2021-12-08 21:10:20 +00:00
const [ showEdited , setShowEdited ] = useState ( false )
2018-02-06 03:07:30 +00:00
const permalink = ` /r/ ${ props . subreddit } /comments/ ${ props . link _id } /_/ ${ props . id } / `
Improve permalinks and add parent link to comments
Beforehand, visiting a permalink would cause Unddit to begin loading
from the Post's first comment. If the max-to-download setting was too
small, and the permalinked comment wasn't yet encountered, no comment
tree would be displayed.
Now, only comments that occur at the same time as the permalinked
comment or later (chronologically) are downloaded.
This also adds a Parent link to each comment. When viewing a permalinked
comment, clicking the top-level Parent link will download only new
comments between the parent and child comments. Likewise, clicking the
'view rest of comments' link will only download new comments from the
first comment up until the previously viewed comment.
In general, Unddit will not re-download comments that it knows it has
previously downloaded, nor download more than max-to-download comments
per link clicked. This can leave 'gaps' in the list of downloaded
comments. If the 'load more comments' link is clicked, Unddit will only
download comments that can fill such gaps between the currently-linked
permalink (or if not viewing one, the first comment) and the last, up to
the max-to-download setting.
2022-03-18 18:10:20 +00:00
const parentlink = props . parent _id == props . link _id ? undefined : (
props . depth == 0 ?
< NavLink
to = { ` /r/ ${ props . subreddit } /comments/ ${ props . link _id } /_/ ${ props . parent _id } / ` }
activeClassName = 'wait'
> parent < / N a v L i n k >
:
2022-04-08 17:54:32 +00:00
// Use a function, not just an object--state is recreated and scrollBehavior is always initialized
< Link to = { ( ) => ( { hash : ` # ${ props . parent _id } ` , state : { scrollBehavior : 'smooth' } } ) } > parent < / L i n k >
Improve permalinks and add parent link to comments
Beforehand, visiting a permalink would cause Unddit to begin loading
from the Post's first comment. If the max-to-download setting was too
small, and the permalinked comment wasn't yet encountered, no comment
tree would be displayed.
Now, only comments that occur at the same time as the permalinked
comment or later (chronologically) are downloaded.
This also adds a Parent link to each comment. When viewing a permalinked
comment, clicking the top-level Parent link will download only new
comments between the parent and child comments. Likewise, clicking the
'view rest of comments' link will only download new comments from the
first comment up until the previously viewed comment.
In general, Unddit will not re-download comments that it knows it has
previously downloaded, nor download more than max-to-download comments
per link clicked. This can leave 'gaps' in the list of downloaded
comments. If the 'load more comments' link is clicked, Unddit will only
download comments that can fill such gaps between the currently-linked
permalink (or if not viewing one, the first comment) and the last, up to
the max-to-download setting.
2022-03-18 18:10:20 +00:00
)
2018-01-14 13:41:00 +00:00
return (
< div id = { props . id } className = { commentStyle } >
2021-11-30 19:29:44 +00:00
< div className = { collapsed ? 'comment-head comment-collapsed' : 'comment-head' } >
2022-03-15 19:17:34 +00:00
< a onClick = { ( ) => setCollapsed ( ! collapsed ) }
onKeyDown = { e => e . key == "Enter" && setCollapsed ( ! collapsed ) }
tabIndex = { 0 }
className = 'comment-collapse' > [ { collapsed ? '+' : '\u2212' } ] < / a >
2018-02-04 16:44:04 +00:00
< span className = 'space' / >
< a
href = { props . author !== '[deleted]' ? ` https://www.reddit.com/user/ ${ props . author } ` : undefined }
2022-02-08 01:21:44 +00:00
className = { props . author === props . postAuthor ? 'author comment-author comment-poster' : 'author comment-author' }
2018-02-04 16:44:04 +00:00
>
2018-01-14 13:41:00 +00:00
{ props . author }
{ props . deleted && ' (deleted by user)' }
< / a >
2018-02-04 16:44:04 +00:00
< span className = 'space' / >
2018-01-14 13:41:00 +00:00
< span className = 'comment-score' > { prettyScore ( props . score ) } point { ( props . score !== 1 ) && 's' } < / s p a n >
2018-02-04 16:44:04 +00:00
< span className = 'space' / >
Improve permalinks and add parent link to comments
Beforehand, visiting a permalink would cause Unddit to begin loading
from the Post's first comment. If the max-to-download setting was too
small, and the permalinked comment wasn't yet encountered, no comment
tree would be displayed.
Now, only comments that occur at the same time as the permalinked
comment or later (chronologically) are downloaded.
This also adds a Parent link to each comment. When viewing a permalinked
comment, clicking the top-level Parent link will download only new
comments between the parent and child comments. Likewise, clicking the
'view rest of comments' link will only download new comments from the
first comment up until the previously viewed comment.
In general, Unddit will not re-download comments that it knows it has
previously downloaded, nor download more than max-to-download comments
per link clicked. This can leave 'gaps' in the list of downloaded
comments. If the 'load more comments' link is clicked, Unddit will only
download comments that can fill such gaps between the currently-linked
permalink (or if not viewing one, the first comment) and the last, up to
the max-to-download setting.
2022-03-18 18:10:20 +00:00
{ props . created _utc &&
< span className = 'comment-time' title = { exactDateTime ( props . created _utc ) } > { prettyDate ( props . created _utc ) } < / s p a n > }
2021-12-20 13:36:32 +00:00
{ ( props . hasOwnProperty ( 'edited_body' ) || props . edited ) &&
2021-12-12 18:20:08 +00:00
< span className = 'comment-time' title = { props . edited ? exactDateTime ( props . edited ) : 'within 3 minutes' }
> * ( last edited { prettyDate ( props . edited ? props . edited : props . created _utc ) } ) < / s p a n > }
2018-01-14 13:41:00 +00:00
< / d i v >
2021-11-30 19:29:44 +00:00
< div style = { collapsed ? { display : 'none' } : { } } >
2021-12-08 21:10:20 +00:00
< div className = 'comment-body' dangerouslySetInnerHTML = { { _ _html : showEdited ? editedInnerHTML : innerHTML } } / >
2021-11-30 19:29:44 +00:00
< div className = 'comment-links' >
2022-04-08 17:54:32 +00:00
< Link to = { ( ) => ( { pathname : permalink , hash : '#comment-info' , state : { scrollBehavior : 'auto' } } ) } > permalink < / L i n k >
2021-11-30 19:29:44 +00:00
< a href = { ` https://www.reddit.com ${ permalink } ` } > reddit < / a >
2022-04-08 17:54:32 +00:00
< a href = { ` https://www.reveddit.com ${ permalink } ` } > reveddit < / a >
Improve permalinks and add parent link to comments
Beforehand, visiting a permalink would cause Unddit to begin loading
from the Post's first comment. If the max-to-download setting was too
small, and the permalinked comment wasn't yet encountered, no comment
tree would be displayed.
Now, only comments that occur at the same time as the permalinked
comment or later (chronologically) are downloaded.
This also adds a Parent link to each comment. When viewing a permalinked
comment, clicking the top-level Parent link will download only new
comments between the parent and child comments. Likewise, clicking the
'view rest of comments' link will only download new comments from the
first comment up until the previously viewed comment.
In general, Unddit will not re-download comments that it knows it has
previously downloaded, nor download more than max-to-download comments
per link clicked. This can leave 'gaps' in the list of downloaded
comments. If the 'load more comments' link is clicked, Unddit will only
download comments that can fill such gaps between the currently-linked
permalink (or if not viewing one, the first comment) and the last, up to
the max-to-download setting.
2022-03-18 18:10:20 +00:00
{ parentlink }
2021-12-08 21:10:20 +00:00
{ props . hasOwnProperty ( 'edited_body' ) &&
2022-03-15 19:17:34 +00:00
< a onClick = { ( ) => setShowEdited ( ! showEdited ) }
onKeyDown = { e => e . key == "Enter" && setShowEdited ( ! showEdited ) }
tabIndex = { 0 }
title = { showEdited ? 'The most recent version is shown; click to show the earliest archived' : 'The earliest archived version is shown; click to show the most recent' }
> * edited < / a > }
2021-11-30 19:29:44 +00:00
< / d i v >
< div >
{ props . replies . map ( comment => (
< Comment
key = { comment . id }
{ ... comment }
depth = { props . depth + 1 }
2021-12-12 21:19:29 +00:00
postAuthor = { props . postAuthor }
2021-11-30 19:29:44 +00:00
/ >
) ) }
< / d i v >
2018-02-04 16:44:04 +00:00
< / d i v >
2018-01-14 13:41:00 +00:00
< / d i v >
)
}
2018-02-04 16:44:04 +00:00
export default Comment