2024-06-13 21:54:14 +02:00
|
|
|
import { createAppAsyncThunk } from "app/async-thunk"
|
|
|
|
|
import { client } from "app/client"
|
2025-02-14 14:05:09 +01:00
|
|
|
import { incrementUnreadCount } from "app/tree/slice"
|
2024-06-13 21:54:14 +02:00
|
|
|
import type { CollapseRequest } from "app/types"
|
2025-02-14 14:05:09 +01:00
|
|
|
import { flattenCategoryTree } from "app/utils"
|
2024-06-13 21:54:14 +02:00
|
|
|
|
|
|
|
|
export const reloadTree = createAppAsyncThunk("tree/reload", async () => await client.category.getRoot().then(r => r.data))
|
2025-02-14 13:49:46 +01:00
|
|
|
|
2024-06-13 21:54:14 +02:00
|
|
|
export const collapseTreeCategory = createAppAsyncThunk(
|
|
|
|
|
"tree/category/collapse",
|
|
|
|
|
async (req: CollapseRequest) => await client.category.collapse(req)
|
|
|
|
|
)
|
2025-02-14 14:05:09 +01:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|