11import React from "react"
2- import { IconAlignLeft , IconEdit , IconX } from "@tabler/icons-react"
32import "../type/DataTypeTypeInputComponent.style.scss"
43import { DataTypeJSONInputTreeComponent } from "./DataTypeJSONInputTreeComponent" ;
54import { DataTypeInputComponentProps } from "../DataTypeInputComponent" ;
6- import { LiteralValue , NodeFunction , NodeParameterValue } from "@code0-tech/sagittarius-graphql-types" ;
7- import { NodeBadgeComponent } from "../../badges/NodeBadgeComponent" ;
8- import { ReferenceBadgeComponent } from "../../badges/ReferenceBadgeComponent" ;
9- import {
10- Button ,
11- Card ,
12- Flex ,
13- InputDescription ,
14- InputLabel ,
15- InputMessage ,
16- Text ,
17- useService ,
18- useStore
19- } from "@code0-tech/pictor" ;
20- import { ButtonGroup } from "@code0-tech/pictor/dist/components/button-group/ButtonGroup" ;
21- import { FunctionSuggestionMenuComponent } from "@edition/function/components/suggestion/FunctionSuggestionMenuComponent" ;
5+ import { LiteralValue , NodeFunction , ReferenceValue } from "@code0-tech/sagittarius-graphql-types" ;
6+ import { InputDescription , InputLabel , Spacing } from "@code0-tech/pictor" ;
227import {
238 DataTypeJSONInputEditDialogComponent
249} from "@edition/datatype/components/inputs/json/DataTypeJSONInputEditDialogComponent" ;
25- import { FlowService } from "@edition/flow/services/Flow.service" ;
26- import { useValue } from "@edition/datatype/hooks/DataType.value.hook" ;
10+ import { DataTypeInputValueComponent } from "@edition/datatype/components/inputs/DataTypeInputValueComponent" ;
11+ import { useDebouncedCallback } from "use-debounce" ;
12+ import { DataInput } from "@code0-tech/triangulum/dist/util/schema.util" ;
2713
2814export interface EditableJSONEntry {
2915 key : string
@@ -33,21 +19,27 @@ export interface EditableJSONEntry {
3319
3420export type DataTypeJSONInputComponentProps = DataTypeInputComponentProps
3521
22+ //TODO render fallback value if undefined based on schema
3623export const DataTypeJSONInputComponent : React . FC < DataTypeJSONInputComponentProps > = ( props ) => {
3724
25+ const { schema, title, description, suggestions, formValidation, initialValue, onChange} = props
3826
39- const { schema, title, description, formValidation, onChange} = props
40-
41- const flowService = useService ( FlowService )
42- const flowStore = useStore ( FlowService )
43-
44- const suggestions = [ ]
4527 const [ editDialogOpen , setEditDialogOpen ] = React . useState ( false )
46- const [ editEntry , setEditEntry ] = React . useState < EditableJSONEntry | null > ( null )
28+ const [ editEntry , setEditEntry ] = React . useState < EditableJSONEntry | undefined > ( undefined )
4729 const [ collapsedState , setCollapsedStateRaw ] = React . useState < Record < string , boolean > > ( { } )
4830
31+ const onChangeDebounced = useDebouncedCallback ( ( value : LiteralValue | NodeFunction | ReferenceValue | undefined ) => {
32+ formValidation ?. setValue ?.( value )
33+ onChange ?.( value )
34+ } , 200 )
4935
50- const [ value , setValue ] = React . useState < NodeParameterValue | NodeFunction | null > ( null )
36+ const value = React . useMemo (
37+ ( ) => initialValue ?? ( schema . functionSchema . input === "list" ? {
38+ __typename : "LiteralValue" ,
39+ value : [ ]
40+ } as LiteralValue : generateDefaultDataValue ( schema . functionSchema as DataInput ) ) ,
41+ [ initialValue , schema ]
42+ )
5143
5244 const setCollapsedState = ( path : string [ ] , collapsed : boolean ) => {
5345 setCollapsedStateRaw ( prev => ( { ...prev , [ path . join ( "." ) ] : collapsed } ) )
@@ -65,65 +57,47 @@ export const DataTypeJSONInputComponent: React.FC<DataTypeJSONInputComponentProp
6557 key = { `edit-dialog-${ editEntry ?. path . join ( "-" ) } -${ editDialogOpen } ` }
6658 open = { editDialogOpen }
6759 entry = { editEntry }
68- value = { value as any }
60+ value = { value as LiteralValue }
6961 onOpenChange = { open => setEditDialogOpen ( open ) }
70- onObjectChange = { v => {
71- formValidation ?. setValue ?.( v )
72- setValue ( v ?? null )
73- // @ts -ignore
74- onChange ?.( )
75- } }
62+ onObjectChange = { onChangeDebounced }
7663 />
7764 ) }
7865 < InputLabel > { title } </ InputLabel >
7966 < InputDescription > { description } </ InputDescription >
80- < Card color = "secondary" paddingSize = "xs" >
81- < Flex style = { { gap : ".7rem" } } align = "center" justify = "space-between" >
82- < Flex style = { { gap : ".35rem" } } align = "center" >
83- < Text > { "Object" } </ Text >
84- </ Flex >
85- < ButtonGroup color = { "primary" } >
86- < FunctionSuggestionMenuComponent onSuggestionSelect = { suggestion => {
87- formValidation ?. setValue ?.( suggestion . value )
88- setValue ( suggestion . value )
89- // @ts -ignore
90- onChange ?.( )
91- } }
92- triggerContent = { < Button paddingSize = "xxs" variant = "filled"
93- color = "secondary"
94- onClick = { ( ) => setEditDialogOpen ( true ) } >
95- < IconAlignLeft size = { 13 } />
96- </ Button > } />
97- < Button paddingSize = "xxs"
98- variant = "filled"
99- disabled = { value ?. __typename != "LiteralValue" }
100- color = "secondary"
101- onClick = { ( ) => setEditDialogOpen ( true ) } >
102- < IconEdit size = { 13 } />
103- </ Button >
104- < Button paddingSize = "xxs" variant = "filled" color = "secondary" >
105- < IconX size = { 13 } />
106- </ Button >
107- </ ButtonGroup >
108- </ Flex >
109- < Card paddingSize = "xs" mt = { 0.7 } mb = { - 0.55 } mx = { - 0.55 } >
110- { value ?. __typename === "NodeFunction" || value ?. __typename === "NodeFunctionIdWrapper" ? (
111- < NodeBadgeComponent value = { value } />
112- ) : value ?. __typename === "ReferenceValue" ? (
113- < ReferenceBadgeComponent value = { value } />
114- ) : (
115- < DataTypeJSONInputTreeComponent
116- object = { value as any }
117- onEntryClick = { handleEntryClick }
118- collapsedState = { collapsedState }
119- setCollapsedState = { setCollapsedState }
120- />
121- ) }
122- </ Card >
123- </ Card >
124- { ! formValidation ?. valid && formValidation ?. notValidMessage && (
125- < InputMessage > { formValidation . notValidMessage } </ InputMessage >
126- ) }
67+ < DataTypeInputValueComponent inside
68+ initialValue = { value }
69+ onChange = { onChangeDebounced }
70+ suggestions = { suggestions }
71+ formValidation = { formValidation } >
72+ < DataTypeJSONInputTreeComponent
73+ object = { value as LiteralValue }
74+ onEntryClick = { handleEntryClick }
75+ collapsedState = { collapsedState }
76+ setCollapsedState = { setCollapsedState }
77+ />
78+ < Spacing spacing = { "xxs" } />
79+ </ DataTypeInputValueComponent >
12780 </ >
12881 )
12982}
83+
84+
85+ //TODO: only if required
86+ const generateDefaultDataValue = ( schema : DataInput ) : LiteralValue => {
87+ return {
88+ __typename : "LiteralValue" ,
89+ value : {
90+ ...( Object . entries ( schema . properties ?? { } ) ?. map ( ( [ key , propSchema ] ) => {
91+ if ( ! Array . isArray ( propSchema ) ) {
92+ if ( propSchema . input === "data" ) {
93+ return { [ key ] : generateDefaultDataValue ( propSchema ) . value }
94+ }
95+ if ( propSchema . input === "list" ) {
96+ return { [ key ] : [ ] }
97+ }
98+ return { [ key ] : null }
99+ }
100+ } ) )
101+ }
102+ }
103+ }
0 commit comments