diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.css b/webapp/src/components/AutomationEditor/AutoInfoBox.css index d4293dd..38eff4d 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.css +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.css @@ -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; +} \ No newline at end of file diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx index 55a9da7..c78126d 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx @@ -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; -export const Basic: ComponentStory = args => { - const [state, setState] = useState(args.metadata) +export const NoTags: ComponentStory = args => { + const [[metadata, tags], setState] = useState([args.metadata, args.tags]) return (
{ - args.onUpdate(up) - setState(up) + metadata={metadata} + tags={tags} + onUpdate={(m, t) => { + args.onUpdate(m, t) + setState([m, t]) }} />
) } -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 = 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', + }, +} diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx index 15aedde..f84047c 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx @@ -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 = ({ 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: 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
@@ -40,6 +92,26 @@ export const AutoInfoBox: FC = ({ 'restart' ]} /> +
+

Tags

+ {tags.map(([tagName, tagValue], tagIndex) =>
+ + updateTagName(v, tagIndex)} /> + onUpdateTags(v, tagIndex)} /> + + {error} + +
)} +
+
+ setNewTag([v, newTag[1]])} onEnter={addNewTag} /> + setNewTag([newTag[0], v])} onEnter={addNewTag} /> + + + {error} + +
+
{children}
diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index e39eb1a..f0d998f 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -6,7 +6,7 @@ import { useState } from 'react'; export default { - title: 'AutomationEditor/AutomationEditor', + title: 'App/AutomationList/Editor', component: AutomationEditor, parameters: { actions: { argTypesRegex: '^on.*' } }, args: { diff --git a/webapp/src/components/Inputs/styles.ts b/webapp/src/components/Inputs/styles.ts index 9de26b6..6c4e19e 100644 --- a/webapp/src/components/Inputs/styles.ts +++ b/webapp/src/components/Inputs/styles.ts @@ -44,8 +44,8 @@ export const useInputWrapperStyles = createAppUseStyles<{ position: 'relative', display: 'flex', flexDirection: 'column', - marginTop: noMargin ? 0 : 10, - marginBottom: noMargin ? 0 : '0.25em', + marginTop: noMargin ? 0 : '0.5em', + marginBottom: noMargin ? 0 : '0.5em', }), label: ({ labelSize }) => ({ fontSize: labelSize === 'normal' ? 16 : 10, diff --git a/webapp/src/styles/root.css b/webapp/src/styles/root.css index 854d2e8..6d450a5 100644 --- a/webapp/src/styles/root.css +++ b/webapp/src/styles/root.css @@ -1,24 +1,25 @@ :root { - --primary : #111111; - --secondary : #1c1c1c; - --primary-accent : #bbbbbb; + --primary: #111111; + --secondary: #1c1c1c; + --primary-accent: #bbbbbb; --secondary-accent: #929191; - touch-action : pan-x pan-y; - height : 100%; + --error: #c55; + touch-action: pan-x pan-y; + height: 100%; } html, body, #root { - margin : 0 !important; - padding : 0 !important; - height : 100vh; - width : 100vw; - overflow : hidden !important; - font-family : Tahoma, Geneva, sans-serif; - display : flex !important; - align-items : center; + margin: 0 !important; + padding: 0 !important; + height: 100vh; + width: 100vw; + overflow: hidden !important; + font-family: Tahoma, Geneva, sans-serif; + display: flex !important; + align-items: center; justify-items: center; } @@ -44,4 +45,4 @@ body, * { transition: all 200ms ease-out; -} +} \ No newline at end of file