Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿

refactor: replace error handling functions with useCallback for performance optimization

yoginth.com a43b8b8b f0c4fd9b

verified
+58 -55
+3 -3
apps/web/src/components/Post/OpenAction/CollectAction/CollectActionButton.tsx
··· 7 7 useExecutePostActionMutation 8 8 } from "@hey/indexer"; 9 9 import type { ApolloClientError } from "@hey/types/errors"; 10 - import { useState } from "react"; 10 + import { useCallback, useState } from "react"; 11 11 import { toast } from "sonner"; 12 12 import type { Address } from "viem"; 13 13 import TopUpButton from "@/components/Shared/Account/TopUp/Button"; ··· 80 80 toast.success("Collected successfully"); 81 81 }; 82 82 83 - const onError = (error: ApolloClientError) => { 83 + const onError = useCallback((error: ApolloClientError) => { 84 84 setIsSubmitting(false); 85 85 errorToast(error); 86 - }; 86 + }, []); 87 87 88 88 const { data: balance, loading: balanceLoading } = useBalancesBulkQuery({ 89 89 fetchPolicy: "no-cache",
+2 -2
apps/web/src/components/Settings/Manager/AccountManager/Managers/List.tsx
··· 42 42 toast.success("Manager removed successfully"); 43 43 }; 44 44 45 - const onError = (error: ApolloClientError) => { 45 + const onError = useCallback((error: ApolloClientError) => { 46 46 errorToast(error); 47 47 setRemovingManager(null); 48 - }; 48 + }, []); 49 49 50 50 const request: AccountManagersRequest = { pageSize: PageSize.Fifty }; 51 51 const { data, error, fetchMore, loading } = useAccountManagersQuery({
+3 -3
apps/web/src/components/Settings/Personalize/Form.tsx
··· 11 11 account as accountMetadata, 12 12 MetadataAttributeType 13 13 } from "@lens-protocol/metadata"; 14 - import { useState } from "react"; 14 + import { useCallback, useState } from "react"; 15 15 import { toast } from "sonner"; 16 16 import { z } from "zod"; 17 17 import AvatarUpload from "@/components/Shared/AvatarUpload"; ··· 76 76 toast.success("Account updated"); 77 77 }; 78 78 79 - const onError = (error: ApolloClientError) => { 79 + const onError = useCallback((error: ApolloClientError) => { 80 80 setIsSubmitting(false); 81 81 errorToast(error); 82 - }; 82 + }, []); 83 83 84 84 const [setAccountMetadata] = useSetAccountMetadataMutation({ 85 85 onCompleted: async ({ setAccountMetadata }) => {
+2 -2
apps/web/src/components/Settings/Sessions/List.tsx
··· 23 23 null 24 24 ); 25 25 26 - const onError = (error: ApolloClientError) => { 26 + const onError = useCallback((error: ApolloClientError) => { 27 27 setRevoking(false); 28 28 setRevokeingSessionId(null); 29 29 errorToast(error); 30 - }; 30 + }, []); 31 31 32 32 const onCompleted = () => { 33 33 setRevoking(false);
+3 -3
apps/web/src/components/Settings/Username/UnlinkUsername.tsx
··· 1 1 import getAccount from "@hey/helpers/getAccount"; 2 2 import { useUnassignUsernameFromAccountMutation } from "@hey/indexer"; 3 3 import type { ApolloClientError } from "@hey/types/errors"; 4 - import { useState } from "react"; 4 + import { useCallback, useState } from "react"; 5 5 import { toast } from "sonner"; 6 6 import { Button, H6 } from "@/components/Shared/UI"; 7 7 import errorToast from "@/helpers/errorToast"; ··· 18 18 toast.success("Unlinked"); 19 19 }; 20 20 21 - const onError = (error: ApolloClientError) => { 21 + const onError = useCallback((error: ApolloClientError) => { 22 22 setUnlinking(false); 23 23 errorToast(error); 24 - }; 24 + }, []); 25 25 26 26 const [unassignUsernameFromAccount] = useUnassignUsernameFromAccountMutation({ 27 27 onCompleted: async ({ unassignUsernameFromAccount }) => {
+3 -2
apps/web/src/components/Shared/Account/TopUp/Transfer.tsx
··· 5 5 import { 6 6 type ChangeEvent, 7 7 type RefObject, 8 + useCallback, 8 9 useEffect, 9 10 useRef, 10 11 useState ··· 61 62 toast.success("Transferred successfully"); 62 63 }; 63 64 64 - const onError = (error: ApolloClientError) => { 65 + const onError = useCallback((error: ApolloClientError) => { 65 66 setIsSubmitting(false); 66 67 errorToast(error); 67 - }; 68 + }, []); 68 69 69 70 const { data: transactionReceipt } = useWaitForTransactionReceipt({ 70 71 hash: txHash as Hex,
+3 -3
apps/web/src/components/Shared/Account/Unfollow.tsx
··· 1 1 import { useApolloClient } from "@apollo/client"; 2 2 import { type AccountFragment, useUnfollowMutation } from "@hey/indexer"; 3 3 import type { ApolloClientError } from "@hey/types/errors"; 4 - import { useState } from "react"; 4 + import { useCallback, useState } from "react"; 5 5 import { Button } from "@/components/Shared/UI"; 6 6 import errorToast from "@/helpers/errorToast"; 7 7 import useTransactionLifecycle from "@/hooks/useTransactionLifecycle"; ··· 43 43 setIsSubmitting(false); 44 44 }; 45 45 46 - const onError = (error: ApolloClientError) => { 46 + const onError = useCallback((error: ApolloClientError) => { 47 47 setIsSubmitting(false); 48 48 errorToast(error); 49 - }; 49 + }, []); 50 50 51 51 const [unfollow] = useUnfollowMutation({ 52 52 onCompleted: async ({ unfollow }) => {
+3 -2
apps/web/src/components/Shared/Alert/DeletePost.tsx
··· 1 1 import { useApolloClient } from "@apollo/client"; 2 2 import { useDeletePostMutation } from "@hey/indexer"; 3 3 import type { ApolloClientError } from "@hey/types/errors"; 4 + import { useCallback } from "react"; 4 5 import { toast } from "sonner"; 5 6 import { Alert } from "@/components/Shared/UI"; 6 7 import errorToast from "@/helpers/errorToast"; ··· 25 26 toast.success("Post deleted"); 26 27 }; 27 28 28 - const onError = (error: ApolloClientError) => { 29 + const onError = useCallback((error: ApolloClientError) => { 29 30 errorToast(error); 30 - }; 31 + }, []); 31 32 32 33 const [deletePost, { loading }] = useDeletePostMutation({ 33 34 onCompleted: async ({ deletePost }) => {
+3 -3
apps/web/src/components/Shared/Alert/MuteOrUnmuteAccount.tsx
··· 3 3 import getAccount from "@hey/helpers/getAccount"; 4 4 import { useMuteMutation, useUnmuteMutation } from "@hey/indexer"; 5 5 import type { ApolloClientError } from "@hey/types/errors"; 6 - import { useState } from "react"; 6 + import { useCallback, useState } from "react"; 7 7 import { toast } from "sonner"; 8 8 import { Alert } from "@/components/Shared/UI"; 9 9 import errorToast from "@/helpers/errorToast"; ··· 43 43 toast.success(hasMuted ? "Unmuted successfully" : "Muted successfully"); 44 44 }; 45 45 46 - const onError = (error: ApolloClientError) => { 46 + const onError = useCallback((error: ApolloClientError) => { 47 47 setIsSubmitting(false); 48 48 errorToast(error); 49 - }; 49 + }, []); 50 50 51 51 const [mute] = useMuteMutation({ 52 52 onCompleted,
+3 -3
apps/web/src/components/Shared/Audio/CoverImage.tsx
··· 4 4 import sanitizeDStorageUrl from "@hey/helpers/sanitizeDStorageUrl"; 5 5 import type { ApolloClientError } from "@hey/types/errors"; 6 6 import type { ChangeEvent, Ref } from "react"; 7 - import { useState } from "react"; 7 + import { useCallback, useState } from "react"; 8 8 import { Image, Spinner } from "@/components/Shared/UI"; 9 9 import cn from "@/helpers/cn"; 10 10 import errorToast from "@/helpers/errorToast"; ··· 25 25 }: CoverImageProps) => { 26 26 const [isSubmitting, setIsSubmitting] = useState(false); 27 27 28 - const onError = (error: ApolloClientError) => { 28 + const onError = useCallback((error: ApolloClientError) => { 29 29 setIsSubmitting(false); 30 30 errorToast(error); 31 - }; 31 + }, []); 32 32 33 33 const onChange = async (event: ChangeEvent<HTMLInputElement>) => { 34 34 if (event.target.files?.length) {
+3 -3
apps/web/src/components/Shared/Auth/Login.tsx
··· 10 10 } from "@hey/indexer"; 11 11 import { AnimatePresence, motion } from "motion/react"; 12 12 import type { Dispatch, SetStateAction } from "react"; 13 - import { useState } from "react"; 13 + import { useCallback, useState } from "react"; 14 14 import { toast } from "sonner"; 15 15 import { useAccount, useDisconnect, useSignMessage } from "wagmi"; 16 16 import SingleAccount from "@/components/Shared/Account/SingleAccount"; ··· 34 34 ); 35 35 const [isExpanded, setIsExpanded] = useState(true); 36 36 37 - const onError = (error?: any) => { 37 + const onError = useCallback((error?: any) => { 38 38 setIsSubmitting(false); 39 39 setLoggingInAccountId(null); 40 40 errorToast(error); 41 - }; 41 + }, []); 42 42 43 43 const { disconnect } = useDisconnect(); 44 44 const { address, connector: activeConnector } = useAccount();
+3 -3
apps/web/src/components/Shared/Auth/Signup/ChooseUsername.tsx
··· 14 14 useCreateAccountWithUsernameMutation 15 15 } from "@hey/indexer"; 16 16 import { account as accountMetadata } from "@lens-protocol/metadata"; 17 - import { useState } from "react"; 17 + import { useCallback, useState } from "react"; 18 18 import { toast } from "sonner"; 19 19 import { useAccount, useSignMessage } from "wagmi"; 20 20 import { z } from "zod"; ··· 65 65 setScreen("minting"); 66 66 }; 67 67 68 - const onError = (error?: any) => { 68 + const onError = useCallback((error?: any) => { 69 69 setIsSubmitting(false); 70 70 errorToast(error); 71 - }; 71 + }, []); 72 72 73 73 const { signMessageAsync } = useSignMessage({ mutation: { onError } }); 74 74 const [loadChallenge] = useChallengeMutation({ onError });
+3 -3
apps/web/src/components/Shared/Auth/Signup/Success.tsx
··· 2 2 import { ERRORS } from "@hey/data/errors"; 3 3 import { useSwitchAccountMutation } from "@hey/indexer"; 4 4 import type { ApolloClientError } from "@hey/types/errors"; 5 - import { useEffect } from "react"; 5 + import { useCallback, useEffect } from "react"; 6 6 import { H4, Image } from "@/components/Shared/UI"; 7 7 import errorToast from "@/helpers/errorToast"; 8 8 import reloadAllTabs from "@/helpers/reloadAllTabs"; ··· 12 12 const Success = () => { 13 13 const { accountAddress, onboardingToken } = useSignupStore(); 14 14 15 - const onError = (error: ApolloClientError) => { 15 + const onError = useCallback((error: ApolloClientError) => { 16 16 errorToast(error); 17 - }; 17 + }, []); 18 18 19 19 const [switchAccount] = useSwitchAccountMutation({ onError }); 20 20
+3 -3
apps/web/src/components/Shared/Group/CancelGroupMembershipRequest.tsx
··· 4 4 useCancelGroupMembershipRequestMutation 5 5 } from "@hey/indexer"; 6 6 import type { ApolloClientError } from "@hey/types/errors"; 7 - import { useState } from "react"; 7 + import { useCallback, useState } from "react"; 8 8 import { toast } from "sonner"; 9 9 import { Button } from "@/components/Shared/UI"; 10 10 import errorToast from "@/helpers/errorToast"; ··· 40 40 toast.success("Request cancelled"); 41 41 }; 42 42 43 - const onError = (error: ApolloClientError) => { 43 + const onError = useCallback((error: ApolloClientError) => { 44 44 setIsSubmitting(false); 45 45 errorToast(error); 46 - }; 46 + }, []); 47 47 48 48 const [cancelGroupMembershipRequest] = 49 49 useCancelGroupMembershipRequestMutation({
+3 -3
apps/web/src/components/Shared/Group/Join.tsx
··· 5 5 useRequestGroupMembershipMutation 6 6 } from "@hey/indexer"; 7 7 import type { ApolloClientError } from "@hey/types/errors"; 8 - import { useState } from "react"; 8 + import { useCallback, useState } from "react"; 9 9 import { toast } from "sonner"; 10 10 import { Button } from "@/components/Shared/UI"; 11 11 import errorToast from "@/helpers/errorToast"; ··· 52 52 ); 53 53 }; 54 54 55 - const onError = (error: ApolloClientError) => { 55 + const onError = useCallback((error: ApolloClientError) => { 56 56 setIsSubmitting(false); 57 57 errorToast(error); 58 - }; 58 + }, []); 59 59 60 60 const [joinGroup] = useJoinGroupMutation({ 61 61 onCompleted: async ({ joinGroup }) => {
+3 -3
apps/web/src/components/Shared/Group/Leave.tsx
··· 1 1 import { useApolloClient } from "@apollo/client"; 2 2 import { type GroupFragment, useLeaveGroupMutation } from "@hey/indexer"; 3 3 import type { ApolloClientError } from "@hey/types/errors"; 4 - import { useState } from "react"; 4 + import { useCallback, useState } from "react"; 5 5 import { toast } from "sonner"; 6 6 import { Button } from "@/components/Shared/UI"; 7 7 import errorToast from "@/helpers/errorToast"; ··· 34 34 toast.success("Left group"); 35 35 }; 36 36 37 - const onError = (error: ApolloClientError) => { 37 + const onError = useCallback((error: ApolloClientError) => { 38 38 setIsSubmitting(false); 39 39 errorToast(error); 40 - }; 40 + }, []); 41 41 42 42 const [leaveGroup] = useLeaveGroupMutation({ 43 43 onCompleted: async ({ leaveGroup }) => {
+3 -3
apps/web/src/components/Shared/Modal/Subscribe.tsx
··· 12 12 useJoinGroupMutation 13 13 } from "@hey/indexer"; 14 14 import type { ApolloClientError } from "@hey/types/errors"; 15 - import { useState } from "react"; 15 + import { useCallback, useState } from "react"; 16 16 import SingleAccount from "@/components/Shared/Account/SingleAccount"; 17 17 import TopUpButton from "@/components/Shared/Account/TopUp/Button"; 18 18 import { Button, Image, Spinner, Tooltip } from "@/components/Shared/UI"; ··· 45 45 location.reload(); 46 46 }; 47 47 48 - const onError = (error: ApolloClientError) => { 48 + const onError = useCallback((error: ApolloClientError) => { 49 49 setIsSubmitting(false); 50 50 errorToast(error); 51 - }; 51 + }, []); 52 52 53 53 const tokenBalance = 54 54 balance?.balancesBulk[0].__typename === "Erc20Amount"
+3 -2
apps/web/src/components/Shared/Sidebar/ProBanner.tsx
··· 2 2 import { BANNER_IDS } from "@hey/data/constants"; 3 3 import { useAddPostNotInterestedMutation } from "@hey/indexer"; 4 4 import type { ApolloClientError } from "@hey/types/errors"; 5 + import { useCallback } from "react"; 5 6 import { toast } from "sonner"; 6 7 import { Button, Card, H5 } from "@/components/Shared/UI"; 7 8 import errorToast from "@/helpers/errorToast"; ··· 14 15 const { proBannerDismissed, setProBannerDismissed } = useProStore(); 15 16 const { setShow: setShowProModal } = useProModalStore(); 16 17 17 - const onError = (error: ApolloClientError) => { 18 + const onError = useCallback((error: ApolloClientError) => { 18 19 errorToast(error); 19 - }; 20 + }, []); 20 21 21 22 const [dismissProBanner, { loading }] = useAddPostNotInterestedMutation({ 22 23 onCompleted: () => {
+3 -3
apps/web/src/components/Shared/TipMenu.tsx
··· 10 10 } from "@hey/indexer"; 11 11 import type { ApolloClientError } from "@hey/types/errors"; 12 12 import type { ChangeEvent, RefObject } from "react"; 13 - import { memo, useRef, useState } from "react"; 13 + import { memo, useCallback, useRef, useState } from "react"; 14 14 import { toast } from "sonner"; 15 15 import TopUpButton from "@/components/Shared/Account/TopUp/Button"; 16 16 import LoginButton from "@/components/Shared/LoginButton"; ··· 78 78 toast.success(`Tipped ${amount} ${NATIVE_TOKEN_SYMBOL}`); 79 79 }; 80 80 81 - const onError = (error: ApolloClientError) => { 81 + const onError = useCallback((error: ApolloClientError) => { 82 82 setIsSubmitting(false); 83 83 errorToast(error); 84 - }; 84 + }, []); 85 85 86 86 const cryptoRate = Number(amount); 87 87 const nativeBalance =
+3 -3
apps/web/src/hooks/useImageCropUpload.tsx
··· 8 8 import sanitizeDStorageUrl from "@hey/helpers/sanitizeDStorageUrl"; 9 9 import type { ApolloClientError } from "@hey/types/errors"; 10 10 import type { ChangeEvent } from "react"; 11 - import { useState } from "react"; 11 + import { useCallback, useState } from "react"; 12 12 import type { Area } from "react-easy-crop"; 13 13 import { toast } from "sonner"; 14 14 import uploadCroppedImage, { readFile } from "@/helpers/accountPictureUtils"; ··· 38 38 const [crop, setCrop] = useState({ x: 0, y: 0 }); 39 39 const [zoom, setZoom] = useState(1); 40 40 41 - const onError = (error: ApolloClientError) => { 41 + const onError = useCallback((error: ApolloClientError) => { 42 42 errorToast(error); 43 - }; 43 + }, []); 44 44 45 45 const handleUploadAndSave = async () => { 46 46 try {