forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type AppBskyGraphDefs} from '@atproto/api'
2import {type QueryClient, useQuery} from '@tanstack/react-query'
3
4import {accumulate} from '#/lib/async/accumulate'
5import {STALE} from '#/state/queries'
6import {useAgent, useSession} from '#/state/session'
7
8export type MyListsFilter =
9 | 'all'
10 | 'curate'
11 | 'mod'
12 | 'all-including-subscribed'
13
14const RQKEY_ROOT = 'my-lists'
15export const RQKEY = (filter: MyListsFilter) => [RQKEY_ROOT, filter]
16
17export function useMyListsQuery(filter: MyListsFilter) {
18 const {currentAccount} = useSession()
19 const agent = useAgent()
20 return useQuery<AppBskyGraphDefs.ListView[]>({
21 staleTime: STALE.MINUTES.ONE,
22 queryKey: RQKEY(filter),
23 async queryFn() {
24 let lists: AppBskyGraphDefs.ListView[] = []
25 const promises = [
26 accumulate(cursor =>
27 agent.app.bsky.graph
28 .getLists({
29 actor: currentAccount!.did,
30 cursor,
31 limit: 50,
32 })
33 .then(res => ({
34 cursor: res.data.cursor,
35 items: res.data.lists,
36 })),
37 ),
38 ]
39 if (filter === 'all-including-subscribed' || filter === 'mod') {
40 promises.push(
41 accumulate(cursor =>
42 agent.app.bsky.graph
43 .getListMutes({
44 cursor,
45 limit: 50,
46 })
47 .then(res => ({
48 cursor: res.data.cursor,
49 items: res.data.lists,
50 })),
51 ),
52 )
53 promises.push(
54 accumulate(cursor =>
55 agent.app.bsky.graph
56 .getListBlocks({
57 cursor,
58 limit: 50,
59 })
60 .then(res => ({
61 cursor: res.data.cursor,
62 items: res.data.lists,
63 })),
64 ),
65 )
66 }
67 const resultset = await Promise.all(promises)
68 for (const res of resultset) {
69 for (let list of res) {
70 if (
71 filter === 'curate' &&
72 list.purpose !== 'app.bsky.graph.defs#curatelist'
73 ) {
74 continue
75 }
76 if (
77 filter === 'mod' &&
78 list.purpose !== 'app.bsky.graph.defs#modlist'
79 ) {
80 continue
81 }
82 if (!lists.find(l => l.uri === list.uri)) {
83 lists.push(list)
84 }
85 }
86 }
87 return lists
88 },
89 enabled: !!currentAccount,
90 })
91}
92
93export function invalidate(qc: QueryClient, filter?: MyListsFilter) {
94 if (filter) {
95 qc.invalidateQueries({queryKey: RQKEY(filter)})
96 } else {
97 qc.invalidateQueries({queryKey: [RQKEY_ROOT]})
98 }
99}