@@ -13,6 +13,7 @@ const PAGE_SIZE = 100
1313
1414interface UseGraphApiOptions {
1515 containerTags ?: string [ ]
16+ documentIds ?: string [ ]
1617 enabled ?: boolean
1718}
1819
@@ -81,20 +82,36 @@ function toGraphMemory(mem: ApiMemoryEntry): GraphApiMemory {
8182 }
8283}
8384
84- function toGraphDocument ( doc : ApiDocument ) : GraphApiDocument {
85+ function toGraphDocument (
86+ doc : ApiDocument ,
87+ containerTags ?: string [ ] ,
88+ ) : GraphApiDocument {
89+ const allowedContainerTags = new Set ( containerTags ?. filter ( Boolean ) ?? [ ] )
90+ const memoryEntries =
91+ allowedContainerTags . size > 0
92+ ? doc . memoryEntries . filter (
93+ ( mem ) =>
94+ mem . spaceContainerTag != null &&
95+ allowedContainerTags . has ( mem . spaceContainerTag ) ,
96+ )
97+ : doc . memoryEntries
98+
8599 return {
86100 id : doc . id ,
87101 title : doc . title ,
88102 summary : doc . summary ?? null ,
89103 documentType : doc . type ,
90104 createdAt : doc . createdAt ,
91105 updatedAt : doc . updatedAt ,
92- memories : doc . memoryEntries . map ( toGraphMemory ) ,
106+ memories : memoryEntries . map ( toGraphMemory ) ,
93107 }
94108}
95109
96110export function useGraphApi ( options : UseGraphApiOptions = { } ) {
97- const { containerTags, enabled = true } = options
111+ const { containerTags, documentIds, enabled = true } = options
112+ const filteredDocumentIds = documentIds ?. filter ( Boolean )
113+ const hasDocumentIds =
114+ filteredDocumentIds != null && filteredDocumentIds . length > 0
98115
99116 const {
100117 data,
@@ -104,19 +121,33 @@ export function useGraphApi(options: UseGraphApiOptions = {}) {
104121 hasNextPage,
105122 fetchNextPage,
106123 } = useInfiniteQuery < ApiDocumentsResponse , Error > ( {
107- queryKey : [ "documents-with-memories" , containerTags , [ ] ] ,
124+ queryKey : [
125+ "documents-with-memories" ,
126+ containerTags ,
127+ [ ] ,
128+ filteredDocumentIds ,
129+ ] ,
108130 initialPageParam : 1 ,
109131 queryFn : async ( { pageParam } ) => {
110- const response = await $fetch ( "@post/documents/documents" , {
111- body : {
112- page : pageParam as number ,
113- limit : PAGE_SIZE ,
114- sort : "createdAt" ,
115- order : "desc" ,
116- containerTags,
117- } ,
118- disableValidation : true ,
119- } )
132+ const response = hasDocumentIds
133+ ? await $fetch ( "@post/documents/documents/by-ids" , {
134+ body : {
135+ ids : filteredDocumentIds ,
136+ by : "id" ,
137+ containerTags,
138+ } ,
139+ disableValidation : true ,
140+ } )
141+ : await $fetch ( "@post/documents/documents" , {
142+ body : {
143+ page : pageParam as number ,
144+ limit : PAGE_SIZE ,
145+ sort : "createdAt" ,
146+ order : "desc" ,
147+ containerTags,
148+ } ,
149+ disableValidation : true ,
150+ } )
120151
121152 if ( response . error ) {
122153 throw new Error ( response . error ?. message || "Failed to fetch documents" )
@@ -134,8 +165,10 @@ export function useGraphApi(options: UseGraphApiOptions = {}) {
134165
135166 const documents = useMemo ( ( ) => {
136167 if ( ! data ?. pages ) return [ ]
137- return data . pages . flatMap ( ( page ) => page . documents . map ( toGraphDocument ) )
138- } , [ data ] )
168+ return data . pages . flatMap ( ( page ) =>
169+ page . documents . map ( ( doc ) => toGraphDocument ( doc , containerTags ) ) ,
170+ )
171+ } , [ data , containerTags ] )
139172
140173 const totalCount = data ?. pages [ 0 ] ?. pagination . totalItems ?? 0
141174
0 commit comments