1
Fork 0
tildes-reextended/source/content-scripts/features/jump-to-new-comment.tsx

80 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-06-23 10:52:03 +00:00
import {Component} from "preact";
import {log, querySelector, querySelectorAll} from "../../utilities/exports.js";
type Props = Record<string, unknown>;
type State = {
hidden: boolean;
newCommentCount: number;
previousComment: HTMLElement | undefined;
};
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;
this.state = {
hidden: false,
newCommentCount,
previousComment: undefined,
};
if (newCommentCount === 0) {
2023-06-23 10:52:03 +00:00
log("Jump To New Comment: 0 new comments found, not doing anything.");
} else {
log(
`Jump To New Comment: Initialized for ${newCommentCount} new comments.`,
);
}
}
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");
const newestComment = document.querySelector<HTMLElement>(
2023-06-23 10:52:03 +00:00
".comment.is-comment-new",
);
// 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.");
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();
}
2023-06-23 10:52:03 +00:00
newestComment.scrollIntoView({behavior: "smooth"});
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" : "";
2023-06-23 10:52:03 +00:00
return (
<a
id="trx-jump-to-new-comment"
2023-06-23 10:52:03 +00:00
class={`btn btn-primary ${hidden}`}
onClick={this.jump}
>
2023-06-23 10:52:03 +00:00
Jump To New Comment ({commentsLeft}/{this.state.newCommentCount})
</a>
2023-06-23 10:52:03 +00:00
);
}
}