From 834429cd376d1d2cd9500f2ddb744a9d76b56a55 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 4 Jul 2023 23:58:54 -0300 Subject: [PATCH] fix: add verification for formKeysData --- src/frontend/src/modals/formModal/index.tsx | 65 ++++++++++++--------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/frontend/src/modals/formModal/index.tsx b/src/frontend/src/modals/formModal/index.tsx index fd3ed98c6..bb45ee2a6 100644 --- a/src/frontend/src/modals/formModal/index.tsx +++ b/src/frontend/src/modals/formModal/index.tsx @@ -40,13 +40,26 @@ export default function FormModal({ flow: FlowType; }) { const { tabsState, setTabsState } = useContext(TabsContext); - const [chatValue, setChatValue] = useState( - tabsState[flow.id].formKeysData.input_keys[ - Object.keys(tabsState[flow.id].formKeysData.input_keys).find( - (k) => !tabsState[flow.id].formKeysData.handle_keys.some((j) => j === k) - ) - ] - ); + const [chatValue, setChatValue] = useState(() => { + try { + const { formKeysData } = tabsState[flow.id]; + if (!formKeysData) throw new Error("formKeysData is undefined"); + + const inputKeys = formKeysData.input_keys; + const handleKeys = formKeysData.handle_keys; + + const keyToUse = Object.keys(inputKeys).find( + (k) => !handleKeys.some((j) => j === k) + ); + + return inputKeys[keyToUse]; + } catch (error) { + console.error(error); + // return a sensible default or `undefined` if no default is possible + return undefined; + } + }); + const [chatHistory, setChatHistory] = useState([]); const { reactFlowInstance } = useContext(typesContext); const { setErrorData, setNoticeData } = useContext(alertContext); @@ -382,26 +395,26 @@ export default function FormModal({ Chat {CHAT_FORM_DIALOG_SUBTITLE} -
-
-
- +
+
+
+ Input Variables
-
-
+
+
Name
-
+
Chat Input @@ -413,7 +426,7 @@ export default function FormModal({
-
+
{i} @@ -438,7 +451,7 @@ export default function FormModal({
-
+
{tabsState[ id.current ].formKeysData.handle_keys.some((t) => t === i) && ( @@ -470,7 +483,7 @@ export default function FormModal({ )} {tabsState[id.current].formKeysData.memory_keys.map((i, k) => ( -
+
{i} @@ -483,14 +496,14 @@ export default function FormModal({
-
+