Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import { useApolloClient } from "@apollo/client";
2import {
3 PostDocument,
4 type PostFragment,
5 useCreatePostMutation,
6 usePostLazyQuery
7} from "@hey/indexer";
8import type { ApolloClientError } from "@hey/types/errors";
9import { useCallback } from "react";
10import { useNavigate } from "react-router";
11import { toast } from "sonner";
12import useTransactionLifecycle from "./useTransactionLifecycle";
13import useWaitForTransactionToComplete from "./useWaitForTransactionToComplete";
14
15interface CreatePostProps {
16 commentOn?: PostFragment;
17 onCompleted: () => void;
18 onError: (error: ApolloClientError) => void;
19}
20
21const useCreatePost = ({
22 commentOn,
23 onCompleted,
24 onError
25}: CreatePostProps) => {
26 const navigate = useNavigate();
27 const handleTransactionLifecycle = useTransactionLifecycle();
28 const waitForTransactionToComplete = useWaitForTransactionToComplete();
29 const [getPost] = usePostLazyQuery();
30 const { cache } = useApolloClient();
31 const isComment = Boolean(commentOn);
32
33 const updateCache = useCallback(
34 async (txHash: string, toastId: string | number) => {
35 const { data } = await getPost({
36 fetchPolicy: "cache-and-network",
37 variables: { request: { txHash } }
38 });
39
40 if (!data?.post) {
41 return;
42 }
43
44 const type = isComment ? "Comment" : "Post";
45
46 toast.success(`${type} created successfully!`, {
47 action: {
48 label: "View",
49 onClick: () => navigate(`/posts/${data.post?.slug}`)
50 },
51 id: toastId
52 });
53 cache.modify({
54 fields: {
55 [isComment ? "postReferences" : "posts"]: () => {
56 cache.writeQuery({ data: data.post, query: PostDocument });
57 }
58 }
59 });
60 },
61 [getPost, cache, navigate, isComment]
62 );
63
64 const onCompletedWithTransaction = useCallback(
65 (hash: string) => {
66 const toastId = toast.loading(
67 `${isComment ? "Comment" : "Post"} processing...`
68 );
69 waitForTransactionToComplete(hash).then(() => updateCache(hash, toastId));
70 return onCompleted();
71 },
72 [waitForTransactionToComplete, updateCache, onCompleted, isComment]
73 );
74
75 // Onchain mutations
76 const [createPost] = useCreatePostMutation({
77 onCompleted: async ({ post }) => {
78 if (post.__typename === "PostResponse") {
79 return onCompletedWithTransaction(post.hash);
80 }
81
82 return await handleTransactionLifecycle({
83 onCompleted: onCompletedWithTransaction,
84 onError,
85 transactionData: post
86 });
87 },
88 onError
89 });
90
91 return { createPost };
92};
93
94export default useCreatePost;