Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import { useApolloClient } from "@apollo/client";
2import { useDeletePostMutation } from "@hey/indexer";
3import type { ApolloClientError } from "@hey/types/errors";
4import { useCallback } from "react";
5import { toast } from "sonner";
6import { Alert } from "@/components/Shared/UI";
7import errorToast from "@/helpers/errorToast";
8import useTransactionLifecycle from "@/hooks/useTransactionLifecycle";
9import { useDeletePostAlertStore } from "@/store/non-persisted/alert/useDeletePostAlertStore";
10
11const DeletePost = () => {
12 const { deletingPost, setShowPostDeleteAlert, showPostDeleteAlert } =
13 useDeletePostAlertStore();
14 const { cache } = useApolloClient();
15 const handleTransactionLifecycle = useTransactionLifecycle();
16
17 const updateCache = () => {
18 cache.evict({
19 id: `${deletingPost?.__typename}:${deletingPost?.id}`
20 });
21 };
22
23 const onCompleted = () => {
24 setShowPostDeleteAlert(false);
25 updateCache();
26 toast.success("Post deleted");
27 };
28
29 const onError = useCallback((error: ApolloClientError) => {
30 errorToast(error);
31 }, []);
32
33 const [deletePost, { loading }] = useDeletePostMutation({
34 onCompleted: async ({ deletePost }) => {
35 if (deletePost.__typename === "DeletePostResponse") {
36 return onCompleted();
37 }
38
39 return await handleTransactionLifecycle({
40 onCompleted,
41 onError,
42 transactionData: deletePost
43 });
44 }
45 });
46
47 const handleDelete = async () => {
48 return await deletePost({
49 variables: { request: { post: deletingPost?.id } }
50 });
51 };
52
53 return (
54 <Alert
55 confirmText="Delete"
56 description="This can't be undone and it will be removed from your account, the timeline of any accounts that follow you, and from search results."
57 isPerformingAction={loading}
58 onClose={() => setShowPostDeleteAlert(false)}
59 onConfirm={handleDelete}
60 show={showPostDeleteAlert}
61 title="Delete Post?"
62 />
63 );
64};
65
66export default DeletePost;