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 f0c4fd9b cb2442b3

verified
+45 -35
+3 -3
apps/web/src/components/Common/Layout.tsx
··· 2 2 import { BANNER_IDS } from "@hey/data/constants"; 3 3 import { useMeQuery } from "@hey/indexer"; 4 4 import { useIsClient } from "@uidotdev/usehooks"; 5 - import { memo, useEffect } from "react"; 5 + import { memo, useCallback, useEffect } from "react"; 6 6 import { Outlet, useLocation } from "react-router"; 7 7 import { Toaster, type ToasterProps } from "sonner"; 8 8 import FullPageLoader from "@/components/Shared/FullPageLoader"; ··· 34 34 window.scrollTo(0, 0); 35 35 }, [pathname]); 36 36 37 - const onError = () => { 37 + const onError = useCallback(() => { 38 38 resetPreferences(); 39 39 signOut(); 40 40 reloadAllTabs(); 41 - }; 41 + }, []); 42 42 43 43 const { loading } = useMeQuery({ 44 44 onCompleted: ({ me, proBanner }) => {
+3 -3
apps/web/src/components/Composer/NewPublication.tsx
··· 3 3 import type { PostFragment } from "@hey/indexer"; 4 4 import type { IGif } from "@hey/types/giphy"; 5 5 import type { NewAttachment } from "@hey/types/misc"; 6 - import { useEffect, useState } from "react"; 6 + import { useCallback, useEffect, useState } from "react"; 7 7 import { useHotkeys } from "react-hotkeys-hook"; 8 8 import { toast } from "sonner"; 9 9 import Attachment from "@/components/Composer/Actions/Attachment"; ··· 119 119 reset(); 120 120 }; 121 121 122 - const onError = (error?: any) => { 122 + const onError = useCallback((error?: unknown) => { 123 123 setIsSubmitting(false); 124 124 errorToast(error); 125 - }; 125 + }, []); 126 126 127 127 const { createPost } = useCreatePost({ 128 128 commentOn: post,
+9 -3
apps/web/src/components/Group/Settings/Monetize/SuperJoin.tsx
··· 11 11 useUpdateGroupRulesMutation 12 12 } from "@hey/indexer"; 13 13 import type { ApolloClientError } from "@hey/types/errors"; 14 - import { type RefObject, useEffect, useRef, useState } from "react"; 14 + import { 15 + type RefObject, 16 + useCallback, 17 + useEffect, 18 + useRef, 19 + useState 20 + } from "react"; 15 21 import BackButton from "@/components/Shared/BackButton"; 16 22 import { 17 23 Button, ··· 56 62 location.reload(); 57 63 }; 58 64 59 - const onError = (error: ApolloClientError) => { 65 + const onError = useCallback((error: ApolloClientError) => { 60 66 setIsSubmitting(false); 61 67 errorToast(error); 62 - }; 68 + }, []); 63 69 64 70 const [updateGroupRules] = useUpdateGroupRulesMutation({ 65 71 onCompleted: async ({ updateGroupRules }) => {
+3 -3
apps/web/src/components/Group/Settings/Personalize/Form.tsx
··· 3 3 import { type GroupFragment, useSetGroupMetadataMutation } from "@hey/indexer"; 4 4 import type { ApolloClientError } from "@hey/types/errors"; 5 5 import { group as groupMetadata } from "@lens-protocol/metadata"; 6 - import { useState } from "react"; 6 + import { useCallback, useState } from "react"; 7 7 import { toast } from "sonner"; 8 8 import { z } from "zod"; 9 9 import AvatarUpload from "@/components/Shared/AvatarUpload"; ··· 55 55 toast.success("Group updated"); 56 56 }; 57 57 58 - const onError = (error: ApolloClientError) => { 58 + const onError = useCallback((error: ApolloClientError) => { 59 59 setIsSubmitting(false); 60 60 errorToast(error); 61 - }; 61 + }, []); 62 62 63 63 const [setGroupMetadata] = useSetGroupMetadataMutation({ 64 64 onCompleted: async ({ setGroupMetadata }) => {
+3 -3
apps/web/src/components/Group/Settings/Rules/ApprovalRule.tsx
··· 5 5 useUpdateGroupRulesMutation 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 ToggleWithHelper from "@/components/Shared/ToggleWithHelper"; 11 11 import errorToast from "@/helpers/errorToast"; ··· 32 32 toast.success("Approval rule updated"); 33 33 }; 34 34 35 - const onError = (error: ApolloClientError) => { 35 + const onError = useCallback((error: ApolloClientError) => { 36 36 setIsSubmitting(false); 37 37 errorToast(error); 38 - }; 38 + }, []); 39 39 40 40 const [updateGroupRules] = useUpdateGroupRulesMutation({ 41 41 onCompleted: async ({ updateGroupRules }) => {
+3 -3
apps/web/src/components/Groups/Sidebar/Create/CreateGroupModal.tsx
··· 2 2 import { useCreateGroupMutation } from "@hey/indexer"; 3 3 import type { ApolloClientError } from "@hey/types/errors"; 4 4 import { group } from "@lens-protocol/metadata"; 5 - import { useState } from "react"; 5 + import { useCallback, useState } from "react"; 6 6 import { z } from "zod"; 7 7 import AvatarUpload from "@/components/Shared/AvatarUpload"; 8 8 import { ··· 45 45 setScreen("minting"); 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 [createGroup] = useCreateGroupMutation({ 54 54 onCompleted: async ({ createGroup }) => {
+3 -2
apps/web/src/components/Post/Actions/Like.tsx
··· 11 11 import type { ApolloClientError } from "@hey/types/errors"; 12 12 import { useCounter, useToggle } from "@uidotdev/usehooks"; 13 13 import { AnimateNumber } from "motion-plus-react"; 14 + import { useCallback } from "react"; 14 15 import { toast } from "sonner"; 15 16 import { Tooltip } from "@/components/Shared/UI"; 16 17 import cn from "@/helpers/cn"; ··· 51 52 }); 52 53 }; 53 54 54 - const onError = (error: ApolloClientError) => { 55 + const onError = useCallback((error: ApolloClientError) => { 55 56 errorToast(error); 56 - }; 57 + }, []); 57 58 58 59 const [addReaction] = useAddReactionMutation({ 59 60 onError: (error) => {
+3 -2
apps/web/src/components/Post/Actions/Menu/Bookmark.tsx
··· 8 8 useUndoBookmarkPostMutation 9 9 } from "@hey/indexer"; 10 10 import type { ApolloClientError } from "@hey/types/errors"; 11 + import { useCallback } from "react"; 11 12 import { useLocation } from "react-router"; 12 13 import { toast } from "sonner"; 13 14 import cn from "@/helpers/cn"; ··· 53 54 } 54 55 }; 55 56 56 - const onError = (error: ApolloClientError) => { 57 + const onError = useCallback((error: ApolloClientError) => { 57 58 errorToast(error); 58 - }; 59 + }, []); 59 60 60 61 const [bookmarkPost] = useBookmarkPostMutation({ 61 62 onCompleted: () => toast.success("Post bookmarked!"),
+3 -2
apps/web/src/components/Post/Actions/Menu/HideComment.tsx
··· 7 7 useUnhideReplyMutation 8 8 } from "@hey/indexer"; 9 9 import type { ApolloClientError } from "@hey/types/errors"; 10 + import { useCallback } from "react"; 10 11 import { toast } from "sonner"; 11 12 import { useHiddenCommentFeedStore } from "@/components/Post"; 12 13 import cn from "@/helpers/cn"; ··· 26 27 cache.evict({ id: cache.identify(post) }); 27 28 }; 28 29 29 - const onError = (error: ApolloClientError) => { 30 + const onError = useCallback((error: ApolloClientError) => { 30 31 errorToast(error); 31 - }; 32 + }, []); 32 33 33 34 const [hideComment] = useHideReplyMutation({ 34 35 onCompleted: () => toast.success("Comment hidden"),
+3 -2
apps/web/src/components/Post/Actions/Menu/NotInterested.tsx
··· 8 8 useUndoPostNotInterestedMutation 9 9 } from "@hey/indexer"; 10 10 import type { ApolloClientError } from "@hey/types/errors"; 11 + import { useCallback } from "react"; 11 12 import { toast } from "sonner"; 12 13 import cn from "@/helpers/cn"; 13 14 import errorToast from "@/helpers/errorToast"; ··· 38 39 }); 39 40 }; 40 41 41 - const onError = (error: ApolloClientError) => { 42 + const onError = useCallback((error: ApolloClientError) => { 42 43 errorToast(error); 43 - }; 44 + }, []); 44 45 45 46 const [addPostNotInterested] = useAddPostNotInterestedMutation({ 46 47 onCompleted: () => toast.success("Marked as not Interested"),
+3 -3
apps/web/src/components/Post/Actions/Share/Repost.tsx
··· 5 5 import { type PostFragment, useRepostMutation } from "@hey/indexer"; 6 6 import type { ApolloClientError } from "@hey/types/errors"; 7 7 import { useCounter } from "@uidotdev/usehooks"; 8 - import type { Dispatch, SetStateAction } from "react"; 8 + import { type Dispatch, type SetStateAction, useCallback } from "react"; 9 9 import { toast } from "sonner"; 10 10 import cn from "@/helpers/cn"; 11 11 import errorToast from "@/helpers/errorToast"; ··· 60 60 toast.success("Post has been reposted!"); 61 61 }; 62 62 63 - const onError = (error: ApolloClientError) => { 63 + const onError = useCallback((error: ApolloClientError) => { 64 64 setIsSubmitting(false); 65 65 errorToast(error); 66 - }; 66 + }, []); 67 67 68 68 const [repost] = useRepostMutation({ 69 69 onCompleted: async ({ repost }) => {
+3 -3
apps/web/src/components/Post/Actions/Share/UndoRepost.tsx
··· 4 4 import { ERRORS } from "@hey/data/errors"; 5 5 import { isRepost } from "@hey/helpers/postHelpers"; 6 6 import { type AnyPostFragment, useDeletePostMutation } from "@hey/indexer"; 7 - import type { Dispatch, SetStateAction } from "react"; 7 + import { type Dispatch, type SetStateAction, useCallback } from "react"; 8 8 import { toast } from "sonner"; 9 9 import cn from "@/helpers/cn"; 10 10 import errorToast from "@/helpers/errorToast"; ··· 46 46 toast.success("Undone repost"); 47 47 }; 48 48 49 - const onError = (error?: any) => { 49 + const onError = useCallback((error?: unknown) => { 50 50 setIsSubmitting(false); 51 51 errorToast(error); 52 - }; 52 + }, []); 53 53 54 54 const [undoRepost] = useDeletePostMutation({ 55 55 onCompleted: async ({ deletePost }) => {
+3 -3
apps/web/src/components/Shared/Account/SwitchAccounts.tsx
··· 5 5 useAccountsAvailableQuery, 6 6 useSwitchAccountMutation 7 7 } from "@hey/indexer"; 8 - import { useState } from "react"; 8 + import { useCallback, useState } from "react"; 9 9 import { useAccount } from "wagmi"; 10 10 import Loader from "@/components/Shared/Loader"; 11 11 import { ErrorMessage, Spinner, WarningMessage } from "@/components/Shared/UI"; ··· 24 24 ); 25 25 const { address } = useAccount(); 26 26 27 - const onError = (error?: any) => { 27 + const onError = useCallback((error?: unknown) => { 28 28 setIsSubmitting(false); 29 29 setLoggingInAccountId(null); 30 30 errorToast(error); 31 - }; 31 + }, []); 32 32 33 33 const { data, error, loading } = useAccountsAvailableQuery({ 34 34 skip: !address,