Skip to content

Commit bd2db09

Browse files
committed
add new useListSize hook
1 parent 2574fd4 commit bd2db09

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/api/lists.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
import { unwrapShortHandle } from '.';
4-
import { fetchClearskyApi } from './core';
4+
import { fetchClearskyApi, unwrapClearskyURL } from './core';
55
import { useResolveHandleOrDid } from './resolve-handle-or-did';
66
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
77

@@ -23,6 +23,7 @@ export function useList(handleOrDID) {
2323
}
2424

2525
/**
26+
* Look up the total number of lists to which a given handle/DID belongs
2627
* @param {string} handleOrDID
2728
*/
2829
export function useListTotal(handleOrDID) {
@@ -35,6 +36,18 @@ export function useListTotal(handleOrDID) {
3536
});
3637
}
3738

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+
3851
/**
3952
* @param {string} shortHandle
4053
* @param {number} currentPage
@@ -68,6 +81,7 @@ async function getList(shortHandle, currentPage = 1) {
6881
}
6982

7083
/**
84+
* Gets the total number of lists to which a given handle belongs
7185
* @param {string} shortHandle
7286
*/
7387
async function getListTotal(shortHandle) {
@@ -77,3 +91,24 @@ async function getListTotal(shortHandle) {
7791
const re = await fetchClearskyApi('v1', handleURL);
7892
return re.data;
7993
}
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

Comments
 (0)