aboutsummaryrefslogtreecommitdiff
path: root/packages/memory-graph/src/hooks/use-documents-query.ts
blob: eb9ab8923a01c689bb2b9b29146b6d28fe50e097 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use client";

import { useInfiniteQuery, useQuery } from "@tanstack/react-query";
import { fetchDocuments, type FetchDocumentsOptions } from "@/lib/api-client";
import type { DocumentsResponse } from "@/api-types";

export interface UseDocumentsQueryOptions {
	apiKey: string;
	baseUrl?: string;
	id?: string; // Optional document ID to filter by
	containerTags?: string[];
	limit?: number;
	sort?: "createdAt" | "updatedAt";
	order?: "asc" | "desc";
	enabled?: boolean; // Whether to enable the query
}

/**
 * Hook for fetching a single page of documents
 * Useful when you don't need pagination
 */
export function useDocumentsQuery(options: UseDocumentsQueryOptions) {
	const {
		apiKey,
		baseUrl,
		containerTags,
		limit = 50,
		sort = "createdAt",
		order = "desc",
		enabled = true,
	} = options;

	return useQuery({
		queryKey: ["documents", { apiKey, baseUrl, containerTags, limit, sort, order }],
		queryFn: async () => {
			return fetchDocuments({
				apiKey,
				baseUrl,
				page: 1,
				limit,
				sort,
				order,
				containerTags,
			});
		},
		enabled: enabled && !!apiKey,
		staleTime: 1000 * 60 * 5, // 5 minutes
		retry: 2,
	});
}

/**
 * Hook for fetching documents with infinite scroll/pagination support
 * Automatically handles loading more pages
 */
export function useInfiniteDocumentsQuery(options: UseDocumentsQueryOptions) {
	const {
		apiKey,
		baseUrl,
		containerTags,
		limit = 500,
		sort = "createdAt",
		order = "desc",
		enabled = true,
	} = options;

	return useInfiniteQuery({
		queryKey: ["documents", "infinite", { apiKey, baseUrl, containerTags, limit, sort, order }],
		queryFn: async ({ pageParam = 1 }) => {
			return fetchDocuments({
				apiKey,
				baseUrl,
				page: pageParam,
				limit,
				sort,
				order,
				containerTags,
			});
		},
		initialPageParam: 1,
		getNextPageParam: (lastPage: DocumentsResponse) => {
			const { currentPage, totalPages } = lastPage.pagination;
			return currentPage < totalPages ? currentPage + 1 : undefined;
		},
		enabled: enabled && !!apiKey,
		staleTime: 1000 * 60 * 5, // 5 minutes
		retry: 2,
	});
}

/**
 * Helper to flatten infinite query results into a single documents array
 */
export function flattenDocuments(data: { pages: DocumentsResponse[] } | undefined) {
	if (!data?.pages) return [];
	return data.pages.flatMap((page) => page.documents);
}

/**
 * Helper to get total documents count from infinite query
 */
export function getTotalDocuments(data: { pages: DocumentsResponse[] } | undefined) {
	if (!data?.pages?.[0]) return 0;
	return data.pages[0].pagination.totalItems;
}

/**
 * Helper to get current loaded count from infinite query
 */
export function getLoadedCount(data: { pages: DocumentsResponse[] } | undefined) {
	if (!data?.pages) return 0;
	return data.pages.reduce((sum, page) => sum + page.documents.length, 0);
}