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 } from "@/constants";
8import { useBoardItems } from "./useBoardItems";
9import { Agent } from "http";
10import { getPdsAgent } from "../utils/pds";
11import { useDidStore } from "../stores/did";
12
13export function useBoards(did?: string | null) {
14 const { agent } = useAuth();
15 const store = useBoardsStore();
16 const didStore = useDidStore();
17 const [isLoading, setLoading] = useState(
18 Object.entries(store.boards).length == 0
19 );
20
21 useEffect(() => {
22 if (agent == null) return;
23 const loadBoards = async () => {
24 try {
25 console.log(
26 `Loading board items for ${did ?? agent.assertDid} (raw: ${did})`
27 );
28
29 const resolvedDid = did ?? agent.assertDid;
30 const tempAgent = await getPdsAgent(resolvedDid, didStore, agent);
31
32 const boards = await tempAgent.com.atproto.repo.listRecords({
33 collection: LIST_COLLECTION,
34 repo: did ?? agent.assertDid,
35 limit: 100,
36 });
37
38 for (const board of boards.data.records) {
39 const safeBoard = Board.safeParse(board.value);
40 if (safeBoard.success)
41 store.setBoard(
42 resolvedDid,
43 new AtUri(board.uri).rkey,
44 safeBoard.data
45 );
46 }
47 } finally {
48 setLoading(false);
49 store.setLoading(false);
50 }
51 };
52 loadBoards();
53 }, [agent, did]);
54
55 return { isLoading };
56}