Skip to content

Commit

Permalink
Merge branch 'release-3.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wliyongfeng committed Mar 29, 2024
2 parents 06df969 + d1d8d7e commit 58610b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
29 changes: 17 additions & 12 deletions packages/next-common/components/comment/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Tooltip from "../tooltip";
import CommentItemTemplate from "./itemTemplate";
import { useIsUniversalPostComments } from "next-common/hooks/usePostComments";
import { CommentProvider, useComment } from "./context";
import PolkassemblyCommentItem from "./polkassemblyCommentItem";

function jumpToAnchor(anchorId) {
var anchorElement = document.getElementById(anchorId);
Expand Down Expand Up @@ -174,18 +175,22 @@ function CommentItemImpl({
setIsEdit={setIsEdit}
/>
}
renderReplyItem={(reply) => (
<CommentItem
key={reply._id}
data={reply}
replyToCommentId={replyToCommentId}
isSecondLevel
updateTopLevelComment={updateTopLevelComment || updateComment}
scrollToTopLevelCommentBottom={
scrollToTopLevelCommentBottom || scrollToCommentBottom
}
/>
)}
renderReplyItem={(reply) =>
reply.comment_source === "polkassembly" ? (
<PolkassemblyCommentItem key={reply.id} data={reply} isSecondLevel />
) : (
<CommentItem
key={reply._id}
data={reply}
replyToCommentId={replyToCommentId}
isSecondLevel
updateTopLevelComment={updateTopLevelComment || updateComment}
scrollToTopLevelCommentBottom={
scrollToTopLevelCommentBottom || scrollToCommentBottom
}
/>
)
}
/>
);
}
Expand Down
35 changes: 31 additions & 4 deletions packages/next-common/hooks/usePostComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,38 @@ import dayjs from "dayjs";
import { useDetailType } from "next-common/context/page";
import { detailPageCategory } from "next-common/utils/consts/business/category";
import { PolkassemblyChains } from "next-common/utils/polkassembly";
import { cloneDeep } from "lodash-es";

function getShouldReadPolkassemblyComments(chain) {
return PolkassemblyChains.includes(chain);
}

function mergeComments(polkassemblyComments, subsquareComments) {
const filteredPolkassemblyComments = [];
const newSubsquareComments = cloneDeep(subsquareComments);
for (const polkaItem of polkassemblyComments ?? []) {
const subsquareItem = newSubsquareComments.find(
(item) => item._id === polkaItem.id,
);
if (subsquareItem) {
subsquareItem.replies = subsquareItem.replies || [];
subsquareItem.replies.push(
...polkaItem.replies.map((r) => ({
...r,
comment_source: "polkassembly",
})),
);
} else {
filteredPolkassemblyComments.push(polkaItem);
}
}

return [
...(filteredPolkassemblyComments ?? []),
...(newSubsquareComments ?? []),
].sort((a, b) => dayjs(a.createdAt).unix() - dayjs(b.createdAt).unix());
}

export function usePostCommentsData() {
const comments = useComments();
const chain = useChain();
Expand All @@ -27,10 +54,10 @@ export function usePostCommentsData() {
if (!polkassemblyPostData.loadingComments) {
const data = { ...comments };

data.items = [
...(polkassemblyPostData.comments ?? []),
...(comments.items ?? []),
].sort((a, b) => dayjs(a.createdAt).unix() - dayjs(b.createdAt).unix());
data.items = mergeComments(
polkassemblyPostData.comments,
comments.items,
);

setCommentsData(data);
}
Expand Down

0 comments on commit 58610b5

Please sign in to comment.