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;
}
.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;
@@ -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",
@@ -23,7 +23,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
title,
type,
data,
isSelected,
flags,
initialOpenState,
tagsDB,
...events
@@ -41,7 +41,8 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
<div
className={[
"automation-manager--list-box-group",
isSelected ? "selected" : "",
flags.isSelected ? "selected" : "",
flags.isModified ? "modified" : "",
].join(" ")}
>
<div
@@ -67,6 +68,7 @@ export const ListBoxGroup: FC<ListBoxGroupProps> = ({
onDelete={() =>
events.onAutomationDelete(auto.id, auto.entityId)
}
isSelected={auto.flags.isSelected}
onSelect={() => events.onSelect(auto.id)}
onDescriptionUpdate={(description) =>
events.onAutomationUpdate(
@@ -30,7 +30,7 @@ export type TagModalState =
export const Tags: FC<TagsProps> = ({ automationId, tagsDB }) => {
const [modalState, setModal] = useState<TagModalState>({ open: false });
const tags = tagsDB.getTags(automationId);
const tags = tagsDB.getTags(automationId, true);
const onUpdate = (t: Record<string, string>) =>
tagsDB.update(automationId, t);
@@ -60,7 +60,11 @@ export const AutomationManagerSidebar: FC<AutomationManagerSidebarProps> = ({
);
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 (
@@ -1,4 +1,9 @@
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 const useTagDB = (
automations: Array<{
@@ -8,28 +13,30 @@ export const useTagDB = (
onUpdate: (aid: string, tags: Record<string, string>) => void
) => {
const lastAuto = useRef(JSON.stringify(automations));
const [{ automationTags, changed }, setState] = useState({
automationTags: convertAutoListToMap(automations),
changed: new Set<string>(),
const [{ original, changed }, setState] = useState<TagDBState>({
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<string, string>) {
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);
}
}
}
@@ -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);
@@ -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<AutomationManagerAutoItem>((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<AutomationManagerItemFlags>(
(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<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;
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<AutomationManagerAuto & { isSelected: boolean }>;
data: Array<AutomationManagerAutoItem>;
}
);
@@ -41,12 +41,6 @@ export const useHassCollection = <T, D extends {} = {}>(
}
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,