Add "m" keyboard shortcut to get to the create my link page (#12451)

This commit is contained in:
Joakim Sørensen 2022-04-27 00:11:09 +02:00 committed by GitHub
parent 8ae2d4e93a
commit 049ddd5f84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 210 additions and 176 deletions

View File

@ -12,12 +12,7 @@ import "../../layouts/hass-error-screen";
import { HomeAssistant, Route } from "../../types";
import { documentationUrl } from "../../util/documentation-url";
const getRedirect = (
path: string,
hasSupervisor: boolean
): Redirect | undefined =>
((
{
export const getMyRedirects = (hasSupervisor: boolean): Redirects => ({
developer_states: {
redirect: "/developer-tools/state",
},
@ -188,8 +183,12 @@ const getRedirect = (
// Moved from Supervisor panel in 2022.5
redirect: "/config/info",
},
} as Redirects
)[path]);
});
const getRedirect = (
path: string,
hasSupervisor: boolean
): Redirect | undefined => getMyRedirects(hasSupervisor)?.[path];
export type ParamType = "url" | "string";

View File

@ -1,11 +1,14 @@
import type { PropertyValues } from "lit";
import tinykeys from "tinykeys";
import { isComponentLoaded } from "../common/config/is_component_loaded";
import { mainWindow } from "../common/dom/get_main_window";
import {
QuickBarParams,
showQuickBar,
} from "../dialogs/quick-bar/show-dialog-quick-bar";
import { Constructor, HomeAssistant } from "../types";
import { storeState } from "../util/ha-pref-storage";
import { showToast } from "../util/toast";
import { HassElement } from "./hass-element";
declare global {
@ -32,6 +35,7 @@ export default <T extends Constructor<HassElement>>(superClass: T) =>
tinykeys(window, {
e: (ev) => this._showQuickBar(ev),
c: (ev) => this._showQuickBar(ev, true),
m: (ev) => this._createMyLink(ev),
});
}
@ -43,6 +47,36 @@ export default <T extends Constructor<HassElement>>(superClass: T) =>
showQuickBar(this, { commandMode });
}
private async _createMyLink(e: KeyboardEvent) {
if (!this._canOverrideAlphanumericInput(e) || !this.hass) {
return;
}
const targetPath = mainWindow.location.pathname;
const myPanel = await import("../panels/my/ha-panel-my");
for (const [slug, redirect] of Object.entries(
myPanel.getMyRedirects(isComponentLoaded(this.hass, "hassio"))
)) {
if (redirect.redirect === targetPath) {
window.open(
`https://my.home-assistant.io/create-link/?redirect=${slug}`,
"_blank"
);
return;
}
}
showToast(this, {
message: this.hass.localize(
"ui.notification_toast.no_matching_link_found",
{
path: targetPath,
}
),
});
}
private _canShowQuickBar(e: KeyboardEvent) {
return (
this.hass?.user?.is_admin &&

View File

@ -1083,7 +1083,8 @@
"wrapping_up_startup": "Wrapping up startup, not everything will be available until it is finished.",
"integration_starting": "Starting {integration}, not everything will be available until it is finished.",
"triggered": "Triggered {name}",
"dismiss": "Dismiss"
"dismiss": "Dismiss",
"no_matching_link_found": "No matching my link found for {path}"
},
"sidebar": {
"external_app_configuration": "App Configuration",