"use client"; import { v4 as uuid4 } from "uuid"; // import classNames from "classnames"; import { NodeObject, LinkObject } from "react-force-graph-2d"; import { ChangeEvent, useEffect, useImperativeHandle, useRef, useState } from "react"; import { DeleteIcon } from "@/ui/Icons"; // import { FeedbackForm } from "@/ui/Partials"; import { CTAButton, Input, NeutralButton, Select } from "@/ui/elements"; interface GraphControlsProps { data?: { nodes: NodeObject[]; links: LinkObject[]; }; isAddNodeFormOpen: boolean; ref: React.RefObject; onFitIntoView: () => void; onGraphShapeChange: (shape: string) => void; } export interface GraphControlsAPI { setSelectedNode: (node: NodeObject | null) => void; getSelectedNode: () => NodeObject | null; } // type ActivityLog = { // id: string; // timestamp: number; // activity: string; // }; type NodeProperty = { id: string; name: string; value: string; }; // const formatter = new Intl.DateTimeFormat("en-GB", { dateStyle: "short", timeStyle: "medium" }); const DEFAULT_GRAPH_SHAPE = "lr"; const GRAPH_SHAPES = [{ value: "none", label: "None", }, { value: "td", label: "Top-down", }, { value: "bu", label: "Bottom-up", }, { value: "lr", label: "Left-right", }, { value: "rl", label: "Right-left", }, { value: "radialin", label: "Radial-in", }, { value: "radialout", label: "Radial-out", }]; export default function GraphControls({ data, isAddNodeFormOpen, onGraphShapeChange, onFitIntoView, ref }: GraphControlsProps) { const [selectedNode, setSelectedNode] = useState(null); const [nodeProperties, setNodeProperties] = useState([]); const [newProperty, setNewProperty] = useState({ id: uuid4(), name: "", value: "", }); const handlePropertyChange = (property: NodeProperty, property_key: string, event: ChangeEvent) => { const value = event.target.value; setNodeProperties(nodeProperties.map((nodeProperty) => (nodeProperty.id === property.id ? {...nodeProperty, [property_key]: value } : nodeProperty))); }; const handlePropertyAdd = () => { if (newProperty.name && newProperty.value) { setNodeProperties([...nodeProperties, newProperty]); setNewProperty({ id: uuid4(), name: "", value: "" }); } else { alert("Please fill in both name and value fields for the new property."); } }; const handlePropertyDelete = (property: NodeProperty) => { setNodeProperties(nodeProperties.filter((nodeProperty) => nodeProperty.id !== property.id)); }; const handleNewPropertyChange = (property: NodeProperty, property_key: string, event: ChangeEvent) => { const value = event.target.value; setNewProperty({...property, [property_key]: value }); }; useImperativeHandle(ref, () => ({ setSelectedNode, getSelectedNode: () => selectedNode, })); // const [selectedTab, setSelectedTab] = useState("nodeDetails"); const handleGraphShapeControl = (event: ChangeEvent) => { setIsAuthShapeChangeEnabled(false); onGraphShapeChange(event.target.value); }; const [isAuthShapeChangeEnabled, setIsAuthShapeChangeEnabled] = useState(true); const shapeChangeTimeout = useRef(); useEffect(() => { onGraphShapeChange(DEFAULT_GRAPH_SHAPE); const graphShapesNum = GRAPH_SHAPES.length; function switchShape(shapeIndex: number) { if (!isAuthShapeChangeEnabled || !data) { if (shapeChangeTimeout.current) { clearTimeout(shapeChangeTimeout.current); shapeChangeTimeout.current = null; } return; } shapeChangeTimeout.current = setTimeout(() => { const newValue = GRAPH_SHAPES[shapeIndex].value; onGraphShapeChange(newValue); const graphShapeSelectElement = document.getElementById("graph-shape-select") as HTMLSelectElement; graphShapeSelectElement.value = newValue; switchShape((shapeIndex + 1) % graphShapesNum); }, 5000) as unknown as number; }; switchShape(0); setTimeout(() => { onFitIntoView(); }, 500); return () => { if (shapeChangeTimeout.current) { clearTimeout(shapeChangeTimeout.current); shapeChangeTimeout.current = null; } }; }, [data, isAuthShapeChangeEnabled, onFitIntoView, onGraphShapeChange]); return ( <>
{/* */} {/* */}
{/* {selectedTab === "nodeDetails" && ( */} <>
Fit Graph into View
{isAddNodeFormOpen ? (
{}}>
Source Node ID:
{nodeProperties.map((property) => (
))}
Add
Add Node
) : ( selectedNode ? (
ID: {selectedNode.id}
Label: {selectedNode.label}
{Object.entries(selectedNode.properties).map(([key, value]) => (
{key.charAt(0).toUpperCase() + key.slice(1)}: {typeof value === "object" ? JSON.stringify(value) : value as string}
))}
{/* {}}>Edit Node */}
) : ( No node selected. ) )} {/* )} */} {/* {selectedTab === "feedback" && (
{}} />
)} */}
); }