diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.css b/webapp/src/components/AutomationManager/ListBoxGroup/index.css index 6afa590..f50e3f8 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.css +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.css @@ -13,6 +13,10 @@ user-select: none; } +.automation-manager--list-box-group.modified { + border: 1px var(--mui-success-main) solid; +} + .automation-manager--list-box-group { border-color: var(--mui-text-secondary); border-width: 0.125px; diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx index 44cc6c6..45a3133 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx @@ -14,7 +14,10 @@ const { make, componentMeta } = makeStory({ args: { title: "Example", type: "items", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, data: [ { id: "1", @@ -23,7 +26,10 @@ const { make, componentMeta } = makeStory({ description: "Amazing automation!", tags: {}, state: "on", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, }, { id: "2", @@ -31,7 +37,10 @@ const { make, componentMeta } = makeStory({ entityId: "automation.2", description: "Cool!", state: "on", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, tags: { room: "main bedroom", routine: "night time", @@ -48,16 +57,25 @@ export const Basic = make({}); export const Nested = make({ title: "Top Group", type: "group", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, data: [ { title: "Subgroup 1", type: "group", - isSelected: true, + flags: { + isSelected: true, + isModified: false, + }, data: [ { type: "items", - isSelected: true, + flags: { + isSelected: true, + isModified: false, + }, title: "Subsub group", data: [ { @@ -66,7 +84,10 @@ export const Nested = make({ description: "", title: "Automation #1", state: "on", - isSelected: true, + flags: { + isSelected: true, + isModified: false, + }, tags: {}, }, { @@ -75,7 +96,10 @@ export const Nested = make({ description: "another one", title: "Automation #2", state: "off", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, tags: {}, }, { @@ -84,7 +108,10 @@ export const Nested = make({ description: "bad one", title: "Automation #3", state: "offing", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, tags: { type: "bad", }, @@ -96,20 +123,29 @@ export const Nested = make({ { title: "Subgroup 2", type: "group", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, data: [], }, { title: "Other", type: "items", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, data: [ { id: "4", entityId: "automation.4", description: "Cool thing", title: "Automation #4", - isSelected: false, + flags: { + isSelected: false, + isModified: false, + }, state: "on", tags: { type: "good", diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx index 2710b35..dffaafb 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx @@ -23,7 +23,7 @@ export const ListBoxGroup: FC = ({ title, type, data, - isSelected, + flags, initialOpenState, tagsDB, ...events @@ -41,7 +41,8 @@ export const ListBoxGroup: FC = ({
= ({ onDelete={() => events.onAutomationDelete(auto.id, auto.entityId) } + isSelected={auto.flags.isSelected} onSelect={() => events.onSelect(auto.id)} onDescriptionUpdate={(description) => events.onAutomationUpdate( diff --git a/webapp/src/components/AutomationManager/MetadataBox/Tags.tsx b/webapp/src/components/AutomationManager/MetadataBox/Tags.tsx index db55803..c5a8a3c 100644 --- a/webapp/src/components/AutomationManager/MetadataBox/Tags.tsx +++ b/webapp/src/components/AutomationManager/MetadataBox/Tags.tsx @@ -30,7 +30,7 @@ export type TagModalState = export const Tags: FC = ({ automationId, tagsDB }) => { const [modalState, setModal] = useState({ open: false }); - const tags = tagsDB.getTags(automationId); + const tags = tagsDB.getTags(automationId, true); const onUpdate = (t: Record) => tagsDB.update(automationId, t); diff --git a/webapp/src/components/AutomationManager/Sidebar/index.tsx b/webapp/src/components/AutomationManager/Sidebar/index.tsx index 2b7ad5b..87aa03b 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.tsx +++ b/webapp/src/components/AutomationManager/Sidebar/index.tsx @@ -60,7 +60,11 @@ export const AutomationManagerSidebar: FC = ({ ); const items = grouping.top.map((i) => - convertGroupingToItems(i, selectedAutomationId, automations, grouping) + convertGroupingToItems(i, automations, grouping, (aid, t) => ({ + ...t, + isSelected: aid === selectedAutomationId, + isModified: tagsDB.isModified(aid), + })) ); return ( diff --git a/webapp/src/components/AutomationManager/TagDB.ts b/webapp/src/components/AutomationManager/TagDB.ts index 4099ffa..905a6a6 100644 --- a/webapp/src/components/AutomationManager/TagDB.ts +++ b/webapp/src/components/AutomationManager/TagDB.ts @@ -1,4 +1,9 @@ import { useEffect, useRef, useState } from "react"; + +type TagDBState = { + original: Record>; + changed: Record>; +}; export type TagDB = ReturnType; export const useTagDB = ( automations: Array<{ @@ -8,28 +13,30 @@ export const useTagDB = ( onUpdate: (aid: string, tags: Record) => void ) => { const lastAuto = useRef(JSON.stringify(automations)); - const [{ automationTags, changed }, setState] = useState({ - automationTags: convertAutoListToMap(automations), - changed: new Set(), + const [{ original, changed }, setState] = useState({ + original: convertAutoListToMap(automations), + changed: {}, }); - const allTags = computeAllTags(automationTags); + const allTags = computeAllTags(original); const allTagNames = Object.keys(allTags); - const isModified = changed.size > 0; - + const isModified = Object.keys(changed).length > 0; useEffect(() => { const c = JSON.stringify(automations); if (lastAuto.current !== c) { lastAuto.current = c; setState({ - automationTags: convertAutoListToMap(automations), - changed: new Set(), + original: convertAutoListToMap(automations), + changed: {}, }); } }, [automations, setState]); return { - getTags(automationId: string) { - return automationTags[automationId] ?? {}; + getTags(automationId: string, includeChanged: boolean = false) { + if (includeChanged) { + return changed[automationId] ?? original[automationId] ?? {}; + } + return original[automationId] ?? {}; }, getTagNames(myTags: string[]): string[] { return allTagNames.filter((n) => !myTags.includes(n)).sort(); @@ -39,27 +46,33 @@ export const useTagDB = ( ? Array.from(allTags[tagName].values()).sort() : []; }, - isModified(aid: string) { - return changed.has(aid); + isModified(aid: string): boolean { + return aid in changed; }, update(aid: string, tags: Record) { - if (JSON.stringify(tags) !== JSON.stringify(automationTags[aid])) { - setState({ - changed: changed.add(aid), - automationTags: { - ...automationTags, - [aid]: tags, - }, - }); + const update = JSON.stringify(tags); + if (aid in changed) { + if (update === JSON.stringify(changed[aid])) { + return; + } + } else if (update === JSON.stringify(original[aid])) { + return; } + setState({ + changed: { + ...changed, + [aid]: tags, + }, + original, + }); }, save(aid: string | null) { if (isModified) { if (aid) { - onUpdate(aid, automationTags[aid]); + onUpdate(aid, changed[aid]); } else { - for (const aid of changed.values()) { - onUpdate(aid, automationTags[aid]); + for (const [aid, change] of Object.entries(changed)) { + onUpdate(aid, change); } } } diff --git a/webapp/src/components/AutomationManager/automationGrouper.test.ts b/webapp/src/components/AutomationManager/automationGrouper.test.ts index bffe0bf..a90a245 100644 --- a/webapp/src/components/AutomationManager/automationGrouper.test.ts +++ b/webapp/src/components/AutomationManager/automationGrouper.test.ts @@ -99,7 +99,7 @@ test("bigAutoList groupings has not duplicate ids", () => { test("convert grouper to items when no tags are selected", () => { const grouping = makeGrouping(automations, [], null); const items = grouping.top.map((i) => - convertGroupingToItems(i, "", automations, grouping) + convertGroupingToItems(i, automations, grouping) ); expect(items.filter((i) => i.type === "group")).toHaveLength(0); expect(items).toHaveLength(1); @@ -109,7 +109,7 @@ test("convert grouper to items when no tags are selected", () => { test("convert grouper to items when 1 tag is selected", () => { const grouping = makeGrouping(automations, ["Room"], null); const items = grouping.top.map((i) => - convertGroupingToItems(i, "", automations, grouping) + convertGroupingToItems(i, automations, grouping) ); expect(grouping.top).toHaveLength(3); expect(items.filter((i) => i.type === "group")).toHaveLength(0); @@ -125,7 +125,7 @@ test("convert grouper to items when 1 tag is selected", () => { test("convert grouper to items when 2 tag is selected", () => { const grouping = makeGrouping(automations, ["Room", "Type"], null); const items = grouping.top.map((i) => - convertGroupingToItems(i, "", automations, grouping) + convertGroupingToItems(i, automations, grouping) ); expect(grouping.top).toHaveLength(3); expect(items.filter((i) => i.type === "group")).toHaveLength(2); diff --git a/webapp/src/components/AutomationManager/automationGrouper.tsx b/webapp/src/components/AutomationManager/automationGrouper.tsx index 3d8de4d..6022859 100644 --- a/webapp/src/components/AutomationManager/automationGrouper.tsx +++ b/webapp/src/components/AutomationManager/automationGrouper.tsx @@ -1,4 +1,9 @@ -import { AutomationManagerAuto, AutomationManagerItem } from "./types"; +import { + AutomationManagerAuto, + AutomationManagerAutoItem, + AutomationManagerItem, + AutomationManagerItemFlags, +} from "./types"; const makeGrouper = () => { const groupIdNames: string[] = []; @@ -149,33 +154,59 @@ export const makeGrouping = ( export const convertGroupingToItems = ( currentGroupIdx: number, - selectedAutomationId: string | null, autos: AutomationManagerAuto[], - grouping: AutomationGrouping + grouping: AutomationGrouping, + computeFlags: ( + aid: string, + f: AutomationManagerItemFlags + ) => AutomationManagerItemFlags = (aid, t) => t ): AutomationManagerItem => { const groupData = grouping.getData(currentGroupIdx); const automationIds = grouping.getAutomations(currentGroupIdx); if (automationIds.length > 0) { + const data = automationIds.map((i) => { + return { + ...autos[i], + flags: computeFlags(autos[i].id, { + isSelected: false, + isModified: false, + }), + }; + }); return { type: "items", title: groupData.name, - data: automationIds.map((i) => { - return { - ...autos[i], - isSelected: autos[i].id === selectedAutomationId, - }; - }), - isSelected: groupData.selected, + data, + flags: data.reduce( + (out, a) => ({ + isSelected: out.isSelected || a.flags.isSelected, + isModified: out.isModified || a.flags.isModified, + }), + { + isSelected: false, + isModified: false, + } + ), }; } const subgroups = grouping.getSubGroups(currentGroupIdx); + const data = subgroups.map((i) => + convertGroupingToItems(i, autos, grouping, computeFlags) + ); return { type: "group", title: groupData.name, - data: subgroups.map((i) => - convertGroupingToItems(i, selectedAutomationId, autos, grouping) + data, + flags: data.reduce( + (out, a) => ({ + isSelected: out.isSelected || a.flags.isSelected, + isModified: out.isModified || a.flags.isModified, + }), + { + isSelected: false, + isModified: false, + } ), - isSelected: groupData.selected, }; }; diff --git a/webapp/src/components/AutomationManager/types.ts b/webapp/src/components/AutomationManager/types.ts index fdb9cf0..3c0bc34 100644 --- a/webapp/src/components/AutomationManager/types.ts +++ b/webapp/src/components/AutomationManager/types.ts @@ -10,10 +10,16 @@ export type AutomationManagerAuto = AutomationManagerAutoUpdatable & { issue?: string; isNew?: boolean; }; - +export type AutomationManagerAutoItem = AutomationManagerAuto & { + flags: AutomationManagerItemFlags; +}; +export type AutomationManagerItemFlags = { + isSelected: boolean; + isModified: boolean; +}; export type AutomationManagerItem = { title: string; - isSelected: boolean; + flags: AutomationManagerItemFlags; } & ( | { type: "group"; @@ -21,6 +27,6 @@ export type AutomationManagerItem = { } | { type: "items"; - data: Array; + data: Array; } ); diff --git a/webapp/src/haService/useHassCollection.ts b/webapp/src/haService/useHassCollection.ts index 88dfdea..b06450d 100644 --- a/webapp/src/haService/useHassCollection.ts +++ b/webapp/src/haService/useHassCollection.ts @@ -41,12 +41,6 @@ export const useHassCollection = ( } const unsub = collection.subscribe((d) => { if (d) { - console.log( - "updating with", - Object.keys(d).length, - Object.fromEntries(Object.entries(d).map(([k, v]) => [k, v.state])), - getHACollection - ); setState({ ready: true, collection: d,