feat: dont shink unread when query refreshed (#134)

* feat: dont shink unread when query refreshed

* fix: update

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
Co-authored-by: Innei <i@innei.in>
This commit is contained in:
DIYgod
2024-07-15 23:48:01 +08:00
committed by GitHub
co-authored by Innei
parent 4d90bf6e41
commit a79bda7ae5
4 changed files with 54 additions and 26 deletions
+5
View File
@@ -26,6 +26,11 @@ export const setHydrated = (v: boolean) => {
_isHydrated = v
}
/**
* @description Check if database data is hydrated to store, or current database is ready.
* If users disabled data persist, it's always false, that means you can't do operation with database.
*
*/
export const isHydrated = () => _isHydrated
export const hydrateDatabaseToStore = async () => {
+10
View File
@@ -146,3 +146,13 @@ export const pluralize = (
}
return postfix(noun, rule)
}
export const omitObjectUndefinedValue = (obj: Record<string, any>) => {
const newObj = {}
for (const key in obj) {
if (obj[key] !== undefined) {
newObj[key] = obj[key]
}
}
return newObj
}
+26 -23
View File
@@ -73,10 +73,26 @@ export const useEntriesByView = () => {
view,
...(unreadOnly === true && { read: false }),
})
const entries = useEntryIdsByFeedIdOrView(isAllFeeds ? view : feedId!, {
unread: unreadOnly,
view,
})
const remoteEntryIds = query.data?.pages
?.map((page) => page.data?.map((entry) => entry.entries.id))
.flat() as string[]
const currentEntries = useEntryIdsByFeedIdOrView(
isAllFeeds ? view : feedId!,
{
unread: unreadOnly,
view,
},
)
// If remote data is not available, we use the local data, get the local data length
// FIXME: remote first, then local store data
// NOTE: We still can't use the store's data handling directly.
// Imagine that the local data may be persistent, and then if there are incremental updates to the data on the server side,
// then we have no way to incrementally update the data.
// We need to add an interface to incrementally update the data based on the version hash.
const entries = remoteEntryIds || currentEntries
useHotkeys(
shortcuts.entries.refetch.key,
@@ -93,7 +109,7 @@ export const useEntriesByView = () => {
useEffect(() => {
prevEntries.current = []
}, [routeParams.feedId, routeParams.view])
const localEntries = useMemo(() => {
const mergedEntries = useMemo(() => {
if (!unreadOnly) {
prevEntries.current = []
return entries
@@ -102,35 +118,22 @@ export const useEntriesByView = () => {
prevEntries.current = entries
return entries
}
if (entries.length > prevEntries.current.length) {
prevEntries.current = entries
return entries
}
// merge the new entries with the old entries, and unique them
const nextIds = [...new Set([...prevEntries.current, ...entries])]
prevEntries.current = nextIds
return nextIds
}, [entries, prevEntries, unreadOnly])
const sortLocalEntries = () =>
const sortEntries = () =>
isCollection ?
sortEntriesIdByStarAt(localEntries) :
sortEntriesIdByEntryPublishedAt(localEntries)
const remoteEntryIds = query.data?.pages
?.map((page) => page.data?.map((entry) => entry.entries.id))
.flat() as string[]
sortEntriesIdByStarAt(mergedEntries) :
sortEntriesIdByEntryPublishedAt(mergedEntries)
return {
...query,
// If remote data is not available, we use the local data, get the local data length
// FIXME: remote first, then local store data
// NOTE: We still can't use the store's data handling directly.
// Imagine that the local data may be persistent, and then if there are incremental updates to the data on the server side,
// then we have no way to incrementally update the data.
// We need to add an interface to incrementally update the data based on the version hash.
entriesIds: remoteEntryIds ?? sortLocalEntries(),
totalCount: query.data?.pages?.[0]?.total ?? localEntries.length,
entriesIds: sortEntries(),
totalCount: query.data?.pages?.[0]?.total ?? mergedEntries.length,
}
}
+13 -3
View File
@@ -1,5 +1,8 @@
import { apiClient } from "@renderer/lib/api-fetch"
import { getEntriesParams } from "@renderer/lib/utils"
import {
getEntriesParams,
omitObjectUndefinedValue,
} from "@renderer/lib/utils"
import type {
CombinedEntryModel,
EntryModel,
@@ -7,7 +10,7 @@ import type {
} from "@renderer/models"
import { EntryService } from "@renderer/services"
import { produce } from "immer"
import { merge, omit } from "lodash-es"
import { isNil, merge, omit } from "lodash-es"
import { isHydrated } from "../../initialize/hydrate"
import { feedActions } from "../feed"
@@ -98,7 +101,8 @@ class EntryActions {
produce(state, (draft) => {
const entry = draft.flatMapEntries[entryId]
if (!entry) return
Object.assign(entry, changed)
Object.assign(entry, omitObjectUndefinedValue(changed))
return draft
}),
)
@@ -204,6 +208,12 @@ class EntryActions {
this.patch(entryId, {
read,
})
if (!isHydrated()) return
if (!isNil(read)) {
EntryService.bulkStoreReadStatus({
[entryId]: read,
})
}
}
markReadByFeedId(feedId: string) {