Skip to content

Commit

Permalink
fix: read state immutable update (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei authored May 28, 2024
1 parent 2574279 commit 8b9b4b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/renderer/src/components/entry-column/article-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) {

<div
className={cn(
"my-0.5 break-words font-medium text-foreground/60",
!entry.read && "text-black dark:text-white/90",
"my-0.5 break-words font-medium",
entry.read ? "text-zinc-500" : "text-black dark:text-white/90",
)}
>
{entry.title}
Expand Down
19 changes: 11 additions & 8 deletions src/renderer/src/hooks/useUpdateEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import { Queries } from "@renderer/queries"
import type { InfiniteData, QueryKey } from "@tanstack/react-query"
import { useQueryClient } from "@tanstack/react-query"
import { produce } from "immer"

export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
const queryClient = useQueryClient()
Expand All @@ -19,17 +20,19 @@ export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
})
entriesData.forEach(([key, data]: [QueryKey, unknown]) => {
const assertData = data as InfiniteData<ListResponse<EntriesResponse>>
const list = assertData?.pages?.[0]?.data
if (list) {
for (const item of list) {
if (item.id === entryId) {
for (const [key, value] of Object.entries(changed)) {
item[key] = value
const finaldata = produce(assertData, (assertData) => {
const list = assertData?.pages?.[0]?.data
if (list) {
for (const item of list) {
if (item.id === entryId) {
for (const [key, value] of Object.entries(changed)) {
item[key] = value
}
}
queryClient.setQueryData<typeof assertData>(key, assertData)
}
}
}
})
queryClient.setQueryData<typeof assertData>(key, finaldata)
})
}

Expand Down

0 comments on commit 8b9b4b6

Please sign in to comment.