Skip to content

Commit

Permalink
feat: update unread count
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 29, 2024
1 parent fb6b136 commit d177f9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/renderer/src/components/entry-column/item-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function EntryItemWrapper({

const updateEntry = useUpdateEntry({
entryId: entry?.entries.id,
feedId: entry?.feeds.id,
})

const read = useMutation({
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/hooks/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const useEntryActions = ({

const updateEntry = useUpdateEntry({
entryId: entry?.entries.id,
feedId: entry?.feeds.id,
})

const collect = useMutation({
Expand Down
31 changes: 30 additions & 1 deletion src/renderer/src/hooks/useUpdateEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import { Queries } from "@renderer/queries"
import type { Response as SubscriptionsResponse } from "@renderer/queries/subscriptions"
import type { InfiniteData, QueryKey } from "@tanstack/react-query"
import { useQueryClient } from "@tanstack/react-query"
import { produce } from "immer"

export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
export const useUpdateEntry = ({
entryId,
feedId,
}: {
entryId?: string
feedId?: string
}) => {
const queryClient = useQueryClient()

const updateEntry = (changed: Partial<EntriesResponse[number]>) => {
Expand Down Expand Up @@ -34,6 +41,28 @@ export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
})
queryClient.setQueryData<typeof assertData>(key, finaldata)
})

if (changed.read !== undefined) {
const entriesData = queryClient.getQueriesData({
queryKey: ["subscriptions"],
})
entriesData.forEach(([key, data]: [QueryKey, unknown]) => {
const chage = changed.read ? -1 : 1
const assertData = data as SubscriptionsResponse
const finaldata = produce(assertData, (assertData) => {
for (const list of assertData.list) {
for (const item of list.list) {
if (item.feeds.id === feedId) {
assertData.unread += chage
list.unread += chage
item.unread = (item.unread || 0) + chage
}
}
}
})
queryClient.setQueryData<typeof assertData>(key, finaldata)
})
}
}

return updateEntry
Expand Down

0 comments on commit d177f9f

Please sign in to comment.