Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import React from "react"
import React, {useMemo} from "react"
import {IconEdit, IconX} from "@tabler/icons-react"
import "../type/DataTypeTypeInputComponent.style.scss"
import {Flow, LiteralValue} from "@code0-tech/sagittarius-graphql-types";
import {Button, Card, Flex, InputDescription, InputLabel, InputMessage, InputProps, Text} from "@code0-tech/pictor";
import {
Button,
Card,
Flex,
InputDescription,
InputLabel,
InputMessage,
InputProps,
Text,
useService,
useStore
} from "@code0-tech/pictor";
import {ButtonGroup} from "@code0-tech/pictor/dist/components/button-group/ButtonGroup";
import {
DataTypeTypeInputEditDialogComponent
} from "@edition/datatype/components/inputs/datatype/DataTypeTypeInputEditDialogComponent";
import {DataTypeTypeEditorInput} from "@edition/datatype/components/inputs/datatype/DataTypeTypeEditorInput";
import {DataTypeJSONInputTreeComponent} from "@edition/datatype/components/inputs/json/DataTypeJSONInputTreeComponent";
import {useValueExtractionAction} from "@edition/flow/components/FlowWorkerProvider";
import {DatatypeService} from "@edition/datatype/services/Datatype.service";


export interface DataTypeJSONInputComponentProps extends Omit<InputProps<any | null>, "wrapperComponent" | "type"> {
Expand All @@ -21,27 +34,43 @@ export const DataTypeTypeInputComponent: React.FC<DataTypeJSONInputComponentProp

const {initialValue, title, description, formValidation, onChange} = props

const dataTypeService = useService(DatatypeService)
const dataTypeStore = useStore(DatatypeService)
const valueFromTypeAction = useValueExtractionAction()

const [literalValue, setLiteralValue] = React.useState<string | null>(initialValue)
const [type, setType] = React.useState<string | null>(initialValue)
const [literalValue, setLiteralValue] = React.useState<LiteralValue>()
const [editDialogOpen, setEditDialogOpen] = React.useState(false)

const dataTypes = useMemo(
() => dataTypeService.values(),
[dataTypeStore]
)

const handleClear = React.useCallback(
() => {
setLiteralValue("")
},
[null]
() => setType(""),
[]
)

React.useEffect(() => {
formValidation?.setValue(literalValue)
formValidation?.setValue(type)
const timeout = setTimeout(() => {
// @ts-ignore
onChange?.()
}, 200)

if (type) {
valueFromTypeAction.execute({
type: type,
dataTypes: dataTypes
}).then(val => {
setLiteralValue(val as LiteralValue)
})
}

return () => clearTimeout(timeout);

}, [literalValue])
}, [type])

return (
<>
Expand All @@ -50,9 +79,7 @@ export const DataTypeTypeInputComponent: React.FC<DataTypeJSONInputComponentProp
open={editDialogOpen}
value={initialValue}
onOpenChange={open => setEditDialogOpen(open)}
onTypeChange={v => {
setLiteralValue(v ?? null)
}}
onTypeChange={v => setType(v ?? null)}
/>
<div>
<InputLabel>{title}</InputLabel>
Expand All @@ -75,7 +102,7 @@ export const DataTypeTypeInputComponent: React.FC<DataTypeJSONInputComponentProp
</ButtonGroup>
</Flex>
<Card paddingSize="xs" mt={0.7} mb={-0.55} mx={-0.55}>
<></>
<DataTypeJSONInputTreeComponent object={literalValue ?? {}}/>
</Card>
</Card>
{!formValidation?.valid && formValidation?.notValidMessage && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,8 @@ export const DataTypeTypeInputEditDialogComponent: React.FC<DataTypeJSONInputEdi
const typeFromValueAction = useTypeExtractionAction()
const [humanValue, setHumanValue] = useState("")
const [tsValue, setTsValue] = useState(value ?? "")
const [literalValue, setLiteralValue] = useState<LiteralValue>({
__typename: "LiteralValue",
value: {}
} as LiteralValue)
const [editVariant, setEditVariant] = useState<"manual" | "value">("manual")
const [literalValue, setLiteralValue] = useState<LiteralValue>()
const [editVariant, setEditVariant] = useState<"manual" | "value">("value")

const dataTypes = useMemo(
() => dataTypeService.values(),
Expand All @@ -414,7 +411,9 @@ export const DataTypeTypeInputEditDialogComponent: React.FC<DataTypeJSONInputEdi
valueFromTypeAction.execute({
type: initialTs,
dataTypes: dataTypes
}).then(val => setLiteralValue(val as any))
}).then(val => {
setLiteralValue(val as any)
})
}
}, [open])

Expand Down Expand Up @@ -461,12 +460,15 @@ export const DataTypeTypeInputEditDialogComponent: React.FC<DataTypeJSONInputEdi
onChange={handleSchemaChange}/>
}, [humanValue, handleSchemaChange])

