Scroll to comments on post's x comments button (#312)

* scroll to comments on post's x comments button

* scrolls down to comments using url params
This commit is contained in:
hammsvietro 2021-08-11 22:00:26 -03:00 committed by GitHub
parent 6b4265bafe
commit 3870703b12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -466,13 +466,15 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
let post_view = this.props.post_view; let post_view = this.props.post_view;
return ( return (
<div class="d-flex justify-content-between justify-content-lg-start flex-wrap text-muted font-weight-bold mb-1"> <div class="d-flex justify-content-between justify-content-lg-start flex-wrap text-muted font-weight-bold mb-1">
<button class="btn btn-link text-muted p-0"> <button
class="btn btn-link text-muted p-0"
>
<Link <Link
className="text-muted small" className="text-muted small"
title={i18n.t("number_of_comments", { title={i18n.t("number_of_comments", {
count: post_view.counts.comments, count: post_view.counts.comments,
})} })}
to={`/post/${post_view.post.id}`} to={`/post/${post_view.post.id}?scrollToComments=true`}
> >
<Icon icon="message-square" classes="icon-inline mr-1" /> <Icon icon="message-square" classes="icon-inline mr-1" />
{i18n.t("number_of_comments", { {i18n.t("number_of_comments", {

View file

@ -1,5 +1,5 @@
import { Component, linkEvent, createRef, RefObject } from "inferno";
import autosize from "autosize"; import autosize from "autosize";
import { Component, linkEvent } from "inferno";
import { import {
AddAdminResponse, AddAdminResponse,
AddModToCommunityResponse, AddModToCommunityResponse,
@ -73,6 +73,7 @@ interface PostState {
loading: boolean; loading: boolean;
crossPosts: PostView[]; crossPosts: PostView[];
siteRes: GetSiteResponse; siteRes: GetSiteResponse;
commentSectionRef?: RefObject<HTMLDivElement>;
showSidebarMobile: boolean; showSidebarMobile: boolean;
} }
@ -90,6 +91,7 @@ export class Post extends Component<any, PostState> {
loading: true, loading: true,
crossPosts: [], crossPosts: [],
siteRes: this.isoData.site_res, siteRes: this.isoData.site_res,
commentSectionRef: null,
showSidebarMobile: false, showSidebarMobile: false,
}; };
@ -97,6 +99,7 @@ export class Post extends Component<any, PostState> {
super(props, context); super(props, context);
this.state = this.emptyState; this.state = this.emptyState;
this.state.commentSectionRef = createRef();
this.parseMessage = this.parseMessage.bind(this); this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage); this.subscription = wsSubscribe(this.parseMessage);
@ -114,6 +117,8 @@ export class Post extends Component<any, PostState> {
this.fetchCrossPosts(); this.fetchCrossPosts();
if (this.state.commentId) { if (this.state.commentId) {
this.scrollCommentIntoView(); this.scrollCommentIntoView();
} else if (new URLSearchParams(this.props.location.search).get('scrollToComments')) {
this.scrollIntoCommentSection();
} }
} }
} else { } else {
@ -183,6 +188,10 @@ export class Post extends Component<any, PostState> {
this.scrollCommentIntoView(); this.scrollCommentIntoView();
} }
if (new URLSearchParams(this.props.location.search).get('scrollToComments') ) {
this.scrollIntoCommentSection();
}
// Necessary if you are on a post and you click another post (same route) // Necessary if you are on a post and you click another post (same route)
if (_lastProps.location.pathname !== _lastProps.history.location.pathname) { if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
// TODO Couldnt get a refresh working. This does for now. // TODO Couldnt get a refresh working. This does for now.
@ -203,6 +212,10 @@ export class Post extends Component<any, PostState> {
this.markScrolledAsRead(this.state.commentId); this.markScrolledAsRead(this.state.commentId);
} }
scrollIntoCommentSection() {
this.state.commentSectionRef.current?.scrollIntoView();
}
// TODO this needs some re-work // TODO this needs some re-work
markScrolledAsRead(commentId: number) { markScrolledAsRead(commentId: number) {
let found = this.state.postRes.comments.find( let found = this.state.postRes.comments.find(
@ -277,7 +290,7 @@ export class Post extends Component<any, PostState> {
} }
enableNsfw={this.state.siteRes.site_view.site.enable_nsfw} enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
/> />
<div className="mb-2" /> <div ref={this.state.commentSectionRef} className="mb-2" />
<CommentForm <CommentForm
postId={this.state.postId} postId={this.state.postId}
disabled={pv.post.locked} disabled={pv.post.locked}
@ -490,6 +503,9 @@ export class Post extends Component<any, PostState> {
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
if (!this.state.commentId) restoreScrollPosition(this.context); if (!this.state.commentId) restoreScrollPosition(this.context);
if (new URLSearchParams(this.props.location.search).get('scrollToComments') ) {
this.scrollIntoCommentSection();
}
} else if (op == UserOperation.CreateComment) { } else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg).data; let data = wsJsonToRes<CommentResponse>(msg).data;