Tag management

This commit is contained in:
ari committed 2022-04-25 14:37:41 -04:00
1 parent 81bd15436e
commit 19ea12a70e
6 files changed
+181 -42

No files matched your search

@@ -1,18 +1,62 @@
.automation-editor--info-box {
min-width : 25px;
padding-left : 10px;
padding-right : 10px;
display : flex;
flex-direction : column;
min-width: 25px;
height: 100%;
padding-left: 10px;
padding-right: 10px;
display: flex;
flex-direction: column;
justify-content: flex-start;
overflow : auto;
padding-top : 5;
padding-bottom : 10;
position : relative;
overflow: auto;
position: relative;
}
/* .automation-editor--info-box.hide {} */
.automation-editor--info-box.hide>.automation-editor--info-box-inner {
display: none;
}
.automation-editor--info-box--tags {
margin: 1em;
padding: 0.25em 0.75em 1em 0.75em;
border-color: var(--primary-accent);
border-width: 1px;
border-style: ridge;
border-radius: 5px;
}
.automation-editor--info-box--tags>h1 {
font-size: 1.5em;
}
.automation-editor--info-box--tag {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.automation-editor--info-box--tag>button {
height: 1.25em;
width: 1.25em;
font-size: 1em;
display: flex;
justify-content: center;
align-items: center;
}
.automation-editor--info-box--tag--error {
width: 100%;
display: flex;
justify-content: center;
padding: 0.5em 0;
margin: 0 30%;
}
.automation-editor--info-box--tag--error.hide {
height: 0;
opacity: 0;
}
.automation-editor--info-box--tag--error.show {
height: fit-content;
opacity: 1;
}
@@ -3,29 +3,33 @@ import { useState } from "react";
import { AutoInfoBox } from "./AutoInfoBox";
export default {
title: 'AutomationEditor/AutoInfoBox',
title: 'App/AutomationList/Editor/InfoBox',
component: AutoInfoBox,
parameters: { actions: { argTypesRegex: '^on.*' } },
} as ComponentMeta<typeof AutoInfoBox>;
export const Basic: ComponentStory<typeof AutoInfoBox> = args => {
const [state, setState] = useState(args.metadata)
export const NoTags: ComponentStory<typeof AutoInfoBox> = args => {
const [[metadata, tags], setState] = useState([args.metadata, args.tags])
return (
<div className="page">
<AutoInfoBox
className=""
metadata={state}
onUpdate={up => {
args.onUpdate(up)
setState(up)
metadata={metadata}
tags={tags}
onUpdate={(m, t) => {
args.onUpdate(m, t)
setState([m, t])
}}
/>
</div>
)
}
Basic.args = {
...Basic.args,
NoTags.args = {
...NoTags.args,
tags: {
},
metadata: {
id: "random",
alias: "Random",
@@ -36,3 +40,21 @@ Basic.args = {
mode: 'single',
},
}
export const SomeTags: ComponentStory<typeof AutoInfoBox> = NoTags.bind({})
SomeTags.args = {
...SomeTags.args,
tags: [
["Room", "Bathroom"],
["Type", "Climate"],
],
metadata: {
id: "2432asd23",
alias: "Bathroom Vent",
description: "Turn the vent when the humidity is high",
trigger_variables: {
'wowo': '!'
},
mode: 'single',
},
}
@@ -1,28 +1,80 @@
import "./AutoInfoBox.css";
import { AutomationMetadata } from "types/automations";
import { AutomationData, AutomationMetadata } from "types/automations";
import InputList from "components/Inputs/InputList";
import InputText from "components/Inputs/InputText";
import InputTextArea from "components/Inputs/InputTextArea";
import { FC } from "react";
import { FC, useRef, useState } from 'react';
import { AddIcon, TrashIcon } from "components/Icons";
import { Button } from "components/Inputs/Button";
interface Props {
className: string;
metadata: AutomationMetadata;
onUpdate: (m: AutomationMetadata) => void;
tags: Array<[string, string]>;
onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void;
}
export const AutoInfoBox: FC<Props> = ({
metadata,
tags,
onUpdate,
className,
children,
}) => {
// state
const [newTag, setNewTag] = useState<[string, string]>(["", ""])
const [[errorIndex, error], _setError] = useState<[number | 'new', string]>([-1, ""]);
const errorTimeoutId = useRef(-1);
// aliases
const setError = (index: number | 'new', msg: string) => {
_setError([index, msg])
if (errorTimeoutId.current >= 0) {
window.clearTimeout(errorTimeoutId.current);
}
errorTimeoutId.current = window.setTimeout(() => _setError([-1, ""]), 5000);
}
const onUpdateMetadata = <K extends keyof AutomationMetadata>(k: K) => (update: AutomationMetadata[K]) => onUpdate({
...metadata,
[k]: update
})
}, tags);
const validTagName = (newName: string): string | undefined => {
if (newName.length <= 0) {
return "Must be at least 1 character long"
}
for (const [name, _] of tags) {
if (name.trim() === newName.trim()) {
return `The tag '${name}' already exists`
}
}
}
const updateTagName = (newName: string, tagIndex: number) => {
const error = validTagName(newName);
if (error) {
return setError(tagIndex, error)
}
onUpdate(metadata, [
...tags.slice(0, tagIndex),
[newName, tags[tagIndex][1]],
...tags.slice(tagIndex + 1),
])
}
const onUpdateTags = (tagValue: string, tagIndex: number) => onUpdate(metadata, [
...tags.slice(0, tagIndex),
[tags[tagIndex][0], tagValue],
...tags.slice(tagIndex + 1),
])
const addNewTag = () => {
const error = validTagName(newTag[0]);
if (error) {
return setError('new', error)
}
onUpdate(metadata, tags.concat([newTag]));
setNewTag(['', '']);
}
const onRemoveTag = (tagIndex: number) => onUpdate(metadata, [
...tags.slice(0, tagIndex),
...tags.slice(tagIndex + 1),
])
// render
return <div className={`automation-editor--info-box ${className}`}>
<div className="automation-editor--info-box-inner">
@@ -40,6 +92,26 @@ export const AutoInfoBox: FC<Props> = ({
'restart'
]}
/>
<div className="automation-editor--info-box--tags">
<h1>Tags</h1>
{tags.map(([tagName, tagValue], tagIndex) => <div className="automation-editor--info-box--tag">
<Button onClick={() => onRemoveTag(tagIndex)} ><TrashIcon /></Button>
<InputText value={tagName} label="Name" onChange={v => updateTagName(v, tagIndex)} />
<InputText value={tagValue} label="Tag" onChange={v => onUpdateTags(v, tagIndex)} />
<span className={["automation-editor--info-box--tag--error", (errorIndex === tagIndex) ? "show" : "hide"].join(" ")}>
{error}
</span>
</div>)}
<hr />
<div className="automation-editor--info-box--tag">
<InputText value={newTag[0]} label="Name" onChange={v => setNewTag([v, newTag[1]])} onEnter={addNewTag} />
<InputText value={newTag[1]} label="Tag" onChange={v => setNewTag([newTag[0], v])} onEnter={addNewTag} />
<Button onClick={addNewTag} ><AddIcon /></Button>
<span className={["automation-editor--info-box--tag--error", (errorIndex === 'new') ? "show" : "hide"].join(" ")}>
{error}
</span>
</div>
</div>
</div>
{children}
</div>
@@ -6,7 +6,7 @@ import { useState } from 'react';
export default {
title: 'AutomationEditor/AutomationEditor',
title: 'App/AutomationList/Editor',
component: AutomationEditor,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {