1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import type { InfiniteData } from "@tanstack/query-core";
import { atom } from "jotai";
import { atomWithInfiniteQuery } from "jotai-tanstack-query";
import type { components } from "../api/generated";
import { api } from "../services/api-client";
type ArticleConnection = components["schemas"]["ArticleConnection"];
export const articleViewAtom = atom<"read" | "unread">("unread");
export const articleFeedFilterAtom = atom<string | null>(null);
export const articlesInfiniteAtom = atomWithInfiniteQuery<
ArticleConnection,
Error,
InfiniteData<ArticleConnection, string | null>,
string[],
string | null
>((get) => {
const view = get(articleViewAtom);
const feedId = get(articleFeedFilterAtom);
return {
queryKey: ["articles", view, feedId ?? "all"],
queryFn: async ({ pageParam }) => {
const query: { feedId?: string; after?: string } = {};
if (feedId) query.feedId = feedId;
if (pageParam) query.after = pageParam;
if (view === "read") {
const { data } = await api.GET("/api/articles/read", {
params: { query },
});
return data ?? { articles: [], pageInfo: { hasNextPage: false } };
}
const { data } = await api.GET("/api/articles/unread", {
params: { query },
});
return data ?? { articles: [], pageInfo: { hasNextPage: false } };
},
initialPageParam: null as string | null,
getNextPageParam: (lastPage: ArticleConnection) =>
lastPage.pageInfo.hasNextPage
? (lastPage.pageInfo.endCursor ?? null)
: null,
};
});
|