Fix logs, dont require advanced mode, allow provider in url and my (#19355)

* Fix logs, dont require advanced mode, allow provider in url and my

* Add params to my link with quick key `m`
This commit is contained in:
Bram Kragten 2024-01-10 13:50:07 +01:00 committed by GitHub
parent 877c9b007b
commit 85beefec4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 16 deletions

View File

@ -1,6 +1,8 @@
import { mainWindow } from "../dom/get_main_window";
export const extractSearchParamsObject = (): Record<string, string> => {
const query = {};
const searchParams = new URLSearchParams(location.search);
const searchParams = new URLSearchParams(mainWindow.location.search);
for (const [key, value] of searchParams.entries()) {
query[key] = value;
}
@ -8,7 +10,7 @@ export const extractSearchParamsObject = (): Record<string, string> => {
};
export const extractSearchParam = (param: string): string | null => {
const urlParams = new URLSearchParams(window.location.search);
const urlParams = new URLSearchParams(mainWindow.location.search);
return urlParams.get(param);
};
@ -21,7 +23,7 @@ export const createSearchParam = (params: Record<string, string>): string => {
};
export const addSearchParam = (params: Record<string, string>): string => {
const urlParams = new URLSearchParams(window.location.search);
const urlParams = new URLSearchParams(mainWindow.location.search);
Object.entries(params).forEach(([key, value]) => {
urlParams.set(key, value);
});
@ -29,7 +31,7 @@ export const addSearchParam = (params: Record<string, string>): string => {
};
export const removeSearchParam = (param: string): string => {
const urlParams = new URLSearchParams(window.location.search);
const urlParams = new URLSearchParams(mainWindow.location.search);
urlParams.delete(param);
return urlParams.toString();
};

View File

@ -119,7 +119,6 @@ class HassSubpage extends LitElement {
font-size: 20px;
height: var(--header-height);
padding: 8px 12px;
pointer-events: none;
background-color: var(--app-header-background-color);
font-weight: 400;
color: var(--app-header-text-color, white);

View File

@ -14,6 +14,8 @@ import { HomeAssistant, Route } from "../../../types";
import "./error-log-card";
import "./system-log-card";
import type { SystemLogCard } from "./system-log-card";
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { navigate } from "../../../common/navigate";
const logProviders: LogProvider[] = [
{
@ -50,13 +52,11 @@ export class HaConfigLogs extends LitElement {
@property({ type: Boolean }) public isWide!: boolean;
@property({ type: Boolean }) public showAdvanced!: boolean;
@property({ attribute: false }) public route!: Route;
@state() private _filter = extractSearchParam("filter") || "";
@query("system-log-card", true) private systemLog?: SystemLogCard;
@query("system-log-card") private systemLog?: SystemLogCard;
@state() private _selectedLogProvider = "core";
@ -64,16 +64,15 @@ export class HaConfigLogs extends LitElement {
public connectedCallback() {
super.connectedCallback();
if (this.systemLog && this.systemLog.loaded) {
this.systemLog.fetchData();
const systemLog = this.systemLog;
if (systemLog && systemLog.loaded) {
systemLog.fetchData();
}
}
protected firstUpdated(changedProps): void {
super.firstUpdated(changedProps);
if (isComponentLoaded(this.hass, "hassio")) {
this._getInstalledAddons();
}
this._init();
}
private async _filterChanged(ev) {
@ -111,8 +110,7 @@ export class HaConfigLogs extends LitElement {
.header=${this.hass.localize("ui.panel.config.logs.caption")}
back-path="/config/system"
>
${isComponentLoaded(this.hass, "hassio") &&
this.hass.userData?.showAdvanced
${isComponentLoaded(this.hass, "hassio")
? html`
<ha-button-menu slot="toolbar-icon">
<ha-button
@ -169,6 +167,36 @@ export class HaConfigLogs extends LitElement {
private _selectProvider(ev) {
this._selectedLogProvider = (ev.currentTarget as any).provider;
navigate(`/config/logs?provider=${this._selectedLogProvider}`);
}
private async _init() {
if (isComponentLoaded(this.hass, "hassio")) {
await this._getInstalledAddons();
}
const providerKey = extractSearchParam("provider");
if (providerKey) {
if (
isComponentLoaded(this.hass, "hassio") &&
this._logProviders.find((p) => p.key === providerKey)
) {
this._selectedLogProvider = providerKey;
} else {
showAlertDialog(this, {
title:
this.hass.localize("ui.panel.config.logs.provider_not_found") ||
"Log provider not found",
text: this.hass.localize(
"ui.panel.config.logs.provider_not_available",
{
provider:
this._logProviders.find((p) => p.key === providerKey)?.name ||
providerKey,
}
),
});
}
}
}
private async _getInstalledAddons() {
@ -209,6 +237,7 @@ export class HaConfigLogs extends LitElement {
}
search-input.header {
--mdc-ripple-color: transparant;
margin-left: -16px;
}
.content {
direction: ltr;

View File

@ -189,6 +189,9 @@ export const getMyRedirects = (hasSupervisor: boolean): Redirects => ({
},
logs: {
redirect: "/config/logs",
params: {
provider: "string?",
},
},
repairs: {
component: "repairs",

View File

@ -10,6 +10,7 @@ import { Constructor, HomeAssistant } from "../types";
import { storeState } from "../util/ha-pref-storage";
import { showToast } from "../util/toast";
import { HassElement } from "./hass-element";
import { extractSearchParamsObject } from "../common/url/search-params";
declare global {
interface HASSDomEvents {
@ -117,6 +118,14 @@ export default <T extends Constructor<HassElement>>(superClass: T) =>
)) {
if (targetPath.startsWith(redirect.redirect)) {
myParams.append("redirect", slug);
if (redirect.params) {
const params = extractSearchParamsObject();
for (const key of Object.keys(redirect.params)) {
if (key in params) {
myParams.append(key, params[key]);
}
}
}
window.open(
`https://my.home-assistant.io/create-link/?${myParams.toString()}`,
"_blank"

View File

@ -2157,7 +2157,9 @@
"custom_integration": "custom integration",
"error_from_custom_integration": "This error originated from a custom integration.",
"show_full_logs": "Show full logs",
"download_full_log": "Download full log"
"download_full_log": "Download full log",
"provider_not_found": "Log provider not found",
"provider_not_available": "Logs for ''{provider}'' are not available on your system."
},
"lovelace": {
"caption": "Dashboards",