diff --git a/commafeed-client/src/app/tree/thunks.ts b/commafeed-client/src/app/tree/thunks.ts index 83ee2b66..38b98529 100644 --- a/commafeed-client/src/app/tree/thunks.ts +++ b/commafeed-client/src/app/tree/thunks.ts @@ -1,6 +1,8 @@ import { createAppAsyncThunk } from "app/async-thunk" import { client } from "app/client" +import { incrementUnreadCount } from "app/tree/slice" import type { CollapseRequest } from "app/types" +import { flattenCategoryTree } from "app/utils" export const reloadTree = createAppAsyncThunk("tree/reload", async () => await client.category.getRoot().then(r => r.data)) @@ -8,3 +10,26 @@ export const collapseTreeCategory = createAppAsyncThunk( "tree/category/collapse", async (req: CollapseRequest) => await client.category.collapse(req) ) + +export const newFeedEntriesDiscovered = createAppAsyncThunk( + "tree/new-feed-entries-discovered", + async ({ feedId, amount }: { feedId: number; amount: number }, thunkApi) => { + const root = thunkApi.getState().tree.rootCategory + if (!root) return + + const feed = flattenCategoryTree(root) + .flatMap(c => c.feeds) + .some(f => f.id === feedId) + if (!feed) { + // feed not found in the tree, reload the tree completely + thunkApi.dispatch(reloadTree()) + } else { + thunkApi.dispatch( + incrementUnreadCount({ + feedId, + amount, + }) + ) + } + } +) diff --git a/commafeed-client/src/hooks/useWebSocket.ts b/commafeed-client/src/hooks/useWebSocket.ts index b13958c6..3d8a13ae 100644 --- a/commafeed-client/src/hooks/useWebSocket.ts +++ b/commafeed-client/src/hooks/useWebSocket.ts @@ -1,6 +1,6 @@ import { setWebSocketConnected } from "app/server/slice" import { type AppDispatch, useAppDispatch, useAppSelector } from "app/store" -import { incrementUnreadCount } from "app/tree/slice" +import { newFeedEntriesDiscovered } from "app/tree/thunks" import { useEffect } from "react" import WebsocketHeartbeatJs from "websocket-heartbeat-js" @@ -9,7 +9,7 @@ const handleMessage = (dispatch: AppDispatch, message: string) => { const type = parts[0] if (type === "new-feed-entries") { dispatch( - incrementUnreadCount({ + newFeedEntriesDiscovered({ feedId: +parts[1], amount: +parts[2], })