feat: clear prevEntryIds after refreshing and fetching (#141)
* feat: clear prevEntryIds after refreshing and fetching * fix: types * fix: memo Signed-off-by: Innei <i@innei.in> * chore: remove `toString()` Signed-off-by: Innei <i@innei.in> * feat: scroll to top after list reset * feat: remove unnecessary effect * Revert "feat: remove unnecessary effect" This reverts commit fe63d01c0736ab84d03fa9c25f22cd77f2b06754. * feat: remove unnecessary effect --------- Signed-off-by: Innei <i@innei.in> Co-authored-by: Innei <i@innei.in>
This commit is contained in:
@@ -57,7 +57,11 @@ export const useEntryMarkReadHandler = (entriesIds: string[]) => {
|
||||
scrollMarkUnread,
|
||||
])
|
||||
}
|
||||
export const useEntriesByView = () => {
|
||||
export const useEntriesByView = ({
|
||||
onReset,
|
||||
}: {
|
||||
onReset?: () => void
|
||||
}) => {
|
||||
const routeParams = useRouteParms()
|
||||
const unreadOnly = useGeneralSettingKey("unreadOnly")
|
||||
|
||||
@@ -70,9 +74,13 @@ export const useEntriesByView = () => {
|
||||
view,
|
||||
...(unreadOnly === true && { read: false }),
|
||||
})
|
||||
const remoteEntryIds = query.data?.pages
|
||||
?.map((page) => page.data?.map((entry) => entry.entries.id))
|
||||
.flat() as string[]
|
||||
const remoteEntryIds = useMemo(
|
||||
() =>
|
||||
query.data?.pages
|
||||
?.map((page) => page.data?.map((entry) => entry.entries.id))
|
||||
.flat() as string[],
|
||||
[query.data?.pages],
|
||||
)
|
||||
|
||||
const currentEntries = useEntryIdsByFeedIdOrView(
|
||||
isAllFeeds ? view : feedId!,
|
||||
@@ -104,16 +112,17 @@ export const useEntriesByView = () => {
|
||||
const prevEntryIdsRef = useRef(entryIds)
|
||||
|
||||
useEffect(() => {
|
||||
prevEntryIdsRef.current = []
|
||||
}, [routeParams.feedId, routeParams.view])
|
||||
if (!query.isFetching) {
|
||||
prevEntryIdsRef.current = entryIds
|
||||
setMergedEntries(entryIds)
|
||||
onReset?.()
|
||||
}
|
||||
}, [query.isFetching])
|
||||
|
||||
const [mergedEntries, setMergedEntries] = useState<string[]>([])
|
||||
|
||||
const entryIdsAsDeps = entryIds.toString()
|
||||
useEffect(() => {
|
||||
if (!unreadOnly) {
|
||||
prevEntryIdsRef.current = []
|
||||
setMergedEntries(entryIds)
|
||||
return
|
||||
}
|
||||
if (!prevEntryIdsRef.current) {
|
||||
prevEntryIdsRef.current = entryIds
|
||||
setMergedEntries(entryIds)
|
||||
@@ -123,7 +132,7 @@ export const useEntriesByView = () => {
|
||||
const nextIds = [...new Set([...prevEntryIdsRef.current, ...entryIds])]
|
||||
prevEntryIdsRef.current = nextIds
|
||||
setMergedEntries(nextIds)
|
||||
}, [unreadOnly, entryIds.toString()])
|
||||
}, [entryIdsAsDeps])
|
||||
|
||||
const sortEntries = () =>
|
||||
isCollection ?
|
||||
@@ -133,6 +142,9 @@ export const useEntriesByView = () => {
|
||||
return {
|
||||
...query,
|
||||
|
||||
refetch: useCallback(() => {
|
||||
query.refetch()
|
||||
}, [query]),
|
||||
entriesIds: sortEntries(),
|
||||
totalCount: query.data?.pages?.[0]?.total ?? mergedEntries.length,
|
||||
}
|
||||
|
||||
@@ -58,7 +58,14 @@ import { useEntriesByView, useEntryMarkReadHandler } from "./hooks"
|
||||
import { EntryItem } from "./item"
|
||||
|
||||
export function EntryColumn() {
|
||||
const entries = useEntriesByView()
|
||||
const virtuosoRef = useRef<VirtuosoHandle>(null)
|
||||
const entries = useEntriesByView({
|
||||
onReset: useCallback(() => {
|
||||
virtuosoRef.current?.scrollIntoView({
|
||||
index: 0,
|
||||
})
|
||||
}, []),
|
||||
})
|
||||
const { entriesIds, isFetchingNextPage } = entries
|
||||
|
||||
const {
|
||||
@@ -86,6 +93,7 @@ export function EntryColumn() {
|
||||
const handleMarkReadInRange = useEntryMarkReadHandler(entriesIds)
|
||||
|
||||
const virtuosoOptions = {
|
||||
virtuosoRef,
|
||||
components: {
|
||||
List: ListContent,
|
||||
|
||||
@@ -150,6 +158,7 @@ export function EntryColumn() {
|
||||
<VirtuosoGrid
|
||||
listClassName="grid grid-cols-2 gap-3 md:grid-cols-3 lg:grid-cols-4 px-4"
|
||||
{...virtuosoOptions}
|
||||
ref={virtuosoOptions.virtuosoRef}
|
||||
/>
|
||||
) :
|
||||
(
|
||||
@@ -359,11 +368,11 @@ const EmptyList = forwardRef<HTMLDivElement, HTMLMotionProps<"div">>(
|
||||
},
|
||||
)
|
||||
|
||||
const EntryList: FC<VirtuosoProps<string, unknown>> = ({
|
||||
const EntryList: FC<VirtuosoProps<string, unknown> & {
|
||||
virtuosoRef: React.RefObject<VirtuosoHandle>
|
||||
}> = ({
|
||||
...virtuosoOptions
|
||||
}) => {
|
||||
const virtuosoRef = useRef<VirtuosoHandle>(null)
|
||||
|
||||
const dataRef = useRefValue(virtuosoOptions.data!)
|
||||
const currentEntryIdRef = useRefValue(useRouteEntryId())
|
||||
|
||||
@@ -402,7 +411,7 @@ const EntryList: FC<VirtuosoProps<string, unknown>> = ({
|
||||
|
||||
const nextIndex = Math.min(currentActiveEntryIndex + 1, data.length - 1)
|
||||
|
||||
virtuosoRef.current?.scrollIntoView({
|
||||
virtuosoOptions.virtuosoRef.current?.scrollIntoView({
|
||||
index: nextIndex,
|
||||
})
|
||||
const nextId = data![nextIndex]
|
||||
@@ -426,7 +435,7 @@ const EntryList: FC<VirtuosoProps<string, unknown>> = ({
|
||||
data.length - 1 :
|
||||
Math.max(0, currentActiveEntryIndex - 1)
|
||||
|
||||
virtuosoRef.current?.scrollIntoView({
|
||||
virtuosoOptions.virtuosoRef.current?.scrollIntoView({
|
||||
index: nextIndex,
|
||||
})
|
||||
const nextId = data![nextIndex]
|
||||
@@ -450,7 +459,7 @@ const EntryList: FC<VirtuosoProps<string, unknown>> = ({
|
||||
<Virtuoso
|
||||
onKeyDown={handleKeyDown}
|
||||
{...virtuosoOptions}
|
||||
ref={virtuosoRef}
|
||||
ref={virtuosoOptions.virtuosoRef}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user