From d7bc90ba0aa298c5606b1819ab99d2e2091e3bd1 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 27 Nov 2025 22:51:05 +0800 Subject: [PATCH] refactor: optimize SortByUnreadFeedList component and improve state handling - Wrapped SortByUnreadFeedList in memo for performance optimization. - Utilized useCallback for the sortedList computation to prevent unnecessary re-renders. - Enhanced the handling of onboarding and regular categories by ensuring a copy of ids is used. - Minor adjustments in SubscriptionList and hooks for consistency and clarity. Signed-off-by: Innei --- .../sort-by/SortByUnreadList.tsx | 45 ++++++++++--------- .../subscription-list/SubscriptionList.tsx | 1 + .../store/src/modules/unread/hooks.ts | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/subscription-column/sort-by/SortByUnreadList.tsx b/apps/desktop/layer/renderer/src/modules/subscription-column/sort-by/SortByUnreadList.tsx index 2cb8a998a..619b8d2e2 100644 --- a/apps/desktop/layer/renderer/src/modules/subscription-column/sort-by/SortByUnreadList.tsx +++ b/apps/desktop/layer/renderer/src/modules/subscription-column/sort-by/SortByUnreadList.tsx @@ -1,37 +1,42 @@ import { isOnboardingFeedUrl } from "@follow/store/constants/onboarding" import { useFeedStore } from "@follow/store/feed/store" import { useSortedCategoriesByUnread } from "@follow/store/unread/hooks" -import { Fragment } from "react" +import { Fragment, memo, useCallback } from "react" import { useFeedListSortSelector } from "../atom" import { FeedCategoryAutoHideUnread } from "../FeedCategory" import type { FeedListProps } from "./types" -export const SortByUnreadFeedList = ({ view, data, categoryOpenStateData }: FeedListProps) => { +export const SortByUnreadFeedList = memo(({ view, data, categoryOpenStateData }: FeedListProps) => { const isDesc = useFeedListSortSelector((s) => s.order === "desc") const sortedByUnread = useSortedCategoriesByUnread(data, isDesc) // Separate categories with onboarding feeds and regular categories - const sortedList = useFeedStore((state) => { - if (!sortedByUnread) return [] - const onboardingCategories: [string, string[]][] = [] - const regularCategories: [string, string[]][] = [] + const sortedList = useFeedStore( + useCallback( + (state) => { + if (!sortedByUnread) return [] + const onboardingCategories: [string, string[]][] = [] + const regularCategories: [string, string[]][] = [] - for (const [category, ids] of sortedByUnread) { - const hasOnboardingFeed = ids.some((id) => { - const feed = state.feeds[id] - return feed && isOnboardingFeedUrl(feed.url) - }) + for (const [category, ids] of sortedByUnread) { + const hasOnboardingFeed = ids.some((id) => { + const feed = state.feeds[id] + return feed && isOnboardingFeedUrl(feed.url) + }) - if (hasOnboardingFeed) { - onboardingCategories.push([category, ids]) - } else { - regularCategories.push([category, ids]) - } - } + if (hasOnboardingFeed) { + onboardingCategories.push([category, ids.concat()]) + } else { + regularCategories.push([category, ids.concat()]) + } + } - return [...onboardingCategories, ...regularCategories] - }) + return [...onboardingCategories, ...regularCategories] + }, + [sortedByUnread], + ), + ) return ( @@ -45,4 +50,4 @@ export const SortByUnreadFeedList = ({ view, data, categoryOpenStateData }: Feed ))} ) -} +}) diff --git a/apps/desktop/layer/renderer/src/modules/subscription-column/subscription-list/SubscriptionList.tsx b/apps/desktop/layer/renderer/src/modules/subscription-column/subscription-list/SubscriptionList.tsx index f11a4915e..1b9902a8a 100644 --- a/apps/desktop/layer/renderer/src/modules/subscription-column/subscription-list/SubscriptionList.tsx +++ b/apps/desktop/layer/renderer/src/modules/subscription-column/subscription-list/SubscriptionList.tsx @@ -49,6 +49,7 @@ import type { SubscriptionProps } from "./SubscriptionListGuard" const SubscriptionImpl = ({ ref, className, view, isSubscriptionLoading }: SubscriptionProps) => { const autoGroup = useGeneralSettingKey("autoGroup") const feedsData = useFeedsGroupedData(view, autoGroup) + const listSubIds = useSubscriptionListIds(view) const inboxSubIds = useInboxList( useCallback( diff --git a/packages/internal/store/src/modules/unread/hooks.ts b/packages/internal/store/src/modules/unread/hooks.ts index 66102e72b..e584f7dda 100644 --- a/packages/internal/store/src/modules/unread/hooks.ts +++ b/packages/internal/store/src/modules/unread/hooks.ts @@ -134,7 +134,7 @@ export const useSortedCategoriesByUnread = ( Object.keys(folderUnread) .sort((a, b) => folderUnread[b]! - folderUnread[a]!) .forEach((key) => { - sortedList.push([key, categories[key]!]) + sortedList.push([key, categories[key]!.concat()]) }) if (!isDesc) {