import "./AutoInfoBox.css"; import { AutomationMetadata } from "types/automations"; import { InputList } from "components/Inputs/InputList"; import InputText from "components/Inputs/InputText"; import { FC, ReactNode, useRef, useState } from 'react'; import { AddIcon, TrashIcon } from "components/Icons"; import { Button } from "components/Inputs/Button"; import InputAutoText from "components/Inputs/InputAutoText"; import { TagDB } from "components/AutomationList/TagDB"; interface Props { className: string; metadata: AutomationMetadata; tags: Array<[string, string]>; onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void; tagDB: TagDB; children?: ReactNode; } export const AutoInfoBox: FC = ({ metadata, tags, onUpdate, className, children, tagDB, }) => { // state const [newTag, setNewTag] = useState<[string, string]>(["", ""]) const [[errorIndex, error], _setError] = useState<[number | 'new', string]>([-1, ""]); const errorTimeoutId = useRef(-1); // aliases const foundNewTagNames = tagDB.getTagNames(tags.map(([n, _]) => n)); 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 "Name must be at least 1 character long" } // eslint-disable-next-line 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.map(x => x.trim()) as [string, string] ])); setNewTag(['', '']); } const onRemoveTag = (tagIndex: number) => onUpdate(metadata, [ ...tags.slice(0, tagIndex), ...tags.slice(tagIndex + 1), ]) // render return

Tags

{tags.map(([tagName, tagValue], tagIndex) =>
updateTagName(v, tagIndex)} options={foundNewTagNames} error={errorIndex === tagIndex ? error : undefined} /> onUpdateTags(v, tagIndex)} options={tagDB.getTagValues(tagName)} error={errorIndex === tagIndex ? error : undefined} />
)}

setNewTag([v, newTag[1]])} options={foundNewTagNames} error={errorIndex === 'new' ? error : undefined} /> setNewTag([newTag[0], v])} options={tagDB.getTagValues(newTag[0])} error={errorIndex === 'new' ? error : undefined} />
{children}
}