refactoring data passed by the event

This commit is contained in:
Lucas Oliveira 2023-02-19 23:26:41 -03:00
commit 5ead7c4c32
7 changed files with 57 additions and 54 deletions

View file

@ -3,17 +3,19 @@ import { Input } from "@mui/material";
import { Handle, Position } from "reactflow"; import { Handle, Position } from "reactflow";
import { isValidConnection, nodeColors } from "../../utils"; import { isValidConnection, nodeColors } from "../../utils";
import ToggleComponent from "../../components/toggleComponent"; import ToggleComponent from "../../components/toggleComponent";
import { useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import { typesContext } from "../../contexts/typesContext";
export default function BooleanNode({ data }) { export default function BooleanNode({ data }) {
const [enabled, setEnabled] = useState(false); const [enabled, setEnabled] = useState(false);
const {types} = useContext(typesContext);
return ( return (
<div className="prompt-node relative bg-white rounded-lg solid border flex flex-col justify-center"> <div className="prompt-node relative bg-white rounded-lg solid border flex flex-col justify-center">
<div className="w-full flex items-center justify-between gap-8 p-4 bg-gray-50 border-b "> <div className="w-full flex items-center justify-between gap-8 p-4 bg-gray-50 border-b ">
<div className="flex items-center gap-4 text-lg"> <div className="flex items-center gap-4 text-lg">
<CheckCircleIcon <CheckCircleIcon
className="w-10 h-10 p-1 rounded" className="w-10 h-10 p-1 rounded"
style={{ color: nodeColors[data.type] }} style={{ color: nodeColors[types[data.type]] }}
/> />
Boolean Boolean
</div> </div>
@ -26,15 +28,15 @@ export default function BooleanNode({ data }) {
</button> </button>
</div> </div>
<div className="w-full flex justify-center p-5 h-full"> <div className="w-full flex justify-center p-5 h-full">
<ToggleComponent enabled={enabled} disabled={false} setEnabled={(x) => {setEnabled(x); data.enabled = x}} /> <ToggleComponent enabled={enabled} disabled={false} setEnabled={(x) => {setEnabled(x); data.value = x}} />
</div> </div>
<Handle <Handle
type="target" type="target"
position={Position.Right} position={Position.Right}
id={data.name} id={data.type}
isValidConnection={(connection) => isValidConnection(data,connection)} isValidConnection={(connection) => isValidConnection(data,connection)}
className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none" className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none"
style={{ borderLeftColor: nodeColors[data.type] }} style={{ borderLeftColor: nodeColors[types[data.type]] }}
></Handle> ></Handle>
</div> </div>
); );

View file

@ -3,10 +3,13 @@ import InputComponent from "../../components/inputComponent";
import { isValidConnection, nodeColors, snakeToNormalCase } from "../../utils"; import { isValidConnection, nodeColors, snakeToNormalCase } from "../../utils";
import { Handle, Position } from "reactflow"; import { Handle, Position } from "reactflow";
import Tooltip from "../../components/TooltipComponent"; import Tooltip from "../../components/TooltipComponent";
import { typesContext } from "../../contexts/typesContext";
import { useContext } from "react";
export default function ChatInputNode({ data }) { export default function ChatInputNode({ data }) {
const {types} = useContext(typesContext);
return ( return (
<div className="prompt-node relative rounded-lg solid border flex justify-center align-center py-3 px-6 bg-gray-50" style={{color: nodeColors['chat']}}> <div className="prompt-node relative rounded-lg solid border flex justify-center align-center py-3 px-6 bg-gray-50" style={{color: nodeColors[types[data.type]]}}>
<Tooltip title="Prefix: str"> <Tooltip title="Prefix: str">
<Handle <Handle
type="source" type="source"
@ -16,7 +19,7 @@ export default function ChatInputNode({ data }) {
isValidConnection(data, connection) isValidConnection(data, connection)
} }
className="ml-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none" className="ml-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none"
style={{ borderLeftColor: nodeColors['chat'] }} style={{ borderLeftColor: nodeColors[types[data.type]] }}
></Handle> ></Handle>
</Tooltip> </Tooltip>
<Tooltip title={"Message: str"}> <Tooltip title={"Message: str"}>
@ -26,10 +29,10 @@ export default function ChatInputNode({ data }) {
id={'str|str|'+data.id} id={'str|str|'+data.id}
isValidConnection={(connection) => isValidConnection(data,connection)} isValidConnection={(connection) => isValidConnection(data,connection)}
className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none" className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none"
style={{borderLeftColor: nodeColors['chat']}} style={{borderLeftColor: nodeColors[types[data.type]]}}
></Handle> ></Handle>
</Tooltip> </Tooltip>
<div className="flex gap-3 text-lg font-medium items-center" style={{color: nodeColors['chat']}}> <div className="flex gap-3 text-lg font-medium items-center" style={{color: nodeColors[types[data.type]]}}>
<Bars3CenterLeftIcon className="h-8 w-8 mt-1" /> <Bars3CenterLeftIcon className="h-8 w-8 mt-1" />
Input Input
</div> </div>

View file

@ -3,10 +3,13 @@ import { Handle, Position } from "reactflow";
import InputComponent from "../../components/inputComponent"; import InputComponent from "../../components/inputComponent";
import { isValidConnection, nodeColors, snakeToNormalCase } from "../../utils"; import { isValidConnection, nodeColors, snakeToNormalCase } from "../../utils";
import Tooltip from "../../components/TooltipComponent"; import Tooltip from "../../components/TooltipComponent";
import { useContext } from "react";
import { typesContext } from "../../contexts/typesContext";
export default function ChatOutputNode({ data }) { export default function ChatOutputNode({ data }) {
const {types} = useContext(typesContext);
return ( return (
<div className="prompt-node relative rounded-lg solid border flex justify-center align-center py-3 px-6 bg-gray-50" style={{color: nodeColors['chat']}}> <div className="prompt-node relative rounded-lg solid border flex justify-center align-center py-3 px-6 bg-gray-50" style={{color: nodeColors[types[data.type]]}}>
<Tooltip title="Message: str"> <Tooltip title="Message: str">
<Handle <Handle
type="source" type="source"
@ -18,7 +21,7 @@ export default function ChatOutputNode({ data }) {
></Handle> ></Handle>
</Tooltip> </Tooltip>
<div className="flex gap-3 text-lg font-medium items-center" style={{color: nodeColors['chat']}}> <div className="flex gap-3 text-lg font-medium items-center" style={{color: nodeColors[types[data.type]]}}>
Output Output
<Bars3CenterLeftIcon className="h-8 w-8 mt-1" /> <Bars3CenterLeftIcon className="h-8 w-8 mt-1" />
</div> </div>

View file

@ -8,11 +8,11 @@ import {
} from "../../utils"; } from "../../utils";
import ParameterComponent from "./components/parameterComponent"; import ParameterComponent from "./components/parameterComponent";
import { typesContext } from "../../contexts/typesContext"; import { typesContext } from "../../contexts/typesContext";
import { useContext } from "react"; import { useContext, useEffect } from "react";
export default function GenericNode({ data }) { export default function GenericNode({ data }) {
const Icon = nodeIcons[data.type];
const {types} = useContext(typesContext); const {types} = useContext(typesContext);
const Icon = nodeIcons[types[data.type]];
return ( return (
<div className="prompt-node relative bg-white w-96 rounded-lg solid border flex flex-col justify-center"> <div className="prompt-node relative bg-white w-96 rounded-lg solid border flex flex-col justify-center">
@ -20,9 +20,9 @@ export default function GenericNode({ data }) {
<div className="w-full flex items-center truncate gap-4 text-lg"> <div className="w-full flex items-center truncate gap-4 text-lg">
<Icon <Icon
className="w-10 h-10 p-1 rounded" className="w-10 h-10 p-1 rounded"
style={{ color: nodeColors[data.type] }} style={{ color: nodeColors[types[data.type]] }}
/> />
<div className="truncate">{data.name}</div> <div className="truncate">{data.type}</div>
</div> </div>
<button onClick={data.onDelete}> <button onClick={data.onDelete}>
<TrashIcon className="w-6 h-6 hover:text-red-500"></TrashIcon> <TrashIcon className="w-6 h-6 hover:text-red-500"></TrashIcon>
@ -74,10 +74,10 @@ export default function GenericNode({ data }) {
<div className="px-5 py-2 mt-2 text-center">Output:</div> <div className="px-5 py-2 mt-2 text-center">Output:</div>
<ParameterComponent <ParameterComponent
data={data} data={data}
color={nodeColors[data.type]} color={nodeColors[types[data.type]]}
title={data.name} title={data.type}
tooltipTitle={"Type: str"} tooltipTitle={"Type: str"}
id={data.name + "|" + data.id + data.node.base_classes.map((b) => ("|" + b))} id={data.type + "|" + data.id + data.node.base_classes.map((b) => ("|" + b))}
type={'str'} type={'str'}
left={false} left={false}
/> />

View file

@ -7,10 +7,12 @@ import {
snakeToNormalCase, snakeToNormalCase,
} from "../../utils"; } from "../../utils";
import { Handle, Position } from "reactflow"; import { Handle, Position } from "reactflow";
import { useEffect } from "react"; import { useContext, useEffect } from "react";
import Tooltip from "../../components/TooltipComponent"; import Tooltip from "../../components/TooltipComponent";
import { typesContext } from "../../contexts/typesContext";
export default function InputNode({ data }) { export default function InputNode({ data }) {
const {types} = useContext(typesContext);
return ( return (
<div className="prompt-node relative bg-white w-96 rounded-lg solid border flex flex-col justify-center"> <div className="prompt-node relative bg-white w-96 rounded-lg solid border flex flex-col justify-center">
<Tooltip title="Prefix: str"> <Tooltip title="Prefix: str">
@ -22,7 +24,7 @@ export default function InputNode({ data }) {
isValidConnection(data, connection) isValidConnection(data, connection)
} }
className="ml-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none" className="ml-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none"
style={{ borderLeftColor: nodeColors[data.type] }} style={{ borderLeftColor: nodeColors[types[data.type]] }}
></Handle> ></Handle>
</Tooltip> </Tooltip>
@ -30,7 +32,7 @@ export default function InputNode({ data }) {
<div className="flex items-center gap-4 text-lg"> <div className="flex items-center gap-4 text-lg">
<Bars3CenterLeftIcon <Bars3CenterLeftIcon
className="w-10 h-10 p-1 rounded" className="w-10 h-10 p-1 rounded"
style={{ color: nodeColors[data.type] }} style={{ color: nodeColors[types[data.type]] }}
/> />
String String
</div> </div>
@ -47,17 +49,17 @@ export default function InputNode({ data }) {
disabled={false} disabled={false}
value="" value=""
onChange={(e) => { onChange={(e) => {
data.text = e; data.value = e;
}} }}
/> />
</div> </div>
<Handle <Handle
type="target" type="target"
position={Position.Right} position={Position.Right}
id={data.name} id={data.type}
isValidConnection={(connection) => isValidConnection(data, connection)} isValidConnection={(connection) => isValidConnection(data, connection)}
className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none" className="-mr-1 bg-transparent border-solid border-l-8 border-y-transparent border-y-8 border-r-0 rounded-none"
style={{ borderLeftColor: nodeColors[data.type] }} style={{ borderLeftColor: nodeColors[types[data.type]] }}
></Handle> ></Handle>
</div> </div>
); );

View file

@ -17,18 +17,12 @@ export default function ExtraSidebar() {
useEffect(() => { useEffect(() => {
getAll().then((d) => { getAll().then((d) => {
setData(d.data); setData(d.data);
// console.log(d.data);
});
}, []);
useEffect(() => {
if(data){
setTypes( setTypes(
Object.keys(data).reduce( Object.keys(d.data).reduce(
(acc, curr) => { (acc, curr) => {
Object.keys(data[curr]).forEach((c) => { Object.keys(d.data[curr]).forEach((c) => {
acc[c] = curr; acc[c] = curr;
data[curr][c].base_classes?.forEach((b) => { d.data[curr][c].base_classes?.forEach((b) => {
acc[b] = curr; acc[b] = curr;
}); });
}); });
@ -43,8 +37,8 @@ export default function ExtraSidebar() {
} }
) )
); );
} });
}, [data, setTypes]) }, []);
function onDragStart(event: React.DragEvent<any>, data) { function onDragStart(event: React.DragEvent<any>, data) {
event.dataTransfer.effectAllowed = "move"; event.dataTransfer.effectAllowed = "move";
@ -67,8 +61,7 @@ export default function ExtraSidebar() {
style={{ borderLeftColor: nodeColors[d] }} style={{ borderLeftColor: nodeColors[d] }}
onDragStart={(event) => onDragStart={(event) =>
onDragStart(event, { onDragStart(event, {
type: d, type: t,
name: t,
node: data[d][t], node: data[d][t],
}) })
} }
@ -94,8 +87,8 @@ export default function ExtraSidebar() {
style={{ borderLeftColor: nodeColors["chat"] }} style={{ borderLeftColor: nodeColors["chat"] }}
onDragStart={(event) => onDragStart={(event) =>
onDragStart(event, { onDragStart(event, {
type: "chat", type: "chatInput",
name: "chatInput", node: {},
}) })
} }
> >
@ -112,8 +105,8 @@ export default function ExtraSidebar() {
style={{ borderLeftColor: nodeColors["chat"] }} style={{ borderLeftColor: nodeColors["chat"] }}
onDragStart={(event) => onDragStart={(event) =>
onDragStart(event, { onDragStart(event, {
type: "chat", type: "chatOutput",
name: "chatOutput", node: {},
}) })
} }
> >
@ -136,8 +129,8 @@ export default function ExtraSidebar() {
style={{ borderLeftColor: nodeColors["advanced"] }} style={{ borderLeftColor: nodeColors["advanced"] }}
onDragStart={(event) => onDragStart={(event) =>
onDragStart(event, { onDragStart(event, {
type: "advanced", type: "str",
name: "str", node: {},
}) })
} }
> >
@ -154,8 +147,8 @@ export default function ExtraSidebar() {
style={{ borderLeftColor: nodeColors["advanced"] }} style={{ borderLeftColor: nodeColors["advanced"] }}
onDragStart={(event) => onDragStart={(event) =>
onDragStart(event, { onDragStart(event, {
type: "advanced", type: "bool",
name: "bool", node: {},
}) })
} }
> >

View file

@ -80,8 +80,8 @@ export default function FlowPage() {
const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect(); const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect();
let data = JSON.parse(event.dataTransfer.getData("json")); let data = JSON.parse(event.dataTransfer.getData("json"));
if ( if (
data.name !== "chatInput" || data.type !== "chatInput" ||
(data.name === "chatInput" && (data.type === "chatInput" &&
!reactFlowInstance.getNodes().some((n) => n.type === "chatInputNode")) !reactFlowInstance.getNodes().some((n) => n.type === "chatInputNode"))
) { ) {
const position = reactFlowInstance.project({ const position = reactFlowInstance.project({
@ -89,24 +89,24 @@ export default function FlowPage() {
y: event.clientY - reactflowBounds.top, y: event.clientY - reactflowBounds.top,
}); });
let newId = getId(); let newId = getId();
const newNode = { const newNode = {
id: newId, id: newId,
type: type:
data.name === "str" (data.type === "str"
? "inputNode" ? "inputNode"
: data.name === "chatInput" : (data.type === "chatInput"
? "chatInputNode" ? "chatInputNode"
: data.name === "chatOutput" : (data.type === "chatOutput"
? "chatOutputNode" ? "chatOutputNode"
: data.name === "bool" : (data.type === "bool"
? "booleanNode" ? "booleanNode"
: "genericNode", : "genericNode")))),
position, position,
data: { data: {
...data, ...data,
id: newId, id: newId,
input: "", value: null,
enabled: false,
reactFlowInstance, reactFlowInstance,
onDelete: () => { onDelete: () => {
setNodes( setNodes(