fix: add optional chaining and improve code robustness (#6262)
🔧 (switchOutputView/index.tsx): Fix issue with accessing nested properties in flowPoolNode data object 🔧 (switchOutputView/index.tsx): Fix issue with accessing nested properties in resultMessage object
This commit is contained in:
parent
ffbc97bfc9
commit
7e132b37f4
1 changed files with 22 additions and 14 deletions
|
|
@ -25,13 +25,15 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
|
||||||
type,
|
type,
|
||||||
}) => {
|
}) => {
|
||||||
const flowPool = useFlowStore((state) => state.flowPool);
|
const flowPool = useFlowStore((state) => state.flowPool);
|
||||||
|
|
||||||
const flowPoolNode = (flowPool[nodeId] ?? [])[
|
const flowPoolNode = (flowPool[nodeId] ?? [])[
|
||||||
(flowPool[nodeId]?.length ?? 1) - 1
|
(flowPool[nodeId]?.length ?? 1) - 1
|
||||||
];
|
];
|
||||||
|
|
||||||
let results: OutputLogType | LogsLogType =
|
let results: OutputLogType | LogsLogType =
|
||||||
(type === "Outputs"
|
(type === "Outputs"
|
||||||
? flowPoolNode?.data?.outputs[outputName]
|
? flowPoolNode?.data?.outputs?.[outputName]
|
||||||
: flowPoolNode?.data?.logs[outputName]) ?? {};
|
: flowPoolNode?.data?.logs?.[outputName]) ?? {};
|
||||||
const resultType = results?.type;
|
const resultType = results?.type;
|
||||||
let resultMessage = results?.message ?? {};
|
let resultMessage = results?.message ?? {};
|
||||||
const RECORD_TYPES = ["data", "object", "array", "message"];
|
const RECORD_TYPES = ["data", "object", "array", "message"];
|
||||||
|
|
@ -40,18 +42,20 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultMessageMemoized = useMemo(() => {
|
const resultMessageMemoized = useMemo(() => {
|
||||||
|
if (!resultMessage) return "";
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof resultMessage === "string" &&
|
typeof resultMessage === "string" &&
|
||||||
resultMessage.length > MAX_TEXT_LENGTH
|
resultMessage.length > MAX_TEXT_LENGTH
|
||||||
) {
|
) {
|
||||||
resultMessage = `${resultMessage.substring(0, MAX_TEXT_LENGTH)}...`;
|
return `${resultMessage.substring(0, MAX_TEXT_LENGTH)}...`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(resultMessage)) {
|
if (Array.isArray(resultMessage)) {
|
||||||
resultMessage = resultMessage.map((item) => {
|
return resultMessage.map((item) => {
|
||||||
if (item && typeof item.data === "object") {
|
if (item?.data && typeof item?.data === "object") {
|
||||||
const truncatedData = Object.fromEntries(
|
const truncatedData = Object.fromEntries(
|
||||||
Object.entries(item.data).map(([key, value]) => {
|
Object.entries(item?.data).map(([key, value]) => {
|
||||||
if (typeof value === "string" && value.length > MAX_TEXT_LENGTH) {
|
if (typeof value === "string" && value.length > MAX_TEXT_LENGTH) {
|
||||||
return [key, `${value.substring(0, MAX_TEXT_LENGTH)}...`];
|
return [key, `${value.substring(0, MAX_TEXT_LENGTH)}...`];
|
||||||
}
|
}
|
||||||
|
|
@ -74,8 +78,8 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
|
||||||
</Case>
|
</Case>
|
||||||
<Case condition={resultType === "error" || resultType === "ValueError"}>
|
<Case condition={resultType === "error" || resultType === "ValueError"}>
|
||||||
<ErrorOutput
|
<ErrorOutput
|
||||||
value={`${resultMessageMemoized.errorMessage}\n\n${resultMessageMemoized.stackTrace}`}
|
value={`${resultMessageMemoized?.errorMessage}\n\n${resultMessageMemoized?.stackTrace}`}
|
||||||
></ErrorOutput>
|
/>
|
||||||
</Case>
|
</Case>
|
||||||
|
|
||||||
<Case condition={resultType === "text"}>
|
<Case condition={resultType === "text"}>
|
||||||
|
|
@ -86,10 +90,14 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
|
||||||
<DataOutputComponent
|
<DataOutputComponent
|
||||||
rows={
|
rows={
|
||||||
Array.isArray(resultMessageMemoized)
|
Array.isArray(resultMessageMemoized)
|
||||||
? (resultMessageMemoized as Array<any>).every((item) => item.data)
|
? (resultMessageMemoized as Array<any>).every(
|
||||||
? (resultMessageMemoized as Array<any>).map((item) => item.data)
|
(item) => item?.data,
|
||||||
|
)
|
||||||
|
? (resultMessageMemoized as Array<any>).map(
|
||||||
|
(item) => item?.data,
|
||||||
|
)
|
||||||
: resultMessageMemoized
|
: resultMessageMemoized
|
||||||
: Object.keys(resultMessageMemoized).length > 0
|
: Object.keys(resultMessageMemoized)?.length > 0
|
||||||
? [resultMessageMemoized]
|
? [resultMessageMemoized]
|
||||||
: []
|
: []
|
||||||
}
|
}
|
||||||
|
|
@ -119,10 +127,10 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
|
||||||
<DataOutputComponent
|
<DataOutputComponent
|
||||||
rows={
|
rows={
|
||||||
Array.isArray(results)
|
Array.isArray(results)
|
||||||
? (results as Array<any>).every((item) => item.data)
|
? (results as Array<any>).every((item) => item?.data)
|
||||||
? (results as Array<any>).map((item) => item.data)
|
? (results as Array<any>).map((item) => item?.data)
|
||||||
: results
|
: results
|
||||||
: Object.keys(results).length > 0
|
: Object.keys(results)?.length > 0
|
||||||
? [results]
|
? [results]
|
||||||
: []
|
: []
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue