diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css index 10e07d4..9cc0612 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -55,74 +55,6 @@ color: var(--secondary); } -.automation-list-box--body--item:hover, .automation-list-box--body--item.selected { - background-color: var(--primary-accent); - color: var(--primary); -} - -.automation-list-box--body--item:hover>.automation-list-box--body--item--tags>span { - border-color: var(--primary); -} - -.automation-list-box--body--item--tags--add:hover { - border-color: var(--primary); - color: var(--primary-accent); - background-color: var(--secondary); -} - -.automation-list-box--body--item { - padding-left: 1em; - cursor: pointer; - display: flex; - justify-content: left; - align-items: flex-start; - gap: 1em; - flex-wrap: wrap; -} - -.automation-list-box--body--item::before { - content: "⊚ "; -} - -.automation-list-box--body--item--title { - display: flex; - flex-direction: column; - padding-left: 0.25em; - flex: 1; -} - -.automation-list-box--body--item--title>b { - font-size: 1em; -} - -.automation-list-box--body--item--title>span, .automation-list-box--body--item--title>b>span { - font-size: 0.75em; -} - -.automation-list-box--body--item>button { - padding: 0.25em !important; - margin: 0.5em !important; -} - -.automation-list-box--body--item--tags { - flex: 1; - display: flex; - flex-wrap: wrap; - font-size: 0.75em; - justify-content: left; - align-items: flex-start; - overflow: auto; - max-height: 100%; - gap: 0.25em; -} - -.automation-list-box--body--item--tags>span { - border-color: var(--primary-accent); - border-style: solid; - border-width: 1px; - border-radius: 0.5em; - padding: 0.1em 0.5em; -} .automation-list-box--bottom { margin-top: 0.5em; diff --git a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx index 7573dbd..326552f 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx @@ -4,6 +4,7 @@ import { AutomationListBox } from "./AutomationListBox"; import { createMockAuto } from "utils/mocks"; import { Page } from "components/Page"; import { CookiesProvider } from "react-cookie"; +import { makeTagDB } from "./TagDB"; export default { title: "App/AutomationList/Box", @@ -19,6 +20,7 @@ const Template: ComponentStory = (args) => { { diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts index 0beaac1..5463d19 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.test.ts +++ b/webapp/src/components/AutomationList/AutomationListBox.test.ts @@ -1,31 +1,22 @@ -import { getTagList } from './AutomationListBox'; -import { createMockAuto } from 'utils/mocks'; +import { getTagList } from "./AutomationListBox"; +import { createMockAuto } from "utils/mocks"; -// const makeDummy = (id: string, tags: Record) => ({ -// condition: [], -// metadata: { -// id, -// alias: '', -// description: '', -// mode: 'single', -// }, -// tags, -// sequence: [], -// trigger: [], -// }) +test("getTagList", () => { + expect( + getTagList([ + createMockAuto(), + createMockAuto(), + createMockAuto(), + createMockAuto(), + ]) + ).toStrictEqual([]); -test('getTagList', () => { - expect(getTagList([ - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - ])).toStrictEqual([]) - - expect(getTagList([ - createMockAuto({ "Room": "Kitchen", "Area": "Oven" }), - createMockAuto({ "Room": "Attic" }), - createMockAuto({ "Room": "Office" }), - createMockAuto({ "Area": "Balcony" }), - ])).toStrictEqual(["Room", "Area"]) -}) \ No newline at end of file + expect( + getTagList([ + createMockAuto({ Room: "Kitchen", Area: "Oven" }), + createMockAuto({ Room: "Attic" }), + createMockAuto({ Room: "Office" }), + createMockAuto({ Area: "Balcony" }), + ]) + ).toStrictEqual(["Room", "Area"]); +}); diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index d24a6e0..999722b 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -9,12 +9,13 @@ import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; import { useCookies } from "react-cookie"; import { TagDB } from "components/AutomationList/TagDB"; -export type Props = { +export type AutomationListBoxProps = { automations: AutomationData[]; selected: number; onSelectAutomation: (i: number) => void; onRemove: (i: number) => void; onAdd: () => void; + onUpdate: (a: AutomationData, i: number) => void; tagsDB: TagDB; }; @@ -52,13 +53,14 @@ const useAutomationListBoxState = () => { }; }; -export const AutomationListBox: FC = ({ +export const AutomationListBox: FC = ({ automations, selected, onSelectAutomation, onAdd, onRemove, tagsDB, + onUpdate, }) => { const tags = getTagList(automations); const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } = @@ -95,6 +97,7 @@ export const AutomationListBox: FC = ({ grouping={grouping} selectedAutomationIdx={selected} tagsDB={tagsDB} + onSave={onUpdate} />
diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.css b/webapp/src/components/AutomationList/AutomationListBoxGroup.css new file mode 100644 index 0000000..759f2ca --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBoxGroup.css @@ -0,0 +1,50 @@ +.automation-list-box--body--item:hover, .automation-list-box--body--item.selected { + outline-color: var(--primary-accent); + outline-style: solid; + outline-width: 1px; + background-color: var(--secondary); +} + +.automation-list-box--body--item:hover>.automation-list-box--body--item--tags>span { + border-color: var(--primary); +} + +.automation-list-box--body--item--tags--add:hover { + border-color: var(--primary); + color: var(--primary-accent); + background-color: var(--secondary); +} + +.automation-list-box--body--item { + padding-left: 1em; + cursor: pointer; + display: flex; + gap: 1em; +} + +.automation-list-box--body--item::before { + content: "⊚ "; +} + +.automation-list-box--body--item--buttons { + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + height: 100%; + gap: 0.25em; + margin: 0.5em +} + +.automation-list-box--body--item--buttons>button { + padding: 0.25em; + margin: 0; +} + +.automation-list-box--body--item--buttons .save { + color: var(--green-light) +} + +.automation-list-box--body--item--buttons .save:hover { + color: var(--green) +} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx index 28a1458..2aabea3 100644 --- a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx +++ b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx @@ -1,10 +1,13 @@ +import "./AutomationListBoxGroup.css"; + import { TrashIcon } from "components/Icons"; import { MetadataBox } from "./AutomationMetadataBox"; import { AutomationData } from "types/automations"; import { AutomationGrouping } from "./automationGrouper"; -import { FC, useState } from "react"; +import { FC, useEffect, useState } from "react"; import { ButtonIcon } from "components/Icons/ButtonIcons"; import { TagDB } from "./TagDB"; +import Save from "@mui/icons-material/Save"; export type AutomationListBoxEvents = { onSelectAutomation: (i: number) => void; @@ -26,6 +29,7 @@ export type AutomationListBoxGroupProps = { autos: Array<[AutomationData, number]>; selectedAutomationIdx: number; tagsDB: TagDB; + onSave: (a: AutomationData, i: number) => void; }; export const AutomationListBoxGroup: FC = ({ events, @@ -33,6 +37,7 @@ export const AutomationListBoxGroup: FC = ({ autos, selectedAutomationIdx, tagsDB, + onSave, }) => { if (grouping.top.length === 1) { return ( @@ -45,7 +50,8 @@ export const AutomationListBoxGroup: FC = ({ }, events, selectedAutomationIdx, - tagsDB + tagsDB, + onSave )} ); @@ -63,6 +69,7 @@ export const AutomationListBoxGroup: FC = ({ events={events} selectedAutomationIdx={selectedAutomationIdx} tagsDB={tagsDB} + onSave={onSave} /> ))} @@ -75,7 +82,8 @@ export const convertGroupsToItems = ( { grouping, currentNode }: AutomationListBoxGroups, events: AutomationListBoxEvents, selectedAutomationIdx: number, - tagsDB: TagDB + tagsDB: TagDB, + onSave: (a: AutomationData, i: number) => void ) => { const automations = grouping.getAutomations(currentNode); if (automations.length > 0) { @@ -86,6 +94,7 @@ export const convertGroupsToItems = ( auto={autos[autoIndex]} isSelected={selectedAutomationIdx === autos[autoIndex][1]} tagsDB={tagsDB} + onSave={(a) => onSave(a, autoIndex)} /> )); } @@ -102,6 +111,7 @@ export const convertGroupsToItems = ( events={events} selectedAutomationIdx={selectedAutomationIdx} tagsDB={tagsDB} + onSave={onSave} /> )); } @@ -114,7 +124,8 @@ export const AutomationListBoxGroupItem: FC<{ autos: Array<[AutomationData, number]>; selectedAutomationIdx: number; tagsDB: TagDB; -}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB }) => { + onSave: (a: AutomationData, i: number) => void; +}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB, onSave }) => { const groupData = groups.grouping.getData(groups.currentNode); const automations = groups.grouping.getAutomations(groups.currentNode); const [open, setOpen] = useState(false); @@ -145,7 +156,8 @@ export const AutomationListBoxGroupItem: FC<{ groups, events, selectedAutomationIdx, - tagsDB + tagsDB, + onSave )}
); @@ -156,28 +168,73 @@ export const AutomationListBoxItem: FC<{ isSelected: boolean; events: AutomationListBoxEvents; tagsDB: TagDB; + onSave: (a: AutomationData) => void; }> = ({ events: { onRemove, onSelectAutomation }, auto: [auto, autoIndex], isSelected, tagsDB, + onSave, }) => { + // state + const [{ tags, isModified }, setState] = useState({ + isModified: false, + tags: Object.entries(auto.tags), + }); + + // effects + useEffect(() => { + setState({ + isModified: false, + tags: Object.entries(auto.tags), + }); + }, [auto]); + + // alias + const setTags = (t: [string, string][]) => + setState({ + isModified: true, + tags: t, + }); + // sub elements let title = "BadAuto<>"; if (auto.metadata) { title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`; } + + // render return (
onSelectAutomation(autoIndex)} > - - onRemove(autoIndex)} icon={} /> + +
+ {isModified && ( + } + onClick={() => + onSave({ + ...auto, + tags: Object.fromEntries(tags), + }) + } + /> + )} + onRemove(autoIndex)} icon={} /> +
); }; diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.css b/webapp/src/components/AutomationList/AutomationMetadataBox.css index eccc50e..4f76f31 100644 --- a/webapp/src/components/AutomationList/AutomationMetadataBox.css +++ b/webapp/src/components/AutomationList/AutomationMetadataBox.css @@ -1,3 +1,39 @@ +.automation-list-box--body--item--title { + display: flex; + flex-direction: column; + padding-left: 0.25em; + flex: 1; +} + +.automation-list-box--body--item--title>b { + font-size: 1em; +} + +.automation-list-box--body--item--title>span, .automation-list-box--body--item--title>b>span { + font-size: 0.75em; +} + + +.automation-list-box--body--item--tags { + flex: 1; + display: flex; + flex-wrap: wrap; + font-size: 0.75em; + justify-content: left; + align-items: flex-start; + overflow: auto; + max-height: 100%; + gap: 0.25em; +} + +.automation-list-box--body--item--tags>span { + border-color: var(--primary-accent); + border-style: solid; + border-width: 1px; + border-radius: 0.5em; + padding: 0.1em 0.5em; +} + .automation-list-box--body--item--tags--item { color: white; border-color: white; diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx index fe6e6f0..b695900 100644 --- a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx +++ b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx @@ -4,24 +4,37 @@ import { FC, useState } from "react"; import { AutomationMetadata } from "types/automations"; import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; import { TagDB } from "./TagDB"; -import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; export const MetadataBox: FC<{ metadata: AutomationMetadata; - tags: Record; + onUpdate: (t: [string, string][]) => void; + tags: [string, string][]; tagsDB: TagDB; -}> = (auto) => { - const [tags, setTags] = useState(auto.tags); - const tagOptions = auto.tagsDB.getTagNames(Object.keys(tags)); +}> = ({ tags, onUpdate, tagsDB, metadata }) => { + // state + const [newTag, setNewTag] = useState< + | { + on: false; + } + | { + on: true; + name: string; + value: string; + } + >({ on: false }); + // alias + const tagOptions = tagsDB.getTagNames(tags.map((t) => t[0])); + + // render let title = {"BadAuto<>"}; - if (auto.metadata) { + if (metadata) { title = ( <> - {String(auto.metadata.alias ?? "").slice(0, 15)}{" "} - ({String(auto.metadata.id).slice(0, 5)}) + {String(metadata.alias ?? "").slice(0, 15)}{" "} + ({String(metadata.id).slice(0, 5)}) - {String(auto.metadata.description ?? "").slice(0, 25)} + {String(metadata.description ?? "").slice(0, 25)} ); } @@ -29,23 +42,56 @@ export const MetadataBox: FC<{
{title}
- {Object.keys(tags).map((tagName) => ( + {tags.map(([tagName, tagValue], tagIndex) => ( - setTags( - cleanUpUndefined({ - ...tags, - [tagName]: undefined, - [newName]: newValue, - }) - ) + onUpdate([ + ...tags.slice(0, tagIndex), + [newName, newValue], + ...tags.slice(tagIndex + 1), + ]) } /> ))} - + + {newTag.on && ( + { + if (name.trim() !== "" && value.trim() !== "") { + setNewTag({ + on: false, + }); + onUpdate([...tags, [name, value]]); + } else { + setNewTag({ + on: true, + name, + value, + }); + } + }} + /> + )} + {!newTag.on && ( + + setNewTag({ + on: true, + name: "", + value: "", + }) + } + > + + + + )}
); @@ -55,8 +101,11 @@ export const MetadataBoxTag: FC<{ value: [string, string]; onChange: (t: [string, string]) => void; options: [string[], string[]]; + initialState?: [boolean, boolean]; }> = (props) => { - const [[editMode1, editMode2], setEditMode] = useState([false, false]); + const [[editMode1, editMode2], setEditMode] = useState( + props.initialState ? props.initialState : [false, false] + ); const toggle1 = () => setEditMode([!editMode1, editMode2]); const toggle2 = () => setEditMode([editMode1, !editMode2]); return ( diff --git a/webapp/src/components/AutomationList/TagDB.ts b/webapp/src/components/AutomationList/TagDB.ts index ed9fac3..13b2a66 100644 --- a/webapp/src/components/AutomationList/TagDB.ts +++ b/webapp/src/components/AutomationList/TagDB.ts @@ -2,23 +2,26 @@ import { AutomationData } from "types/automations"; export type TagDB = ReturnType; export const makeTagDB = (automations: AutomationData[]) => { - const allTags: Record> = {}; - automations.forEach(({ tags }) => - Object.keys(tags).forEach(tagName => { - const tagValue = tags[tagName]; - if (!allTags[tagName]) { - allTags[tagName] = new Set() - } - allTags[tagName].add(tagValue); - }) - ); - const allTagNames = Object.keys(allTags); - return { - getTagNames(myTags: string[]): string[] { - return allTagNames.filter(n => !myTags.includes(n)).sort() - }, - getTagValues(tagName: string): string[] { - return allTags[tagName] ? Array.from(allTags[tagName].values()).sort() : []; - } - } -} \ No newline at end of file + const allTags: Record> = {}; + automations.forEach(({ tags }) => + Object.keys(tags).forEach((tagName) => { + const tagValue = tags[tagName]; + if (!allTags[tagName]) { + allTags[tagName] = new Set(); + } + allTags[tagName].add(tagValue); + }) + ); + const allTagNames = Object.keys(allTags); + return { + getTagNames(myTags: string[]): string[] { + console.log(myTags, allTagNames); + return allTagNames.filter((n) => !myTags.includes(n)).sort(); + }, + getTagValues(tagName: string): string[] { + return allTags[tagName] + ? Array.from(allTags[tagName].values()).sort() + : []; + }, + }; +}; diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index 2bd101f..14cd05a 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -1,7 +1,7 @@ import "./index.css"; import "./index.mobile.css"; import { AutomationEditor } from "components/AutomationEditor"; -import { FC, ReactNode, useState } from "react"; +import { FC, ReactNode, useState, useRef, useEffect } from "react"; import { AutomationData } from "types/automations"; import { defaultAutomation } from "utils/defaults"; import { ButtonIcon } from "components/Icons/ButtonIcons"; @@ -15,6 +15,7 @@ import CircularProgress from "@mui/material/CircularProgress"; import { useCookies } from "react-cookie"; import { useHA } from "haService"; import { ArrowBack } from "@mui/icons-material"; +import { useSnackbar } from "notistack"; interface AutomationListParams { automations: AutomationData[]; @@ -60,12 +61,21 @@ export const AutomationList: FC = ({ children, }) => { // state + const snackbr = useSnackbar(); const { hideList, setHideList, current, setCurrent } = useAutomationListState(); const { isMobile } = useWindowSize(); + const firstRender = useRef(true); // alias const currentAuto = automations.length > 0 ? automations[current] : null; const tagDB = makeTagDB(automations); + // effect + useEffect(() => { + if (firstRender.current) { + snackbr.enqueueSnackbar("Saved.", { variant: "info" }); + } + firstRender.current = true; + }, [automations]); // render return (
= ({ > onUpdate(i, a)} onAdd={() => { onAdd(defaultAutomation(String(Date.now()))); setCurrent(automations.length);