(supabase: SupabaseClient<Database>)
| 12 | }; |
| 13 | |
| 14 | export default function useDocumentsLoader(supabase: SupabaseClient<Database>) { |
| 15 | const PAGE_SIZE = 10; |
| 16 | |
| 17 | const [allDocumentCount, setAllDocumentCount] = useState(0); |
| 18 | const [docPage, setDocPage] = useState<number>(1); |
| 19 | const [docs, setDocs] = useState<Document[]>([]); |
| 20 | |
| 21 | const fetchAllSectionCount = async () => { |
| 22 | const { data } = await supabase.rpc("get_all_page_section_counts"); |
| 23 | if (data) { |
| 24 | setAllDocumentCount(data); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | const deleteDocument = async (document: Document) => { |
| 29 | const documentChunkIds = document.docChunks.map((docChunk) => |
| 30 | docChunk.id.toString(), |
| 31 | ); |
| 32 | |
| 33 | const { error } = await supabase |
| 34 | .from("doc_chunks") |
| 35 | .delete() |
| 36 | .in("id", documentChunkIds); |
| 37 | |
| 38 | return error; |
| 39 | }; |
| 40 | |
| 41 | const fetchPage = async (page: number) => { |
| 42 | const { data: documents, error } = await supabase.rpc("get_sections", { |
| 43 | _limit: PAGE_SIZE, |
| 44 | _offset: (page - 1) * PAGE_SIZE, |
| 45 | }); |
| 46 | |
| 47 | if (error) { |
| 48 | console.error(error); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (!documents?.length) return; |
| 53 | |
| 54 | const newDocuments: Document[] = []; |
| 55 | |
| 56 | for (const document of documents) { |
| 57 | try { |
| 58 | const documentRowIds = document.ids.split(","); |
| 59 | const { data: documentChunks } = await supabase |
| 60 | .from("doc_chunks") |
| 61 | .select("*") |
| 62 | .in("id", documentRowIds); |
| 63 | |
| 64 | if (documentChunks?.length) { |
| 65 | newDocuments.push({ |
| 66 | docChunks: documentChunks, |
| 67 | pageName: document.result_page_title, |
| 68 | sectionName: document.result_section_title, |
| 69 | createdAt: document.latest_created_at, |
| 70 | url: document.result_page_url, |
| 71 | }); |
no test coverage detected