chore: merge the rest of the nodes back into this structure, and make sure that start and end nodes are not deletable.
This commit is contained in:
@@ -64,35 +64,34 @@ const VisProgUI = () => {
|
|||||||
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
} = useFlowStore(useShallow(selector)); // instructs the editor to use the corresponding functions from the FlowStore
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.outerEditorContainer}>
|
<div className={`${styles.innerEditorContainer} round-lg border-lg`}>
|
||||||
<div className={styles.innerEditorContainer}>
|
<ReactFlow
|
||||||
<ReactFlow
|
nodes={nodes}
|
||||||
nodes={nodes}
|
edges={edges}
|
||||||
edges={edges}
|
defaultEdgeOptions={DEFAULT_EDGE_OPTIONS}
|
||||||
defaultEdgeOptions={DEFAULT_EDGE_OPTIONS}
|
nodeTypes={NodeTypes}
|
||||||
nodeTypes={NodeTypes}
|
onNodesChange={onNodesChange}
|
||||||
onNodesChange={onNodesChange}
|
onEdgesChange={onEdgesChange}
|
||||||
onEdgesChange={onEdgesChange}
|
onReconnect={onReconnect}
|
||||||
onReconnect={onReconnect}
|
onReconnectStart={onReconnectStart}
|
||||||
onReconnectStart={onReconnectStart}
|
onReconnectEnd={onReconnectEnd}
|
||||||
onReconnectEnd={onReconnectEnd}
|
onConnect={onConnect}
|
||||||
onConnect={onConnect}
|
snapToGrid
|
||||||
snapToGrid
|
fitView
|
||||||
fitView
|
proOptions={{hideAttribution: true}}
|
||||||
proOptions={{hideAttribution: true}}
|
>
|
||||||
>
|
<Panel position="top-center" className={styles.dndPanel}>
|
||||||
<Panel position="top-center" className={styles.dndPanel}>
|
<DndToolbar/> {/* contains the drag and drop panel for nodes */}
|
||||||
<DndToolbar/> {/* contains the drag and drop panel for nodes */}
|
</Panel>
|
||||||
</Panel>
|
<Controls/>
|
||||||
<Controls/>
|
<Background/>
|
||||||
<Background/>
|
</ReactFlow>
|
||||||
</ReactFlow>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Places the VisProgUI component inside a ReactFlowProvider
|
* Places the VisProgUI component inside a ReactFlowProvider
|
||||||
*
|
*
|
||||||
@@ -112,6 +111,7 @@ function VisualProgrammingUI() {
|
|||||||
function runProgram() {
|
function runProgram() {
|
||||||
const program = graphReducer();
|
const program = graphReducer();
|
||||||
console.log(program);
|
console.log(program);
|
||||||
|
console.log(JSON.stringify(program, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,12 +6,18 @@ import { EndNodeDefaults } from "./nodes/EndNode.default";
|
|||||||
import { StartNodeDefaults } from "./nodes/StartNode.default";
|
import { StartNodeDefaults } from "./nodes/StartNode.default";
|
||||||
import { PhaseNodeDefaults } from "./nodes/PhaseNode.default";
|
import { PhaseNodeDefaults } from "./nodes/PhaseNode.default";
|
||||||
import { NormNodeDefaults } from "./nodes/NormNode.default";
|
import { NormNodeDefaults } from "./nodes/NormNode.default";
|
||||||
|
import GoalNode, { GoalConnects, GoalReduce } from "./nodes/GoalNode";
|
||||||
|
import { GoalNodeDefaults } from "./nodes/GoalNode.default";
|
||||||
|
import TriggerNode, { TriggerConnects, TriggerReduce } from "./nodes/TriggerNode";
|
||||||
|
import { TriggerNodeDefaults } from "./nodes/TriggerNode.default";
|
||||||
|
|
||||||
export const NodeTypes = {
|
export const NodeTypes = {
|
||||||
start: StartNode,
|
start: StartNode,
|
||||||
end: EndNode,
|
end: EndNode,
|
||||||
phase: PhaseNode,
|
phase: PhaseNode,
|
||||||
norm: NormNode,
|
norm: NormNode,
|
||||||
|
goal: GoalNode,
|
||||||
|
trigger: TriggerNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Default node data for creation
|
// Default node data for creation
|
||||||
@@ -20,6 +26,8 @@ export const NodeDefaults = {
|
|||||||
end: EndNodeDefaults,
|
end: EndNodeDefaults,
|
||||||
phase: PhaseNodeDefaults,
|
phase: PhaseNodeDefaults,
|
||||||
norm: NormNodeDefaults,
|
norm: NormNodeDefaults,
|
||||||
|
goal: GoalNodeDefaults,
|
||||||
|
trigger: TriggerNodeDefaults,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NodeReduces = {
|
export const NodeReduces = {
|
||||||
@@ -27,6 +35,8 @@ export const NodeReduces = {
|
|||||||
end: EndReduce,
|
end: EndReduce,
|
||||||
phase: PhaseReduce,
|
phase: PhaseReduce,
|
||||||
norm: NormReduce,
|
norm: NormReduce,
|
||||||
|
goal: GoalReduce,
|
||||||
|
trigger: TriggerReduce,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const NodeConnects = {
|
export const NodeConnects = {
|
||||||
@@ -34,4 +44,13 @@ export const NodeConnects = {
|
|||||||
end: EndConnects,
|
end: EndConnects,
|
||||||
phase: PhaseConnects,
|
phase: PhaseConnects,
|
||||||
norm: NormConnects,
|
norm: NormConnects,
|
||||||
|
goal: GoalConnects,
|
||||||
|
trigger: TriggerConnects,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to tell the visual program if we're allowed to delete them...
|
||||||
|
// Right now it doesn't take in any values, but that could also be done later.
|
||||||
|
export const NodeDeletes = {
|
||||||
|
start: () => false,
|
||||||
|
end: () => false,
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
type XYPosition,
|
type XYPosition,
|
||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import type { FlowState } from './VisProgTypes';
|
import type { FlowState } from './VisProgTypes';
|
||||||
import { NodeDefaults, NodeConnects } from './NodeRegistry';
|
import { NodeDefaults, NodeConnects, NodeDeletes } from './NodeRegistry';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,21 +20,22 @@ import { NodeDefaults, NodeConnects } from './NodeRegistry';
|
|||||||
* @param data the data in the node to create
|
* @param data the data in the node to create
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function createNode(id: string, type: string, position: XYPosition, data: Record<string, unknown>) {
|
function createNode(id: string, type: string, position: XYPosition, data: Record<string, unknown>, deletable? : boolean) {
|
||||||
const defaultData = NodeDefaults[type as keyof typeof NodeDefaults]
|
const defaultData = NodeDefaults[type as keyof typeof NodeDefaults]
|
||||||
const newData = {
|
const newData = {
|
||||||
id: id,
|
id: id,
|
||||||
type: type,
|
type: type,
|
||||||
position: position,
|
position: position,
|
||||||
data: data,
|
data: data,
|
||||||
|
deletable: deletable,
|
||||||
}
|
}
|
||||||
return {...defaultData, ...newData}
|
return {...defaultData, ...newData}
|
||||||
}
|
}
|
||||||
|
|
||||||
//* Initial nodes, created by using createNode. */
|
//* Initial nodes, created by using createNode. */
|
||||||
const initialNodes : Node[] = [
|
const initialNodes : Node[] = [
|
||||||
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}),
|
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}, false),
|
||||||
createNode('end', 'end', {x: 370, y: 100}, {label: "End"}),
|
createNode('end', 'end', {x: 370, y: 100}, {label: "End"}, false),
|
||||||
createNode('phase-1', 'phase', {x:200, y:100}, {label: "Phase 1", children: ['end', 'start']}),
|
createNode('phase-1', 'phase', {x:200, y:100}, {label: "Phase 1", children: ['end', 'start']}),
|
||||||
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
createNode('norms-1', 'norm', {x:-200, y:100}, {label: "Initial Norms", normList: ["Be a robot", "get good"]}),
|
||||||
];
|
];
|
||||||
@@ -92,11 +93,19 @@ const useFlowStore = create<FlowState>((set, get) => ({
|
|||||||
set({ edgeReconnectSuccessful: true });
|
set({ edgeReconnectSuccessful: true });
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteNode: (nodeId) =>
|
deleteNode: (nodeId) => {
|
||||||
set({
|
// Let's find our node to check if they have a special deletion function
|
||||||
|
const ourNode = get().nodes.find((n)=>n.id==nodeId);
|
||||||
|
const ourFunction = Object.entries(NodeDeletes).find(([t])=>t==ourNode?.type)?.[1]
|
||||||
|
|
||||||
|
// If there's no function, OR, our function tells us we can delete it, let's do so...
|
||||||
|
if (ourFunction == undefined || ourFunction()) {
|
||||||
|
set({
|
||||||
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
nodes: get().nodes.filter((n) => n.id !== nodeId),
|
||||||
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
edges: get().edges.filter((e) => e.source !== nodeId && e.target !== nodeId),
|
||||||
}),
|
})}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
setNodes: (nodes) => set({ nodes }),
|
setNodes: (nodes) => set({ nodes }),
|
||||||
setEdges: (edges) => set({ edges }),
|
setEdges: (edges) => set({ edges }),
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export type EndNode = Node<EndNodeData>
|
|||||||
export default function EndNode(props: NodeProps<Node>) {
|
export default function EndNode(props: NodeProps<Node>) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
<Toolbar nodeId={props.id} allowDelete={false}/>
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeEnd}`}>
|
<div className={`${styles.defaultNode} ${styles.nodeEnd}`}>
|
||||||
<div className={"flex-row gap-sm"}>
|
<div className={"flex-row gap-sm"}>
|
||||||
End
|
End
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import type { GoalNodeData } from "./GoalNode";
|
|||||||
export const GoalNodeDefaults: GoalNodeData = {
|
export const GoalNodeDefaults: GoalNodeData = {
|
||||||
label: "Goal Node",
|
label: "Goal Node",
|
||||||
droppable: true,
|
droppable: true,
|
||||||
GoalList: [],
|
description: "The robot will strive towards this goal",
|
||||||
|
achieved: false,
|
||||||
hasReduce: true,
|
hasReduce: true,
|
||||||
};
|
};
|
||||||
@@ -8,6 +8,8 @@ import {
|
|||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import { Toolbar } from '../components/NodeComponents';
|
import { Toolbar } from '../components/NodeComponents';
|
||||||
import styles from '../../VisProg.module.css';
|
import styles from '../../VisProg.module.css';
|
||||||
|
import { TextField } from '../../../../components/TextField';
|
||||||
|
import useFlowStore from '../VisProgStores';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default data dot a Goal node
|
* The default data dot a Goal node
|
||||||
@@ -17,8 +19,9 @@ import styles from '../../VisProg.module.css';
|
|||||||
*/
|
*/
|
||||||
export type GoalNodeData = {
|
export type GoalNodeData = {
|
||||||
label: string;
|
label: string;
|
||||||
|
description: string;
|
||||||
droppable: boolean;
|
droppable: boolean;
|
||||||
GoalList: string[];
|
achieved: boolean;
|
||||||
hasReduce: boolean;
|
hasReduce: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,23 +40,47 @@ export function GoalNodeCanConnect(connection: Connection | Edge): boolean {
|
|||||||
* @returns React.JSX.Element
|
* @returns React.JSX.Element
|
||||||
*/
|
*/
|
||||||
export default function GoalNode(props: NodeProps<Node>) {
|
export default function GoalNode(props: NodeProps<Node>) {
|
||||||
const label_input_id = `Goal_${props.id}_label_input`;
|
|
||||||
const data = props.data as GoalNodeData;
|
const data = props.data as GoalNodeData;
|
||||||
return (
|
const {updateNodeData} = useFlowStore();
|
||||||
<>
|
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
const text_input_id = `goal_${props.id}_text_input`;
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeGoal}`}>
|
const checkbox_id = `goal_${props.id}_checkbox`;
|
||||||
<div className={"flex-row gap-sm"}>
|
|
||||||
<label htmlFor={label_input_id}></label>
|
const setDescription = (value: string) => {
|
||||||
{props.data.label as string}
|
updateNodeData(props.id, {...data, description: value});
|
||||||
</div>
|
}
|
||||||
{data.GoalList.map((Goal) => (<div>{Goal}</div>))}
|
|
||||||
<Handle type="target" position={Position.Right} id="phase"/>
|
const setAchieved = (value: boolean) => {
|
||||||
|
updateNodeData(props.id, {...data, achieved: value});
|
||||||
|
}
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<Toolbar nodeId={props.id} allowDelete={true}/>
|
||||||
|
<div className={`${styles.defaultNode} ${styles.nodeGoal} flex-col gap-sm`}>
|
||||||
|
<div className={"flex-row gap-md"}>
|
||||||
|
<label htmlFor={text_input_id}>Goal:</label>
|
||||||
|
<TextField
|
||||||
|
id={text_input_id}
|
||||||
|
value={data.description}
|
||||||
|
setValue={(val) => setDescription(val)}
|
||||||
|
placeholder={"To ..."}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</>
|
<div className={"flex-row gap-md align-center"}>
|
||||||
);
|
<label htmlFor={checkbox_id}>Achieved:</label>
|
||||||
|
<input
|
||||||
|
id={checkbox_id}
|
||||||
|
type={"checkbox"}
|
||||||
|
value={data.achieved ? "checked" : ""}
|
||||||
|
onChange={(e) => setAchieved(e.target.checked)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<Handle type="source" position={Position.Right} id="GoalSource"/>
|
||||||
|
</div>
|
||||||
|
</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reduces each Goal, including its children down into its relevant data.
|
* Reduces each Goal, including its children down into its relevant data.
|
||||||
* @param props: The Node Properties of this node.
|
* @param props: The Node Properties of this node.
|
||||||
@@ -66,7 +93,7 @@ export function GoalReduce(node: Node, nodes: Node[]) {
|
|||||||
const data = node.data as GoalNodeData;
|
const data = node.data as GoalNodeData;
|
||||||
return {
|
return {
|
||||||
label: data.label,
|
label: data.label,
|
||||||
list: data.GoalList,
|
achieved: data.achieved,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export type StartNode = Node<StartNodeData>
|
|||||||
export default function StartNode(props: NodeProps<Node>) {
|
export default function StartNode(props: NodeProps<Node>) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
<Toolbar nodeId={props.id} allowDelete={false}/>
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeStart}`}>
|
<div className={`${styles.defaultNode} ${styles.nodeStart}`}>
|
||||||
<div className={"flex-row gap-sm"}>
|
<div className={"flex-row gap-sm"}>
|
||||||
Start
|
Start
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import type { TriggerNodeData } from "./TriggerNode";
|
|||||||
export const TriggerNodeDefaults: TriggerNodeData = {
|
export const TriggerNodeDefaults: TriggerNodeData = {
|
||||||
label: "Trigger Node",
|
label: "Trigger Node",
|
||||||
droppable: true,
|
droppable: true,
|
||||||
TriggerList: [],
|
triggers: [{id: "help-trigger", keyword:"help"}],
|
||||||
|
triggerType: "keywords",
|
||||||
hasReduce: true,
|
hasReduce: true,
|
||||||
};
|
};
|
||||||
@@ -8,6 +8,10 @@ import {
|
|||||||
} from '@xyflow/react';
|
} from '@xyflow/react';
|
||||||
import { Toolbar } from '../components/NodeComponents';
|
import { Toolbar } from '../components/NodeComponents';
|
||||||
import styles from '../../VisProg.module.css';
|
import styles from '../../VisProg.module.css';
|
||||||
|
import useFlowStore from '../VisProgStores';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { RealtimeTextField, TextField } from '../../../../components/TextField';
|
||||||
|
import duplicateIndices from '../../../../utils/duplicateIndices';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default data dot a Trigger node
|
* The default data dot a Trigger node
|
||||||
@@ -18,12 +22,12 @@ import styles from '../../VisProg.module.css';
|
|||||||
export type TriggerNodeData = {
|
export type TriggerNodeData = {
|
||||||
label: string;
|
label: string;
|
||||||
droppable: boolean;
|
droppable: boolean;
|
||||||
TriggerList: string[];
|
triggerType: unknown;
|
||||||
|
triggers: [unknown];
|
||||||
hasReduce: boolean;
|
hasReduce: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export type TriggerNode = Node<TriggerNodeData>
|
export type TriggerNode = Node<TriggerNodeData>
|
||||||
|
|
||||||
|
|
||||||
@@ -37,21 +41,28 @@ export function TriggerNodeCanConnect(connection: Connection | Edge): boolean {
|
|||||||
* @returns React.JSX.Element
|
* @returns React.JSX.Element
|
||||||
*/
|
*/
|
||||||
export default function TriggerNode(props: NodeProps<Node>) {
|
export default function TriggerNode(props: NodeProps<Node>) {
|
||||||
const label_input_id = `Trigger_${props.id}_label_input`;
|
const data = props.data as TriggerNodeData
|
||||||
const data = props.data as TriggerNodeData;
|
const {updateNodeData} = useFlowStore();
|
||||||
return (
|
|
||||||
<>
|
const setKeywords = (keywords: Keyword[]) => {
|
||||||
<Toolbar nodeId={props.id} allowDelete={true}/>
|
updateNodeData(props.id, {...data, triggers: keywords});
|
||||||
<div className={`${styles.defaultNode} ${styles.nodeTrigger}`}>
|
}
|
||||||
<div className={"flex-row gap-sm"}>
|
|
||||||
<label htmlFor={label_input_id}></label>
|
return <>
|
||||||
{props.data.label as string}
|
<Toolbar nodeId={props.id} allowDelete={true}/>
|
||||||
</div>
|
<div className={`${styles.defaultNode} ${styles.nodeTrigger} flex-col gap-sm`}>
|
||||||
{data.TriggerList.map((Trigger) => (<div>{Trigger}</div>))}
|
{data.triggerType === "emotion" && (
|
||||||
<Handle type="target" position={Position.Right} id="phase"/>
|
<div className={"flex-row gap-md"}>Emotion?</div>
|
||||||
</div>
|
)}
|
||||||
</>
|
{data.triggerType === "keywords" && (
|
||||||
);
|
<Keywords
|
||||||
|
keywords={[{id: "test", keyword: " test"}]}
|
||||||
|
setKeywords={setKeywords}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<Handle type="source" position={Position.Right} id="TriggerSource"/>
|
||||||
|
</div>
|
||||||
|
</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,7 +77,7 @@ export function TriggerReduce(node: Node, nodes: Node[]) {
|
|||||||
const data = node.data as TriggerNodeData;
|
const data = node.data as TriggerNodeData;
|
||||||
return {
|
return {
|
||||||
label: data.label,
|
label: data.label,
|
||||||
list: data.TriggerList,
|
list: data.triggers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,3 +87,90 @@ export function TriggerConnects(thisNode: Node, otherNode: Node, isThisSource: b
|
|||||||
console.warn("Impossible node connection called in EndConnects")
|
console.warn("Impossible node connection called in EndConnects")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type EmotionTriggerNodeProps = {
|
||||||
|
type: "emotion";
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Keyword = { id: string, keyword: string };
|
||||||
|
|
||||||
|
export type KeywordTriggerNodeProps = {
|
||||||
|
type: "keywords";
|
||||||
|
value: Keyword[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TriggerNodeProps = EmotionTriggerNodeProps | KeywordTriggerNodeProps;
|
||||||
|
|
||||||
|
function KeywordAdder({ addKeyword }: { addKeyword: (keyword: string) => void }) {
|
||||||
|
const [input, setInput] = useState("");
|
||||||
|
|
||||||
|
const text_input_id = "keyword_adder_input";
|
||||||
|
|
||||||
|
return <div className={"flex-row gap-md"}>
|
||||||
|
<label htmlFor={text_input_id}>New Keyword:</label>
|
||||||
|
<RealtimeTextField
|
||||||
|
id={text_input_id}
|
||||||
|
value={input}
|
||||||
|
setValue={setInput}
|
||||||
|
onCommit={() => {
|
||||||
|
if (!input) return;
|
||||||
|
addKeyword(input);
|
||||||
|
setInput("");
|
||||||
|
}}
|
||||||
|
placeholder={"..."}
|
||||||
|
className={"flex-1"}
|
||||||
|
/>
|
||||||
|
</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Keywords({
|
||||||
|
keywords,
|
||||||
|
setKeywords,
|
||||||
|
}: {
|
||||||
|
keywords: Keyword[];
|
||||||
|
setKeywords: (keywords: Keyword[]) => void;
|
||||||
|
}) {
|
||||||
|
type Interpolatable = string | number | boolean | bigint | null | undefined;
|
||||||
|
|
||||||
|
const inputElementId = (id: Interpolatable) => `keyword_${id}_input`;
|
||||||
|
|
||||||
|
/** Indices of duplicates in the keyword array. */
|
||||||
|
const [duplicates, setDuplicates] = useState<number[]>([]);
|
||||||
|
|
||||||
|
function replace(id: string, value: string) {
|
||||||
|
value = value.trim();
|
||||||
|
const newKeywords = value === ""
|
||||||
|
? keywords.filter((kw) => kw.id != id)
|
||||||
|
: keywords.map((kw) => kw.id === id ? {...kw, keyword: value} : kw);
|
||||||
|
setKeywords(newKeywords);
|
||||||
|
setDuplicates(duplicateIndices(newKeywords.map((kw) => kw.keyword)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function add(value: string) {
|
||||||
|
value = value.trim();
|
||||||
|
if (value === "") return;
|
||||||
|
const newKeywords = [...keywords, {id: crypto.randomUUID(), keyword: value}];
|
||||||
|
setKeywords(newKeywords);
|
||||||
|
setDuplicates(duplicateIndices(newKeywords.map((kw) => kw.keyword)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<span>Triggers when {keywords.length <= 1 ? "the keyword is" : "all keywords are"} spoken.</span>
|
||||||
|
{[...keywords].map(({id, keyword}, index) => {
|
||||||
|
return <div key={id} className={"flex-row gap-md"}>
|
||||||
|
<label htmlFor={inputElementId(id)}>Keyword:</label>
|
||||||
|
<TextField
|
||||||
|
id={inputElementId(id)}
|
||||||
|
value={keyword}
|
||||||
|
setValue={(val) => replace(id, val)}
|
||||||
|
placeholder={"..."}
|
||||||
|
className={"flex-1"}
|
||||||
|
invalid={duplicates.includes(index)}
|
||||||
|
/>
|
||||||
|
</div>;
|
||||||
|
})}
|
||||||
|
<KeywordAdder addKeyword={add} />
|
||||||
|
</>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user