Files
Folo/apps/renderer/src/modules/entry-column/hooks/useEntryMarkReadHandler.tsx
T
Innei d98ebaa4f7 refactor: vlist and keep scroll position (#1931)
* init

Signed-off-by: Innei <tukon479@gmail.com>

* feat: grid

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: mark read logci

Signed-off-by: Innei <tukon479@gmail.com>

* chore: remove react-virtuoso

Signed-off-by: Innei <tukon479@gmail.com>

* fix: video

Signed-off-by: Innei <tukon479@gmail.com>

* transition

Signed-off-by: Innei <tukon479@gmail.com>

* fix: image memo loaded status

Signed-off-by: Innei <tukon479@gmail.com>

* fix: sort button

Signed-off-by: Innei <tukon479@gmail.com>

* feat: cjk truncate

Signed-off-by: Innei <tukon479@gmail.com>

---------

Signed-off-by: Innei <tukon479@gmail.com>
2024-11-29 22:51:37 +08:00

56 lines
1.6 KiB
TypeScript

import { views } from "@follow/constants"
import type { Range } from "@tanstack/react-virtual"
import { useMemo } from "react"
import { useDebounceCallback } from "usehooks-ts"
import { useGeneralSettingKey } from "~/atoms/settings/general"
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
import { entryActions } from "~/store/entry"
export const useEntryMarkReadHandler = (entriesIds: string[]) => {
const renderAsRead = useGeneralSettingKey("renderMarkUnread")
const scrollMarkUnread = useGeneralSettingKey("scrollMarkUnread")
const feedView = useRouteParamsSelector((params) => params.view)
const handleRenderAsRead = useDebounceCallback(
({ startIndex, endIndex }: Range, enabled?: boolean) => {
if (!enabled) return
const idSlice = entriesIds?.slice(startIndex, endIndex)
if (!idSlice) return
batchMarkRead(idSlice)
},
)
return useMemo(() => {
if (views[feedView].wideMode && renderAsRead) {
return handleRenderAsRead
}
if (scrollMarkUnread) {
return handleRenderAsRead
}
return
}, [feedView, handleRenderAsRead, renderAsRead, scrollMarkUnread])
}
export function batchMarkRead(ids: string[]) {
const batchLikeIds = [] as [string, string][]
const entriesId2Map = entryActions.getFlattenMapEntries()
for (const id of ids) {
const entry = entriesId2Map[id]
if (!entry) continue
const isRead = entry.read
if (!isRead) {
batchLikeIds.push([entry.feedId, id])
}
}
if (batchLikeIds.length > 0) {
for (const [feedId, id] of batchLikeIds) {
entryActions.markRead({ feedId, entryId: id, read: true })
}
}
}