"use client" import { useState, useCallback } from "react" import { MemoryGraph, type DocumentWithMemories, } from "@supermemory/memory-graph" interface DocumentsResponse { documents: DocumentWithMemories[] pagination: { currentPage: number limit: number totalItems: number totalPages: number } } export default function Home() { const [apiKey, setApiKey] = useState("") const [documents, setDocuments] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isLoadingMore, setIsLoadingMore] = useState(false) const [error, setError] = useState(null) const [hasMore, setHasMore] = useState(false) const [currentPage, setCurrentPage] = useState(0) const [showGraph, setShowGraph] = useState(false) // State for controlled space selection const [selectedSpace, setSelectedSpace] = useState("all") // State for slideshow const [isSlideshowActive, setIsSlideshowActive] = useState(false) const [currentSlideshowNode, setCurrentSlideshowNode] = useState< string | null >(null) const PAGE_SIZE = 500 const fetchDocuments = useCallback( async (page: number, append = false) => { if (!apiKey) return if (page === 1) { setIsLoading(true) } else { setIsLoadingMore(true) } setError(null) try { const response = await fetch("/api/graph", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ apiKey, page, limit: PAGE_SIZE, sort: "createdAt", order: "desc", }), }) if (!response.ok) { const errorData = await response.json() throw new Error(errorData.error || "Failed to fetch documents") } const data: DocumentsResponse = await response.json() if (append) { setDocuments((prev) => [...prev, ...data.documents]) } else { setDocuments(data.documents) } setCurrentPage(data.pagination.currentPage) setHasMore(data.pagination.currentPage < data.pagination.totalPages) setShowGraph(true) } catch (err) { setError(err instanceof Error ? err : new Error("Unknown error")) } finally { setIsLoading(false) setIsLoadingMore(false) } }, [apiKey], ) const loadMoreDocuments = useCallback(async () => { if (hasMore && !isLoadingMore) { await fetchDocuments(currentPage + 1, true) } }, [hasMore, isLoadingMore, currentPage, fetchDocuments]) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (apiKey) { setDocuments([]) setCurrentPage(0) setSelectedSpace("all") fetchDocuments(1) } } // Handle space change const handleSpaceChange = useCallback((spaceId: string) => { setSelectedSpace(spaceId) }, []) // Reset to defaults const handleReset = () => { setSelectedSpace("all") } // Toggle slideshow const handleToggleSlideshow = () => { setIsSlideshowActive((prev) => !prev) } // Handle slideshow node change const handleSlideshowNodeChange = useCallback((nodeId: string | null) => { // Track which node is being shown in slideshow setCurrentSlideshowNode(nodeId) console.log("Slideshow showing node:", nodeId) }, []) // Handle slideshow stop (when user clicks outside) const handleSlideshowStop = useCallback(() => { setIsSlideshowActive(false) }, []) return (
{/* Header */}

Memory Graph Playground

Test the @supermemory/memory-graph package

setApiKey(e.target.value)} className="w-80 rounded-lg border border-zinc-700 bg-zinc-800 px-4 py-2 text-sm text-white placeholder-zinc-500 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500" />
{/* State Display Panel - For Testing */} {showGraph && (
Selected Space: {selectedSpace}
Documents: {documents.length}
)} {/* Main content */}
{!showGraph ? (

Get Started

Enter your Supermemory API key above to visualize your memory graph.

Features to test:

  • ✨ Search and filter by spaces
  • ✨ Arrow key navigation in spaces dropdown
  • Pan and zoom the graph
  • Click on nodes to see details
  • Drag nodes around
  • Filter by space
  • Pagination loads more documents
) : (

No memories found. Add some content to see your graph.

)}
) }