handling missing ids better + more tranlsations

This commit is contained in:
ari committed 2022-09-01 17:21:00 -04:00
1 parent 69a9805c02
commit 774ebe482a
45 files changed
+556 -262

No files matched your search

@@ -23,6 +23,7 @@ const { make, componentMeta } = makeStory({
id: "1",
title: "Example",
entityId: "automation.1",
readonly: false,
description: "Amazing automation!",
tags: {},
state: "on",
@@ -38,6 +39,7 @@ const { make, componentMeta } = makeStory({
id: "2",
title: "Example 2",
entityId: "automation.2",
readonly: false,
description: "Cool!",
state: "on",
source_file: "automation.yaml",
@@ -89,6 +91,7 @@ export const Nested = make({
entityId: "automation.1",
description: "",
title: "Automation #1",
readonly: false,
state: "on",
source_file: "automation.yaml",
source_file_type: "list",
@@ -102,6 +105,7 @@ export const Nested = make({
{
id: "2",
entityId: "automation.2",
readonly: false,
description: "another one",
title: "Automation #2",
state: "off",
@@ -117,6 +121,7 @@ export const Nested = make({
{
id: "3",
entityId: "automation.3",
readonly: false,
description: "bad one",
title: "Automation #3",
state: "offing",
@@ -153,6 +158,7 @@ export const Nested = make({
},
data: [
{
readonly: false,
id: "4",
entityId: "automation.4",
description: "Cool thing",
@@ -52,6 +52,7 @@ export const useAutomationDB = (
const auto = defaultAutomation(String(Date.now()));
setNewAuto([
{
readonly: false,
id: auto.id,
entityId: "",
title: auto.alias ?? "",
@@ -77,13 +78,18 @@ const genMapping = (
tagsDB: TagDB,
langStore: LangStore
) => {
const configData: Record<string, AutomationData> = configAutomations.reduce(
(all, n) => ({
...all,
[n.id]: n,
}),
{}
);
const configDataNoId: AutomationData[] = [];
const configData: Record<string, AutomationData & { id: string }> =
configAutomations.reduce((all, n) => {
if (n.id) {
return {
...all,
[n.id]: n,
};
}
configDataNoId.push(n);
return all;
}, {});
const automatioinMap: Record<
string,
[AutomationManagerAuto, AutomationData | null]
@@ -91,7 +97,7 @@ const genMapping = (
Object.entries(haEntities)
.filter(([entityId, _]) => entityId.toLowerCase().startsWith("automation."))
.forEach(([entityId, entityData], i) => {
const autoId = entityData.attributes.id ?? `${entityData.state}-${i}`;
const autoId = entityData.attributes.id ?? entityId.split(".")[1];
let title = entityData.attributes.friendly_name ?? "";
let description = "";
let issue: string | undefined = ["on", "off"].includes(entityData.state)
@@ -127,12 +133,13 @@ const genMapping = (
source_file_type,
configuration_key,
issue,
readonly: found == null,
},
found,
];
});
Object.values(configData).forEach((auto) => {
Object.values(configData).forEach((auto, i) => {
automatioinMap[auto.id] = [
{
id: auto.id,
@@ -145,10 +152,55 @@ const genMapping = (
source_file_type: auto.source_file_type,
configuration_key: auto.configuration_key,
issue: langStore.get("ISSUES_NO_LOAD_AUTOMATION"),
readonly: false,
},
auto,
];
});
let failedGeneration: number = 0;
configDataNoId.forEach((auto) => {
let generatedAutoId = `automation_${failedGeneration + 1}`;
if (auto.alias) {
generatedAutoId = auto.alias
.toLowerCase()
.replaceAll(/\W/g, " ")
.trim()
.replaceAll(" ", "_");
} else {
failedGeneration += 1;
}
if (automatioinMap[generatedAutoId]) {
automatioinMap[generatedAutoId] = [
{
...automatioinMap[generatedAutoId][0],
issue: langStore.get("ISSUE_MUST_CONTAIN_A_UNQIUE_ID_TO_EDIT", {
sourceFile: auto.source_file,
}),
readonly: true,
},
auto,
];
} else {
automatioinMap[generatedAutoId] = [
{
id: generatedAutoId,
entityId: "",
title: auto.alias ?? "",
description: auto.description ?? "",
state: langStore.get("AUTOMATION_STATE_UNREGISTERED"),
tags: {},
source_file: auto.source_file,
source_file_type: auto.source_file_type,
configuration_key: auto.configuration_key,
issue: langStore.get("ISSUE_MUST_CONTAIN_A_UNQIUE_ID_TO_EDIT", {
sourceFile: auto.source_file,
}),
readonly: true,
},
auto,
];
}
});
return automatioinMap;
};
@@ -50,6 +50,8 @@ export const AutomationManagerLoaded: FC<
{state.currentAutomationId !== null ? (
state.currentAutomation ? (
<AutomationEditor
readonly={state.currentAutomationIsReadOnly}
issue={state.currentAutomationIssue}
dims={{
...DEFAULT_DIMS,
flipped: true,
@@ -37,7 +37,11 @@ export const useAutomationManagerState = ({
.map(({ id, tags = {} }) => ({
id,
tags,
})),
}))
.filter(({ id }) => typeof id === "string") as Array<{
id: string;
tags: Record<string, string>;
}>,
onUpdateTags
);
const automationDB = useAutomationDB(hassEntities, configAutomations, tagsDB);
@@ -78,6 +82,14 @@ export const useAutomationManagerState = ({
get currentAutomationEntityId(): string | null {
return currentAutomation === null ? null : currentAutomation[0].entityId;
},
get currentAutomationIsReadOnly(): boolean {
return currentAutomation === null ? true : currentAutomation[0].readonly;
},
get currentAutomationIssue(): string | undefined {
return currentAutomation === null
? undefined
: currentAutomation[0].issue;
},
async setSelectedAutomationId(i: string | null, force: boolean = false) {
if (methods.currentAutomationIsNew && !force) {
if (!(await stillHaveNewAreYouSure())) {
@@ -94,7 +106,9 @@ export const useAutomationManagerState = ({
}
}
const auto = automationDB.addNew();
methods.setSelectedAutomationId(auto.id, true);
if (auto.id) {
methods.setSelectedAutomationId(auto.id, true);
}
},
editorUpdateAutomation(auto: AutomationData | BareAutomationData) {
if (methods.currentAutomationIsNew) {
@@ -71,6 +71,7 @@ const story = makeStory({
source_file: "automation.yaml",
source_file_type: "list",
configuration_key: ["automation"],
readonly: false,
},
]);
setSelAutoId(id);
@@ -100,6 +101,7 @@ const makeAuto = (
source_file: "automation.yaml",
source_file_type: "list",
configuration_key: ["automation"],
readonly: false,
...data,
};
};
@@ -38,10 +38,11 @@ const automations: AutomationManagerAuto[] = [
description: a.description ?? "",
state: "on",
tags: a.tags,
id: a.id,
id: a.id as string,
isSelected: false,
source_file: "automation.yaml",
source_file_type: "list",
readonly: false,
configuration_key: ["automation"],
}));
@@ -51,7 +52,8 @@ const bigList = bigMockAutoList.map<AutomationManagerAuto>((a) => ({
description: a.description ?? "",
state: "on",
tags: a.tags,
id: a.id,
id: a.id as string,
readonly: false,
isSelected: false,
source_file: "automation.yaml",
source_file_type: "list",
@@ -12,6 +12,7 @@ export type AutomationManagerAuto = AutomationManagerAutoUpdatable & {
source_file: string;
source_file_type: string;
configuration_key: string[];
readonly: boolean;
};
export type AutomationManagerAutoItem = AutomationManagerAuto & {
flags: AutomationManagerItemFlags;