From f6caf90d8d2d2dff0db5fe9db697d81bbd5b3d00 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Sat, 2 Mar 2024 21:07:39 +0100 Subject: [PATCH] perf(inbox): memoize filters to avoid recalculation at every render --- src/views/inbox/inbox.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/views/inbox/inbox.tsx b/src/views/inbox/inbox.tsx index 1cd52825..aee796cb 100644 --- a/src/views/inbox/inbox.tsx +++ b/src/views/inbox/inbox.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useEffect, useMemo, useRef } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { StateSnapshot, Virtuoso, VirtuosoHandle } from 'react-virtuoso'; import { useAccount, useNotifications } from '@plebbit/plebbit-react-hooks'; @@ -50,19 +50,17 @@ const Inbox = () => { const isInInboxPostRepliesView = isInboxPostRepliesView(location.pathname); const isInInboxUnreadView = isInboxUnreadView(location.pathname); - const filterRepliesToUserReplies = useCallback(() => notifications?.filter((comment) => comment.parentCid !== comment.postCid) || [], [notifications]); - - const filterRepliesToUserPosts = useCallback(() => notifications?.filter((comment) => comment.parentCid === comment.postCid) || [], [notifications]); - - const filterUnreadNotifications = useCallback(() => notifications?.filter((comment) => !comment.markedAsRead) || [], [notifications]); + const filteredRepliesToUserReplies = useMemo(() => notifications?.filter((comment) => comment.parentCid !== comment.postCid) || [], [notifications]); + const filteredRepliesToUserPosts = useMemo(() => notifications?.filter((comment) => comment.parentCid === comment.postCid) || [], [notifications]); + const filteredUnreadNotifications = useMemo(() => notifications?.filter((comment) => !comment.markedAsRead) || [], [notifications]); let comments; if (isInInboxCommentRepliesView) { - comments = filterRepliesToUserReplies(); + comments = filteredRepliesToUserReplies; } else if (isInInboxPostRepliesView) { - comments = filterRepliesToUserPosts(); + comments = filteredRepliesToUserPosts; } else if (isInInboxUnreadView) { - comments = filterUnreadNotifications(); + comments = filteredUnreadNotifications; } else { comments = notifications; }