Link logbook row to trace if available (#13599)

* Link logbook row to trace if available

* Update ha-logbook-renderer.ts
This commit is contained in:
Bram Kragten 2022-09-05 19:36:09 +02:00 committed by GitHub
parent aa6ee0f6d2
commit e510e5b371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,6 +34,8 @@ import {
} from "../../resources/styles"; } from "../../resources/styles";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { brandsUrl } from "../../util/brands-url"; import { brandsUrl } from "../../util/brands-url";
import "../../components/ha-icon-next";
import { navigate } from "../../common/navigate";
declare global { declare global {
interface HASSDomEvents { interface HASSDomEvents {
@ -156,8 +158,26 @@ class HaLogbookRenderer extends LitElement {
}) })
: undefined; : undefined;
const traceContext =
triggerDomains.includes(item.domain!) &&
item.context_id! in this.traceContexts
? this.traceContexts[item.context_id!]
: undefined;
const hasTrace = traceContext !== undefined;
return html` return html`
<div class="entry-container"> <div
class="entry-container ${classMap({ clickable: hasTrace })}"
.traceLink=${traceContext
? `/config/${traceContext.domain}/trace/${
traceContext.domain === "script"
? `script.${traceContext.item_id}`
: traceContext.item_id
}?run_id=${traceContext.run_id}`
: undefined}
@click=${this._handleClick}
>
${index === 0 || ${index === 0 ||
(item?.when && (item?.when &&
previous?.when && previous?.when &&
@ -186,15 +206,16 @@ class HaLogbookRenderer extends LitElement {
<div class="message-relative_time"> <div class="message-relative_time">
<div class="message"> <div class="message">
${!this.noName // Used for more-info panel (single entity case) ${!this.noName // Used for more-info panel (single entity case)
? this._renderEntity(item.entity_id, item.name) ? this._renderEntity(item.entity_id, item.name, hasTrace)
: ""} : ""}
${this._renderMessage( ${this._renderMessage(
item, item,
seenEntityIds, seenEntityIds,
domain, domain,
historicStateObj historicStateObj,
hasTrace
)} )}
${this._renderContextMessage(item, seenEntityIds)} ${this._renderContextMessage(item, seenEntityIds, hasTrace)}
</div> </div>
<div class="secondary"> <div class="secondary">
<span <span
@ -210,33 +231,15 @@ class HaLogbookRenderer extends LitElement {
capitalize capitalize
></ha-relative-time> ></ha-relative-time>
${item.context_user_id ? html`${this._renderUser(item)}` : ""} ${item.context_user_id ? html`${this._renderUser(item)}` : ""}
${triggerDomains.includes(item.domain!) && ${hasTrace
item.context_id! in this.traceContexts ? `- ${this.hass.localize(
? html` "ui.components.logbook.show_trace"
- )}`
<a
href=${`/config/${
this.traceContexts[item.context_id!].domain
}/trace/${
this.traceContexts[item.context_id!].domain ===
"script"
? `script.${
this.traceContexts[item.context_id!].item_id
}`
: this.traceContexts[item.context_id!].item_id
}?run_id=${
this.traceContexts[item.context_id!].run_id
}`}
@click=${this._close}
>${this.hass.localize(
"ui.components.logbook.show_trace"
)}</a
>
`
: ""} : ""}
</div> </div>
</div> </div>
</div> </div>
${hasTrace ? html`<ha-icon-next></ha-icon-next>` : ""}
</div> </div>
</div> </div>
`; `;
@ -258,7 +261,8 @@ class HaLogbookRenderer extends LitElement {
item: LogbookEntry, item: LogbookEntry,
seenEntityIds: string[], seenEntityIds: string[],
domain?: string, domain?: string,
historicStateObj?: HassEntity historicStateObj?: HassEntity,
noLink?: boolean
) { ) {
if (item.entity_id) { if (item.entity_id) {
if (item.state) { if (item.state) {
@ -291,7 +295,8 @@ class HaLogbookRenderer extends LitElement {
? stripEntityId(message, item.context_entity_id) ? stripEntityId(message, item.context_entity_id)
: message, : message,
seenEntityIds, seenEntityIds,
undefined undefined,
noLink
) )
: ""; : "";
} }
@ -307,7 +312,8 @@ class HaLogbookRenderer extends LitElement {
private _renderUnseenContextSourceEntity( private _renderUnseenContextSourceEntity(
item: LogbookEntry, item: LogbookEntry,
seenEntityIds: string[] seenEntityIds: string[],
noLink: boolean
) { ) {
if ( if (
!item.context_entity_id || !item.context_entity_id ||
@ -320,11 +326,16 @@ class HaLogbookRenderer extends LitElement {
// described event. // described event.
return html` (${this._renderEntity( return html` (${this._renderEntity(
item.context_entity_id, item.context_entity_id,
item.context_entity_id_name item.context_entity_id_name,
noLink
)})`; )})`;
} }
private _renderContextMessage(item: LogbookEntry, seenEntityIds: string[]) { private _renderContextMessage(
item: LogbookEntry,
seenEntityIds: string[],
noLink: boolean
) {
// State change // State change
if (item.context_state) { if (item.context_state) {
const historicStateObj = const historicStateObj =
@ -337,7 +348,11 @@ class HaLogbookRenderer extends LitElement {
return html`${this.hass.localize( return html`${this.hass.localize(
"ui.components.logbook.triggered_by_state_of" "ui.components.logbook.triggered_by_state_of"
)} )}
${this._renderEntity(item.context_entity_id, item.context_entity_id_name)} ${this._renderEntity(
item.context_entity_id,
item.context_entity_id_name,
noLink
)}
${historicStateObj ${historicStateObj
? localizeStateMessage( ? localizeStateMessage(
this.hass, this.hass,
@ -379,11 +394,17 @@ class HaLogbookRenderer extends LitElement {
? "ui.components.logbook.triggered_by_automation" ? "ui.components.logbook.triggered_by_automation"
: "ui.components.logbook.triggered_by_script" : "ui.components.logbook.triggered_by_script"
)} )}
${this._renderEntity(item.context_entity_id, item.context_entity_id_name)} ${this._renderEntity(
item.context_entity_id,
item.context_entity_id_name,
noLink
)}
${item.context_message ${item.context_message
? this._formatMessageWithPossibleEntity( ? this._formatMessageWithPossibleEntity(
contextTriggerSource, contextTriggerSource,
seenEntityIds seenEntityIds,
undefined,
noLink
) )
: ""}`; : ""}`;
} }
@ -394,14 +415,16 @@ class HaLogbookRenderer extends LitElement {
${this._formatMessageWithPossibleEntity( ${this._formatMessageWithPossibleEntity(
item.context_message, item.context_message,
seenEntityIds, seenEntityIds,
item.context_entity_id item.context_entity_id,
noLink
)} )}
${this._renderUnseenContextSourceEntity(item, seenEntityIds)}`; ${this._renderUnseenContextSourceEntity(item, seenEntityIds, noLink)}`;
} }
private _renderEntity( private _renderEntity(
entityId: string | undefined, entityId: string | undefined,
entityName: string | undefined entityName: string | undefined,
noLink?: boolean
) { ) {
const hasState = entityId && entityId in this.hass.states; const hasState = entityId && entityId in this.hass.states;
const displayName = const displayName =
@ -412,19 +435,22 @@ class HaLogbookRenderer extends LitElement {
if (!hasState) { if (!hasState) {
return displayName; return displayName;
} }
return html`<button return noLink
class="link" ? displayName
@click=${this._entityClicked} : html`<button
.entityId=${entityId} class="link"
> @click=${this._entityClicked}
${displayName} .entityId=${entityId}
</button>`; >
${displayName}
</button>`;
} }
private _formatMessageWithPossibleEntity( private _formatMessageWithPossibleEntity(
message: string, message: string,
seenEntities: string[], seenEntities: string[],
possibleEntity?: string possibleEntity?: string,
noLink?: boolean
) { ) {
// //
// As we are looking at a log(book), we are doing entity_id // As we are looking at a log(book), we are doing entity_id
@ -449,7 +475,8 @@ class HaLogbookRenderer extends LitElement {
return html`${messageParts.join(" ")} return html`${messageParts.join(" ")}
${this._renderEntity( ${this._renderEntity(
entityId, entityId,
this.hass.states[entityId].attributes.friendly_name this.hass.states[entityId].attributes.friendly_name,
noLink
)} )}
${messageEnd.join(" ")}`; ${messageEnd.join(" ")}`;
} }
@ -475,7 +502,7 @@ class HaLogbookRenderer extends LitElement {
message.length - possibleEntityName.length message.length - possibleEntityName.length
); );
return html`${message} return html`${message}
${this._renderEntity(possibleEntity, possibleEntityName)}`; ${this._renderEntity(possibleEntity, possibleEntityName, noLink)}`;
} }
} }
return message; return message;
@ -494,8 +521,12 @@ class HaLogbookRenderer extends LitElement {
}); });
} }
private _close(): void { _handleClick(ev) {
setTimeout(() => fireEvent(this, "closed"), 500); if (!ev.currentTarget.traceLink) {
return;
}
navigate(ev.currentTarget.traceLink);
fireEvent(this, "closed");
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
@ -520,10 +551,20 @@ class HaLogbookRenderer extends LitElement {
padding: 8px 16px; padding: 8px 16px;
box-sizing: border-box; box-sizing: border-box;
border-top: 1px solid var(--divider-color); border-top: 1px solid var(--divider-color);
justify-content: space-between;
align-items: center;
} }
.entry.no-entity, ha-icon-next {
.no-name .entry { color: var(--secondary-text-color);
}
.clickable {
cursor: pointer;
}
:not(.clickable) .entry.no-entity,
:not(.clickable) .no-name .entry {
cursor: default; cursor: default;
} }