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 6, 2024
1 parent 4d8c8fc commit ef88d93
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 41 deletions.
98 changes: 76 additions & 22 deletions apps/web/src/components/Settings/Handles/UnlinkHandle.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,88 @@
import IndexStatus from "@components/Shared/IndexStatus";
import errorToast from "@helpers/errorToast";
import { Errors } from "@hey/data/errors";
import selfFundedTransactionData from "@hey/helpers/selfFundedTransactionData";
import sponsoredTransactionData from "@hey/helpers/sponsoredTransactionData";
import { useUnassignUsernameFromAccountMutation } from "@hey/indexer";
import { OptmisticPostType } from "@hey/types/enums";
import type { OptimisticTransaction } from "@hey/types/misc";
import { Button } from "@hey/ui";
import type { FC } from "react";
import { useState } from "react";
import toast from "react-hot-toast";
import { useAccountStatus } from "src/store/non-persisted/useAccountStatus";
import { useAccountStore } from "src/store/persisted/useAccountStore";
import { useTransactionStore } from "src/store/persisted/useTransactionStore";
import { sendEip712Transaction, sendTransaction } from "viem/zksync";
import { useWalletClient } from "wagmi";

const UnlinkHandle: FC = () => {
const { currentAccount } = useAccountStore();
const { isSuspended } = useAccountStatus();
const { addTransaction } = useTransactionStore();
const [unlinking, setUnlinking] = useState<boolean>(false);
const { data: walletClient } = useWalletClient();

const onError = (error: any) => {
const generateOptimisticUnassignUsername = ({
txHash
}: {
txHash: string;
}): OptimisticTransaction => {
return {
txHash,
type: OptmisticPostType.UnassignUsername
};
};

const onCompleted = (hash: string) => {
setUnlinking(false);
errorToast(error);
addTransaction(generateOptimisticUnassignUsername({ txHash: hash }));
toast.success("Unlinked");
};

const [unassignUsernameFromAccount] = useUnassignUsernameFromAccountMutation({
onCompleted: async ({ unassignUsernameFromAccount }) => {
if (
unassignUsernameFromAccount.__typename === "UnassignUsernameResponse"
) {
return onCompleted(unassignUsernameFromAccount.hash);
}

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

return onCompleted(hash);
}

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

return onCompleted(hash);
}
}

if (unassignUsernameFromAccount.__typename === "TransactionWillFail") {
return toast.error(unassignUsernameFromAccount.reason);
}
},
onError: (error) => {
setUnlinking(false);
errorToast(error);
}
});

const handleUnlink = async () => {
if (!currentAccount) {
return;
Expand All @@ -27,28 +92,17 @@ const UnlinkHandle: FC = () => {
return toast.error(Errors.Suspended);
}

try {
setUnlinking(true);
return await createUnlinkHandleFromProfileTypedData({
variables: { request }
});
} catch (error) {
onError(error);
}
setUnlinking(true);

return await unassignUsernameFromAccount({
variables: { request: currentAccount.address }
});
};

return (
<div className="m-5">
{writeHash ? (
<div className="mt-2">
<IndexStatus shouldReload txHash={writeHash} />
</div>
) : (
<Button disabled={unlinking} onClick={handleUnlink} outline>
Un-link handle
</Button>
)}
</div>
<Button className="m-5" disabled={unlinking} onClick={handleUnlink} outline>
Un-link handle
</Button>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,21 @@ const Transaction: FC<TransactionProps> = ({ transaction }) => {
return (
<div className="flex items-center justify-between">
<Tooltip content={transaction.txHash} placement="top">
{transaction.type === OptmisticPostType.Collect ? (
<div className="text-sm">
{transaction.type} on {transaction.collectOn}
</div>
) : transaction.type === OptmisticPostType.Comment ? (
<div className="text-sm">
{transaction.type} on {transaction.commentOn}
</div>
) : transaction.type === OptmisticPostType.Mirror ? (
<div className="text-sm">
{transaction.type} on {transaction.repostOf}
</div>
) : transaction.type === OptmisticPostType.Post ||
transaction.type === OptmisticPostType.Quote ? (
{transaction.type === OptmisticPostType.Post ||
transaction.type === OptmisticPostType.Quote ||
transaction.type === OptmisticPostType.AssignUsername ||
transaction.type === OptmisticPostType.UnassignUsername ? (
<div className="text-sm">{transaction.type}</div>
) : transaction.type === OptmisticPostType.Follow ? (
) : transaction.type === OptmisticPostType.Follow ||
transaction.type === OptmisticPostType.Unfollow ||
transaction.type === OptmisticPostType.Block ||
transaction.type === OptmisticPostType.Unblock ||
transaction.type === OptmisticPostType.Mirror ||
transaction.type === OptmisticPostType.Comment ||
transaction.type === OptmisticPostType.Collect ? (
<div className="text-sm">
{transaction.type} on {transaction.followOn}
</div>
) : transaction.type === OptmisticPostType.Unfollow ? (
<div className="text-sm">
{transaction.type} on {transaction.unfollowOn}
</div>
) : null}
</Tooltip>
<div className="flex items-center space-x-2">
Expand Down

0 comments on commit ef88d93

Please sign in to comment.