1
1
// @ts -check
2
2
3
3
import { unwrapShortHandle } from '.' ;
4
- import { fetchClearskyApi } from './core' ;
4
+ import { fetchClearskyApi , unwrapClearskyURL } from './core' ;
5
5
import { useResolveHandleOrDid } from './resolve-handle-or-did' ;
6
6
import { useInfiniteQuery , useQuery } from '@tanstack/react-query' ;
7
7
@@ -23,6 +23,7 @@ export function useList(handleOrDID) {
23
23
}
24
24
25
25
/**
26
+ * Look up the total number of lists to which a given handle/DID belongs
26
27
* @param {string } handleOrDID
27
28
*/
28
29
export function useListTotal ( handleOrDID ) {
@@ -35,6 +36,18 @@ export function useListTotal(handleOrDID) {
35
36
} ) ;
36
37
}
37
38
39
+ /**
40
+ * Gets the size (length) of a given user list
41
+ * @param {string } listUrl
42
+ */
43
+ export function useListSize ( listUrl ) {
44
+ return useQuery ( {
45
+ enabled : ! ! listUrl ,
46
+ queryKey : [ 'list-size' , listUrl ] ,
47
+ queryFn : ( ) => getListSize ( listUrl ) ,
48
+ } ) ;
49
+ }
50
+
38
51
/**
39
52
* @param {string } shortHandle
40
53
* @param {number } currentPage
@@ -68,6 +81,7 @@ async function getList(shortHandle, currentPage = 1) {
68
81
}
69
82
70
83
/**
84
+ * Gets the total number of lists to which a given handle belongs
71
85
* @param {string } shortHandle
72
86
*/
73
87
async function getListTotal ( shortHandle ) {
@@ -77,3 +91,24 @@ async function getListTotal(shortHandle) {
77
91
const re = await fetchClearskyApi ( 'v1' , handleURL ) ;
78
92
return re . data ;
79
93
}
94
+
95
+ /**
96
+ * Gets the size (length) of a given user list
97
+ * @param {string } listUrl
98
+ * @returns {Promise<{ count: number } | null> } null if response is a 400/404
99
+ */
100
+ async function getListSize ( listUrl ) {
101
+ const apiUrl = unwrapClearskyURL (
102
+ `/api/v1/anon/get-list/specific/total/${ encodeURIComponent ( listUrl ) } `
103
+ ) ;
104
+ const resp = await fetch ( apiUrl ) ;
105
+ if ( resp . ok ) {
106
+ /** @type {{ data: { count: number }, list_uri: string } } */
107
+ const respData = await resp . json ( ) ;
108
+ return respData . data ;
109
+ }
110
+ if ( resp . status === 400 || resp . status === 404 ) {
111
+ return null ;
112
+ }
113
+ throw new Error ( 'getListSize error: ' + resp . statusText ) ;
114
+ }
0 commit comments