From 1e361bb50a785ad70da5f02996ad12ff7e38e869 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Fri, 23 Feb 2024 22:00:53 +0100 Subject: [PATCH] Refactored just logic of IOView --- src/frontend/src/components/IOview/index.tsx | 222 ++++++++---------- .../src/components/newChatView/index.tsx | 2 +- src/frontend/src/constants/constants.ts | 2 +- 3 files changed, 105 insertions(+), 121 deletions(-) diff --git a/src/frontend/src/components/IOview/index.tsx b/src/frontend/src/components/IOview/index.tsx index a85a98ac7..57569c7e7 100644 --- a/src/frontend/src/components/IOview/index.tsx +++ b/src/frontend/src/components/IOview/index.tsx @@ -1,9 +1,7 @@ -import { ReactNode, useState } from "react"; +import { useState } from "react"; import { CHAT_FORM_DIALOG_SUBTITLE } from "../../constants/constants"; import BaseModal from "../../modals/baseModal"; import useFlowStore from "../../stores/flowStore"; -import { NodeType } from "../../types/flow"; -import { isInputType, isOutputType } from "../../utils/reactflowUtils"; import { cn } from "../../utils/utils"; import AccordionComponent from "../AccordionComponent"; import IOInputField from "../IOInputField"; @@ -14,60 +12,32 @@ import { Badge } from "../ui/badge"; import { Button } from "../ui/button"; export default function IOView({ children, open, setOpen }): JSX.Element { - const inputs = useFlowStore((state) => state.inputs); - const outputs = useFlowStore((state) => state.outputs); - const inputIds = inputs.map((obj) => obj.id); - const outputIds = outputs.map((obj) => obj.id); - const nodes = useFlowStore((state) => state.nodes); - const setNode = useFlowStore((state) => state.setNode); - const categories = getCategories(); - const [selectedCategory, setSelectedCategory] = useState(0); - const [showChat, setShowChat] = useState(false); - const [selectedView, setSelectedView] = useState<{ - type: string; - id?: string; - }>(handleInitialView()); - - type CategoriesType = { name: string; icon: string }; - - function handleInitialView() { - if ( - outputs.map((output) => output.type).includes("ChatOutput") || - inputs.map((input) => input.type).includes("ChatInput") - ) { - return { type: "ChatOutput" }; - } - return { type: "" }; - } - - function getCategories() { - const categories: CategoriesType[] = []; - if (inputs.filter((input) => input.type !== "ChatInput").length > 0) - categories.push({ name: "Inputs", icon: "TextCursorInput" }); - if (outputs.filter((output) => output.type !== "ChatOutput").length > 0) - categories.push({ name: "Outputs", icon: "TerminalSquare" }); - return categories; - } - - function handleSelectChange(): ReactNode { - const { type, id } = selectedView; - if (type === "ChatOutput") return ; - if (isInputType(type)) - return ; - if (isOutputType(type)) - return ; - else return undefined; - } - - function UpdateAccordion() { - return (categories[selectedCategory]?.name ?? "Inputs") === "Inputs" - ? inputs - : outputs; - } + const inputs = useFlowStore((state) => state.inputs).filter( + (input) => input.type !== "ChatInput" + ); + const outputs = useFlowStore((state) => state.outputs).filter( + (output) => output.type !== "ChatOutput" + ); + const nodes = useFlowStore((state) => state.nodes).filter( + (node) => + (inputs.some((input) => input.id === node.id) || + outputs.some((output) => output.id === node.id)) && + node.type !== "ChatInput" && + node.type !== "ChatOutput" + ); + const haveChat = useFlowStore((state) => state.outputs).some( + (output) => output.type === "ChatOutput" + ); + const [selectedTab, setSelectedTab] = useState( + inputs.length > 0 ? 1 : outputs.length > 0 ? 2 : 0 + ); + const [selectedViewField, setSelectedViewField] = useState< + { type: string; id: string } | undefined + >(undefined); return ( @@ -88,71 +58,72 @@ export default function IOView({ children, open, setOpen }): JSX.Element {
- {categories.map((category, index) => { - return ( - //hide chat button if chat is alredy on the view - - ); - })} + +
- {(outputs.map((output) => output.type).includes("ChatOutput") || - inputs.map((output) => output.type).includes("chatInput")) && - selectedView.type !== "ChatOutput" && ( - - )} + {selectedViewField && haveChat && ( + + )}
- {categories[selectedCategory]?.name === "Inputs" && ( + {selectedTab === 1 && ( <> Text Inputs )} - {categories[selectedCategory]?.name === "Outputs" && ( + {selectedTab === 2 && ( <> Prompt Outputs )}
- {UpdateAccordion() - .filter( - (input) => - input.type !== "ChatInput" && input.type !== "ChatOutput" + {nodes + .filter((node) => + selectedTab === 1 + ? inputs.some((input) => input.id === node.id) + : outputs.some((output) => output.id === node.id) ) - .map((input, index) => { - const node: NodeType = nodes.find( - (node) => node.id === input.id - )!; + .map((node, index) => { + const input = + selectedTab === 1 + ? inputs.find((input) => input.id === node.id)! + : outputs.find((output) => output.id === node.id)!; return (
{input.id} -
{ - event.stopPropagation(); - setSelectedView({ - type: input.type, - id: input.id, - }); - }} - > - -
+ {haveChat && ( +
{ + event.stopPropagation(); + setSelectedViewField(input); + }} + > + +
+ )}
} key={index} @@ -183,8 +153,8 @@ export default function IOView({ children, open, setOpen }): JSX.Element { >
- {node && - (categories[selectedCategory]?.name === "Inputs" ? ( + {input && + (selectedTab === 1 ? ( - {handleSelectChange() ? ( - handleSelectChange() + {haveChat ? ( + selectedViewField ? ( + inputs.some((input) => input.id === selectedViewField.id) ? ( + + ) : ( + + ) + ) : ( + + ) ) : (