|
| 1 | +import { Button, Dialog } from '@neo4j-ndl/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { OptionType, TupleType } from '../../../../types'; |
| 4 | +import { extractOptions, updateSourceTargetTypeOptions } from '../../../../utils/Utils'; |
| 5 | +import { useFileContext } from '../../../../context/UsersFiles'; |
| 6 | +import ImporterInput from './ImporterInput'; |
| 7 | +import SchemaViz from '../../../Graph/SchemaViz'; |
| 8 | +import PatternContainer from './PatternContainer'; |
| 9 | +import UploadJsonData from './UploadJsonData'; |
| 10 | + |
| 11 | +interface DataImporterDialogProps { |
| 12 | + open: boolean; |
| 13 | + onClose: () => void; |
| 14 | + onApply: ( |
| 15 | + patterns: string[], |
| 16 | + nodeLabels: OptionType[], |
| 17 | + relationshipLabels: OptionType[], |
| 18 | + updatedSource: OptionType[], |
| 19 | + updatedTarget: OptionType[], |
| 20 | + updatedType: OptionType[] |
| 21 | + ) => void; |
| 22 | +} |
| 23 | + |
| 24 | +const DataImporterSchemaDailog = ({ open, onClose, onApply }: DataImporterDialogProps) => { |
| 25 | + const { |
| 26 | + importerPattern, |
| 27 | + setImporterPattern, |
| 28 | + importerNodes, |
| 29 | + setImporterNodes, |
| 30 | + importerRels, |
| 31 | + setImporterRels, |
| 32 | + sourceOptions, |
| 33 | + setSourceOptions, |
| 34 | + targetOptions, |
| 35 | + setTargetOptions, |
| 36 | + typeOptions, |
| 37 | + setTypeOptions, |
| 38 | + } = useFileContext(); |
| 39 | + |
| 40 | + const [openGraphView, setOpenGraphView] = useState<boolean>(false); |
| 41 | + const [viewPoint, setViewPoint] = useState<string>(''); |
| 42 | + const handleCancel = () => { |
| 43 | + onClose(); |
| 44 | + setImporterPattern([]); |
| 45 | + setImporterNodes([]); |
| 46 | + setImporterRels([]); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleImporterCheck = async () => { |
| 50 | + const [newSourceOptions, newTargetOptions, newTypeOptions] = await updateSourceTargetTypeOptions({ |
| 51 | + patterns: importerPattern.map((label) => ({ label, value: label })), |
| 52 | + currentSourceOptions: sourceOptions, |
| 53 | + currentTargetOptions: targetOptions, |
| 54 | + currentTypeOptions: typeOptions, |
| 55 | + setSourceOptions, |
| 56 | + setTargetOptions, |
| 57 | + setTypeOptions, |
| 58 | + }); |
| 59 | + onApply(importerPattern, importerNodes, importerRels, newSourceOptions, newTargetOptions, newTypeOptions); |
| 60 | + onClose(); |
| 61 | + }; |
| 62 | + |
| 63 | + const handleRemovePattern = (patternToRemove: string) => { |
| 64 | + const updatedPatterns = importerPattern.filter((p) => p !== patternToRemove); |
| 65 | + if (updatedPatterns.length === 0) { |
| 66 | + setImporterPattern([]); |
| 67 | + setImporterNodes([]); |
| 68 | + setImporterRels([]); |
| 69 | + return; |
| 70 | + } |
| 71 | + const updatedTuples: TupleType[] = updatedPatterns |
| 72 | + .map((item: string) => { |
| 73 | + const matchResult = item.match(/^(.+?)-\[:([A-Z_]+)\]->(.+)$/); |
| 74 | + if (matchResult) { |
| 75 | + const [source, rel, target] = matchResult.slice(1).map((s) => s.trim()); |
| 76 | + return { |
| 77 | + value: `${source},${rel},${target}`, |
| 78 | + label: `${source} -[:${rel}]-> ${target}`, |
| 79 | + source, |
| 80 | + target, |
| 81 | + type: rel, |
| 82 | + }; |
| 83 | + } |
| 84 | + return null; |
| 85 | + }) |
| 86 | + .filter(Boolean) as TupleType[]; |
| 87 | + const { nodeLabelOptions, relationshipTypeOptions } = extractOptions(updatedTuples); |
| 88 | + setImporterPattern(updatedPatterns); |
| 89 | + setImporterNodes(nodeLabelOptions); |
| 90 | + setImporterRels(relationshipTypeOptions); |
| 91 | + }; |
| 92 | + |
| 93 | + const handleSchemaView = () => { |
| 94 | + setOpenGraphView(true); |
| 95 | + setViewPoint('showSchemaView'); |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <> |
| 100 | + <Dialog isOpen={open} onClose={handleCancel}> |
| 101 | + <Dialog.Header>Entity Graph Extraction Settings</Dialog.Header> |
| 102 | + <Dialog.Content className='n-flex n-flex-col n-gap-token-6 p-6'> |
| 103 | + <ImporterInput /> |
| 104 | + <UploadJsonData |
| 105 | + onSchemaExtracted={({ nodeLabels, relationshipTypes, relationshipObjectTypes, nodeObjectTypes }) => { |
| 106 | + const nodeLabelMap = Object.fromEntries(nodeLabels.map((n) => [n.$id, n.token])); |
| 107 | + const relTypeMap = Object.fromEntries(relationshipTypes.map((r) => [r.$id, r.token])); |
| 108 | + const nodeIdToLabel: Record<string, string> = {}; |
| 109 | + nodeObjectTypes.forEach((nodeObj: any) => { |
| 110 | + const labelRef = nodeObj.labels?.[0]?.$ref; |
| 111 | + if (labelRef && nodeLabelMap[labelRef.slice(1)]) { |
| 112 | + nodeIdToLabel[nodeObj.$id] = nodeLabelMap[labelRef.slice(1)]; |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + const patterns = relationshipObjectTypes.map((relObj) => { |
| 117 | + const fromId = relObj.from.$ref.slice(1); |
| 118 | + const toId = relObj.to.$ref.slice(1); |
| 119 | + const relId = relObj.type.$ref.slice(1); |
| 120 | + const fromLabel = nodeIdToLabel[fromId] || 'source'; |
| 121 | + const toLabel = nodeIdToLabel[toId] || 'target'; |
| 122 | + const relLabel = relTypeMap[relId] || 'type'; |
| 123 | + const pattern = `${fromLabel} -[:${relLabel}]-> ${toLabel}`; |
| 124 | + return pattern; |
| 125 | + }); |
| 126 | + |
| 127 | + const importerTuples = patterns |
| 128 | + .map((p) => { |
| 129 | + const match = p.match(/^(.+?) -\[:(.+?)\]-> (.+)$/); |
| 130 | + if (!match) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + const [_, source, type, target] = match; |
| 134 | + return { |
| 135 | + label: `${source} -[:${type}]-> ${target}`, |
| 136 | + value: `${source},${type},${target}`, |
| 137 | + source, |
| 138 | + target, |
| 139 | + type, |
| 140 | + }; |
| 141 | + }) |
| 142 | + .filter(Boolean) as TupleType[]; |
| 143 | + const { nodeLabelOptions, relationshipTypeOptions } = extractOptions(importerTuples); |
| 144 | + setImporterNodes(nodeLabelOptions); |
| 145 | + setImporterRels(relationshipTypeOptions); |
| 146 | + setImporterPattern(patterns); |
| 147 | + }} |
| 148 | + /> |
| 149 | + <PatternContainer |
| 150 | + pattern={importerPattern} |
| 151 | + handleRemove={handleRemovePattern} |
| 152 | + handleSchemaView={handleSchemaView} |
| 153 | + nodes={importerNodes} |
| 154 | + rels={importerRels} |
| 155 | + /> |
| 156 | + <Dialog.Actions className='n-flex n-justify-end n-gap-token-4 pt-4'> |
| 157 | + <Button onClick={handleCancel} isDisabled={importerPattern.length === 0}> |
| 158 | + Cancel |
| 159 | + </Button> |
| 160 | + <Button onClick={handleImporterCheck} isDisabled={importerPattern.length === 0}> |
| 161 | + Apply |
| 162 | + </Button> |
| 163 | + </Dialog.Actions> |
| 164 | + </Dialog.Content> |
| 165 | + </Dialog> |
| 166 | + {openGraphView && ( |
| 167 | + <SchemaViz |
| 168 | + open={openGraphView} |
| 169 | + setGraphViewOpen={setOpenGraphView} |
| 170 | + viewPoint={viewPoint} |
| 171 | + nodeValues={importerNodes ?? []} |
| 172 | + relationshipValues={importerRels ?? []} |
| 173 | + /> |
| 174 | + )} |
| 175 | + </> |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +export default DataImporterSchemaDailog; |
0 commit comments