Skip to content

Commit 3da3be1

Browse files
committed
fix: code review, #1245
1 parent d8f20a8 commit 3da3be1

6 files changed

Lines changed: 82 additions & 79 deletions

File tree

next/components/notification/notificationItem.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ export default function NotificationItem({ data, onMarkAsRead = () => {} }) {
2929
const { type, data: { space, proposalCid, cid, content } = {} } = data;
3030

3131
const href = useMemo(() => {
32+
let anchor = "";
3233
if (type === "commentMentionUser") {
33-
return `/space/${space}/proposal/${proposalCid}#comment_${cid}`;
34+
anchor = `comment_${cid}`;
3435
}
3536
if (type === "voteMentionUser") {
36-
return `/space/${space}/proposal/${proposalCid}#vote_${cid}`;
37+
anchor = `vote_${cid}`;
3738
}
3839
if (type === "appendantMentionUser") {
39-
return `/space/${space}/proposal/${proposalCid}#appendant_${cid}`;
40+
anchor = `appendant_${cid}`;
4041
}
41-
return `/space/${space}/proposal/${proposalCid}`;
42+
return `/space/${space}/proposal/${proposalCid}?anchor=${anchor}`;
4243
}, [cid, proposalCid, space, type]);
4344

4445
return (

next/components/postDetail/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export default function PostDetail({
6262
space,
6363
votes,
6464
voteStatus,
65-
comments,
65+
commentData,
6666
defaultPage,
6767
myVote,
6868
isSafari = false,
6969
}) {
70-
const { loadSuggestions } = useSuggestions(comments, votes);
70+
const { loadSuggestions } = useSuggestions(commentData?.items, votes);
7171
useJumpToAnchor();
7272
return (
7373
<Wrapper>
@@ -90,7 +90,7 @@ export default function PostDetail({
9090
/>
9191
<PostDiscussion
9292
proposal={data}
93-
comments={comments}
93+
commentData={commentData}
9494
space={space}
9595
loadSuggestions={loadSuggestions}
9696
/>

next/components/postDetail/postDiscussion/index.jsx

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import NoData from "@osn/common-ui/es/NoData";
77
import CommitItem from "./commitItem";
88
import CommitEditor from "./commitEditor";
99

10-
const PaginationWrapper = styled.div`
11-
padding: 20px 0;
12-
display: flex;
13-
align-items: center;
14-
justify-content: center;
15-
`;
16-
1710
const NoCommentWrapper = styled.div`
1811
height: 104px;
1912
border-bottom: 1px solid var(--strokeBorderDefault);
@@ -28,49 +21,61 @@ const NoCommentWrapper = styled.div`
2821
export default function PostDiscussion({
2922
proposal,
3023
space,
31-
comments,
32-
votesPage = 1,
24+
commentData,
3325
loadSuggestions,
3426
}) {
27+
return (
28+
<AccordionPanel
29+
head={
30+
<HeaderWithNumber title="Discussions" number={commentData?.total} />
31+
}
32+
>
33+
<Items commentData={commentData} proposal={proposal} space={space} />
34+
<div className="py-5">
35+
<Pagination
36+
page={commentData?.page}
37+
total={commentData?.total}
38+
pageSize={commentData?.pageSize}
39+
pageKey="discussion_page"
40+
otherQueries={{
41+
page: commentData?.page,
42+
}}
43+
/>
44+
</div>
45+
<div className="px-5">
46+
<CommitEditor
47+
proposal={proposal}
48+
space={space}
49+
loadSuggestions={loadSuggestions}
50+
/>
51+
</div>
52+
</AccordionPanel>
53+
);
54+
}
55+
56+
function Items({ commentData, space, proposal }) {
57+
if (!commentData?.items?.length > 0) {
58+
return (
59+
<NoCommentWrapper>
60+
<NoData message="No current comment data" />
61+
</NoCommentWrapper>
62+
);
63+
}
64+
3565
const getNetwork = (comment) =>
3666
findNetworkConfig(proposal.networksConfig, comment.commenterNetwork);
3767
const spaceSupportMultiChain = space?.networks?.length > 1;
3868

3969
return (
40-
<AccordionPanel
41-
head={<HeaderWithNumber title="Discussions" number={comments?.total} />}
42-
>
43-
{(comments?.items || []).map((item, index) => (
70+
<>
71+
{(commentData?.items || []).map((item, index) => (
4472
<CommitItem
4573
key={index}
4674
item={item}
4775
spaceSupportMultiChain={spaceSupportMultiChain}
4876
space={getNetwork(item)}
4977
/>
5078
))}
51-
<div className="px-5 md:px-8">
52-
{!comments?.items?.length > 0 && (
53-
<NoCommentWrapper>
54-
<NoData message="No current comments" />
55-
</NoCommentWrapper>
56-
)}
57-
<PaginationWrapper>
58-
<Pagination
59-
page={comments?.page}
60-
total={comments?.total}
61-
pageSize={comments?.pageSize}
62-
pageKey="discussion_page"
63-
otherQueries={{
64-
page: votesPage,
65-
}}
66-
/>
67-
</PaginationWrapper>
68-
<CommitEditor
69-
proposal={proposal}
70-
space={space}
71-
loadSuggestions={loadSuggestions}
72-
/>
73-
</div>
74-
</AccordionPanel>
79+
</>
7580
);
7681
}

next/components/postDetail/suggestions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function useSuggestions(comments = [], votes = null) {
1515
}-${user.network}) `;
1616

1717
const fetchIdentitySuggestions = useCallback(async () => {
18-
const commentUsers = (comments?.items || []).map((comment) => ({
18+
const commentUsers = (comments || []).map((comment) => ({
1919
address: comment.address,
2020
network: comment.commenterNetwork,
2121
source: "comment",
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from "react";
1+
import { useEffect, useState } from "react";
22
import { useRouter } from "next/router";
33
import { useMount } from "react-use";
44

@@ -17,19 +17,12 @@ function jumpToAnchor(anchorId) {
1717
});
1818
}
1919

20-
export const useAnchor = () => {
21-
const router = useRouter();
22-
const anchorId = useMemo(() => {
23-
return router.asPath.split("#")[1];
24-
}, [router.asPath]);
25-
26-
return anchorId;
27-
};
28-
2920
export const useJumpToAnchor = () => {
21+
const router = useRouter();
22+
const anchorId = router.query.anchor;
3023
const [isMounted, setIsMounted] = useState();
3124
useMount(() => setIsMounted(true));
32-
const anchorId = useAnchor();
25+
3326
useEffect(() => {
3427
if (anchorId && isMounted) {
3528
setTimeout(() => {
@@ -40,17 +33,11 @@ export const useJumpToAnchor = () => {
4033
};
4134

4235
export const useActiveAnchor = (anchor) => {
43-
const [id, setId] = useState("");
44-
const anchorId = useAnchor();
45-
46-
useEffect(() => {
47-
setTimeout(() => {
48-
setId(anchor);
49-
}, 200);
50-
}, [anchor]);
36+
const router = useRouter();
37+
const anchorId = router.query.anchor;
5138

5239
return {
53-
id,
54-
active: id === anchorId,
40+
id: anchor,
41+
active: anchor === anchorId,
5542
};
5643
};

next/pages/space/[space]/proposal/[id].js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function Index({
2121
space,
2222
votes,
2323
voteStatus,
24-
comments,
24+
commentData,
2525
defaultPage,
2626
myVote,
2727
isSafari,
@@ -114,7 +114,7 @@ export default function Index({
114114
space={space}
115115
votes={votes}
116116
voteStatus={voteStatus}
117-
comments={comments}
117+
commentData={commentData}
118118
defaultPage={defaultPage}
119119
myVote={savedMyVote}
120120
isSafari={isSafari}
@@ -130,11 +130,21 @@ export async function getServerSideProps(context) {
130130
userAgent.includes("Safari") && !userAgent.includes("Chrome");
131131

132132
const { id, space: spaceId } = context.params;
133-
const { page, discussion_page: discussionPage } = context.query;
134-
135-
const nPage = page === "last" ? "last" : parseInt(page) || 1;
136-
const discusPage =
137-
discussionPage === "last" ? "last" : parseInt(discussionPage) || 1;
133+
const { page, discussion_page: discussionPage, anchor = "" } = context.query;
134+
135+
const hasVoteAnchor = anchor.startsWith("vote");
136+
const hasCommentAnchor = anchor.startsWith("comment");
137+
138+
const votePage = hasVoteAnchor
139+
? 1
140+
: page === "last"
141+
? "last"
142+
: parseInt(page) || 1;
143+
const discusPage = hasCommentAnchor
144+
? 1
145+
: discussionPage === "last"
146+
? "last"
147+
: parseInt(discussionPage) || 1;
138148

139149
const { result: detail } = await ssrNextApi.fetch(
140150
`${spaceId}/proposal/${id}`,
@@ -148,17 +158,17 @@ export async function getServerSideProps(context) {
148158
{ result: space },
149159
{ result: votes },
150160
{ result: voteStatus },
151-
{ result: comments },
161+
{ result: commentData },
152162
] = await Promise.all([
153163
ssrNextApi.fetch(`spaces/${spaceId}`),
154164
ssrNextApi.fetch(`${spaceId}/proposal/${detail?.cid}/votes`, {
155-
page: nPage,
156-
pageSize: 50,
165+
page: votePage,
166+
pageSize: hasVoteAnchor ? 200 : 50,
157167
}),
158168
ssrNextApi.fetch(`${spaceId}/proposal/${detail?.cid}/stats`),
159169
ssrNextApi.fetch(`${spaceId}/proposal/${detail?.cid}/comments`, {
160170
page: discusPage,
161-
pageSize: 25,
171+
pageSize: hasCommentAnchor ? 200 : 25,
162172
}),
163173
]);
164174

@@ -179,8 +189,8 @@ export async function getServerSideProps(context) {
179189
space: space ?? null,
180190
votes: votes ?? EmptyQuery,
181191
voteStatus: voteStatus ?? [],
182-
comments: comments ?? EmptyQuery,
183-
defaultPage: { page: nPage, discussionPage: discusPage },
192+
commentData: commentData ?? EmptyQuery,
193+
defaultPage: { page: votePage, discussionPage: discusPage },
184194
myVote: myVote ?? null,
185195
isSafari,
186196
},

0 commit comments

Comments
 (0)