cleaned up styles a bit

This commit is contained in:
ari committed 2022-06-19 13:38:43 -04:00
1 parent 3acbaf576d
commit 451b3638bf
28 files changed
+864 -705

No files matched your search

@@ -1,5 +1,5 @@
import "./AutomationListBox.css";
import { FC, useState } from 'react';
import { FC, useState } from "react";
import { AutomationData } from "types/automations";
import InputMultiSelect from "components/Inputs/InputMultiSelect";
import InputText from "components/Inputs/InputText";
@@ -8,126 +8,135 @@ import { makeGrouping } from "./automationGrouper";
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
import { useCookies } from "react-cookie";
export type Props = {
automations: AutomationData[];
selected: number;
onSelectAutomation: (i: number) => void;
onRemove: (i: number) => void;
onAdd: () => void;
}
automations: AutomationData[];
selected: number;
onSelectAutomation: (i: number) => void;
onRemove: (i: number) => void;
onAdd: () => void;
};
const useAutomationListBoxState = () => {
const [
cookies,
setCookies,
// eslint-disable-next-line
_
] = useCookies(['albSearchText', 'albSelected']);
let initialSelected = [];
try {
initialSelected = JSON.parse(cookies.albSelected)
if (!Array.isArray(initialSelected)) {
initialSelected = [initialSelected]
}
initialSelected = initialSelected.map(Number)
// eslint-disable-next-line
} catch (_) { }
const [searchText, setSearchText] = useState(cookies.albSearchText ?? "");
const [selectedTagIdx, setSelectedTagIdx] = useState<number[]>(initialSelected);
return {
searchText,
setSearchText(v: string) {
setSearchText(v)
setCookies('albSearchText', v)
},
setSelectedTagIdx(n: number[]) {
setSelectedTagIdx(n)
setCookies('albSelected', JSON.stringify(n))
},
selectedTagIdx,
const [
cookies,
setCookies,
// eslint-disable-next-line
_,
] = useCookies(["albSearchText", "albSelected"]);
let initialSelected = [];
try {
initialSelected = JSON.parse(cookies.albSelected);
if (!Array.isArray(initialSelected)) {
initialSelected = [initialSelected];
}
}
initialSelected = initialSelected.map(Number);
// eslint-disable-next-line
} catch (_) {}
const [searchText, setSearchText] = useState(cookies.albSearchText ?? "");
const [selectedTagIdx, setSelectedTagIdx] =
useState<number[]>(initialSelected);
return {
searchText,
setSearchText(v: string) {
setSearchText(v);
setCookies("albSearchText", v);
},
setSelectedTagIdx(n: number[]) {
setSelectedTagIdx(n);
setCookies("albSelected", JSON.stringify(n));
},
selectedTagIdx,
};
};
export const AutomationListBox: FC<Props> = ({
automations,
selected,
onSelectAutomation,
onAdd,
onRemove,
automations,
selected,
onSelectAutomation,
onAdd,
onRemove,
}) => {
const tags = getTagList(automations);
const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } =
useAutomationListBoxState();
const filteredAutomations = filterAutomations(
automations,
searchText.toLowerCase()
);
const grouping = makeGrouping(
filteredAutomations.map(([a, _]) => a),
selectedTagIdx.map((i) => tags[i]),
selected
);
const tags = getTagList(automations);
const {
searchText,
setSearchText,
selectedTagIdx,
setSelectedTagIdx,
} = useAutomationListBoxState();
const filteredAutomations = filterAutomations(automations, searchText.toLowerCase(), selectedTagIdx, tags);
const grouping = makeGrouping(
filteredAutomations.map(([a, _]) => a),
selectedTagIdx.map(i => tags[i]),
selected,
)
return <div className="automation-list-box">
<div className="automation-list-box--nav">
<InputText
label="Search"
value={searchText}
onChange={setSearchText}
/>
<InputMultiSelect
label="Tags"
selected={selectedTagIdx}
onChange={setSelectedTagIdx}
options={tags}
max={3}
/>
</div>
<div className="automation-list-box--body">
<AutomationListBoxGroup
events={{
onSelectAutomation,
onRemove
}}
autos={filteredAutomations}
grouping={grouping}
selectedAutomationIdx={selected}
/>
</div>
<div className="automation-list-box--bottom">
<Button onClick={onAdd}>Add</Button>
<span>Showing Automations: {filteredAutomations.length} out of {automations.length}</span>
</div>
return (
<div className="automation-list-box">
<div className="automation-list-box--nav">
<InputText label="Search" value={searchText} onChange={setSearchText} />
<InputMultiSelect
label="Tags"
selected={selectedTagIdx}
onChange={setSelectedTagIdx}
options={tags}
max={3}
/>
</div>
<div className="automation-list-box--body">
<AutomationListBoxGroup
events={{
onSelectAutomation,
onRemove,
}}
autos={filteredAutomations}
grouping={grouping}
selectedAutomationIdx={selected}
/>
</div>
<div className="automation-list-box--bottom">
<Button onClick={onAdd}>Add</Button>
<span>
Showing Automations: {filteredAutomations.length} out of{" "}
{automations.length}
</span>
</div>
</div>
}
);
};
export const getTagList = (autos: AutomationData[]): string[] => Object.keys(autos
.map(a => Object.keys(a.tags))
.reduce((all: any, keys) => {
export const getTagList = (autos: AutomationData[]): string[] =>
Object.keys(
autos
.map((a) => Object.keys(a.tags))
.reduce((all: any, keys) => {
for (const k of keys) {
all[k] = 1
all[k] = 1;
}
return all;
}, {}))
}, {})
);
const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]): Array<[AutomationData, number]> => autos.map<[AutomationData, number]>((a, i) => [a, i]).filter(([a, i]) => {
if (!a.metadata) {
return true
}
if (searchText.length > 0) {
const filterAutomations = (
autos: AutomationData[],
searchText: string
): Array<[AutomationData, number]> =>
autos
.map<[AutomationData, number]>((a, i) => [a, i])
.filter(([a, i]) => {
if (!a.metadata) {
return true;
}
if (searchText.length > 0) {
if (
!(
(a.metadata.description ?? "").toLocaleLowerCase().includes(searchText) ||
(a.metadata.alias ?? "").toLocaleLowerCase().includes(searchText)
)
!(
(a.metadata.description ?? "")
.toLocaleLowerCase()
.includes(searchText) ||
(a.metadata.alias ?? "").toLocaleLowerCase().includes(searchText)
)
) {
return false
return false;
}
}
return true
})
}
return true;
});
@@ -1,141 +1,166 @@
import { TrashIcon } from "components/Icons";
import { Button } from "components/Inputs/Button";
import { MetadataBox } from "./AutomationMetadataBox";
import { AutomationData } from "types/automations";
import { AutomationGrouping } from "./automationGrouper";
import { FC, useState } from "react";
import { ButtonIcon } from "components/Icons/ButtonIcons";
export type AutomationListBoxEvents = {
onSelectAutomation: (i: number) => void,
onRemove: (i: number) => void,
}
onSelectAutomation: (i: number) => void;
onRemove: (i: number) => void;
};
export type AutomationListBoxGroups = {
grouping: AutomationGrouping,
currentNode: number,
}
grouping: AutomationGrouping;
currentNode: number;
};
export type AutomationListBoxProps = {
events: AutomationListBoxEvents,
groups: AutomationListBoxGroups,
autos: Array<[AutomationData, number]>,
selectedAutomationIdx: number,
events: AutomationListBoxEvents;
groups: AutomationListBoxGroups;
autos: Array<[AutomationData, number]>;
selectedAutomationIdx: number;
};
export type AutomationListBoxGroupProps = {
events: AutomationListBoxEvents,
grouping: AutomationGrouping,
autos: Array<[AutomationData, number]>,
selectedAutomationIdx: number,
}
events: AutomationListBoxEvents;
grouping: AutomationGrouping;
autos: Array<[AutomationData, number]>;
selectedAutomationIdx: number;
};
export const AutomationListBoxGroup: FC<AutomationListBoxGroupProps> = ({
events, grouping, autos, selectedAutomationIdx,
events,
grouping,
autos,
selectedAutomationIdx,
}) => {
if (grouping.top.length === 1) {
return <>
{convertGroupsToItems(autos, {
grouping, currentNode: grouping.top[0],
}, events, selectedAutomationIdx)}
</>
} else {
return <>
{grouping.top.map(groupIdx => <AutomationListBoxGroupItem
key={groupIdx}
autos={autos}
groups={{
grouping,
currentNode: groupIdx
}}
events={events}
selectedAutomationIdx={selectedAutomationIdx}
/>)}
</>
}
}
export const convertGroupsToItems = (
autos: Array<[AutomationData, number]>,
{ grouping, currentNode }: AutomationListBoxGroups,
events: AutomationListBoxEvents,
selectedAutomationIdx: number,
) => {
const automations = grouping.getAutomations(currentNode);
if (automations.length > 0) {
return automations.map(autoIndex =>
<AutomationListBoxItem
key={`${autoIndex}-${autos[autoIndex][1]}`}
events={events}
auto={autos[autoIndex]}
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
/>
)
}
const subgroups = grouping.getSubGroups(currentNode);
if (subgroups.length > 0) {
return subgroups.map(groupIdx => <AutomationListBoxGroupItem
if (grouping.top.length === 1) {
return (
<>
{convertGroupsToItems(
autos,
{
grouping,
currentNode: grouping.top[0],
},
events,
selectedAutomationIdx
)}
</>
);
} else {
return (
<>
{grouping.top.map((groupIdx) => (
<AutomationListBoxGroupItem
key={groupIdx}
autos={autos}
groups={{
grouping,
currentNode: groupIdx
grouping,
currentNode: groupIdx,
}}
events={events}
selectedAutomationIdx={selectedAutomationIdx}
/>)
}
return <></>
}
/>
))}
</>
);
}
};
export const convertGroupsToItems = (
autos: Array<[AutomationData, number]>,
{ grouping, currentNode }: AutomationListBoxGroups,
events: AutomationListBoxEvents,
selectedAutomationIdx: number
) => {
const automations = grouping.getAutomations(currentNode);
if (automations.length > 0) {
return automations.map((autoIndex) => (
<AutomationListBoxItem
key={`${autoIndex}-${autos[autoIndex][1]}`}
events={events}
auto={autos[autoIndex]}
isSelected={selectedAutomationIdx === autos[autoIndex][1]}
/>
));
}
const subgroups = grouping.getSubGroups(currentNode);
if (subgroups.length > 0) {
return subgroups.map((groupIdx) => (
<AutomationListBoxGroupItem
key={groupIdx}
autos={autos}
groups={{
grouping,
currentNode: groupIdx,
}}
events={events}
selectedAutomationIdx={selectedAutomationIdx}
/>
));
}
return <></>;
};
export const AutomationListBoxGroupItem: FC<{
events: AutomationListBoxEvents,
groups: AutomationListBoxGroups,
autos: Array<[AutomationData, number]>,
selectedAutomationIdx: number,
}> = ({
autos, groups, events, selectedAutomationIdx,
}) => {
const groupData = groups.grouping.getData(groups.currentNode);
const automations = groups.grouping.getAutomations(groups.currentNode);
const [open, setOpen] = useState(false);
const isOther = groupData.isOther;
const openIcon = !open ? "⊕" : "⊖";
const total = isOther ? <small>
{automations.length} automation{automations.length !== 1 ? 's' : ''} {groupData.isOther ? "" : "(untagged)"}
</small> : <></>
return <div className={[
"automation-list-box--body--group",
isOther ? 'other' : '',
groupData.selected ? 'selected' : '',
].join(' ')}>
<b onClick={() => setOpen(!open)}>{groupData.name} {openIcon} {total}</b>
{open && convertGroupsToItems(autos, groups, events, selectedAutomationIdx)}
</div>
}
events: AutomationListBoxEvents;
groups: AutomationListBoxGroups;
autos: Array<[AutomationData, number]>;
selectedAutomationIdx: number;
}> = ({ autos, groups, events, selectedAutomationIdx }) => {
const groupData = groups.grouping.getData(groups.currentNode);
const automations = groups.grouping.getAutomations(groups.currentNode);
const [open, setOpen] = useState(false);
const isOther = groupData.isOther;
const openIcon = !open ? "⊕" : "⊖";
const total = isOther ? (
<small>
{automations.length} automation{automations.length !== 1 ? "s" : ""}{" "}
{groupData.isOther ? "" : "(untagged)"}
</small>
) : (
<></>
);
return (
<div
className={[
"automation-list-box--body--group",
isOther ? "other" : "",
groupData.selected ? "selected" : "",
].join(" ")}
>
<b onClick={() => setOpen(!open)}>
{groupData.name} {openIcon} {total}
</b>
{open &&
convertGroupsToItems(autos, groups, events, selectedAutomationIdx)}
</div>
);
};
export const AutomationListBoxItem: FC<{
auto: [AutomationData, number],
isSelected: boolean,
events: AutomationListBoxEvents,
auto: [AutomationData, number];
isSelected: boolean;
events: AutomationListBoxEvents;
}> = ({
events: {
onRemove,
onSelectAutomation,
},
auto: [
auto, autoIndex
],
isSelected,
events: { onRemove, onSelectAutomation },
auto: [auto, autoIndex],
isSelected,
}) => {
let title = "BadAuto<<Missing Metadata>>"
if (auto.metadata) {
title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`
}
return <div
key={autoIndex}
className={["automation-list-box--body--item", isSelected ? 'selected' : ''].join(' ')}
title={title}
onClick={() => onSelectAutomation(autoIndex)}
>
<MetadataBox metadata={auto.metadata} tags={auto.tags} />
<Button onClick={() => onRemove(autoIndex)}><TrashIcon /></Button>
</div>
}
let title = "BadAuto<<Missing Metadata>>";
if (auto.metadata) {
title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`;
}
return (
<div
key={autoIndex}
className={[
"automation-list-box--body--item",
isSelected ? "selected" : "",
].join(" ")}
title={title}
onClick={() => onSelectAutomation(autoIndex)}
>
<MetadataBox metadata={auto.metadata} tags={auto.tags} />
<ButtonIcon onClick={() => onRemove(autoIndex)} icon={<TrashIcon />} />
</div>
);
};
@@ -50,6 +50,7 @@
.automation-list--list.hide>.automation-list--list-hide {
transform: rotate(180deg);
left: 0;
}
/* list children: normal */
@@ -58,8 +59,10 @@
border-width: 1px;
}
.automation-list--list-hide {
.automation-list--list .automation-list--list-hide {
position: absolute;
bottom: 0.5em;
right: 0.5em;
bottom: 0em;
left: 0.5em;
max-width: 1.5em;
max-height: 1.5em;
}
@@ -14,7 +14,7 @@
.automation-list--root.mobile .automation-list--list-hide {
transform: rotate(90deg);
left: 1.25em;
left: 0.25em;
}
.automation-list--root.mobile .automation-editor--info-box {
@@ -33,7 +33,7 @@
}
.automation-list--root.mobile .automation-editor--info-box--icon {
left: 1.25em;
left: 0.5em;
bottom: 0em;
background-color: var(--primary);
transform: rotate(90deg);
@@ -50,8 +50,8 @@
.automation-list--root.mobile .automation-list--list.hide .automation-list--list-hide {
transform: rotate(270deg);
position: fixed;
top: 0.75em;
z-index: 20;
top: 0;
z-index: 5;
}
/* info box hide */
@@ -64,9 +64,9 @@
.automation-list--root.mobile .automation-editor--info-box.hide .automation-editor--info-box--icon {
transform: rotate(270deg);
position: fixed;
top: 0.75em;
background-color: var(--green-opaque);
z-index: 30;
top: 0em;
left: 0.25em;
z-index: 10;
}
@@ -4,7 +4,6 @@ import { AutomationEditor } from "components/AutomationEditor";
import { FC, ReactNode, useState } from "react";
import { AutomationData } from "types/automations";
import { defaultAutomation } from "utils/defaults";
import { ArrowIcon } from "components/Icons";
import { ButtonIcon } from "components/Icons/ButtonIcons";
import { ApiService } from "apiService/core";
import { AutomationListBox } from "./AutomationListBox";
@@ -15,6 +14,7 @@ import LinearProgress from "@mui/material/LinearProgress";
import CircularProgress from "@mui/material/CircularProgress";
import { useCookies } from "react-cookie";
import { useHA } from "haService";
import { ArrowBack } from "@mui/icons-material";
interface AutomationListParams {
automations: AutomationData[];
@@ -97,7 +97,7 @@ export const AutomationList: FC<AutomationListParams> = ({
<ButtonIcon
onClick={() => setHideList(!hideList)}
className="automation-list--list-hide"
Icon={ArrowIcon}
icon={<ArrowBack />}
/>
</div>
<div className="automation-list--viewer">