const jsonInput = <Editor
showValidation={false}
language={"json"}
initialValue={literalValue.value}
onChange={handleJsonChange}
showTooltips={false}/>
const jsonInput = React.useMemo(() => {
return <Editor
key={String(literalValue)}
showValidation={true}
language={"json"}
initialValue={literalValue?.value}
onChange={handleJsonChange}
showTooltips={false}/>
}, [open, literalValue, handleJsonChange])

return (
<Dialog open={editOpen} onOpenChange={(open) => onOpenChange?.(open)}>
Expand All @@ -491,7 +493,7 @@ export const DataTypeTypeInputEditDialogComponent: React.FC<DataTypeJSONInputEdi
}>
<ResizablePanelGroup style={{borderRadius: "1rem"}}>
<ResizablePanel color="primary">
<DataTypeJSONInputTreeComponent object={literalValue}
<DataTypeJSONInputTreeComponent object={literalValue ?? {}}
onEntryClick={() => {
}}
collapsedState={{}}
Expand All @@ -508,9 +510,9 @@ export const DataTypeTypeInputEditDialogComponent: React.FC<DataTypeJSONInputEdi
value={editVariant}
onValueChange={v => v && setEditVariant(v as any)}
style={{transform: "translateX(-50%)", zIndex: 99}}>
<SegmentedControlItem value={"manual"}>
{/*<SegmentedControlItem value={"manual"}>
Schema
</SegmentedControlItem>
</SegmentedControlItem>*/}
<SegmentedControlItem value={"value"}>
From Value
</SegmentedControlItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export interface DataTypeJSONInputTreeComponentProps {
object: LiteralValue
parentKey?: string
isRoot?: boolean
onEntryClick: (entry: EditableJSONEntry) => void
collapsedState: Record<string, boolean>
setCollapsedState: (path: string[], collapsed: boolean) => void
onEntryClick?: (entry: EditableJSONEntry) => void
collapsedState?: Record<string, boolean>
setCollapsedState?: (path: string[], collapsed: boolean) => void
path?: string[]
activePath?: string[] | null
onDoubleClick?: (path: string[], isCollapsed: boolean) => void
Expand Down Expand Up @@ -40,7 +40,7 @@ export const DataTypeJSONInputTreeComponent: React.FC<DataTypeJSONInputTreeCompo
const handleClick = (entry: EditableJSONEntry) => {
if (clickTimeout.current) clearTimeout(clickTimeout.current)
clickTimeout.current = setTimeout(() => {
onEntryClick(entry)
onEntryClick?.(entry)
clickTimeout.current = null
}, CLICK_DELAY)
}
Expand All @@ -53,22 +53,22 @@ export const DataTypeJSONInputTreeComponent: React.FC<DataTypeJSONInputTreeCompo
if (onDoubleClick) {
onDoubleClick(currentPath, isCollapsed)
} else {
setCollapsedState(currentPath, !isCollapsed)
setCollapsedState?.(currentPath, !isCollapsed)
}
}

React.useEffect(() => {
const currentPath = path ?? []
const pathKey = (isRoot ? ["root"] : currentPath).join(".")
if (currentPath.length > 1 && collapsedState[pathKey] === undefined) {
setCollapsedState(currentPath.length === 0 ? ["root"] : currentPath, true)
if (currentPath.length > 1 && collapsedState?.[pathKey] === undefined) {
setCollapsedState?.(currentPath.length === 0 ? ["root"] : currentPath, true)
}
}, [path, isRoot, collapsedState, setCollapsedState])

const renderRoot = () => {
const currentPath = [...path]
const pathKey = "root"
const isCollapsed = collapsedState[pathKey] || false
const isCollapsed = collapsedState?.[pathKey] || false
const isCollapsable = typeof value === "object" && value !== null && (Array.isArray(value) ? value.length > 0 : Object.keys(value).length > 0)
const isActive = Array.isArray(activePath) && activePath.length === 0 && parentKey === undefined
const icon = isCollapsable ? (isCollapsed ? <IconChevronUp size={13}/> : <IconChevronDown size={13}/>) : null
Expand Down Expand Up @@ -98,7 +98,7 @@ export const DataTypeJSONInputTreeComponent: React.FC<DataTypeJSONInputTreeCompo
? Object.entries(value as Record<string, unknown>).map(([key, val]) => {
const currentPath = [...path, key]
const pathKey = currentPath.join(".")
const isCollapsed = collapsedState[pathKey] || false
const isCollapsed = collapsedState?.[pathKey] || false
const isActive = activePath && activePath.length > 0 && currentPath.join(".") === activePath.join(".")
const parentColorValue = parentColor ?? hashToColor("root")
const isCollapsable = typeof (val as any) === "object" && (val as any) !== null && (Array.isArray((val as any)) ? (val as any).length > 0 : Object.keys((val as any) ?? {}).length > 0)
Expand Down