-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathNodeOutputs.tsx
More file actions
149 lines (134 loc) · 5.64 KB
/
NodeOutputs.tsx
File metadata and controls
149 lines (134 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { NeverType, Type, evaluate } from '@chainner/navi';
import { memo, useCallback, useEffect } from 'react';
import { useContext, useContextSelector } from 'use-context-selector';
import { OutputId, OutputKind, Size } from '../../../common/common-types';
import { log } from '../../../common/log';
import { getChainnerScope } from '../../../common/types/chainner-scope';
import { ExpressionJson, fromJson } from '../../../common/types/json';
import { BackendContext } from '../../contexts/BackendContext';
import { GlobalContext, GlobalVolatileContext } from '../../contexts/GlobalNodeState';
import { NodeState } from '../../helpers/nodeState';
import { useAutomaticFeatures } from '../../hooks/useAutomaticFeatures';
import { useIsCollapsedNode } from '../../hooks/useIsCollapsedNode';
import { GenericOutput } from '../outputs/GenericOutput';
import { LargeImageOutput } from '../outputs/LargeImageOutput';
import { OutputContainer } from '../outputs/OutputContainer';
import { OutputProps, UseOutputData } from '../outputs/props';
import { TaggedOutput } from '../outputs/TaggedOutput';
const OutputComponents: Readonly<
Record<OutputKind, React.MemoExoticComponent<(props: OutputProps) => JSX.Element>>
> = {
'large-image': LargeImageOutput,
tagged: TaggedOutput,
generic: GenericOutput,
};
const OutputIsGeneric: Readonly<Record<OutputKind, boolean>> = {
'large-image': false,
tagged: false,
generic: true,
};
const NO_OUTPUT_DATA: UseOutputData<never> = { current: undefined, last: undefined, stale: false };
const evalExpression = (expression: ExpressionJson | null | undefined): Type | undefined => {
if (expression == null) return undefined;
try {
return evaluate(fromJson(expression), getChainnerScope());
} catch (error) {
log.error(error);
}
};
interface NodeOutputProps {
nodeState: NodeState;
animated: boolean;
}
export const NodeOutputs = memo(({ nodeState, animated }: NodeOutputProps) => {
const {
id,
schema,
schemaId,
outputHeight,
setOutputHeight,
nodeWidth,
setWidth,
iteratedOutputs,
} = nodeState;
const { functionDefinitions } = useContext(BackendContext);
const { setManualOutputType } = useContext(GlobalContext);
const outputDataEntry = useContextSelector(GlobalVolatileContext, (c) =>
c.outputDataMap.get(id)
);
const inputHash = useContextSelector(GlobalVolatileContext, (c) => c.inputHashes.get(id));
const stale = inputHash !== outputDataEntry?.inputHash;
const useOutputData = useCallback(
// eslint-disable-next-line prefer-arrow-functions/prefer-arrow-functions, func-names
function <T>(outputId: OutputId): UseOutputData<T> {
if (outputDataEntry) {
const last = outputDataEntry.data?.[outputId] as T | undefined;
if (last !== undefined) {
return { current: stale ? undefined : last, last, stale };
}
}
return NO_OUTPUT_DATA;
},
[outputDataEntry, stale]
);
const currentTypes = stale ? undefined : outputDataEntry?.types;
const { isAutomatic } = useAutomaticFeatures(id, schemaId);
useEffect(() => {
if (isAutomatic) {
for (const output of schema.outputs) {
const type = evalExpression(currentTypes?.[output.id]);
setManualOutputType(id, output.id, type);
}
}
}, [id, currentTypes, schema, setManualOutputType, isAutomatic]);
const isCollapsed = useIsCollapsedNode();
if (isCollapsed) {
// just need the effect for collapsed nodes, not the elements
return null;
}
const functions = functionDefinitions.get(schemaId)?.outputDefaults;
return (
<>
{schema.outputs.map((output) => {
if (schema.inputs.some((i) => i.fused?.outputId === output.id)) {
return null;
}
const definitionType = functions?.get(output.id) ?? NeverType.instance;
const type = nodeState.type.instance?.outputs.get(output.id);
const size =
outputHeight?.[output.id] && nodeWidth
? { height: outputHeight[output.id], width: nodeWidth }
: undefined;
const setSize = (newSize: Readonly<Size>) => {
setOutputHeight(output.id, newSize.height);
setWidth(newSize.width);
};
const OutputType = OutputComponents[output.kind];
return (
<OutputContainer
definitionType={definitionType}
generic={OutputIsGeneric[output.kind]}
id={id}
isConnected={nodeState.connectedOutputs.has(output.id)}
isIterated={iteratedOutputs.has(output.id)}
key={`${id}-${output.id}`}
output={output}
type={type}
>
<OutputType
animated={animated}
definitionType={definitionType}
id={id}
output={output}
schema={nodeState.schema}
setSize={setSize}
size={size}
type={type ?? NeverType.instance}
useOutputData={useOutputData}
/>
</OutputContainer>
);
})}
</>
);
});