mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-17 14:26:35 +00:00
Add authenticated thumbnails to Media Player and lazy fetch thumbnails (#10978)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
74a05929be
commit
ed462dc257
@ -18,6 +18,7 @@ import {
|
|||||||
eventOptions,
|
eventOptions,
|
||||||
property,
|
property,
|
||||||
query,
|
query,
|
||||||
|
queryAll,
|
||||||
state,
|
state,
|
||||||
} from "lit/decorators";
|
} from "lit/decorators";
|
||||||
import { classMap } from "lit/directives/class-map";
|
import { classMap } from "lit/directives/class-map";
|
||||||
@ -26,6 +27,7 @@ import { styleMap } from "lit/directives/style-map";
|
|||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { computeRTLDirection } from "../../common/util/compute_rtl";
|
import { computeRTLDirection } from "../../common/util/compute_rtl";
|
||||||
import { debounce } from "../../common/util/debounce";
|
import { debounce } from "../../common/util/debounce";
|
||||||
|
import { getSignedPath } from "../../data/auth";
|
||||||
import type { MediaPlayerItem } from "../../data/media-player";
|
import type { MediaPlayerItem } from "../../data/media-player";
|
||||||
import {
|
import {
|
||||||
browseLocalMediaPlayer,
|
browseLocalMediaPlayer,
|
||||||
@ -42,7 +44,7 @@ import type { HomeAssistant } from "../../types";
|
|||||||
import { documentationUrl } from "../../util/documentation-url";
|
import { documentationUrl } from "../../util/documentation-url";
|
||||||
import "../entity/ha-entity-picker";
|
import "../entity/ha-entity-picker";
|
||||||
import "../ha-button-menu";
|
import "../ha-button-menu";
|
||||||
import "../ha-card";
|
import type { HaCard } from "../ha-card";
|
||||||
import "../ha-circular-progress";
|
import "../ha-circular-progress";
|
||||||
import "../ha-fab";
|
import "../ha-fab";
|
||||||
import "../ha-icon-button";
|
import "../ha-icon-button";
|
||||||
@ -84,19 +86,27 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
|
|
||||||
@query(".content") private _content?: HTMLDivElement;
|
@query(".content") private _content?: HTMLDivElement;
|
||||||
|
|
||||||
|
@queryAll(".lazythumbnail") private _thumbnails?: HaCard[];
|
||||||
|
|
||||||
private _headerOffsetHeight = 0;
|
private _headerOffsetHeight = 0;
|
||||||
|
|
||||||
private _resizeObserver?: ResizeObserver;
|
private _resizeObserver?: ResizeObserver;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
private _intersectionObserver?: IntersectionObserver;
|
||||||
|
|
||||||
public connectedCallback(): void {
|
public connectedCallback(): void {
|
||||||
super.connectedCallback();
|
super.connectedCallback();
|
||||||
this.updateComplete.then(() => this._attachObserver());
|
this.updateComplete.then(() => this._attachResizeObserver());
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnectedCallback(): void {
|
public disconnectedCallback(): void {
|
||||||
if (this._resizeObserver) {
|
if (this._resizeObserver) {
|
||||||
this._resizeObserver.disconnect();
|
this._resizeObserver.disconnect();
|
||||||
}
|
}
|
||||||
|
if (this._intersectionObserver) {
|
||||||
|
this._intersectionObserver.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public navigateBack() {
|
public navigateBack() {
|
||||||
@ -259,11 +269,8 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
<div class="ha-card-parent">
|
<div class="ha-card-parent">
|
||||||
<ha-card
|
<ha-card
|
||||||
outlined
|
outlined
|
||||||
style=${styleMap({
|
class=${child.thumbnail ? "lazythumbnail" : ""}
|
||||||
backgroundImage: child.thumbnail
|
data-src=${ifDefined(child.thumbnail)}
|
||||||
? `url(${child.thumbnail})`
|
|
||||||
: "none",
|
|
||||||
})}
|
|
||||||
>
|
>
|
||||||
${!child.thumbnail
|
${!child.thumbnail
|
||||||
? html`
|
? html`
|
||||||
@ -391,7 +398,7 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
|
|
||||||
protected firstUpdated(): void {
|
protected firstUpdated(): void {
|
||||||
this._measureCard();
|
this._measureCard();
|
||||||
this._attachObserver();
|
this._attachResizeObserver();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected updated(changedProps: PropertyValues): void {
|
protected updated(changedProps: PropertyValues): void {
|
||||||
@ -402,6 +409,7 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
this._mediaPlayerItems.length
|
this._mediaPlayerItems.length
|
||||||
) {
|
) {
|
||||||
this._setHeaderHeight();
|
this._setHeaderHeight();
|
||||||
|
this._attachIntersectionObserver();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -553,7 +561,7 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachResizeObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await installResizeObserver();
|
await installResizeObserver();
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
@ -564,6 +572,44 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
this._resizeObserver.observe(this);
|
this._resizeObserver.observe(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load thumbnails for images on demand as they become visible.
|
||||||
|
*/
|
||||||
|
private async _attachIntersectionObserver(): Promise<void> {
|
||||||
|
if (!this._thumbnails) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this._intersectionObserver) {
|
||||||
|
this._intersectionObserver = new IntersectionObserver(
|
||||||
|
async (entries, observer) => {
|
||||||
|
await Promise.all(
|
||||||
|
entries.map(async (entry) => {
|
||||||
|
if (!entry.isIntersecting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const thumbnailCard = entry.target as HaCard;
|
||||||
|
let thumbnailUrl = thumbnailCard.dataset.src;
|
||||||
|
if (!thumbnailUrl) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (thumbnailUrl.startsWith("/")) {
|
||||||
|
// Thumbnails served by local API require authentication
|
||||||
|
const signedPath = await getSignedPath(this.hass, thumbnailUrl);
|
||||||
|
thumbnailUrl = signedPath.path;
|
||||||
|
}
|
||||||
|
thumbnailCard.style.backgroundImage = `url(${thumbnailUrl})`;
|
||||||
|
observer.unobserve(thumbnailCard); // loaded, so no need to observe anymore
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const observer = this._intersectionObserver!;
|
||||||
|
this._thumbnails.forEach((thumbnailCard) => {
|
||||||
|
observer.observe(thumbnailCard);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private _closeDialogAction(): void {
|
private _closeDialogAction(): void {
|
||||||
fireEvent(this, "close-dialog");
|
fireEvent(this, "close-dialog");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user