mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 19:56:42 +00:00
Fix logbook username not appearing consistently (#6230)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
27d6a62e67
commit
c69247f190
@ -9,7 +9,6 @@ import {
|
|||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
import { scroll } from "lit-virtualizer";
|
import { scroll } from "lit-virtualizer";
|
||||||
import { formatDate } from "../../common/datetime/format_date";
|
import { formatDate } from "../../common/datetime/format_date";
|
||||||
import { fetchUsers } from "../../data/user";
|
|
||||||
import { formatTimeWithSeconds } from "../../common/datetime/format_time";
|
import { formatTimeWithSeconds } from "../../common/datetime/format_time";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { domainIcon } from "../../common/entity/domain_icon";
|
import { domainIcon } from "../../common/entity/domain_icon";
|
||||||
@ -22,8 +21,7 @@ import { HomeAssistant } from "../../types";
|
|||||||
class HaLogbook extends LitElement {
|
class HaLogbook extends LitElement {
|
||||||
@property() public hass!: HomeAssistant;
|
@property() public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property({ attribute: false })
|
@property() public userIdToName = {};
|
||||||
private _userid_to_name = {};
|
|
||||||
|
|
||||||
@property() public entries: LogbookEntry[] = [];
|
@property() public entries: LogbookEntry[] = [];
|
||||||
|
|
||||||
@ -62,20 +60,6 @@ class HaLogbook extends LitElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _fetchUsers() {
|
|
||||||
const users = await fetchUsers(this.hass);
|
|
||||||
const userid_to_name = {};
|
|
||||||
users.forEach((user) => {
|
|
||||||
userid_to_name[user.id] = user.name;
|
|
||||||
});
|
|
||||||
this._userid_to_name = userid_to_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected firstUpdated(changedProperties: PropertyValues) {
|
|
||||||
super.firstUpdated(changedProperties);
|
|
||||||
this._fetchUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private _renderLogbookItem(
|
private _renderLogbookItem(
|
||||||
item: LogbookEntry,
|
item: LogbookEntry,
|
||||||
index?: number
|
index?: number
|
||||||
@ -86,7 +70,7 @@ class HaLogbook extends LitElement {
|
|||||||
const previous = this.entries[index - 1];
|
const previous = this.entries[index - 1];
|
||||||
const state = item.entity_id ? this.hass.states[item.entity_id] : undefined;
|
const state = item.entity_id ? this.hass.states[item.entity_id] : undefined;
|
||||||
const item_username =
|
const item_username =
|
||||||
item.context_user_id && this._userid_to_name[item.context_user_id];
|
item.context_user_id && this.userIdToName[item.context_user_id];
|
||||||
return html`
|
return html`
|
||||||
<div>
|
<div>
|
||||||
${index === 0 ||
|
${index === 0 ||
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
import { haStyle } from "../../resources/styles";
|
import { haStyle } from "../../resources/styles";
|
||||||
|
import { fetchUsers } from "../../data/user";
|
||||||
import {
|
import {
|
||||||
clearLogbookCache,
|
clearLogbookCache,
|
||||||
getLogbookData,
|
getLogbookData,
|
||||||
@ -32,6 +33,9 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
|
|
||||||
@property({ reflect: true, type: Boolean }) narrow!: boolean;
|
@property({ reflect: true, type: Boolean }) narrow!: boolean;
|
||||||
|
|
||||||
|
@property({ attribute: false })
|
||||||
|
private _userIdToName = {};
|
||||||
|
|
||||||
@property() _startDate: Date;
|
@property() _startDate: Date;
|
||||||
|
|
||||||
@property() _endDate: Date;
|
@property() _endDate: Date;
|
||||||
@ -46,6 +50,8 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
|
|
||||||
@property() private _ranges?: DateRangePickerRanges;
|
@property() private _ranges?: DateRangePickerRanges;
|
||||||
|
|
||||||
|
private _fetchUserDone?: Promise<unknown>;
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@ -112,6 +118,7 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
: html`<ha-logbook
|
: html`<ha-logbook
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.entries=${this._entries}
|
.entries=${this._entries}
|
||||||
|
.userIdToName=${this._userIdToName}
|
||||||
></ha-logbook>`}
|
></ha-logbook>`}
|
||||||
</app-header-layout>
|
</app-header-layout>
|
||||||
`;
|
`;
|
||||||
@ -121,6 +128,8 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
super.firstUpdated(changedProps);
|
super.firstUpdated(changedProps);
|
||||||
this.hass.loadBackendTranslation("title");
|
this.hass.loadBackendTranslation("title");
|
||||||
|
|
||||||
|
this._fetchUserDone = this._fetchUsers();
|
||||||
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
today.setHours(0, 0, 0, 0);
|
today.setHours(0, 0, 0, 0);
|
||||||
const todayEnd = new Date(today);
|
const todayEnd = new Date(today);
|
||||||
@ -184,6 +193,15 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _fetchUsers() {
|
||||||
|
const users = await fetchUsers(this.hass);
|
||||||
|
const userid_to_name = {};
|
||||||
|
users.forEach((user) => {
|
||||||
|
userid_to_name[user.id] = user.name;
|
||||||
|
});
|
||||||
|
this._userIdToName = userid_to_name;
|
||||||
|
}
|
||||||
|
|
||||||
private _dateRangeChanged(ev) {
|
private _dateRangeChanged(ev) {
|
||||||
this._startDate = ev.detail.startDate;
|
this._startDate = ev.detail.startDate;
|
||||||
const endDate = ev.detail.endDate;
|
const endDate = ev.detail.endDate;
|
||||||
@ -209,12 +227,18 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
|
|
||||||
private async _getData() {
|
private async _getData() {
|
||||||
this._isLoading = true;
|
this._isLoading = true;
|
||||||
this._entries = await getLogbookData(
|
const [entries] = await Promise.all([
|
||||||
this.hass,
|
getLogbookData(
|
||||||
this._startDate.toISOString(),
|
this.hass,
|
||||||
this._endDate.toISOString(),
|
this._startDate.toISOString(),
|
||||||
this._entityId
|
this._endDate.toISOString(),
|
||||||
);
|
this._entityId
|
||||||
|
),
|
||||||
|
this._fetchUserDone,
|
||||||
|
]);
|
||||||
|
// Fixed in TS 3.9 but upgrade out of scope for this PR.
|
||||||
|
// @ts-ignore
|
||||||
|
this._entries = entries;
|
||||||
this._isLoading = false;
|
this._isLoading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user