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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-11-27 22:51:05 +08:00
parent 1aa8b63305
commit d7bc90ba0a
3 changed files with 27 additions and 21 deletions
@@ -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 (
<Fragment>
@@ -45,4 +50,4 @@ export const SortByUnreadFeedList = ({ view, data, categoryOpenStateData }: Feed
))}
</Fragment>
)
}
})
@@ -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(
@@ -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) {