// // SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // "use client" import { useEffect, useState } from "react" import type { Triple } from "@/utils/text-processing" import { GraphVisualization } from "@/components/graph-visualization" import { NvidiaIcon } from "@/components/nvidia-icon" import { ArrowLeft, AlertCircle, Network } from "lucide-react" import { GraphDataForm } from "@/components/graph-data-form" import { useRouter } from "next/navigation" export default function GraphPage() { const router = useRouter() const [triples, setTriples] = useState([]) const [documentName, setDocumentName] = useState("") const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [dataSource, setDataSource] = useState<"neo4j" | "api" | "local" | "none">("none") useEffect(() => { const loadGraphData = async () => { try { setLoading(true) setError(null) // Check URL parameters const params = new URLSearchParams(window.location.search) const graphId = params.get("id") const source = params.get("source") console.log("Loading graph with params:", { graphId, source, url: window.location.href }) // First try to load from Neo4j if available if (source !== "local") { try { console.log("Attempting to load graph data from Neo4j") const neo4jResponse = await fetch('/api/neo4j') if (neo4jResponse.ok) { const neo4jData = await neo4jResponse.json() if (neo4jData.triples && Array.isArray(neo4jData.triples) && neo4jData.triples.length > 0) { console.log("Successfully loaded graph data from Neo4j") setTriples(neo4jData.triples) setDocumentName(neo4jData.documentName || "Neo4j Graph") setDataSource("neo4j") setLoading(false) return } else { console.warn("Neo4j returned empty or invalid data, trying other methods") } } else { console.warn(`Neo4j returned status ${neo4jResponse.status}, trying other methods`) } } catch (neo4jError) { console.error("Error accessing Neo4j:", neo4jError) console.log("Continuing with other data sources") } } // If source=local is specified, use localStorage directly if (source === "local") { console.log("Using localStorage as specified in URL") return loadFromLocalStorage() } // If we have a graph ID, try to load from API if (graphId) { try { console.log("Loading graph data from API with ID:", graphId) console.log("Fetching from /api/graph-data?id=" + graphId) const response = await fetch(`/api/graph-data?id=${graphId}`) if (response.ok) { const data = await response.json() console.log("Successfully loaded graph data from API") setTriples(data.triples) setDocumentName(data.documentName) setDataSource("api") return } else { // Check if this is our special response indicating localStorage should be used let errorData; try { errorData = await response.json(); console.log("Error response data:", errorData); if (errorData.useLocalStorage) { console.log("Server indicated to use localStorage fallback") loadFromLocalStorage(); return; } } catch (jsonError) { // Response wasn't JSON, try to get text const errorText = await response.text(); console.error(`Failed to load graph data from API (${response.status}):`, errorText) } // Always try localStorage as a fallback console.log("Attempting to fall back to localStorage") try { loadFromLocalStorage() return } catch (localStorageError) { // If both API and localStorage fail, throw a more descriptive error throw new Error(`Graph data not found (ID: ${graphId}). The data may have been lost due to server restart or session expiration.`) } } } catch (apiError) { console.error("Error accessing API:", apiError) // Fall back to localStorage console.log("Falling back to localStorage due to API error") try { loadFromLocalStorage() return } catch (localStorageError) { throw new Error(`Could not load graph data: ${apiError}. Local storage fallback also failed.`) } } } else { // No graph ID, try localStorage console.log("No graph ID found, trying localStorage") loadFromLocalStorage() return } } catch (error) { console.error("Error loading graph data:", error) setError(error instanceof Error ? error.message : "Unknown error loading graph data") setTriples([]) } finally { setLoading(false) } } const loadFromLocalStorage = () => { try { // Check if localStorage is available if (typeof window === 'undefined' || !window.localStorage) { throw new Error("LocalStorage is not available in this browser") } // Check URL parameters const params = new URLSearchParams(window.location.search) const timestamp = params.get("ts") // Try timestamped version first if timestamp is provided let storedTriples = null let storedDocName = null if (timestamp) { console.log(`Looking for timestamped data with ts=${timestamp}`) storedTriples = localStorage.getItem(`graphTriples_${timestamp}`) storedDocName = localStorage.getItem(`graphDocumentName_${timestamp}`) } // Fall back to non-timestamped version if timestamped version not found if (!storedTriples) { console.log("Timestamped data not found or no timestamp provided, falling back to default keys") storedTriples = localStorage.getItem("graphTriples") storedDocName = localStorage.getItem("graphDocumentName") } if (!storedTriples) { console.warn("No triples data found in localStorage") setTriples([]) setError("No graph data found in localStorage. Please return to the application and create a new graph.") return } try { const parsedTriples = JSON.parse(storedTriples) if (!Array.isArray(parsedTriples)) { setTriples([]) throw new Error("Invalid graph data format in localStorage") } console.log(`Successfully parsed triples from localStorage: ${parsedTriples.length} items`) setTriples(parsedTriples) setDataSource("local") if (storedDocName) { setDocumentName(storedDocName) } else { setDocumentName("Unnamed Document") } } catch (parseError) { console.error("Error parsing JSON from localStorage:", parseError) setTriples([]) throw new Error("The stored graph data appears to be corrupted. Please return to the application and create a new graph.") } } catch (localStorageError) { console.error("Error loading from localStorage:", localStorageError) setTriples([]) throw localStorageError instanceof Error ? localStorageError : new Error("Failed to load graph data from localStorage") } } loadGraphData().catch((err) => { console.error("Unhandled error in loadGraphData:", err) setError(err instanceof Error ? err.message : "Unknown error loading graph data") setLoading(false) }) }, []) const handleBackClick = () => { router.push("/") } if (loading) { return (

Loading graph data...

) } if (error || triples.length === 0) { return (
txt2kg Knowledge Graph Visualization

Error Loading Graph Data

{error || "No graph data found"}

This could be due to browser storage limitations or a missing graph ID.

Alternative Methods

Method 1: Return to Main App

Go back to the main application and try opening the graph again.

Method 2: Manual Data Input

) } return (
txt2kg Knowledge Graph Visualization

Knowledge Graph: {documentName}

{dataSource === "neo4j" ? "Data from Neo4j" : dataSource === "api" ? "Data from API" : "Data from Local Storage"}
) }