Skip to content

Commit

Permalink
🌺 Lens and Hey v3: v36 (#lens-v3)
Browse files Browse the repository at this point in the history
Summary: Migrated to Lens v3 with enhanced error handling and transaction management.

Highlights:

• Introduced `onError` function for consistent error handling across components.
• Wrapped transaction requests in `try-catch` blocks to manage errors effectively.
• Added `sendEip712Transaction` and `sendTransaction` for handling different transaction types.

Read more: https://pierre.co/hey/hey/lens-v3
  • Loading branch information
Yoginth authored and Pierre committed Dec 6, 2024
1 parent 3d88862 commit 7af768e
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 180 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/components/Account/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const Details: FC<DetailsProps> = ({ isSuspended = false, account }) => {
className="text-sm sm:text-base"
slug={getAccount(account).slugWithPrefix}
/>
{account.operations?.isFollowedByMe ? (
{account.operations?.isFollowingMe ? (
<div className="rounded-full bg-gray-200 px-2 py-0.5 text-xs dark:bg-gray-700">
Follows you
</div>
Expand Down
46 changes: 26 additions & 20 deletions apps/web/src/components/Post/Actions/Share/Repost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,46 @@ const Repost: FC<RepostProps> = ({ isLoading, post, setIsLoading }) => {
toast.success("Post has been mirrored!");
};

const onError = (error: any) => {
setIsLoading(false);
errorToast(error);
};

const [repost] = useRepostMutation({
onCompleted: async ({ repost }) => {
if (repost.__typename === "PostResponse") {
return onCompleted(repost.hash);
}

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

return onCompleted(hash);
}

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

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

return onCompleted(hash);
}

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

return onCompleted(hash);
}
} catch (error) {
return onError(error);
}
}

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

if (post.operations?.canRepost === TriStateValue.No) {
Expand Down
55 changes: 31 additions & 24 deletions apps/web/src/components/Settings/Handles/LinkHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const LinkHandle: FC = () => {
toast.success("Linked");
};

const onError = (error: any) => {
setLinkingUsername(null);
errorToast(error);
};

const { data, loading } = useUsernamesQuery({
variables: { request: { owner: currentAccount?.owner } }
});
Expand All @@ -58,37 +63,39 @@ const LinkHandle: FC = () => {
}

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

return onCompleted(hash);
}

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

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

return onCompleted(hash);
}

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

return onCompleted(hash);
}
} catch (error) {
return onError(error);
}
}

if (assignUsernameToAccount.__typename === "TransactionWillFail") {
return toast.error(assignUsernameToAccount.reason);
}
},
onError: (error) => {
setLinkingUsername(null);
errorToast(error);
}
onError
});

const handleLink = async (username: string) => {
Expand Down
56 changes: 31 additions & 25 deletions apps/web/src/components/Settings/Handles/UnlinkHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const UnlinkHandle: FC = () => {
toast.success("Unlinked");
};

const onError = (error: any) => {
setUnlinking(false);
errorToast(error);
};

const [unassignUsernameFromAccount] = useUnassignUsernameFromAccountMutation({
onCompleted: async ({ unassignUsernameFromAccount }) => {
if (
Expand All @@ -48,39 +53,40 @@ const UnlinkHandle: FC = () => {
}

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

return onCompleted(hash);
}
try {
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)
});
if (
unassignUsernameFromAccount.__typename ===
"SelfFundedTransactionRequest"
) {
const hash = await sendTransaction(walletClient, {
account: walletClient.account,
...selfFundedTransactionData(unassignUsernameFromAccount.raw)
});

return onCompleted(hash);
return onCompleted(hash);
}
} catch (error) {
return onError(error);
}
}

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

const handleUnlink = async () => {
Expand All @@ -95,7 +101,7 @@ const UnlinkHandle: FC = () => {
setUnlinking(true);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,42 @@ const ToggleLensManager: FC<ToggleLensManagerProps> = ({
setIsLoading(false);
};

const onError = (error: any) => {
setIsLoading(false);
errorToast(error);
};

const [enableSignless] = useEnableSignlessMutation({
onCompleted: async ({ enableSignless }) => {
if (walletClient) {
if (enableSignless.__typename === "SponsoredTransactionRequest") {
const hash = await sendEip712Transaction(walletClient, {
account: walletClient.account,
...sponsoredTransactionData(enableSignless.raw)
});
try {
if (enableSignless.__typename === "SponsoredTransactionRequest") {
const hash = await sendEip712Transaction(walletClient, {
account: walletClient.account,
...sponsoredTransactionData(enableSignless.raw)
});

return onCompleted(hash);
}
return onCompleted(hash);
}

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

return onCompleted(hash);
return onCompleted(hash);
}
} catch (error) {
return onError(error);
}
}

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

const handleToggleDispatcher = async () => {
Expand Down
40 changes: 38 additions & 2 deletions apps/web/src/components/Settings/Profile/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import getAccountAttribute from "@hey/helpers/getAccountAttribute";
import getAvatar from "@hey/helpers/getAvatar";
import imageKit from "@hey/helpers/imageKit";
import sanitizeDStorageUrl from "@hey/helpers/sanitizeDStorageUrl";
import selfFundedTransactionData from "@hey/helpers/selfFundedTransactionData";
import sponsoredTransactionData from "@hey/helpers/sponsoredTransactionData";
import trimify from "@hey/helpers/trimify";
import { getCroppedImg } from "@hey/image-cropper/cropUtils";
import type { Area } from "@hey/image-cropper/types";
Expand Down Expand Up @@ -43,6 +45,8 @@ 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 { sendEip712Transaction, sendTransaction } from "viem/zksync";
import { useWalletClient } from "wagmi";
import type { z } from "zod";
import { object, string, union } from "zod";

Expand Down Expand Up @@ -97,6 +101,8 @@ const AccountSettingsForm: FC = () => {
useState("");
const [uploadingProfilePicture, setUploadingProfilePicture] = useState(false);

const { data: walletClient } = useWalletClient();

const onCompleted = (hash: string) => {
setIsLoading(false);
toast.success(hash);
Expand All @@ -109,9 +115,39 @@ const AccountSettingsForm: FC = () => {
};

const [setAccountMetadata] = useSetAccountMetadataMutation({
onCompleted: ({ setAccountMetadata }) => {
onCompleted: async ({ setAccountMetadata }) => {
if (setAccountMetadata.__typename === "SetAccountMetadataResponse") {
onCompleted(setAccountMetadata.hash);
return onCompleted(setAccountMetadata.hash);
}

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

return onCompleted(hash);
}

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

return onCompleted(hash);
}
} catch (error) {
return onError(error);
}
}

if (setAccountMetadata.__typename === "TransactionWillFail") {
return toast.error(setAccountMetadata.reason);
}
},
onError
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/components/Settings/Sessions/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ const List: FC = () => {
}

if (error) {
return <ErrorMessage error={error} title="Failed to load sessions" />;
return (
<ErrorMessage
className="m-5"
error={error}
title="Failed to load sessions"
/>
);
}

if (authenticatedSessions?.length === 0) {
Expand Down
Loading

0 comments on commit 7af768e

Please sign in to comment.