2023-06-23 10:52:03 +00:00
|
|
|
import {Component} from "preact";
|
|
|
|
import {log, querySelector, querySelectorAll} from "../../utilities/exports.js";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
type Props = Record<string, unknown>;
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
hidden: boolean;
|
|
|
|
newCommentCount: number;
|
2022-02-23 13:52:06 +00:00
|
|
|
previousComment: HTMLElement | undefined;
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class JumpToNewCommentFeature extends Component<Props, State> {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const newCommentCount = querySelectorAll(".comment.is-comment-new").length;
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
hidden: false,
|
|
|
|
newCommentCount,
|
2022-02-23 13:52:06 +00:00
|
|
|
previousComment: undefined,
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (newCommentCount === 0) {
|
2023-06-23 10:52:03 +00:00
|
|
|
log("Jump To New Comment: 0 new comments found, not doing anything.");
|
2020-10-10 23:32:27 +00:00
|
|
|
} else {
|
|
|
|
log(
|
2022-02-23 13:52:06 +00:00
|
|
|
`Jump To New Comment: Initialized for ${newCommentCount} new comments.`,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jump = () => {
|
|
|
|
// Remove the new comment style from the previous
|
|
|
|
// jumped comment if there is one.
|
2023-06-23 10:52:03 +00:00
|
|
|
this.state.previousComment?.classList.remove("is-comment-new");
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
const newestComment = document.querySelector<HTMLElement>(
|
2023-06-23 10:52:03 +00:00
|
|
|
".comment.is-comment-new",
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// If there are no new comments left, hide the button.
|
|
|
|
if (newestComment === null) {
|
2023-06-23 10:52:03 +00:00
|
|
|
log("Jump To New Comment: Final comment reached, hiding the button.");
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({hidden: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the newest comment is invisible, expand all comments to make it visible.
|
|
|
|
if (newestComment.offsetParent === null) {
|
2023-06-23 10:52:03 +00:00
|
|
|
querySelector<HTMLElement>("[data-js-comment-expand-all-button]").click();
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
newestComment.scrollIntoView({behavior: "smooth"});
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({previousComment: newestComment});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const newCommentCount = this.state.newCommentCount;
|
|
|
|
|
|
|
|
// If there are no new comments, don't render anything.
|
|
|
|
if (newCommentCount === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const commentsLeft = querySelectorAll(".comment.is-comment-new").length;
|
|
|
|
const hidden = this.state.hidden ? "trx-hidden" : "";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return (
|
2022-02-23 13:52:06 +00:00
|
|
|
<a
|
|
|
|
id="trx-jump-to-new-comment"
|
2023-06-23 10:52:03 +00:00
|
|
|
class={`btn btn-primary ${hidden}`}
|
|
|
|
onClick={this.jump}
|
2022-02-23 13:52:06 +00:00
|
|
|
>
|
2023-06-23 10:52:03 +00:00
|
|
|
Jump To New Comment ({commentsLeft}/{this.state.newCommentCount})
|
2022-02-23 13:52:06 +00:00
|
|
|
</a>
|
2023-06-23 10:52:03 +00:00
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|