Skip to content

Commit

Permalink
评论跳转
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink authored and fengxxc committed Feb 9, 2023
1 parent 2faba86 commit 91889f8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/API/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface GetCommentParams {
resource_id: number;
size: number;
page: number;
comment_id?: string;
}

export interface Comment {
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/home/LastComment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function LastComment() {
<ListItem
alignItems="flex-start"
key={item.id}
onClick={() => handleClick(item.resource_id, item.cnname)}
onClick={() => handleClick(item.resource_id, item.id, item.cnname)}
button
>
<ListItemAvatar>
Expand Down
16 changes: 13 additions & 3 deletions src/app/pages/search/CommentDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard";
import { useSnackbar } from "notistack";

import { CommentResult, postMetrics } from "API";
import { formatAvatar, formatComment, noop, getGravatar} from "utils";
import { formatAvatar, formatComment, noop, getGravatar } from "utils";
import { useGoResourcePage } from "hooks";

const useStyles = makeStyles((theme: Theme) =>
Expand All @@ -45,6 +45,9 @@ const useStyles = makeStyles((theme: Theme) =>
alignItems: "center",
justifyContent: "space-between",
},
forceCursor: {
cursor: "pointer",
},
})
);

Expand Down Expand Up @@ -81,7 +84,9 @@ export default function CommentDrawer(props: CommentResourcePropTypes) {
<Card style={{ margin: "12px 0" }}>
<CardHeader
avatar={
<Avatar style={{ fontSize: "0.875rem", color: "inherit" }} src={getGravatar(content.username)}>{formatAvatar(content.username)}</Avatar>
<Avatar style={{ fontSize: "0.875rem", color: "inherit" }} src={getGravatar(content.username)}>
{formatAvatar(content.username)}
</Avatar>
}
title={content.username}
subheader={content.date}
Expand Down Expand Up @@ -113,7 +118,12 @@ export default function CommentDrawer(props: CommentResourcePropTypes) {

<Typography align="right" variant="body2">
来自:
<Link onClick={() => handleClick(content.resourceID, content.resourceName)}>{content.resourceName}</Link>
<Link
className={classes.forceCursor}
onClick={() => handleClick(content.resourceID, content.commentID, content.resourceName)}
>
{content.resourceName}
</Link>
</Typography>
</Box>
</Box>
Expand Down
9 changes: 8 additions & 1 deletion src/component/CommentList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ export function CommentList(props: CommentListPropTypes) {
};

React.useEffect(() => {
getComment({ resource_id: id, page, size: PAGE_SIZE })
const requestParams = {
resource_id: id,
page,
size: PAGE_SIZE,
comment_id: window.location.href.split("#")[1] || undefined,
};

getComment(requestParams)
.then((res) => {
if (res) {
// @ts-ignore
Expand Down
1 change: 1 addition & 0 deletions src/features/Comment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface CommentPropTypes {
export function CommentComponent(props: CommentPropTypes) {
const { id, loading, title = "" } = props;
const [commentList, setCommentList] = React.useState<Array<Comment>>([]);

return (
<div>
{title && (
Expand Down
6 changes: 4 additions & 2 deletions src/features/Notification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function Notification() {
const handleClickComment = (notice: NoticeItem) => {
patchNotifications({ comment_id: notice.id, verb: "read" }).catch(noop);
noticePopupState.close();
toResourcePage(notice.resource_id);
toResourcePage(notice.resource_id, notice.id, "");
};

const handleLoadMore = () => {
Expand All @@ -75,7 +75,9 @@ export function Notification() {
classes={{ list: classes.list }}
>
{total === 0 && (
<Typography align="center" color="textSecondary" >╮(╯-╰)╭ 暂无消息</Typography>
<Typography align="center" color="textSecondary">
╮(╯-╰)╭ 暂无消息
</Typography>
)}
{unreadList.map((item, index) => (
<MenuItem
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useGoResourcePage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { useHistory } from "react-router-dom";
import { waitForElement } from "../utils";

export function useGoResourcePage() {
const history = useHistory();

return (id: number, title?: string) => {
return (id: number, uid: string, title?: string) => {
if (id === 233) {
history.push("/discuss");
history.push(`/discuss#${uid}`);
} else {
history.push({
pathname: "/resource",
search: `?id=${id}`,
state: { title: title || "资源页" },
});
}
// jump to element
waitForElement(uid).then((elm) => {
document.getElementById(uid)?.scrollIntoView({ behavior: "smooth" });
});
};
}
21 changes: 21 additions & 0 deletions src/utils/assetsHelper.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export const toAbsoluteUrl = (pathname: string) => process.env.PUBLIC_URL + pathname;

export function waitForElement(id: string) {
return new Promise((resolve) => {
if (document.getElementById(id)) {
return resolve(document.getElementById(id));
}

const observer = new MutationObserver((mutations) => {
if (document.getElementById(id)) {
resolve(document.getElementById(id));
observer.disconnect();
}
});

observer.observe(document.body, {
childList: true,
subtree: true,
});
return null;
});
}

0 comments on commit 91889f8

Please sign in to comment.