1- ' use client' ;
1+ " use client" ;
22
3- import { useState , useCallback , useEffect } from ' react' ;
4- import { MemoryGraph } from '@/lib/ui/ memory-graph' ;
5- import type { DocumentWithMemories } from ' @/lib/types/supermemory' ;
3+ import { useState , useCallback , useEffect } from " react" ;
4+ import { MemoryGraph } from "@supermemory/ memory-graph" ;
5+ import type { DocumentWithMemories } from " @/lib/types/supermemory" ;
66
77interface MemoryGraphViewProps {
88 isActive ?: boolean ; // Whether the profile chat is currently active
@@ -19,40 +19,51 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
1919 const [ hasInitialized , setHasInitialized ] = useState ( false ) ;
2020
2121 // Fetch documents
22- const fetchDocuments = useCallback ( async ( page : number , limit : number = 500 ) => {
23- try {
24- console . log ( '[MemoryGraph] Fetching documents - page:' , page , 'limit:' , limit ) ;
25- const response = await fetch ( '/api/documents' , {
26- method : 'POST' ,
27- headers : {
28- 'Content-Type' : 'application/json' ,
29- } ,
30- body : JSON . stringify ( {
22+ const fetchDocuments = useCallback (
23+ async ( page : number , limit : number = 500 ) => {
24+ try {
25+ console . log (
26+ "[MemoryGraph] Fetching documents - page:" ,
3127 page ,
28+ "limit:" ,
3229 limit ,
33- sort : 'createdAt' ,
34- order : 'desc' ,
35- } ) ,
36- } ) ;
37-
38- if ( ! response . ok ) {
39- throw new Error ( 'Failed to fetch documents' ) ;
30+ ) ;
31+ const response = await fetch ( "/api/documents" , {
32+ method : "POST" ,
33+ headers : {
34+ "Content-Type" : "application/json" ,
35+ } ,
36+ body : JSON . stringify ( {
37+ page,
38+ limit,
39+ sort : "createdAt" ,
40+ order : "desc" ,
41+ } ) ,
42+ } ) ;
43+
44+ if ( ! response . ok ) {
45+ throw new Error ( "Failed to fetch documents" ) ;
46+ }
47+
48+ const data = await response . json ( ) ;
49+ console . log (
50+ "[MemoryGraph] Received documents:" ,
51+ data . documents ?. length || 0 ,
52+ ) ;
53+ return data ;
54+ } catch ( err ) {
55+ console . error ( "[MemoryGraph] Error fetching documents:" , err ) ;
56+ throw err ;
4057 }
41-
42- const data = await response . json ( ) ;
43- console . log ( '[MemoryGraph] Received documents:' , data . documents ?. length || 0 ) ;
44- return data ;
45- } catch ( err ) {
46- console . error ( '[MemoryGraph] Error fetching documents:' , err ) ;
47- throw err ;
48- }
49- } , [ ] ) ;
58+ } ,
59+ [ ] ,
60+ ) ;
5061
5162 // Load initial documents when component becomes active
5263 const loadInitialDocuments = useCallback ( async ( ) => {
5364 if ( ! isActive ) return ;
5465
55- console . log ( ' [MemoryGraph] Loading initial documents...' ) ;
66+ console . log ( " [MemoryGraph] Loading initial documents..." ) ;
5667 setIsLoading ( true ) ;
5768 setError ( null ) ;
5869 try {
@@ -62,9 +73,12 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
6273 setCurrentPage ( 1 ) ;
6374 setHasMore ( data . pagination . currentPage < data . pagination . totalPages ) ;
6475 setHasInitialized ( true ) ; // Mark as initialized after first load
65- console . log ( '[MemoryGraph] Initial load complete. Total loaded:' , data . documents ?. length || 0 ) ;
76+ console . log (
77+ "[MemoryGraph] Initial load complete. Total loaded:" ,
78+ data . documents ?. length || 0 ,
79+ ) ;
6680 } catch ( err ) {
67- console . error ( ' [MemoryGraph] Initial load error:' , err ) ;
81+ console . error ( " [MemoryGraph] Initial load error:" , err ) ;
6882 setError ( err as Error ) ;
6983 setHasInitialized ( true ) ; // Mark as initialized even on error
7084 } finally {
@@ -76,32 +90,44 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
7690 const loadMoreDocuments = useCallback ( async ( ) => {
7791 if ( isLoadingMore || ! hasMore || ! isActive ) return ;
7892
79- console . log ( ' [MemoryGraph] Loading more documents...' ) ;
93+ console . log ( " [MemoryGraph] Loading more documents..." ) ;
8094 setIsLoadingMore ( true ) ;
8195 try {
8296 const nextPage = currentPage + 1 ;
8397 const data = await fetchDocuments ( nextPage , 100 ) ;
8498
8599 if ( data . documents && data . documents . length > 0 ) {
86- setDocuments ( prev => [ ...prev , ...data . documents ] ) ;
87- setTotalLoaded ( prev => prev + data . documents . length ) ;
100+ setDocuments ( ( prev ) => [ ...prev , ...data . documents ] ) ;
101+ setTotalLoaded ( ( prev ) => prev + data . documents . length ) ;
88102 setCurrentPage ( nextPage ) ;
89103 setHasMore ( data . pagination . currentPage < data . pagination . totalPages ) ;
90- console . log ( '[MemoryGraph] Loaded more. Total now:' , totalLoaded + data . documents . length ) ;
104+ console . log (
105+ "[MemoryGraph] Loaded more. Total now:" ,
106+ totalLoaded + data . documents . length ,
107+ ) ;
91108 } else {
92109 setHasMore ( false ) ;
93110 }
94111 } catch ( err ) {
95- console . error ( ' [MemoryGraph] Error loading more documents:' , err ) ;
112+ console . error ( " [MemoryGraph] Error loading more documents:" , err ) ;
96113 } finally {
97114 setIsLoadingMore ( false ) ;
98115 }
99- } , [ currentPage , hasMore , isLoadingMore , isActive , fetchDocuments , totalLoaded ] ) ;
116+ } , [
117+ currentPage ,
118+ hasMore ,
119+ isLoadingMore ,
120+ isActive ,
121+ fetchDocuments ,
122+ totalLoaded ,
123+ ] ) ;
100124
101125 // Load documents when component becomes active
102126 useEffect ( ( ) => {
103127 if ( isActive && ! hasInitialized && ! isLoading ) {
104- console . log ( '[MemoryGraph] Component became active, loading documents...' ) ;
128+ console . log (
129+ "[MemoryGraph] Component became active, loading documents..." ,
130+ ) ;
105131 loadInitialDocuments ( ) ;
106132 }
107133 } , [ isActive , hasInitialized , isLoading , loadInitialDocuments ] ) ;
@@ -112,7 +138,7 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
112138
113139 // Refresh every 60 seconds while viewing
114140 const interval = setInterval ( ( ) => {
115- console . log ( ' [MemoryGraph] Auto-refreshing documents...' ) ;
141+ console . log ( " [MemoryGraph] Auto-refreshing documents..." ) ;
116142 loadInitialDocuments ( ) ;
117143 } , 60000 ) ;
118144
@@ -125,11 +151,23 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
125151 < div className = "absolute inset-0 flex items-center justify-center" >
126152 < div className = "text-center p-8" >
127153 < div className = "text-gray-400 mb-4" >
128- < svg className = "w-16 h-16 mx-auto mb-4" fill = "none" viewBox = "0 0 24 24" stroke = "currentColor" >
129- < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = { 1.5 } d = "M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
154+ < svg
155+ className = "w-16 h-16 mx-auto mb-4"
156+ fill = "none"
157+ viewBox = "0 0 24 24"
158+ stroke = "currentColor"
159+ >
160+ < path
161+ strokeLinecap = "round"
162+ strokeLinejoin = "round"
163+ strokeWidth = { 1.5 }
164+ d = "M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
165+ />
130166 </ svg >
131167 </ div >
132- < h3 className = "text-lg font-medium text-gray-300 mb-2" > No Memories Yet</ h3 >
168+ < h3 className = "text-lg font-medium text-gray-300 mb-2" >
169+ No Memories Yet
170+ </ h3 >
133171 < p className = "text-sm text-gray-500" >
134172 Start chatting with Supermemory to create your memory graph
135173 </ p >
@@ -150,4 +188,4 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
150188 />
151189 </ div >
152190 ) ;
153- }
191+ }
0 commit comments