"use client" import type React from "react" import { useState } from "react" import type { Triple } from "@/utils/text-processing" import { GraphVisualization } from "./graph-visualization" export function GraphDataForm() { const [triples, setTriples] = useState([]) const [documentName, setDocumentName] = useState("") const [dataSubmitted, setDataSubmitted] = useState(false) const [error, setError] = useState(null) const [jsonInput, setJsonInput] = useState("") const handleSubmit = (e: React.FormEvent) => { e.preventDefault() setError(null) try { if (!jsonInput.trim()) { throw new Error("Please enter JSON data") } const parsedTriples = JSON.parse(jsonInput) if (!Array.isArray(parsedTriples)) { throw new Error("Invalid data format. Expected an array of triples.") } // Validate that each item has the required fields for (const triple of parsedTriples) { if (!triple.subject || !triple.predicate || !triple.object) { throw new Error("Each triple must have 'subject', 'predicate', and 'object' properties.") } } // Store in localStorage for persistence localStorage.setItem("graphTriples", jsonInput) if (documentName) { localStorage.setItem("graphDocumentName", documentName) } setTriples(parsedTriples) setDataSubmitted(true) } catch (err) { setError(err instanceof Error ? err.message : "Failed to parse JSON data") } } const handleJsonChange = (e: React.ChangeEvent) => { setJsonInput(e.target.value) } if (dataSubmitted) { return (

Knowledge Graph: {documentName || "Custom Data"}

) } return (

If automatic data loading failed, you can paste your triples data here in JSON format.

{error && (

{error}

)}

Expected JSON Format

The data should be an array of objects, each with "subject", "predicate", and "object" properties:

          {`[
  {
    "subject": "NVIDIA",
    "predicate": "develops",
    "object": "GPUs"
  },
  {
    "subject": "GPUs",
    "predicate": "used for",
    "object": "AI training"
  }
]`}
        

You can export this format directly from the main application using the "Export as JSON" option.

setDocumentName(e.target.value)} className="w-full bg-background border border-border rounded-lg p-3 text-foreground" placeholder="My Document" />
) }