Add a github option when copying from system health (#7663)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Joakim Sørensen 2020-11-13 18:20:24 +01:00 committed by GitHub
parent 6558c2c065
commit b9f802939c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 27 deletions

View File

@ -1,7 +1,9 @@
import "@material/mwc-button/mwc-button"; import "@material/mwc-button/mwc-button";
import "@material/mwc-icon-button"; import "@material/mwc-icon-button";
import "../../../components/ha-circular-progress"; import { ActionDetail } from "@material/mwc-list/mwc-list-foundation";
import "@material/mwc-list/mwc-list-item";
import { mdiContentCopy } from "@mdi/js"; import { mdiContentCopy } from "@mdi/js";
import "@polymer/paper-tooltip/paper-tooltip";
import { import {
css, css,
CSSResult, CSSResult,
@ -9,21 +11,22 @@ import {
internalProperty, internalProperty,
LitElement, LitElement,
property, property,
query,
TemplateResult, TemplateResult,
} from "lit-element"; } from "lit-element";
import { formatDateTime } from "../../../common/datetime/format_date_time";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import "../../../components/ha-button-menu";
import "../../../components/ha-card"; import "../../../components/ha-card";
import "@polymer/paper-tooltip/paper-tooltip"; import "../../../components/ha-circular-progress";
import type { PaperTooltipElement } from "@polymer/paper-tooltip/paper-tooltip"; import "../../../components/ha-svg-icon";
import { domainToName } from "../../../data/integration"; import { domainToName } from "../../../data/integration";
import { import {
subscribeSystemHealthInfo, subscribeSystemHealthInfo,
SystemHealthInfo,
SystemCheckValueObject, SystemCheckValueObject,
SystemHealthInfo,
} from "../../../data/system_health"; } from "../../../data/system_health";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { formatDateTime } from "../../../common/datetime/format_date_time"; import { showToast } from "../../../util/toast";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
const sortKeys = (a: string, b: string) => { const sortKeys = (a: string, b: string) => {
if (a === "homeassistant") { if (a === "homeassistant") {
@ -46,8 +49,6 @@ class SystemHealthCard extends LitElement {
@internalProperty() private _info?: SystemHealthInfo; @internalProperty() private _info?: SystemHealthInfo;
@query("paper-tooltip", true) private _toolTip?: PaperTooltipElement;
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.hass) { if (!this.hass) {
return html``; return html``;
@ -152,18 +153,21 @@ class SystemHealthCard extends LitElement {
<div class="card-header-text"> <div class="card-header-text">
${domainToName(this.hass.localize, "system_health")} ${domainToName(this.hass.localize, "system_health")}
</div> </div>
<mwc-icon-button id="copy" @click=${this._copyInfo}> <ha-button-menu
<ha-svg-icon .path=${mdiContentCopy}></ha-svg-icon> corner="BOTTOM_START"
</mwc-icon-button> slot="toolbar-icon"
<paper-tooltip @action=${this._copyInfo}
manual-mode
for="copy"
position="left"
animation-delay="0"
offset="4"
> >
${this.hass.localize("ui.common.copied")} <mwc-icon-button slot="trigger" alt="menu">
</paper-tooltip> <ha-svg-icon .path=${mdiContentCopy}></ha-svg-icon>
</mwc-icon-button>
<mwc-list-item>
${this.hass.localize("ui.panel.config.info.copy_raw")}
</mwc-list-item>
<mwc-list-item>
${this.hass.localize("ui.panel.config.info.copy_github")}
</mwc-list-item>
</ha-button-menu>
</h1> </h1>
<div class="card-content">${sections}</div> <div class="card-content">${sections}</div>
</ha-card> </ha-card>
@ -193,13 +197,24 @@ class SystemHealthCard extends LitElement {
}); });
} }
private _copyInfo(): void { private _copyInfo(ev: CustomEvent<ActionDetail>): void {
const github = ev.detail.index === 1;
let haContent: string | undefined; let haContent: string | undefined;
const domainParts: string[] = []; const domainParts: string[] = [];
for (const domain of Object.keys(this._info!).sort(sortKeys)) { for (const domain of Object.keys(this._info!).sort(sortKeys)) {
const domainInfo = this._info![domain]; const domainInfo = this._info![domain];
const parts = [`${domainToName(this.hass.localize, domain)}\n`]; let first = true;
const parts = [
`${
github && domain !== "homeassistant"
? `<details><summary>${domainToName(
this.hass.localize,
domain
)}</summary>\n`
: ""
}`,
];
for (const key of Object.keys(domainInfo.info)) { for (const key of Object.keys(domainInfo.info)) {
let value: unknown; let value: unknown;
@ -217,23 +232,31 @@ class SystemHealthCard extends LitElement {
} else { } else {
value = domainInfo.info[key]; value = domainInfo.info[key];
} }
if (github && first) {
parts.push(`${key}: ${value}`); parts.push(`${key} | ${value}\n-- | --`);
first = false;
} else {
parts.push(`${key}${github ? " | " : ": "}${value}`);
}
} }
if (domain === "homeassistant") { if (domain === "homeassistant") {
haContent = parts.join("\n"); haContent = parts.join("\n");
} else { } else {
domainParts.push(parts.join("\n")); domainParts.push(parts.join("\n"));
if (github && domain !== "homeassistant") {
domainParts.push("</details>");
}
} }
} }
copyToClipboard( copyToClipboard(
`System Health\n\n${haContent}\n\n${domainParts.join("\n\n")}` `${github ? "## " : ""}System Health\n${haContent}\n\n${domainParts.join(
"\n\n"
)}`
); );
this._toolTip!.show(); showToast(this, { message: this.hass.localize("ui.common.copied") });
setTimeout(() => this._toolTip?.hide(), 3000);
} }
static get styles(): CSSResult { static get styles(): CSSResult {

View File

@ -899,6 +899,8 @@
}, },
"info": { "info": {
"caption": "Info", "caption": "Info",
"copy_raw": "Raw Text",
"copy_github": "For GitHub",
"description": "View info about your Home Assistant installation", "description": "View info about your Home Assistant installation",
"home_assistant_logo": "Home Assistant logo", "home_assistant_logo": "Home Assistant logo",
"path_configuration": "Path to configuration.yaml: {path}", "path_configuration": "Path to configuration.yaml: {path}",