-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule viewer using AstNode instead of operator (#66)
- Loading branch information
Showing
23 changed files
with
529 additions
and
1,227 deletions.
There are no files selected for viewing
46 changes: 26 additions & 20 deletions
46
packages/app-builder/src/components/Scenario/Formula/Formula.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
import { | ||
isConstantOperator, | ||
isDataFieldOperator, | ||
isMathOperator, | ||
type AstNode, | ||
isConstantNode, | ||
isIdentifier, | ||
isMathAst, | ||
isPayload, | ||
} from '@app-builder/models'; | ||
import { type Operator } from '@marble-api'; | ||
import { assertNever } from '@typescript-utils'; | ||
import { useEditorIdentifiers } from '@app-builder/services/editor'; | ||
|
||
import { NotImplemented } from './NotImplemented'; | ||
import { Constant, DataField, Math, Not } from './Operators'; | ||
import { Constant, Math } from './Operators'; | ||
import { Default } from './Operators/Default'; | ||
import { Identifier } from './Operators/Identifier'; | ||
import { Payload } from './Operators/Payload'; | ||
|
||
interface FormulaProps { | ||
formula: Operator; | ||
formula: AstNode; | ||
isRoot?: boolean; | ||
} | ||
|
||
export function Formula({ formula, isRoot = false }: FormulaProps) { | ||
if (isConstantOperator(formula)) { | ||
return <Constant operator={formula} isRoot={isRoot} />; | ||
const editorIdentifier = useEditorIdentifiers(); | ||
console.log('NAME : ', formula.name ?? ''); | ||
if (isConstantNode(formula)) { | ||
return <Constant node={formula} isRoot={isRoot} />; | ||
} | ||
console.log('NOT CONSTANT : ', formula.name ?? ''); | ||
|
||
if (isDataFieldOperator(formula)) { | ||
return <DataField operator={formula} isRoot={isRoot} />; | ||
} | ||
// if (isDataFieldOperator(formula)) { | ||
// return <DataField operator={formula} isRoot={isRoot} />; | ||
// } | ||
|
||
if (isMathOperator(formula)) { | ||
return <Math operator={formula} isRoot={isRoot} />; | ||
if (isMathAst(formula)) { | ||
return <Math node={formula} isRoot={isRoot} />; | ||
} | ||
|
||
if (formula.type === 'NOT') { | ||
return <Not operator={formula} isRoot={isRoot} />; | ||
if (isPayload(formula)) { | ||
return <Payload node={formula} isRoot={isRoot} />; | ||
} | ||
|
||
if (formula.type === 'ROUND_FLOAT') { | ||
return <NotImplemented value={JSON.stringify(formula, null, 2)} />; | ||
if (isIdentifier(formula, editorIdentifier)) { | ||
return <Identifier node={formula} isRoot={isRoot} />; | ||
} | ||
|
||
assertNever('unknwon Operator:', formula); | ||
return <Default node={formula} isRoot={isRoot} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/app-builder/src/components/Scenario/Formula/Operators/Default.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type AstNode } from '@app-builder/models'; | ||
import clsx from 'clsx'; | ||
|
||
import { Condition } from './Condition'; | ||
|
||
function DefaultValue(node: AstNode) { | ||
let value = (node.name ?? '') + '('; | ||
for (const child of node.children) { | ||
if (child.name === null) { | ||
value += child.constant ?? '' + ', '; | ||
} else { | ||
value += DefaultValue(child) + ', '; | ||
} | ||
} | ||
Object.keys(node.namedChildren).forEach((key) => { | ||
const child = node.namedChildren[key]; | ||
if (child.name === null) { | ||
const constant = (child.constant ?? '').toString(); | ||
value += key + ': ' + constant + ', '; | ||
} else { | ||
value += key + ': ' + DefaultValue(child) + ', '; | ||
} | ||
}); | ||
if (value.slice(-2) === ', ') { | ||
value = value.substring(0, value.length - 2); | ||
} | ||
value += ')'; | ||
return value; | ||
} | ||
|
||
export function Default({ node, isRoot }: { node: AstNode; isRoot?: boolean }) { | ||
return ( | ||
<Condition.Container isRoot={isRoot}> | ||
<Condition.Item isRoot={isRoot}> | ||
<span | ||
className={clsx( | ||
'text-grey-100 flex whitespace-pre text-center font-medium' | ||
)} | ||
> | ||
{DefaultValue(node)} | ||
</span> | ||
</Condition.Item> | ||
</Condition.Container> | ||
); | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/app-builder/src/components/Scenario/Formula/Operators/Identifier.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
adaptAstNodeToViewModelFromIdentifier, | ||
type AstNode, | ||
} from '@app-builder/models'; | ||
import { useEditorIdentifiers } from '@app-builder/services/editor'; | ||
import { Tooltip } from '@ui-design-system'; | ||
|
||
import { Condition } from './Condition'; | ||
|
||
interface CustomListProps { | ||
node: AstNode; | ||
isRoot?: boolean; | ||
} | ||
|
||
export function Identifier({ node, isRoot }: CustomListProps) { | ||
const editorIdentifier = useEditorIdentifiers(); | ||
const viewModel = adaptAstNodeToViewModelFromIdentifier( | ||
node, | ||
editorIdentifier | ||
); | ||
console.log(JSON.stringify(node, null, 2)); | ||
return ( | ||
<Condition.Container isRoot={isRoot}> | ||
<Condition.Item isRoot={isRoot}> | ||
{viewModel.tooltip ? ( | ||
<Tooltip.Default | ||
content={ | ||
<span className="font-medium text-purple-100"> | ||
{viewModel.tooltip} | ||
</span> | ||
} | ||
> | ||
<span | ||
// Hack to have text-ellipsis truncate beggining of the fields | ||
dir="rtl" | ||
className="max-w-[250px] overflow-hidden text-ellipsis font-medium text-purple-100 max-md:max-w-[150px]" | ||
> | ||
{viewModel.label} | ||
</span> | ||
</Tooltip.Default> | ||
) : ( | ||
<span | ||
// Hack to have text-ellipsis truncate beggining of the fields | ||
dir="rtl" | ||
className="max-w-[250px] overflow-hidden text-ellipsis font-medium text-purple-100 max-md:max-w-[150px]" | ||
> | ||
{viewModel.label} | ||
</span> | ||
)} | ||
</Condition.Item> | ||
</Condition.Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/app-builder/src/components/Scenario/Formula/Operators/Not.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.