Fix comment context bug

This commit is contained in:
SleeplessOne1917 2023-08-03 22:30:41 -04:00
parent 8edf69559e
commit 3ec08b1479
6 changed files with 36 additions and 40 deletions

View file

@ -1194,7 +1194,7 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
<> <>
<Link <Link
className={classnames} className={classnames}
to={`/comment/${parentCommentId}`} to={`/post/${cv.comment.post_id}/comment/${parentCommentId}`}
title={title} title={title}
> >
<Icon icon="link" classes="icon-inline" /> <Icon icon="link" classes="icon-inline" />

View file

@ -334,7 +334,7 @@ function renderModlogType({ type_, view }: ModlogType) {
const mrc = view as ModRemoveCommentView; const mrc = view as ModRemoveCommentView;
const { const {
mod_remove_comment: { reason, removed }, mod_remove_comment: { reason, removed },
comment: { id, content }, comment: { id, content, post_id },
commenter, commenter,
} = mrc; } = mrc;
@ -342,7 +342,7 @@ function renderModlogType({ type_, view }: ModlogType) {
<> <>
<span>{removed ? "Removed " : "Restored "}</span> <span>{removed ? "Removed " : "Restored "}</span>
<span> <span>
Comment <Link to={`/comment/${id}`}>{content}</Link> Comment <Link to={`/post/${post_id}/comment/${id}`}>{content}</Link>
</span> </span>
<span> <span>
{" "} {" "}

View file

@ -5,10 +5,8 @@ import {
editWith, editWith,
enableDownvotes, enableDownvotes,
enableNsfw, enableNsfw,
getCommentIdFromProps,
getCommentParentId, getCommentParentId,
getDepthFromComment, getDepthFromComment,
getIdFromProps,
myAuth, myAuth,
setIsoData, setIsoData,
updateCommunityBlock, updateCommunityBlock,
@ -92,17 +90,20 @@ import { HtmlTags } from "../common/html-tags";
import { Icon, Spinner } from "../common/icon"; import { Icon, Spinner } from "../common/icon";
import { Sidebar } from "../community/sidebar"; import { Sidebar } from "../community/sidebar";
import { PostListing } from "./post-listing"; import { PostListing } from "./post-listing";
import { RouteComponentProps } from "inferno-router/dist/Route";
const commentsShownInterval = 15; const commentsShownInterval = 15;
interface PostParams {
post_id?: number;
comment_id?: number;
}
type PostData = RouteDataResponse<{ type PostData = RouteDataResponse<{
postRes: GetPostResponse; postRes: GetPostResponse;
commentsRes: GetCommentsResponse; commentsRes: GetCommentsResponse;
}>; }>;
interface PostState { interface PostState {
postId?: number;
commentId?: number;
postRes: RequestState<GetPostResponse>; postRes: RequestState<GetPostResponse>;
commentsRes: RequestState<GetCommentsResponse>; commentsRes: RequestState<GetCommentsResponse>;
commentSort: CommentSortType; commentSort: CommentSortType;
@ -116,14 +117,15 @@ interface PostState {
isIsomorphic: boolean; isIsomorphic: boolean;
} }
export class Post extends Component<any, PostState> { export class Post extends Component<
RouteComponentProps<PostParams>,
PostState
> {
private isoData = setIsoData<PostData>(this.context); private isoData = setIsoData<PostData>(this.context);
private commentScrollDebounced: () => void; private commentScrollDebounced: () => void;
state: PostState = { state: PostState = {
postRes: { state: "empty" }, postRes: { state: "empty" },
commentsRes: { state: "empty" }, commentsRes: { state: "empty" },
postId: getIdFromProps(this.props),
commentId: getCommentIdFromProps(this.props),
commentSort: "Hot", commentSort: "Hot",
commentViewType: CommentViewType.Tree, commentViewType: CommentViewType.Tree,
scrolled: false, scrolled: false,
@ -136,7 +138,6 @@ export class Post extends Component<any, PostState> {
constructor(props: any, context: any) { constructor(props: any, context: any) {
super(props, context); super(props, context);
this.handleDeleteCommunityClick = this.handleDeleteCommunityClick =
this.handleDeleteCommunityClick.bind(this); this.handleDeleteCommunityClick.bind(this);
this.handleEditCommunity = this.handleEditCommunity.bind(this); this.handleEditCommunity = this.handleEditCommunity.bind(this);
@ -200,16 +201,17 @@ export class Post extends Component<any, PostState> {
}); });
const auth = myAuth(); const auth = myAuth();
const { post_id, comment_id } = this.props.match.params;
this.setState({ this.setState({
postRes: await HttpService.client.getPost({ postRes: await HttpService.client.getPost({
id: this.state.postId, id: post_id,
comment_id: this.state.commentId, comment_id: comment_id,
auth, auth,
}), }),
commentsRes: await HttpService.client.getComments({ commentsRes: await HttpService.client.getComments({
post_id: this.state.postId, post_id: this.props.match.params.post_id,
parent_id: this.state.commentId, parent_id: comment_id,
max_depth: commentTreeMaxDepth, max_depth: commentTreeMaxDepth,
sort: this.state.commentSort, sort: this.state.commentSort,
type_: "All", type_: "All",
@ -220,7 +222,9 @@ export class Post extends Component<any, PostState> {
setupTippy(); setupTippy();
if (!this.state.commentId) restoreScrollPosition(this.context); if (!comment_id) {
restoreScrollPosition(this.context);
}
if (this.checkScrollIntoCommentsParam) { if (this.checkScrollIntoCommentsParam) {
this.scrollIntoCommentSection(); this.scrollIntoCommentSection();
@ -234,11 +238,13 @@ export class Post extends Component<any, PostState> {
}: InitialFetchRequest): Promise<PostData> { }: InitialFetchRequest): Promise<PostData> {
const pathSplit = path.split("/"); const pathSplit = path.split("/");
const pathType = pathSplit.at(1);
const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined; const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
const comment_id = pathSplit.at(4) ? Number(pathSplit.at(4)) : undefined;
const postForm: GetPost = { const postForm: GetPost = {
auth, auth,
id,
comment_id,
}; };
const commentsForm: GetComments = { const commentsForm: GetComments = {
@ -247,17 +253,10 @@ export class Post extends Component<any, PostState> {
type_: "All", type_: "All",
saved_only: false, saved_only: false,
auth, auth,
post_id: id,
parent_id: comment_id,
}; };
// Set the correct id based on the path type
if (pathType === "post") {
postForm.id = id;
commentsForm.post_id = id;
} else {
postForm.comment_id = id;
commentsForm.parent_id = id;
}
return { return {
postRes: await client.getPost(postForm), postRes: await client.getPost(postForm),
commentsRes: await client.getComments(commentsForm), commentsRes: await client.getComments(commentsForm),
@ -609,7 +608,7 @@ export class Post extends Component<any, PostState> {
return ( return (
res.state === "success" && ( res.state === "success" && (
<div> <div>
{!!this.state.commentId && ( {!!this.props.match.params.comment_id && (
<> <>
<button <button
className="ps-0 d-block btn btn-link text-muted" className="ps-0 d-block btn btn-link text-muted"
@ -665,9 +664,11 @@ export class Post extends Component<any, PostState> {
commentTree(): CommentNodeI[] { commentTree(): CommentNodeI[] {
if (this.state.commentsRes.state === "success") { if (this.state.commentsRes.state === "success") {
const { comment_id } = this.props.match.params;
return buildCommentsTree( return buildCommentsTree(
this.state.commentsRes.data.comments, this.state.commentsRes.data.comments,
!!this.state.commentId, !!comment_id,
); );
} else { } else {
return []; return [];
@ -704,11 +705,12 @@ export class Post extends Component<any, PostState> {
handleViewContext(i: Post) { handleViewContext(i: Post) {
if (i.state.commentsRes.state === "success") { if (i.state.commentsRes.state === "success") {
const parentId = getCommentParentId( const comment = i.state.commentsRes.data.comments.at(0)?.comment;
i.state.commentsRes.data.comments.at(0)?.comment, const parentId = getCommentParentId(comment);
);
if (parentId) { if (parentId) {
i.context.router.history.push(`/comment/${parentId}`); i.context.router.history.push(
`/post/${comment?.post_id}/comment/${parentId}`,
);
} }
} }
} }

View file

@ -67,12 +67,12 @@ export const routes: IRoutePropsWithFetch<Record<string, any>>[] = [
fetchInitialData: Communities.fetchInitialData, fetchInitialData: Communities.fetchInitialData,
}, },
{ {
path: `/post/:post_id`, path: `/post/:post_id/comment/:comment_id`,
component: Post, component: Post,
fetchInitialData: Post.fetchInitialData, fetchInitialData: Post.fetchInitialData,
}, },
{ {
path: `/comment/:comment_id`, path: `/post/:post_id`,
component: Post, component: Post,
fetchInitialData: Post.fetchInitialData, fetchInitialData: Post.fetchInitialData,
}, },

View file

@ -1,4 +0,0 @@
export default function getIdFromProps(props: any): number | undefined {
const id = props.match.params.post_id;
return id ? Number(id) : undefined;
}

View file

@ -27,7 +27,6 @@ import getCommentIdFromProps from "./get-comment-id-from-props";
import getCommentParentId from "./get-comment-parent-id"; import getCommentParentId from "./get-comment-parent-id";
import getDataTypeString from "./get-data-type-string"; import getDataTypeString from "./get-data-type-string";
import getDepthFromComment from "./get-depth-from-comment"; import getDepthFromComment from "./get-depth-from-comment";
import getIdFromProps from "./get-id-from-props";
import getRecipientIdFromProps from "./get-recipient-id-from-props"; import getRecipientIdFromProps from "./get-recipient-id-from-props";
import getUpdatedSearchId from "./get-updated-search-id"; import getUpdatedSearchId from "./get-updated-search-id";
import initializeSite from "./initialize-site"; import initializeSite from "./initialize-site";
@ -84,7 +83,6 @@ export {
getCommentParentId, getCommentParentId,
getDataTypeString, getDataTypeString,
getDepthFromComment, getDepthFromComment,
getIdFromProps,
getRecipientIdFromProps, getRecipientIdFromProps,
getUpdatedSearchId, getUpdatedSearchId,
initializeSite, initializeSite,