2023-07-29 23:00:03 +03:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { darkTheme, GraphCanvas, GraphEdge, GraphNode, lightTheme } from 'reagraph';
|
|
|
|
|
2023-07-29 21:23:18 +03:00
|
|
|
import { useRSForm } from '../../context/RSFormContext';
|
2023-07-29 23:00:03 +03:00
|
|
|
import { useConceptTheme } from '../../context/ThemeContext';
|
|
|
|
import { resources } from '../../utils/constants';
|
2023-07-29 21:23:18 +03:00
|
|
|
|
|
|
|
function EditorTermGraph() {
|
|
|
|
const { schema } = useRSForm();
|
2023-07-29 23:00:03 +03:00
|
|
|
const { darkMode } = useConceptTheme();
|
|
|
|
|
|
|
|
const nodes: GraphNode[] = useMemo(() => {
|
|
|
|
return schema?.items.map(cst => {
|
|
|
|
return {
|
|
|
|
id: String(cst.id),
|
|
|
|
label: (cst.term.resolved || cst.term.raw) ? `${cst.alias}: ${cst.term.resolved || cst.term.raw}` : cst.alias
|
|
|
|
}}
|
|
|
|
) ?? [];
|
|
|
|
}, [schema?.items]);
|
2023-07-29 21:23:18 +03:00
|
|
|
|
2023-07-29 23:00:03 +03:00
|
|
|
const edges = useMemo(() => {
|
|
|
|
const result: GraphEdge[] = [];
|
|
|
|
let edgeID = 1;
|
|
|
|
schema?.graph.nodes.forEach(source => {
|
|
|
|
source.adjacent.forEach(target => {
|
|
|
|
result.push({
|
|
|
|
id: String(edgeID),
|
|
|
|
source: String(source.id),
|
|
|
|
target: String(target)
|
|
|
|
});
|
|
|
|
edgeID += 1;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}, [schema?.graph]);
|
2023-07-29 21:23:18 +03:00
|
|
|
|
|
|
|
return (
|
2023-07-29 23:00:03 +03:00
|
|
|
<div className='flex-wrap w-full h-full overflow-auto'>
|
|
|
|
<div className='relative border w-[1240px] h-[800px] 2xl:w-[1880px] 2xl:h-[800px]'>
|
|
|
|
<GraphCanvas
|
|
|
|
nodes={nodes}
|
|
|
|
edges={edges}
|
|
|
|
draggable
|
|
|
|
theme={darkMode ? darkTheme : lightTheme}
|
|
|
|
labelFontUrl={resources.graph_font}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-07-29 21:23:18 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-29 23:00:03 +03:00
|
|
|
|
2023-07-29 21:23:18 +03:00
|
|
|
export default EditorTermGraph;
|