1- ' use client' ;
1+ " use client" ;
22
3- import { useState , useCallback } from 'react' ;
4- import { Dialog , DialogContent , DialogHeader , DialogTitle } from '@/components/ui/dialog' ;
5- import { MemoryGraph } from '@/lib/ui/memory-graph' ;
6- import { Button } from '@/components/ui/button' ;
7- import { Network } from 'lucide-react' ;
8- import type { DocumentWithMemories } from '@/lib/types/supermemory' ;
3+ import { useState , useCallback } from "react" ;
4+ import {
5+ Dialog ,
6+ DialogContent ,
7+ DialogHeader ,
8+ DialogTitle ,
9+ } from "@/components/ui/dialog" ;
10+ import { MemoryGraph } from "@supermemory/memory-graph" ;
11+ import { Button } from "@/components/ui/button" ;
12+ import { Network } from "lucide-react" ;
13+ import type { DocumentWithMemories } from "@/lib/types/supermemory" ;
914
1015interface MemoryGraphDialogProps {
1116 open ?: boolean ;
@@ -16,7 +21,7 @@ interface MemoryGraphDialogProps {
1621export function MemoryGraphDialog ( {
1722 open : controlledOpen ,
1823 onOpenChange : controlledOnOpenChange ,
19- triggerButton = false
24+ triggerButton = false ,
2025} : MemoryGraphDialogProps ) {
2126 const [ internalOpen , setInternalOpen ] = useState ( false ) ;
2227 const [ documents , setDocuments ] = useState < DocumentWithMemories [ ] > ( [ ] ) ;
@@ -32,32 +37,35 @@ export function MemoryGraphDialog({
3237 const onOpenChange = controlledOnOpenChange || setInternalOpen ;
3338
3439 // Fetch documents when dialog opens
35- const fetchDocuments = useCallback ( async ( page : number , limit : number = 500 ) => {
36- try {
37- const response = await fetch ( '/api/documents' , {
38- method : 'POST' ,
39- headers : {
40- 'Content-Type' : 'application/json' ,
41- } ,
42- body : JSON . stringify ( {
43- page,
44- limit,
45- sort : 'createdAt' ,
46- order : 'desc' ,
47- } ) ,
48- } ) ;
40+ const fetchDocuments = useCallback (
41+ async ( page : number , limit : number = 500 ) => {
42+ try {
43+ const response = await fetch ( "/api/documents" , {
44+ method : "POST" ,
45+ headers : {
46+ "Content-Type" : "application/json" ,
47+ } ,
48+ body : JSON . stringify ( {
49+ page,
50+ limit,
51+ sort : "createdAt" ,
52+ order : "desc" ,
53+ } ) ,
54+ } ) ;
4955
50- if ( ! response . ok ) {
51- throw new Error ( ' Failed to fetch documents' ) ;
52- }
56+ if ( ! response . ok ) {
57+ throw new Error ( " Failed to fetch documents" ) ;
58+ }
5359
54- const data = await response . json ( ) ;
55- return data ;
56- } catch ( err ) {
57- console . error ( 'Error fetching documents:' , err ) ;
58- throw err ;
59- }
60- } , [ ] ) ;
60+ const data = await response . json ( ) ;
61+ return data ;
62+ } catch ( err ) {
63+ console . error ( "Error fetching documents:" , err ) ;
64+ throw err ;
65+ }
66+ } ,
67+ [ ] ,
68+ ) ;
6169
6270 // Load initial documents
6371 const loadInitialDocuments = useCallback ( async ( ) => {
@@ -86,28 +94,31 @@ export function MemoryGraphDialog({
8694 const data = await fetchDocuments ( nextPage , 100 ) ;
8795
8896 if ( data . documents && data . documents . length > 0 ) {
89- setDocuments ( prev => [ ...prev , ...data . documents ] ) ;
90- setTotalLoaded ( prev => prev + data . documents . length ) ;
97+ setDocuments ( ( prev ) => [ ...prev , ...data . documents ] ) ;
98+ setTotalLoaded ( ( prev ) => prev + data . documents . length ) ;
9199 setCurrentPage ( nextPage ) ;
92100 setHasMore ( data . pagination . currentPage < data . pagination . totalPages ) ;
93101 } else {
94102 setHasMore ( false ) ;
95103 }
96104 } catch ( err ) {
97- console . error ( ' Error loading more documents:' , err ) ;
105+ console . error ( " Error loading more documents:" , err ) ;
98106 // Don't set error state for pagination failures
99107 } finally {
100108 setIsLoadingMore ( false ) ;
101109 }
102110 } , [ currentPage , hasMore , isLoadingMore , fetchDocuments ] ) ;
103111
104112 // Handle dialog open state change
105- const handleOpenChange = useCallback ( ( newOpen : boolean ) => {
106- onOpenChange ( newOpen ) ;
107- if ( newOpen && documents . length === 0 ) {
108- loadInitialDocuments ( ) ;
109- }
110- } , [ onOpenChange , documents . length , loadInitialDocuments ] ) ;
113+ const handleOpenChange = useCallback (
114+ ( newOpen : boolean ) => {
115+ onOpenChange ( newOpen ) ;
116+ if ( newOpen && documents . length === 0 ) {
117+ loadInitialDocuments ( ) ;
118+ }
119+ } ,
120+ [ onOpenChange , documents . length , loadInitialDocuments ] ,
121+ ) ;
111122
112123 // Render trigger button if requested
113124 if ( triggerButton && ! controlledOpen ) {
@@ -165,4 +176,4 @@ export function MemoryGraphDialog({
165176 </ DialogContent >
166177 </ Dialog >
167178 ) ;
168- }
179+ }
0 commit comments