From b906670a5a3fda765c28c172de93449bdd1bb1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 14 Jul 2022 10:28:07 +0200 Subject: [PATCH] Fix issues with generating my link (#610) --- src/panels/hacs-repository-panel.ts | 44 +++++++++++++++++++---------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/panels/hacs-repository-panel.ts b/src/panels/hacs-repository-panel.ts index 967e5566..371dffee 100644 --- a/src/panels/hacs-repository-panel.ts +++ b/src/panels/hacs-repository-panel.ts @@ -60,23 +60,37 @@ export class HacsRepositoryPanel extends LitElement { @state() private _error?: string; - protected async firstUpdated(changedProperties: PropertyValues): Promise { - super.firstUpdated(changedProperties); - document.body.addEventListener("keydown", (ev: KeyboardEvent) => { - if (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey) { - // Ignore if modifier keys are pressed + public connectedCallback() { + super.connectedCallback(); + document.body.addEventListener("keydown", this._generateMyLink); + } + + public disconnectedCallback() { + super.disconnectedCallback(); + document.body.removeEventListener("keydown", this._generateMyLink); + } + + private _generateMyLink = (ev: KeyboardEvent) => { + if (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey) { + // Ignore if modifier keys are pressed + return; + } + if (ev.key === "m" && mainWindow.location.pathname.startsWith("/hacs/repository/")) { + if (!this._repository) { return; } - if (["m"].includes(ev.key)) { - const myParams = new URLSearchParams({ - redirect: "hacs_repository", - owner: this._repository!.full_name.split("/")[0], - repository: this._repository!.full_name.split("/")[0], - category: this._repository!.category, - }); - window.open(`https://my.home-assistant.io/create-link/?${myParams.toString()}`, "_blank"); - } - }); + const myParams = new URLSearchParams({ + redirect: "hacs_repository", + owner: this._repository!.full_name.split("/")[0], + repository: this._repository!.full_name.split("/")[1], + category: this._repository!.category, + }); + window.open(`https://my.home-assistant.io/create-link/?${myParams.toString()}`, "_blank"); + } + }; + + protected async firstUpdated(changedProperties: PropertyValues): Promise { + super.firstUpdated(changedProperties); const params = extractSearchParamsObject(); if (Object.entries(params).length) {