@@ -26,9 +26,15 @@ import {
2626 Text ,
2727 Textarea ,
2828 TextInput ,
29+ Tooltip ,
2930} from "@mantine/core" ;
3031import { useListState } from "@mantine/hooks" ;
31- import { IconCheck , IconPlus , IconX } from "@tabler/icons-react" ;
32+ import {
33+ IconCheck ,
34+ IconInfoCircle ,
35+ IconPlus ,
36+ IconX ,
37+ } from "@tabler/icons-react" ;
3238import { UUID } from "crypto" ;
3339import { useEffect , useState } from "react" ;
3440import { v4 as uuidv4 } from "uuid" ;
@@ -39,6 +45,7 @@ import { getSideCondition } from "@/lib/utils/rule";
3945import { RuleIdent } from "../rule/ruleParts" ;
4046import Formula from "../formula/formula" ;
4147import { cornersOfRectangle } from "@dnd-kit/core/dist/utilities/algorithms/helpers" ;
48+ import GrammarTooltip from "../grammarTooltip" ;
4249
4350const Matcher = ( ) => {
4451 const {
@@ -56,22 +63,17 @@ const Matcher = () => {
5663
5764 const current_node = nodes ?. find ( ( n ) => n . name == target ) ;
5865
59-
60-
6166 const sideConditionText = getSideCondition ( current_rule ! . name ) ;
6267
6368 const allIdentifiers = getAllIdentifiers ( current_rule ! ) ;
6469
65- const [ formulaIdentifier , formulaIdentifierHandler ] = useListState < number > (
66- allIdentifiers
67- . filter ( ( ident ) => ident . type === "Formula" )
68- . map ( ( ident ) => ident . value ) ,
69- ) ;
70- const [ elementIdentifier , elementIdentifierHandler ] = useListState < string > (
71- allIdentifiers
72- . filter ( ( ident ) => ident . type === "Element" )
73- . map ( ( ident ) => ident . value ) ,
74- ) ;
70+ const formulaIdentifier = allIdentifiers
71+ . filter ( ( ident ) => ident . type === "Formula" )
72+ . map ( ( ident ) => ident . value ) ;
73+
74+ const elementIdentifier = allIdentifiers
75+ . filter ( ( ident ) => ident . type === "Element" )
76+ . map ( ( ident ) => ident . value ) ;
7577
7678 const [ formulaMatcher , formulaMatcherHandler ] =
7779 useListState < FormulaMappingType > ( [ ] ) ;
@@ -90,19 +92,11 @@ const Matcher = () => {
9092 const [ applyError , setApplyError ] = useState < string | undefined > ( undefined ) ;
9193
9294 useEffect ( ( ) => {
93- if ( counter == undefined ) {
95+ if ( counter == undefined || allIdentifiers == undefined ) {
9496 setHighlighted ( undefined ) ;
9597 } else {
96- if ( counter < formulaIdentifier . length ) {
97- setHighlighted ( formulaIdentifier [ counter ] ) ;
98- } else if (
99- counter <
100- formulaIdentifier . length + elementIdentifier . length
101- ) {
102- setHighlighted ( elementIdentifier [ counter - formulaIdentifier . length ] ) ;
103- } else {
104- setHighlighted ( undefined ) ;
105- }
98+ let identifier = allIdentifiers [ counter ] ;
99+ setHighlighted ( identifier . value ) ;
106100 }
107101 } , [ counter ] ) ;
108102
@@ -135,6 +129,7 @@ const Matcher = () => {
135129 return ! elementMatcher . find ( ( m ) => m . from === ident . value ) ;
136130 }
137131 } ) ;
132+ console . log ( identifier_index ) ;
138133 if ( identifier_index . length == 0 ) {
139134 setCounter ( undefined ) ;
140135 return ;
@@ -161,20 +156,23 @@ const Matcher = () => {
161156
162157 const handleClick = ( f : FormulaType ) => {
163158 if ( counter == undefined ) return ;
164- if ( counter < formulaIdentifier . length ) {
165- formulaMatcherHandler . append ( { from : formulaIdentifier [ counter ] , to : f } ) ;
166- next ( ) ;
167- } else if (
168- counter >= formulaIdentifier . length &&
169- counter < formulaIdentifier . length + elementIdentifier . length
159+ let lhs_matching = allIdentifiers [ counter ] ;
160+ if (
161+ lhs_matching . type == "Element" &&
162+ f . type === "Ident" &&
163+ f . body . type === "Element"
170164 ) {
171- if ( f . type === "Ident" && f . body . type === "Element" ) {
172- elementMatcherHandler . append ( {
173- from : elementIdentifier [ counter - formulaIdentifier . length ] ,
174- to : f . body . value ,
175- } ) ;
176- next ( ) ;
177- }
165+ elementMatcherHandler . append ( {
166+ from : lhs_matching . value ,
167+ to : f . body . value ,
168+ } ) ;
169+ next ( ) ;
170+ return ;
171+ }
172+
173+ if ( lhs_matching . type === "Formula" ) {
174+ formulaMatcherHandler . append ( { from : lhs_matching . value , to : f } ) ;
175+ next ( ) ;
178176 }
179177 } ;
180178
@@ -333,12 +331,19 @@ const Matcher = () => {
333331 </ Stack >
334332
335333 < Stack >
336- < Box pt = { "xs" } >
334+ < Text maw = { 300 } >
335+ Match the highlighted part by clicking on the formula.
336+ </ Text >
337+ < Text maw = { 300 } >
338+ If you need a formula not present in your statement, use the input
339+ field.
340+ </ Text >
341+ < Center pt = { "xs" } >
337342 < Statement
338343 statement = { current_node ?. statement ! }
339344 click = { handleClick }
340345 />
341- </ Box >
346+ </ Center >
342347 < Group >
343348 < TextInput
344349 value = { customFormula }
@@ -365,7 +370,7 @@ const Matcher = () => {
365370 />
366371 ) }
367372
368- < Center pt = { "md " } >
373+ < Center pt = { "xl " } >
369374 < ButtonGroup >
370375 < Button
371376 variant = "light"
@@ -379,7 +384,13 @@ const Matcher = () => {
379384 >
380385 Clear
381386 </ Button >
382- < Button disabled = { counter != undefined } onClick = { applyRule } >
387+ < Button
388+ disabled = {
389+ counter != undefined ||
390+ ( sideConditionText != undefined && ! sideCondition )
391+ }
392+ onClick = { applyRule }
393+ >
383394 Apply
384395 </ Button >
385396 </ ButtonGroup >
0 commit comments