supermemory/apps/web/components/graph-layout-view.tsx
Mahesh Sanikommu 14e688087d feat(graph): auto-paginate consumer graph + loading progress chip
Accounts with more than 100 documents previously only saw the newest
100 in the memory graph until they manually zoomed out (and even then,
only one extra page per zoom). For users with thousands of documents
this made the graph appear nearly empty.

Adds an opt-in `autoFetchAll` flag to `useGraphApi` that chains
`fetchNextPage()` until every page is loaded, and renders a top-right
progress chip ("X memories - Y/Z documents") with a spinner while pages
stream in. The chip self-hides on completion. Only the consumer
GraphLayoutView opts in; modal and chat-rail variants remain scoped to
their existing single-page behaviour.

This is a stopgap for high-memory users. A proper server-side
sampling/clustering endpoint is needed to scale past tens of thousands
of nodes and will be tracked separately.
2026-05-18 10:06:07 -07:00

87 lines
2.4 KiB
TypeScript

"use client"
import { memo, useCallback, useRef } from "react"
import { useQueryState } from "nuqs"
import Image from "next/image"
import { Share2 } from "lucide-react"
import { MemoryGraph } from "./memory-graph"
import { useProject } from "@/stores"
import { useGraphHighlights } from "@/stores/highlights"
import { Button } from "@ui/components/button"
import { cn } from "@lib/utils"
import { dmSansClassName } from "@/lib/fonts"
import { ShareModal } from "./share-modal"
import { shareParam } from "@/lib/search-params"
export const GraphLayoutView = memo(function GraphLayoutView({
onOpenDocument,
}: {
onOpenDocument?: (documentId: string) => void
}) {
const { effectiveContainerTags } = useProject()
const { documentIds: allHighlightDocumentIds } = useGraphHighlights()
const [isShareModalOpen, setIsShareModalOpen] = useQueryState(
"share",
shareParam,
)
const canvasRef = useRef<HTMLCanvasElement>(null)
const handleShare = useCallback(() => {
setIsShareModalOpen(true)
}, [setIsShareModalOpen])
const handleCloseShareModal = useCallback(() => {
setIsShareModalOpen(false)
}, [setIsShareModalOpen])
return (
<div className="relative h-full min-h-[calc(100dvh-8.5rem)] w-full md:min-h-0">
{/* Full-width graph */}
<div className="absolute inset-0">
<MemoryGraph
containerTags={effectiveContainerTags}
variant="consumer"
highlightDocumentIds={allHighlightDocumentIds}
highlightsVisible
maxNodes={undefined}
canvasRef={canvasRef}
onOpenDocument={onOpenDocument}
autoFetchAll
/>
</div>
{/* Share graph button - top left */}
<div className="absolute left-3 top-3 z-15 md:left-4 md:top-4">
<Button
variant="headers"
className={cn(
"size-10 rounded-full p-0 md:size-auto md:h-10! md:px-4",
"md:gap-2 md:text-base",
dmSansClassName(),
)}
onClick={handleShare}
aria-label="Share graph"
>
<Image
src="/icons/share-graph.svg"
alt="Share"
width={16}
height={16}
className="hidden md:block"
/>
<Share2 className="size-4 md:hidden" />
<span className="hidden md:inline">Share graph</span>
</Button>
</div>
{/* Share modal */}
<ShareModal
isOpen={isShareModalOpen}
onClose={handleCloseShareModal}
graphCanvasRef={canvasRef}
/>
</div>
)
})
GraphLayoutView.displayName = "GraphLayoutView"