Skip to content

Commit

Permalink
Migrate to Lens v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoginth committed Dec 5, 2024
1 parent 79c2abb commit 00a131d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
47 changes: 7 additions & 40 deletions apps/web/src/components/Composer/NewPublication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import { POST } from "@hey/data/tracking";
import collectModuleParams from "@hey/helpers/collectModuleParams";
import getAccount from "@hey/helpers/getAccount";
import getMentions from "@hey/helpers/getMentions";
import removeQuoteOn from "@hey/helpers/removeQuoteOn";
import type { CreatePostRequest, Post } from "@hey/indexer";
import type { CreatePostRequest, Post, PostResponse } from "@hey/indexer";
import type { IGif } from "@hey/types/giphy";
import type { NewAttachment } from "@hey/types/misc";
import { Button, Card, ErrorMessage, H6 } from "@hey/ui";
import { Button, Card, H6 } from "@hey/ui";
import { MetadataAttributeType } from "@lens-protocol/metadata";
import dynamic from "next/dynamic";
import type { FC } from "react";
Expand Down Expand Up @@ -165,45 +164,20 @@ const NewPublication: FC<NewPublicationProps> = ({ className, post }) => {
errorToast(error);
};

const onCompleted = (
__typename?:
| "CreateMomokaPublicationResult"
| "LensProfileManagerRelayError"
| "RelayError"
| "RelaySuccess"
) => {
if (
__typename === "RelayError" ||
__typename === "LensProfileManagerRelayError"
) {
const onCompleted = (post?: PostResponse) => {
if (post?.__typename !== "PostResponse") {
return onError();
}

// Reset states
reset();

// Track in leafwatch
const eventProperties = {
comment_on: isComment ? post?.id : null,
post_collect_module: collectModule.type,
post_has_attachments: attachments.length > 0,
post_has_poll: showPollEditor,
post_is_live: showLiveVideoEditor,
post_reference_module: selectedReferenceModule,
post_reference_module_degrees_of_separation:
selectedReferenceModule ===
ReferenceModuleType.DegreesOfSeparationReferenceModule
? degreesOfSeparation
: null,
quote_on: isQuote ? quotedPost?.id : null
};
Leafwatch.track(
isComment ? POST.NEW_COMMENT : isQuote ? POST.NEW_QUOTE : POST.NEW_POST,
eventProperties
isComment ? POST.NEW_COMMENT : isQuote ? POST.NEW_QUOTE : POST.NEW_POST
);
};

const { createPost, error } = useCreatePost({
const { createPost } = useCreatePost({
commentOn: post,
onCompleted,
onError,
Expand Down Expand Up @@ -368,13 +342,6 @@ const NewPublication: FC<NewPublicationProps> = ({ className, post }) => {

return (
<Card className={className} onClick={() => setShowEmojiPicker(false)}>
{error ? (
<ErrorMessage
className="!rounded-none"
error={error}
title="Transaction failed!"
/>
) : null}
<Editor />
{postContentError ? (
<H6 className="mt-1 px-5 pb-3 text-red-500">{postContentError}</H6>
Expand All @@ -385,7 +352,7 @@ const NewPublication: FC<NewPublicationProps> = ({ className, post }) => {
<NewAttachments attachments={attachments} />
{quotedPost ? (
<Wrapper className="m-5" zeroPadding>
<QuotedPost isNew post={removeQuoteOn(quotedPost as Quote)} />
<QuotedPost isNew post={quotedPost as Post} />
</Wrapper>
) : null}
<div className="divider mx-5" />
Expand Down
41 changes: 37 additions & 4 deletions apps/web/src/hooks/useCreatePost.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import selfFundedTransactionData from "@hey/helpers/selfFundedTransactionData";
import sponsoredTransactionData from "@hey/helpers/sponsoredTransactionData";
import {
type CreatePostRequest,
type Post,
type PostResponse,
useCreatePostMutation
} from "@hey/indexer";
import { OptmisticPostType } from "@hey/types/enums";
import type { OptimisticTransaction } from "@hey/types/misc";
import toast from "react-hot-toast";
import { usePostStore } from "src/store/non-persisted/post/usePostStore";
import { useTransactionStore } from "src/store/persisted/useTransactionStore";
import { sendEip712Transaction, sendTransaction } from "viem/zksync";
import { useWalletClient } from "wagmi";

interface CreatePostProps {
commentOn?: Post;
onCompleted: (status?: any) => void;
onCompleted: (post?: PostResponse) => void;
onError: (error: any) => void;
quoteOf?: Post;
}
Expand All @@ -23,6 +29,7 @@ const useCreatePost = ({
}: CreatePostProps) => {
const { postContent } = usePostStore();
const { addTransaction } = useTransactionStore();
const { data: walletClient } = useWalletClient();

const isComment = Boolean(commentOn);
const isQuote = Boolean(quoteOf);
Expand All @@ -46,10 +53,36 @@ const useCreatePost = ({

// Onchain mutations
const [post] = useCreatePostMutation({
onCompleted: ({ post }) => {
onCompleted(post.__typename);
onCompleted: async ({ post }) => {
if (post.__typename === "PostResponse") {
addTransaction(generateOptimisticPublication({ txHash: post.hash }));
return onCompleted(post);
}

if (walletClient) {
if (post.__typename === "SponsoredTransactionRequest") {
const hash = await sendEip712Transaction(walletClient, {
account: walletClient.account,
...sponsoredTransactionData(post.raw)
});
addTransaction(generateOptimisticPublication({ txHash: hash }));

return onCompleted();
}

if (post.__typename === "SelfFundedTransactionRequest") {
const hash = await sendTransaction(walletClient, {
account: walletClient.account,
...selfFundedTransactionData(post.raw)
});
addTransaction(generateOptimisticPublication({ txHash: hash }));

return onCompleted();
}
}

if (post.__typename === "TransactionWillFail") {
return toast.error(post.reason);
}
},
onError
Expand All @@ -62,7 +95,7 @@ const useCreatePost = ({
}
};

return { createPost, error };
return { createPost };
};

export default useCreatePost;

0 comments on commit 00a131d

Please sign in to comment.