Skip to content

Commit

Permalink
Better typings
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed May 21, 2024
1 parent de26e53 commit 3021768
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
16 changes: 9 additions & 7 deletions src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import {
computeCards,
computeSection,
} from "../../common/generate-lovelace-config";
import {
findLovelaceContainer,
parseLovelaceContainerPath,
} from "../lovelace-path";
import "./hui-card-picker";
import "./hui-entity-picker-table";
import { CreateCardDialogParams } from "./show-create-card-dialog";
import { showEditCardDialog } from "./show-edit-card-dialog";
import { showSuggestCardDialog } from "./show-suggest-card-dialog";
import {
findLovelaceContainer,
parseLovelaceContainerPath,
} from "../lovelace-path";

declare global {
interface HASSDomEvents {
Expand Down Expand Up @@ -274,15 +274,17 @@ export class HuiCreateDialogCard

let sectionOptions: Partial<LovelaceSectionConfig> = {};

const { sectionIndex } = parseLovelaceContainerPath(this._params!.path);
const { viewIndex, sectionIndex } = parseLovelaceContainerPath(
this._params!.path
);
const isSection = sectionIndex !== undefined;

// If we are in a section, we want to keep the section options for the preview
if (isSection) {
const containerConfig = findLovelaceContainer(
this._params!.lovelaceConfig!,
this._params!.path!
) as LovelaceSectionConfig;
[viewIndex, sectionIndex]
);
if (!isStrategySection(containerConfig)) {
const { cards, title, ...rest } = containerConfig;
sectionOptions = rest;
Expand Down
11 changes: 4 additions & 7 deletions src/panels/lovelace/editor/config-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const addSection = (
viewIndex: number,
sectionConfig: LovelaceSectionRawConfig
): LovelaceConfig => {
const view = findLovelaceContainer(config, [viewIndex]) as LovelaceViewConfig;
const view = findLovelaceContainer(config, [viewIndex]);
if (isStrategyView(view)) {
throw new Error("Deleting sections in a strategy is not supported.");
}
Expand All @@ -246,7 +246,7 @@ export const deleteSection = (
viewIndex: number,
sectionIndex: number
): LovelaceConfig => {
const view = findLovelaceContainer(config, [viewIndex]) as LovelaceViewConfig;
const view = findLovelaceContainer(config, [viewIndex]);
if (isStrategyView(view)) {
throw new Error("Deleting sections in a strategy is not supported.");
}
Expand All @@ -267,7 +267,7 @@ export const insertSection = (
sectionIndex: number,
sectionConfig: LovelaceSectionRawConfig
): LovelaceConfig => {
const view = findLovelaceContainer(config, [viewIndex]) as LovelaceViewConfig;
const view = findLovelaceContainer(config, [viewIndex]);
if (isStrategyView(view)) {
throw new Error("Inserting sections in a strategy is not supported.");
}
Expand All @@ -291,10 +291,7 @@ export const moveSection = (
fromPath: [number, number],
toPath: [number, number]
): LovelaceConfig => {
const section = findLovelaceContainer(
config,
fromPath
) as LovelaceSectionRawConfig;
const section = findLovelaceContainer(config, fromPath);

let newConfig = deleteSection(config, fromPath[0], fromPath[1]);
newConfig = insertSection(newConfig, toPath[0], toPath[1], section);
Expand Down
10 changes: 9 additions & 1 deletion src/panels/lovelace/editor/lovelace-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ export const getLovelaceContainerPath = (
path: LovelaceCardPath
): LovelaceContainerPath => path.slice(0, -1) as LovelaceContainerPath;

export const findLovelaceContainer = (
type FindLovelaceContainer = {
(config: LovelaceConfig, path: [number]): LovelaceViewRawConfig;
(config: LovelaceConfig, path: [number, number]): LovelaceSectionRawConfig;
(
config: LovelaceConfig,
path: LovelaceContainerPath
): LovelaceViewRawConfig | LovelaceSectionRawConfig;
};
export const findLovelaceContainer: FindLovelaceContainer = (
config: LovelaceConfig,
path: LovelaceContainerPath
): LovelaceViewRawConfig | LovelaceSectionRawConfig => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class HuiDialogEditSection
this._config = findLovelaceContainer(this._params.lovelaceConfig, [
this._params.viewIndex,
this._params.sectionIndex,
]) as LovelaceSectionRawConfig;
]);
}

public closeDialog() {
Expand Down
6 changes: 1 addition & 5 deletions src/panels/lovelace/views/hui-sections-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "../../../components/ha-icon-button";
import "../../../components/ha-sortable";
import "../../../components/ha-svg-icon";
import type { LovelaceViewElement } from "../../../data/lovelace";
import { LovelaceSectionRawConfig } from "../../../data/lovelace/config/section";
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
import type { HomeAssistant } from "../../../types";
Expand Down Expand Up @@ -193,10 +192,7 @@ export class SectionsView extends LitElement implements LovelaceViewElement {

const path = [this.index!, index] as [number, number];

const section = findLovelaceContainer(
this.lovelace!.config,
path
) as LovelaceSectionRawConfig;
const section = findLovelaceContainer(this.lovelace!.config, path);

const title = section.title?.trim();
const cardCount = "cards" in section && section.cards?.length;
Expand Down

0 comments on commit 3021768

Please sign in to comment.