fix: list unread count

This commit is contained in:
DIYgod
2025-02-24 17:57:40 +08:00
parent 2f5d6c2168
commit 7c090e57b8
8 changed files with 152 additions and 146 deletions
@@ -8,13 +8,11 @@ import { m, useAnimationControls } from "framer-motion"
import { useCallback, useEffect, useRef, useState } from "react"
import { useAudioPlayerAtomSelector } from "~/atoms/player"
import { useUISettingKey } from "~/atoms/settings/ui"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
import { useEntry } from "~/store/entry"
import { useFeedById } from "~/store/feed"
import { feedIconSelector } from "~/store/feed/selector"
import { useUnreadByView } from "~/store/unread/hooks"
import { ProfileButton } from "../../user/ProfileButton"
import { PodcastButton } from "./components/PodcastButton"
@@ -102,8 +100,6 @@ const ViewTabs = ({ onViewChange }: { onViewChange?: (view: number) => void }) =
const view = useRouteParamsSelector((s) => s.view)
const navigate = useNavigateEntry()
const unreadByView = useUnreadByView()
const showCount = useUISettingKey("sidebarShowUnreadCount")
return (
<div
className="flex w-full shrink items-center justify-between gap-4 overflow-x-auto overflow-y-hidden text-xl text-theme-vibrancyFg"
@@ -121,15 +117,7 @@ const ViewTabs = ({ onViewChange }: { onViewChange?: (view: number) => void }) =
onViewChange?.(item.view)
}}
>
<div className="relative flex flex-col items-center">
{item.icon}
{showCount && (
<span className="mt-px text-[8px] leading-none">
{unreadByView[item.view]! > 99 ? "99+" : unreadByView[item.view] || "0"}
</span>
)}
</div>
<div className="relative flex flex-col items-center">{item.icon}</div>
</MotionButtonBase>
))}
</div>
@@ -29,6 +29,7 @@ import { useInboxById } from "~/store/inbox"
import { useListById } from "~/store/list"
import { useSubscriptionByFeedId } from "~/store/subscription"
import { useFeedUnreadStore } from "~/store/unread"
import { useUnreadByListId } from "~/store/unread/hooks"
import { useSelectedFeedIdsState } from "./atom"
import { DraggableContext } from "./context"
@@ -241,7 +242,7 @@ const ListItemImpl: Component<{
const isActive = useRouteParamsSelector((routerParams) => routerParams.listId === listId)
const items = useListActions({ listId, view })
const listUnread = useFeedUnreadStore((state) => state.data[listId] || 0)
const listUnread = useUnreadByListId(listId)
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false)
const subscription = useSubscriptionByFeedId(listId)!
@@ -8,7 +8,12 @@ import { useEventListener } from "usehooks-ts"
import { useAuthQuery } from "~/hooks/common"
import { Queries } from "~/queries"
import { useCategoryOpenStateByView } from "~/store/subscription"
import {
useCategoryOpenStateByView,
useFeedsGroupedData,
useInboxesGroupedData,
useListsGroupedData,
} from "~/store/subscription"
import {
resetSelectedFeedIds,
@@ -16,14 +21,7 @@ import {
useSelectedFeedIdsState,
} from "./atom"
import { DraggableContext } from "./context"
import {
EmptyFeedList,
ListHeader,
StarredItem,
useFeedsGroupedData,
useInboxesGroupedData,
useListsGroupedData,
} from "./FeedList.shared"
import { EmptyFeedList, ListHeader, StarredItem } from "./FeedList.shared"
import { useShouldFreeUpSpace } from "./hook"
import { SortableFeedList, SortByAlphabeticalInbox, SortByAlphabeticalList } from "./sort-by"
@@ -7,16 +7,14 @@ import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
import { useAuthQuery } from "~/hooks/common"
import { Queries } from "~/queries"
import { useCategoryOpenStateByView } from "~/store/subscription"
import {
EmptyFeedList,
ListHeader,
StarredItem,
useCategoryOpenStateByView,
useFeedsGroupedData,
useInboxesGroupedData,
useListsGroupedData,
} from "./FeedList.shared"
} from "~/store/subscription"
import { EmptyFeedList, ListHeader, StarredItem } from "./FeedList.shared"
import { SortableFeedList, SortByAlphabeticalInbox, SortByAlphabeticalList } from "./sort-by"
import { feedColumnStyles } from "./styles"
@@ -1,116 +1,39 @@
import { isMobile } from "@follow/components/hooks/useMobile.js"
import { RootPortal } from "@follow/components/ui/portal/index.js"
import type { FeedViewType } from "@follow/constants"
import { views } from "@follow/constants"
import { stopPropagation } from "@follow/utils/dom"
import { cn } from "@follow/utils/utils"
import * as HoverCard from "@radix-ui/react-hover-card"
import { useQuery } from "@tanstack/react-query"
import { AnimatePresence, m } from "framer-motion"
import { memo, useCallback, useMemo, useRef, useState } from "react"
import { memo, useRef, useState } from "react"
import { useTranslation } from "react-i18next"
import { Link } from "react-router"
import { useOnClickOutside } from "usehooks-ts"
import { useGeneralSettingSelector } from "~/atoms/settings/general"
import { IconOpacityTransition } from "~/components/ux/transition/icon"
import { FEED_COLLECTION_LIST } from "~/constants"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteFeedId } from "~/hooks/biz/useRouteParams"
import { useAuthQuery } from "~/hooks/common"
import { Queries } from "~/queries"
import {
subscriptionActions,
useCategoryOpenStateByView,
useSubscriptionByView,
} from "~/store/subscription"
import { feedUnreadActions, useFeedUnreadStore } from "~/store/unread"
import { subscriptionActions, useCategoryOpenStateByView } from "~/store/subscription"
import { feedUnreadActions } from "~/store/unread"
import { useUnreadByView } from "~/store/unread/hooks"
import { getFeedListSort, setFeedListSortBy, setFeedListSortOrder, useFeedListSort } from "./atom"
import { feedColumnStyles } from "./styles"
import { UnreadNumber } from "./UnreadNumber"
export const useFeedsGroupedData = (view: FeedViewType) => {
const { data: remoteData } = useAuthQuery(Queries.subscription.byView(view))
const data = useSubscriptionByView(view) || remoteData
const autoGroup = useGeneralSettingSelector((state) => state.autoGroup)
return useMemo(() => {
if (!data || data.length === 0) return {}
const groupFolder = {} as Record<string, string[]>
for (const subscription of data.filter((s) => !!s)) {
const category =
subscription.category || (autoGroup ? subscription.defaultCategory : subscription.feedId)
if (category) {
if (!groupFolder[category]) {
groupFolder[category] = []
}
groupFolder[category].push(subscription.feedId)
}
}
return groupFolder
}, [autoGroup, data])
}
export const useListsGroupedData = (view: FeedViewType) => {
const { data: remoteData } = useAuthQuery(Queries.subscription.byView(view))
const data = useSubscriptionByView(view) || remoteData
return useMemo(() => {
if (!data || data.length === 0) return {}
const lists = data.filter((s) => s && "listId" in s)
const groupFolder = {} as Record<string, string[]>
for (const subscription of lists.filter((s) => !!s)) {
groupFolder[subscription.feedId] = [subscription.feedId]
}
return groupFolder
}, [data])
}
export const useInboxesGroupedData = (view: FeedViewType) => {
const { data: remoteData } = useAuthQuery(Queries.subscription.byView(view))
const data = useSubscriptionByView(view) || remoteData
return useMemo(() => {
if (!data || data.length === 0) return {}
const inboxes = data.filter((s) => s && "inboxId" in s)
const groupFolder = {} as Record<string, string[]>
for (const subscription of inboxes.filter((s) => !!s)) {
if (!subscription.inboxId) continue
groupFolder[subscription.inboxId] = [subscription.inboxId]
}
return groupFolder
}, [data])
}
const useUpdateUnreadCount = () => {
export const ListHeader = ({ view }: { view: number }) => {
useAuthQuery(Queries.subscription.byView())
useAuthQuery(Queries.subscription.unreadAll(), {
refetchInterval: false,
})
}
export const ListHeader = ({ view }: { view: number }) => {
const { t } = useTranslation()
const feedsData = useFeedsGroupedData(view)
const categoryOpenStateData = useCategoryOpenStateByView(view)
const expansion = Object.values(categoryOpenStateData).every((value) => value === true)
useUpdateUnreadCount()
useQuery({
queryKey: ["fetchUnreadByView", view],
@@ -121,21 +44,7 @@ export const ListHeader = ({ view }: { view: number }) => {
refetchOnWindowFocus: true,
})
const totalUnread = useFeedUnreadStore(
useCallback(
(state) => {
let unread = 0
for (const category in feedsData) {
for (const feedId of feedsData[category]!) {
unread += state.data[feedId] || 0
}
}
return unread
},
[feedsData],
),
)
const totalUnread = useUnreadByView(view)
const navigateEntry = useNavigateEntry()
@@ -39,7 +39,7 @@ const ViewSwitchButton: FC<{
isActive: boolean
setActive: () => void
}> = ({ view, isActive, setActive }) => {
const unreadByView = useUnreadByView()
const unreadByView = useUnreadByView(view)
const { t } = useTranslation()
const showSidebarUnreadCount = useUISettingKey("sidebarShowUnreadCount")
const item = views.find((item) => item.view === view)!
@@ -74,13 +74,13 @@ const ViewSwitchButton: FC<{
{item.icon}
{showSidebarUnreadCount ? (
<div className="text-[0.625rem] font-medium leading-none">
{unreadByView[view]! > 99 ? <span className="-mr-0.5">99+</span> : unreadByView[view]}
{unreadByView > 99 ? <span className="-mr-0.5">99+</span> : unreadByView}
</div>
) : (
<i
className={cn(
"i-mgc-round-cute-fi text-[0.25rem]",
unreadByView[view] ? (isActive ? "opacity-100" : "opacity-60") : "opacity-0",
unreadByView ? (isActive ? "opacity-100" : "opacity-60") : "opacity-0",
)}
/>
)}
+66 -1
View File
@@ -1,5 +1,7 @@
import { FeedViewType, viewList } from "@follow/constants"
import { useCallback } from "react"
import { useCallback, useMemo } from "react"
import { useGeneralSettingSelector } from "~/atoms/settings/general"
import { useFeedStore } from "../feed"
import {
@@ -139,3 +141,66 @@ export const useAllInboxes = () => {
),
)
}
export const useFeedsGroupedData = (view: FeedViewType) => {
const data = useSubscriptionByView(view)
const autoGroup = useGeneralSettingSelector((state) => state.autoGroup)
return useMemo(() => {
if (!data || data.length === 0) return {}
const groupFolder = {} as Record<string, string[]>
for (const subscription of data.filter((s) => !!s)) {
const category =
subscription.category || (autoGroup ? subscription.defaultCategory : subscription.feedId)
if (category) {
if (!groupFolder[category]) {
groupFolder[category] = []
}
groupFolder[category].push(subscription.feedId)
}
}
return groupFolder
}, [autoGroup, data])
}
export const useListsGroupedData = (view: FeedViewType) => {
const data = useSubscriptionByView(view)
return useMemo(() => {
if (!data || data.length === 0) return {}
const lists = data.filter((s) => s && "listId" in s)
const groupFolder = {} as Record<string, string[]>
for (const subscription of lists.filter((s) => !!s)) {
groupFolder[subscription.feedId] = [subscription.feedId]
}
return groupFolder
}, [data])
}
export const useInboxesGroupedData = (view: FeedViewType) => {
const data = useSubscriptionByView(view)
return useMemo(() => {
if (!data || data.length === 0) return {}
const inboxes = data.filter((s) => s && "inboxId" in s)
const groupFolder = {} as Record<string, string[]>
for (const subscription of inboxes.filter((s) => !!s)) {
if (!subscription.inboxId) continue
groupFolder[subscription.inboxId] = [subscription.inboxId]
}
return groupFolder
}, [data])
}
+62 -15
View File
@@ -1,28 +1,75 @@
import { useCallback } from "react"
import type { FeedViewType } from "@follow/constants"
import { useCallback, useMemo } from "react"
import { useAuthQuery } from "~/hooks/common/useBizQuery"
import { Queries } from "~/queries"
import { useSubscriptionStore } from "../subscription/store"
import { useListStore } from "../list/store"
import { useSubscriptionByView } from "../subscription/hooks"
import { useFeedUnreadStore } from "."
export const useUnreadByView = () => {
useAuthQuery(Queries.subscription.byView())
const idByView = useSubscriptionStore((state) => state.feedIdByView)
export const useUnreadByView = (view: FeedViewType) => {
const subscriptions = useSubscriptionByView(view)
const ids = useMemo(() => {
const ids = {
feedIds: [] as string[],
listIds: [] as string[],
inboxIds: [] as string[],
}
subscriptions.forEach((subscription) => {
if (subscription?.listId) {
ids.listIds.push(subscription.listId)
} else if (subscription?.inboxId) {
ids.inboxIds.push(subscription.inboxId)
} else if (subscription?.feedId) {
ids.feedIds.push(subscription.feedId)
}
})
return ids
}, [subscriptions])
const allFeedIds = useMemo(() => {
const state = useListStore.getState()
const listList = ids.listIds.map((id) => state.lists[id])
return new Set([...listList.flatMap((list) => list?.feedIds || []), ...ids.feedIds])
}, [ids])
const totalUnread = useFeedUnreadStore(
useCallback(
(state) => {
const unread = {} as Record<number, number>
let unread = 0
for (const view in idByView) {
unread[view] = idByView[view].reduce(
(acc: number, feedId: string) => acc + (state.data[feedId] || 0),
0,
)
for (const feedId of allFeedIds) {
unread += state.data[feedId] || 0
}
for (const inboxId of ids.inboxIds) {
unread += state.data[inboxId] || 0
}
return unread
},
[idByView],
[allFeedIds, ids.inboxIds],
),
)
return totalUnread
}
export const useUnreadByListId = (listId: string) => {
const list = useListStore((state) => state.lists[listId])
const totalUnread = useFeedUnreadStore(
useCallback(
(state) => {
if (!list?.feedIds) return 0
let unread = 0
for (const feedId of list.feedIds) {
unread += state.data[feedId] || 0
}
return unread
},
[list?.feedIds],
),
)