"use client"; /** Compact depth-1 neighbourhood of a page (for the page sidebar / dashboard). Click a node to open it; link to the full graph. */ import Link from "next/link"; import { useRouter } from "next/navigation"; import { useRef, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Share2, Maximize2 } from "lucide-react"; import { api } from "@/lib/client/api"; import { cn } from "@/lib/utils"; import { Spinner } from "@/components/ui/primitives"; import type { GraphData, GraphNode } from "@/lib/graph/types"; import { GraphCanvas, type GraphCanvasHandle } from "./GraphCanvas"; export function PageGraphCard({ pageId, depth = 1, height = 220, className, title = "Connections" }: { pageId: string; depth?: 1 | 2 | 3; height?: number; className?: string; title?: string | null }) { const r = useRouter(); const canvas = useRef(null); const [hover, setHover] = useState(null); const q = useQuery({ queryKey: ["graph-neighbors", pageId, depth], queryFn: () => api(`/api/graph/neighbors?pageId=${pageId}&depth=${depth}`), staleTime: 30_000 }); const data = q.data; const others = data ? data.nodes.length - 1 : 0; return (
{title !== null &&
{title}{data ? ` · ${others} linked` : ""}{data?.stats.suggested ? ` · ${data.stats.suggested} suggested` : ""} Open graph
}
{q.isLoading ?
: !data || others === 0 ?
No connections yet — mention pages with @ or run AI suggestions from the graph.
: { if (n && n.id !== pageId && n.kind !== "tag" && n.kind !== "attachment") r.push(`/p/${n.id}`); else if (n?.kind === "attachment") window.open(`/api/files/${n.id.slice(4)}`, "_blank"); }} />} {hover && hover.id !== pageId &&
{hover.icon ? `${hover.icon} ` : ""}{hover.title}
}
); }