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:
Cristhian Zanforlin Lousa 2025-02-12 10:56:26 -03:00 committed by GitHub
commit 7e132b37f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,13 +25,15 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
type,
}) => {
const flowPool = useFlowStore((state) => state.flowPool);
const flowPoolNode = (flowPool[nodeId] ?? [])[
(flowPool[nodeId]?.length ?? 1) - 1
];
let results: OutputLogType | LogsLogType =
(type === "Outputs"
? flowPoolNode?.data?.outputs[outputName]
: flowPoolNode?.data?.logs[outputName]) ?? {};
? flowPoolNode?.data?.outputs?.[outputName]
: flowPoolNode?.data?.logs?.[outputName]) ?? {};
const resultType = results?.type;
let resultMessage = results?.message ?? {};
const RECORD_TYPES = ["data", "object", "array", "message"];
@ -40,18 +42,20 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
}
const resultMessageMemoized = useMemo(() => {
if (!resultMessage) return "";
if (
typeof resultMessage === "string" &&
resultMessage.length > MAX_TEXT_LENGTH
) {
resultMessage = `${resultMessage.substring(0, MAX_TEXT_LENGTH)}...`;
return `${resultMessage.substring(0, MAX_TEXT_LENGTH)}...`;
}
if (Array.isArray(resultMessage)) {
resultMessage = resultMessage.map((item) => {
if (item && typeof item.data === "object") {
return resultMessage.map((item) => {
if (item?.data && typeof item?.data === "object") {
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) {
return [key, `${value.substring(0, MAX_TEXT_LENGTH)}...`];
}
@ -74,8 +78,8 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
</Case>
<Case condition={resultType === "error" || resultType === "ValueError"}>
<ErrorOutput
value={`${resultMessageMemoized.errorMessage}\n\n${resultMessageMemoized.stackTrace}`}
></ErrorOutput>
value={`${resultMessageMemoized?.errorMessage}\n\n${resultMessageMemoized?.stackTrace}`}
/>
</Case>
<Case condition={resultType === "text"}>
@ -86,10 +90,14 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
<DataOutputComponent
rows={
Array.isArray(resultMessageMemoized)
? (resultMessageMemoized as Array<any>).every((item) => item.data)
? (resultMessageMemoized as Array<any>).map((item) => item.data)
? (resultMessageMemoized as Array<any>).every(
(item) => item?.data,
)
? (resultMessageMemoized as Array<any>).map(
(item) => item?.data,
)
: resultMessageMemoized
: Object.keys(resultMessageMemoized).length > 0
: Object.keys(resultMessageMemoized)?.length > 0
? [resultMessageMemoized]
: []
}
@ -119,10 +127,10 @@ const SwitchOutputView: React.FC<SwitchOutputViewProps> = ({
<DataOutputComponent
rows={
Array.isArray(results)
? (results as Array<any>).every((item) => item.data)
? (results as Array<any>).map((item) => item.data)
? (results as Array<any>).every((item) => item?.data)
? (results as Array<any>).map((item) => item?.data)
: results
: Object.keys(results).length > 0
: Object.keys(results)?.length > 0
? [results]
: []
}