latest work
This commit is contained in:
commit
5f486a5f89
37 files changed
+12606
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { useState } from "react";
|
||||
import { AutomationNodeType, AutomationNodeTypes } from "../../automation/types";
|
||||
import { typeList } from "../../automation/useNodeState";
|
||||
import InputList from "../InputList";
|
||||
import InputWrapper from "../InputWrapper";
|
||||
|
||||
export interface Props<T extends AutomationNodeType> {
|
||||
node_type: T,
|
||||
onClose: () => void,
|
||||
onCreate: (newnode: AutomationNodeTypes[T]) => void,
|
||||
}
|
||||
export default function NewNodeModal<T extends AutomationNodeType>({
|
||||
node_type,
|
||||
onClose,
|
||||
onCreate,
|
||||
}: Props<T>) {
|
||||
const [selectedSubType, setSubType] = useState<string>(typeList[node_type][0]);
|
||||
|
||||
return <div className="new-node-modal">
|
||||
<div className="new-node-modal--background" onClick={() => onClose()}></div>
|
||||
<div className="new-node-modal--inner" onClick={e => e.preventDefault()}>
|
||||
<div className="new-node-modal--title">Create {node_type[0].toUpperCase()}{node_type.slice(1)}</div>
|
||||
<div className="new-node-modal--body">
|
||||
<InputWrapper label="Type:">
|
||||
<InputList options={typeList[node_type]} current={selectedSubType} onChange={n => setSubType(n)}/>
|
||||
</InputWrapper>
|
||||
</div>
|
||||
<div className="new-node-modal--footer">
|
||||
<button className="secondary" onClick={() => onClose()}>Close</button>
|
||||
<button onClick={() => onCreate({} as any)}>Create</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { useState } from "react";
|
||||
import * as types from "../../automation/types";
|
||||
import { getNameFromAction } from "../../automation/utils";
|
||||
import AutomationNode from "../Node";
|
||||
import NewNodeModal from "./NewNodeModal";
|
||||
|
||||
export interface Props {
|
||||
automation: types.Automation;
|
||||
}
|
||||
export default function AutomationEditor({ automation }: Props) {
|
||||
// state
|
||||
const [modalState, setModalState] = useState<types.AutomationNodeType | null>(null);
|
||||
const [state, setState] = useState(automation);
|
||||
|
||||
// helper state functions
|
||||
const addNode = <T extends types.AutomationNodeType>(node_type: T, node: types.AutomationNodeTypes[T]) => {
|
||||
const nodes: types.AutomationNode[] = state[node_type];
|
||||
setState({
|
||||
...state,
|
||||
[node_type]: nodes.concat([node as any])
|
||||
});
|
||||
}
|
||||
const removeNode = <T extends types.AutomationNodeType>(node_type: T, node_id: number) => {
|
||||
const nodes: types.AutomationNode[] = state[node_type];
|
||||
setState({
|
||||
...state,
|
||||
[node_type]: nodes
|
||||
.slice(0, node_id)
|
||||
.concat(
|
||||
nodes.slice(
|
||||
node_id + 1,
|
||||
nodes.length
|
||||
)
|
||||
)
|
||||
});
|
||||
}
|
||||
// helper element functions
|
||||
const makeChild = (node_type: types.AutomationNodeType) => {
|
||||
const nodes: types.AutomationNode[] = state[node_type];
|
||||
return [
|
||||
<div className="automation-box--elm-list" key="list">
|
||||
{state[node_type].map((n, i) => (
|
||||
<AutomationNode
|
||||
key={i}
|
||||
title={getNameFromAction(n)}
|
||||
showX={false}
|
||||
initialOpenState={false}
|
||||
onXClick={() => removeNode(node_type, i)}
|
||||
/>
|
||||
))}
|
||||
</div>,
|
||||
<button
|
||||
key="btn"
|
||||
className="elm-list"
|
||||
onClick={() => setModalState(node_type)}
|
||||
>
|
||||
Add +
|
||||
</button>
|
||||
];
|
||||
}
|
||||
|
||||
return <div className="automation-editor">
|
||||
<AutomationNode
|
||||
title={`${automation.alias} (${automation.mode})`}
|
||||
initialOpenState={true}
|
||||
showChildren={true}
|
||||
showX={false}
|
||||
>
|
||||
<div className="automation-editor--root">
|
||||
<h4>Trigger</h4>
|
||||
{makeChild("trigger")}
|
||||
<h4>Condition</h4>
|
||||
{makeChild("condition")}
|
||||
<h4>Actions</h4>
|
||||
{makeChild("action")}
|
||||
</div>
|
||||
</AutomationNode>
|
||||
{modalState && <NewNodeModal
|
||||
node_type={modalState}
|
||||
onClose={() => setModalState(null)}
|
||||
onCreate={node => {
|
||||
addNode(modalState, node);
|
||||
setModalState(null);
|
||||
}}
|
||||
/>}
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { SelectHTMLAttributes } from "react";
|
||||
|
||||
|
||||
export interface Props {
|
||||
current: string;
|
||||
options: readonly string[];
|
||||
onChange: (n: string) => void;
|
||||
id?: string;
|
||||
className?: string;
|
||||
}
|
||||
export default function InputList({current, options, onChange, ...selattr}: Props) {
|
||||
return <select {...selattr} value={current} onChange={e => {
|
||||
e.preventDefault();
|
||||
onChange(e.currentTarget.value);
|
||||
}}>
|
||||
{options.map((t, i) => <option id={String(i)} key={i}>{t}</option>)}
|
||||
</select>
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PropsWithChildren } from "react";
|
||||
|
||||
|
||||
export interface Props {
|
||||
label: string;
|
||||
}
|
||||
export default function InputWrapper({label, children}: PropsWithChildren<Props>) {
|
||||
return <div className="input-wrapper">
|
||||
<label>{label}</label>
|
||||
{children}
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { PropsWithChildren, useState } from "react";
|
||||
|
||||
export interface Props {
|
||||
title: string;
|
||||
showX?: boolean;
|
||||
showChildren?: boolean;
|
||||
initialOpenState?: boolean;
|
||||
onXClick?: () => void;
|
||||
}
|
||||
export default function AutomationNode({
|
||||
children,
|
||||
title,
|
||||
showX=false,
|
||||
showChildren=false,
|
||||
initialOpenState=false,
|
||||
onXClick = () => {},
|
||||
}: PropsWithChildren<Props>) {
|
||||
const [isOpen, setOpen] = useState(initialOpenState);
|
||||
console.log(children);
|
||||
return <div className="automation-node">
|
||||
<div className="automation-node-inner">
|
||||
<div
|
||||
className="automation-node-inner-edge left"
|
||||
onClick={() => onXClick()}
|
||||
>
|
||||
{showX && (
|
||||
<div className="automation-node-side-button">X</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="automation-node-inner-text">{title}</div>
|
||||
<div
|
||||
className="automation-node-inner-edge right"
|
||||
onClick={() => setOpen(!isOpen)}
|
||||
>
|
||||
{showChildren && <div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>}
|
||||
</div>
|
||||
</div>
|
||||
{showChildren&&<div className={`automation-node-children ${isOpen ? "open" : "closed"}`}>
|
||||
{children}
|
||||
</div>}
|
||||
</div>
|
||||
}
|
||||
Reference in new issue
Block a user