Skip to content

Commit

Permalink
Implement reply as proxy, #3909
Browse files Browse the repository at this point in the history
  • Loading branch information
hyifeng committed Oct 23, 2024
1 parent 88197ee commit 2f1103f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 39 deletions.
36 changes: 27 additions & 9 deletions packages/next-common/components/comment/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function CommentEditor(
createCommentReply,
updateComment,
createPostProxyComment,
createCommentProxyReply,
} = useCommentActions();

const createComment = async (realAddress) => {
Expand All @@ -78,16 +79,33 @@ function CommentEditor(
let result;

if (comment) {
result = await createCommentReply(post, comment, content, contentType);
} else if (realAddress) {
result = await createPostProxyComment(
realAddress,
post,
content,
contentType,
);
if (realAddress) {
result = await createCommentProxyReply(
realAddress,
post,
comment,
content,
contentType,
);
} else {
result = await createCommentReply(
post,
comment,
content,
contentType,
);
}
} else {
result = await createPostComment(post, content, contentType);
if (realAddress) {
result = await createPostProxyComment(
realAddress,
post,
content,
contentType,
);
} else {
result = await createPostComment(post, content, contentType);
}
}

if (!isMounted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import Popup from "../popup/wrapper/Popup";
import { ProxiedAccounts } from "../switchSignerPopup";
import { noop } from "lodash-es";
import SignerPopupWrapper from "../popupWithSigner/signerPopupWrapper";
import { GeneralProxiesProvider } from "next-common/context/proxy";

export default function SelectProxyAccountPopup({ onClose, onSelect = noop }) {
return (
<SignerPopupWrapper onClose={onClose}>
<Popup title="Select Address" className="w-[640px]" onClose={onClose}>
<div className="flex flex-col gap-[24px]">
<ProxiedAccounts onSelect={onSelect} />
</div>
<GeneralProxiesProvider>
<div className="flex flex-col gap-[24px]">
<ProxiedAccounts onSelect={onSelect} />
</div>
</GeneralProxiesProvider>
</Popup>
</SignerPopupWrapper>
);
Expand Down
9 changes: 7 additions & 2 deletions packages/next-common/components/splitButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ export default function SplitButton({
loading,
disabled,
dropdownContent,
children,
...props
}) {
const [showDropdown, setShowDropdown] = useState(false);
const ref = useRef();
useClickAway(ref, () => setShowDropdown(false));

if (loading) {
return <PrimaryButton loading={loading} {...props} />;
return (
<PrimaryButton loading={loading} {...props}>
{children}
</PrimaryButton>
);
}

return (
Expand All @@ -33,7 +38,7 @@ export default function SplitButton({
{...props}
className="rounded-tr-none rounded-br-none"
>
Comment
{children}
</PrimaryButton>
<div ref={ref} className="relative">
<PrimaryButton
Expand Down
25 changes: 1 addition & 24 deletions packages/next-common/components/splitCommentButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@ import { SystemComment } from "@osn/icons/subsquare";
import SplitMenuButton from "../splitMenuButton";
import SelectProxyAccountPopup from "../selectProxyAccountPopup";
import { useState } from "react";
import {
GeneralProxiesProvider,
useMyProxied,
} from "next-common/context/proxy";
import PrimaryButton from "next-common/lib/button/primary";

function SplitCommentButtonImpl({
export default function SplitCommentButton({
action,
onClickComment,
onClickCommentAsProxy,
...props
}) {
const { proxies } = useMyProxied();

const [isSelectProxyAccountPopupOpen, setIsSelectProxyAccountPopupOpen] =
useState(false);

if (!proxies.length) {
return (
<PrimaryButton {...props} onClick={onClickComment}>
{action}
</PrimaryButton>
);
}

return (
<>
<SplitMenuButton
Expand Down Expand Up @@ -57,11 +42,3 @@ function SplitCommentButtonImpl({
</>
);
}

export default function SplitCommentButton(props) {
return (
<GeneralProxiesProvider>
<SplitCommentButtonImpl {...props} />
</GeneralProxiesProvider>
);
}
27 changes: 27 additions & 0 deletions packages/next-common/sima/actions/proxyComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,30 @@ export function useCreateDiscussionProxyComment() {
[signSimaMessage],
);
}

export function useCreateDiscussionCommentProxyReply() {
const signSimaMessage = useSignSimaMessage();

return useCallback(
async (realAddress, post, comment, content, contentType) => {
checkSimaDataSource(comment);

const entity = {
action: "comment",
cid: comment.cid,
...getContentField(content, contentType),
timestamp: Date.now(),
real: {
address: realAddress,
section: "proxy",
},
};
const data = await signSimaMessage(entity);
return await nextApi.post(
`sima/discussions/${post.cid}/comments/${comment.cid}/replies`,
data,
);
},
[signSimaMessage],
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import {
import { useDiscussionCommentUpVote } from "next-common/sima/actions/upVote";
import { useDiscussionCommentCancelUpVote } from "next-common/sima/actions/cancelUpVote";
import { useGetComment } from "next-common/noSima/actions/comment";
import { useCreateDiscussionProxyComment } from "next-common/sima/actions/proxyComment";
import {
useCreateDiscussionCommentProxyReply,
useCreateDiscussionProxyComment,
} from "next-common/sima/actions/proxyComment";

export function DiscussionCommentActionsProvider({ children }) {
const getComment = useGetComment();
const createPostComment = useCreateDiscussionComment();
const createPostProxyComment = useCreateDiscussionProxyComment();
const createCommentReply = useCreateDiscussionCommentReply();
const createCommentProxyReply = useCreateDiscussionCommentProxyReply();
const upVoteComment = useDiscussionCommentUpVote();
const cancelUpVoteComment = useDiscussionCommentCancelUpVote();

Expand All @@ -25,6 +29,7 @@ export function DiscussionCommentActionsProvider({ children }) {
upVoteComment,
cancelUpVoteComment,
createPostProxyComment,
createCommentProxyReply,
}}
>
{children}
Expand Down

0 comments on commit 2f1103f

Please sign in to comment.