Skip to content

Commit 4d9ca64

Browse files
committed
feat: better performance
1 parent a9a8980 commit 4d9ca64

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

src/packages/ce/src/function/components/files/FunctionFileTriggerComponent.tsx

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
2-
import {Alert, InputSyntaxSegment, Spacing, Text, useForm, useService, useStore} from "@code0-tech/pictor";
3-
import {Flow, LiteralValue, NodeFunction, NodeParameterValue} from "@code0-tech/sagittarius-graphql-types";
2+
import {Alert, Spacing, Text, useForm, useService, useStore} from "@code0-tech/pictor";
3+
import {Flow, LiteralValue, Namespace, NamespaceProject} from "@code0-tech/sagittarius-graphql-types";
44
import {FlowTypeService} from "@edition/flowtype/services/FlowType.service";
55
import {FlowService} from "@edition/flow/services/Flow.service";
66
import {useFlowValidation} from "@edition/flow/hooks/Flow.validation.hook";
@@ -11,33 +11,40 @@ import {
1111
FALLBACK_FLOW_TYPE_SETTING_DESCRIPTION,
1212
FALLBACK_FLOW_TYPE_SETTING_NAME
1313
} from "@core/util/fallback-translations";
14-
import {useNodes} from "@xyflow/react";
14+
import {useNodesData} from "@xyflow/react";
1515
import {NodeSchema} from "@code0-tech/triangulum";
1616

1717
export interface FunctionFileTriggerComponentProps {
18-
instance: Flow
18+
flowId: Flow['id']
19+
namespaceId: Namespace['id']
20+
projectId: NamespaceProject['id']
1921
}
2022

21-
export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponentProps> = (props) => {
23+
export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponentProps> = React.memo((props) => {
2224

23-
const {instance} = props
25+
const {flowId, namespaceId, projectId} = props
2426

2527
const flowTypeService = useService(FlowTypeService)
2628
const flowTypeStore = useStore(FlowTypeService)
2729
const flowService = useService(FlowService)
28-
const validation = useFlowValidation(instance.id)
30+
const validation = useFlowValidation(flowId)
2931
const changedValue = React.useRef(false)
30-
const [, startTransition] = React.useTransition()
32+
const changedSettings = React.useRef<Set<string>>(new Set())
33+
34+
const instance = React.useMemo(
35+
() => flowService.getById(flowId, {namespaceId, projectId}),
36+
[flowService, flowId, namespaceId, projectId]
37+
)
3138

3239
const definition = React.useMemo(
33-
() => flowTypeService.getById(instance.type?.id!!),
34-
[flowTypeStore, instance]
40+
() => flowTypeService.getById(instance?.type?.id!!),
41+
[flowTypeStore, instance?.type?.id]
3542
)
3643

3744
const initialValues: Record<string | "inputType", any> = React.useMemo(() => {
3845
const values: Record<string, any> = {}
3946
definition?.flowTypeSettings?.forEach((setting, index) => {
40-
const flowSetting = instance.settings?.nodes?.[index]
47+
const flowSetting = instance?.settings?.nodes?.[index]
4148
values[setting.id!] = {
4249
__typename: "LiteralValue",
4350
value: flowSetting?.value,
@@ -46,11 +53,11 @@ export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponent
4653
return values
4754
}, [definition])
4855

49-
const flowNode = useNodes().find(value => value.id == instance.id)
56+
const flowNode = useNodesData(flowId!)
5057

5158
const triggerValidation = React.useMemo(
5259
() => validation?.find(v => v.nodeId === null && v.parameterIndex === null),
53-
[validation]
60+
[validation?.length]
5461
)
5562

5663
const settingsValidations = React.useMemo(() => {
@@ -65,24 +72,27 @@ export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponent
6572
}
6673
})
6774
return values
68-
}, [validation, instance, definition])
75+
}, [validation?.length, definition])
6976

7077
const onSubmit = React.useCallback((values: Record<string, LiteralValue | undefined>) => {
71-
startTransition(async () => {
78+
React.startTransition(async () => {
7279
for (const flowTypeSetting of definition?.flowTypeSettings ?? []) {
7380

81+
if (!changedSettings.current.has(flowTypeSetting.id!)) continue
82+
7483
const index = definition?.flowTypeSettings?.findIndex(p => p?.id === flowTypeSetting?.id)
7584
if (typeof index !== "number") return
7685

7786
const value = values[flowTypeSetting.id!]
78-
await flowService.setSettingValue(props.instance.id, index, value?.value, flowTypeSetting.identifier)
87+
await flowService.setSettingValue(flowId, index, value?.value, flowTypeSetting.identifier)
7988

80-
changedValue.current = false
89+
changedSettings.current.delete(flowTypeSetting.id!)
8190
}
91+
changedValue.current = false
8292
})
8393
}, [flowService, definition])
8494

85-
const [inputs, validate, values] = useForm<Record<string, LiteralValue | undefined>>({
95+
const [inputs, validate] = useForm<Record<string, LiteralValue | undefined>>({
8696
initialValues: initialValues,
8797
validate: settingsValidations,
8898
truthyValidationBeforeSubmit: false,
@@ -91,16 +101,12 @@ export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponent
91101
})
92102

93103
React.useEffect(
94-
() => {
95-
if (changedValue.current)
96-
validate()
97-
},
98-
[values]
104+
() => validate(undefined, false),
105+
[validation]
99106
)
100107

101108
React.useEffect(
102-
() => validate(undefined, false),
103-
[validation]
109+
() => console.log("test")
104110
)
105111

106112
return <>
@@ -132,12 +138,17 @@ export const FunctionFileTriggerComponent: React.FC<FunctionFileTriggerComponent
132138
schema={(flowNode?.data?.schema as NodeSchema[])?.[index]}
133139
description={description}
134140
clearable
135-
onChange={() => changedValue.current = true}
141+
key={settingDefinition.id}
142+
onChange={() => {
143+
changedValue.current = true
144+
changedSettings.current.add(settingDefinition.id!)
145+
validate()
146+
}}
136147
{...inputs.getInputProps(settingDefinition.id!)}
137148
/>
138149
<Spacing spacing={"xl"}/>
139150
</div>
140151

141152
})}
142153
</>
143-
}
154+
})

0 commit comments

Comments
 (0)