From 40a61649df8973e1dabf5c5780457180bd57fe78 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 15 Jun 2023 14:32:08 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(chatComponent):=20add=20state?= =?UTF-8?q?=20to=20track=20if=20all=20nodes=20are=20valid=20during=20build?= =?UTF-8?q?=20trigger=20The=20build=20trigger=20component=20now=20tracks?= =?UTF-8?q?=20if=20all=20nodes=20are=20valid=20during=20the=20build=20proc?= =?UTF-8?q?ess.=20This=20is=20done=20by=20adding=20a=20state=20variable=20?= =?UTF-8?q?called=20`valid`=20which=20is=20set=20to=20true=20by=20default.?= =?UTF-8?q?=20If=20any=20node=20is=20invalid,=20the=20`valid`=20state=20is?= =?UTF-8?q?=20set=20to=20false.=20This=20ensures=20that=20the=20error=20me?= =?UTF-8?q?ssage=20is=20displayed=20when=20there=20are=20invalid=20nodes.?= =?UTF-8?q?=20The=20`streamNodeData`=20function=20now=20takes=20a=20callba?= =?UTF-8?q?ck=20function=20`onStreamComplete`=20which=20is=20called=20when?= =?UTF-8?q?=20the=20SSE=20stream=20is=20complete.=20This=20callback=20func?= =?UTF-8?q?tion=20is=20used=20to=20set=20the=20`isBuilt`=20state=20variabl?= =?UTF-8?q?e.=20=F0=9F=90=9B=20fix(chatComponent):=20fix=20bug=20where=20b?= =?UTF-8?q?uild=20trigger=20would=20not=20show=20error=20message=20when=20?= =?UTF-8?q?nodes=20are=20invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatComponent/buildTrigger/index.tsx | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx index 21e4b75a6..024fa5e18 100644 --- a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx +++ b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx @@ -25,6 +25,7 @@ export default function BuildTrigger({ const { updateSSEData } = useSSE(); const { reactFlowInstance } = useContext(typesContext); const { setErrorData } = useContext(alertContext); + const [valid, setValid] = useState(true); async function handleBuild(flow: FlowType) { try { @@ -44,7 +45,9 @@ export default function BuildTrigger({ setIsBuilding(true); try { - const allNodesValid = await streamNodeData(flow); + const allNodesValid = await streamNodeData(flow, (isAllNodesValid) => { + setIsBuilt(isAllNodesValid); + }); await enforceMinimumLoadingTime(startTime, minimumLoadingTime); setIsBuilt(allNodesValid); } catch (error) { @@ -59,15 +62,14 @@ export default function BuildTrigger({ } } - async function streamNodeData(flow: FlowType) { + async function streamNodeData(flow: FlowType, onStreamComplete) { // Make a POST request to send the flow data and receive a unique session ID const response = await postBuildInit(flow); const { flowId } = response.data; - // Use the session ID to establish an SSE connection using EventSource - let validationResults = []; const apiUrl = `/api/v1/build/stream/${flowId}`; const eventSource = new EventSource(apiUrl); + try { eventSource.onmessage = (event) => { // If the event is parseable, return @@ -78,23 +80,21 @@ export default function BuildTrigger({ // if the event is the end of the stream, close the connection if (parsedData.end_of_stream) { eventSource.close(); + onStreamComplete(valid); return; } // Otherwise, process the data - const isValid = processStreamResult(parsedData); - validationResults.push(isValid); + processStreamResult(parsedData); }; eventSource.onerror = (error) => { console.error("EventSource failed:", error); eventSource.close(); + onStreamComplete(valid); }; - // Return true if all nodes are valid, false otherwise - return validationResults.every((result) => result); } catch (e) { console.log(e); eventSource.close(); - return false; } } @@ -106,7 +106,9 @@ export default function BuildTrigger({ } catch (err) { console.log("Error parsing stream data: ", err); } - return parsedData.valid; + if (!parsedData.valid) { + setValid(false); + } } async function enforceMinimumLoadingTime(