better tag edit viz

This commit is contained in:
ari committed 2022-08-02 12:51:22 -04:00
1 parent 34826acb97
commit f4261f9701
10 files changed
+154 -64

No files matched your search

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