Scrapboard.org client
1"use client";
2import { PropsWithChildren, useEffect, useState } from "react";
3import { useAuth } from "@/lib/hooks/useAuth";
4import { useFeedDefsStore } from "../stores/feedDefs";
5import { AtUri } from "@atproto/api";
6import { Board, useBoardsStore } from "../stores/boards";
7import { LIST_COLLECTION, LIST_ITEM_COLLECTION } from "@/constants";
8import { BoardItem, useBoardItemsStore } from "../stores/boardItems";
9import { getAllRecords } from "../records";
10import { getPdsAgent } from "../utils/pds";
11import { useDidStore } from "../stores/did";
12
13export function useBoardItems(did?: string | null) {
14 const { agent } = useAuth();
15 const store = useBoardItemsStore();
16 const [isLoading, setLoading] = useState(store.boardItems.size == 0);
17 const didStore = useDidStore();
18
19 useEffect(() => {
20 if (agent == null) return;
21 const loadItems = async () => {
22 try {
23 console.log(
24 `Loading board items for ${did ?? agent.assertDid} (raw: ${did})`
25 );
26
27 const resolvedDid = did ?? agent.assertDid;
28 const tempAgent = await getPdsAgent(resolvedDid, didStore, agent);
29 const boards = await getAllRecords({
30 collection: LIST_ITEM_COLLECTION,
31 repo: resolvedDid,
32 limit: 100,
33 agent: tempAgent,
34 });
35
36 for (const item of boards) {
37 const safeItem = BoardItem.safeParse(item.value);
38 if (safeItem.success)
39 store.setBoardItem(new AtUri(item.uri).rkey, safeItem.data);
40 else
41 console.warn(`${item.uri} could not be parsed safely`, item.value);
42 }
43 } finally {
44 setLoading(false);
45 store.setLoading(false);
46 }
47 };
48 loadItems();
49 }, [agent]);
50
51 return { isLoading };
52}