import { useRef } from "react"; import { Cardinality, ObjectType, Tab } from "../../data/constants"; import { calcPath } from "../../utils/calcPath"; import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks"; export default function Relationship({ data }) { const { settings } = useSettings(); const { tables } = useDiagram(); const { layout } = useLayout(); const { selectedElement, setSelectedElement } = useSelect(); const pathRef = useRef(); let cardinalityStart = "1"; let cardinalityEnd = "1"; switch (data.cardinality) { case Cardinality.MANY_TO_ONE: cardinalityStart = "n"; cardinalityEnd = "1"; break; case Cardinality.ONE_TO_MANY: cardinalityStart = "1"; cardinalityEnd = "n"; break; case Cardinality.ONE_TO_ONE: cardinalityStart = "1"; cardinalityEnd = "1"; break; default: break; } let cardinalityStartX = 0; let cardinalityEndX = 0; let cardinalityStartY = 0; let cardinalityEndY = 0; const cardinalityOffset = 28; if (pathRef.current) { const pathLength = pathRef.current.getTotalLength(); const point1 = pathRef.current.getPointAtLength(cardinalityOffset); cardinalityStartX = point1.x; cardinalityStartY = point1.y; const point2 = pathRef.current.getPointAtLength( pathLength - cardinalityOffset, ); cardinalityEndX = point2.x; cardinalityEndY = point2.y; } const edit = () => { if (!layout.sidebar) { setSelectedElement((prev) => ({ ...prev, element: ObjectType.RELATIONSHIP, id: data.id, open: true, })); } else { setSelectedElement((prev) => ({ ...prev, currentTab: Tab.RELATIONSHIPS, element: ObjectType.RELATIONSHIP, id: data.id, open: true, })); if (selectedElement.currentTab !== Tab.RELATIONSHIPS) return; document .getElementById(`scroll_ref_${data.id}`) .scrollIntoView({ behavior: "smooth" }); } }; return ( {pathRef.current && settings.showCardinality && ( <> {cardinalityStart} {cardinalityEnd} )} ); }