Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 77 additions & 16 deletions src/components/media-player/ha-media-player-browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class HaMediaPlayerBrowse extends LitElement {

@internalProperty() private _loading = false;

@internalProperty() private _error?: { message: string; code: string };

@internalProperty() private _mediaPlayerItems: MediaPlayerItem[] = [];

private _resizeObserver?: ResizeObserver;
Expand All @@ -92,11 +94,54 @@ export class HaMediaPlayerBrowse extends LitElement {
this._navigate(item);
}

private _renderError(err: { message: string; code: string }) {
if (err.message === "Path does not exist.") {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a backend change this should be:

Suggested change
if (err.message === "Path does not exist.") {
if (err.code === "no_media_folder") {

return html`
<h2>No local media found.</h2>
<p>
It looks like you have not yet created a media directory.
<br />Create a directory with the name <b>"media"</b> in the
configuration directory of Home Assistant (${this.hass.config.config_dir}). <br />Place your video,
audio and image files in this directory to be able to browse and play
them in the browser or on supported media players.
</p>

<p>
Check the
<a
href="https://www.home-assistant.io/integrations/media_source/#local-media"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the RC site if the version contains a "b" here?
This will go to a non-exsisting site during the beta

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do that, we should get a helper method and apply that everywhere. I think it's fine for now.

target="_blank"
rel="noreferrer"
>documentation</a
>
for more info
</p>
`;
}
return err.message;
}

protected render(): TemplateResult {
if (this._loading) {
return html`<ha-circular-progress active></ha-circular-progress>`;
}

if (this._error && !this._mediaPlayerItems.length) {
if (this.dialog) {
this._closeDialogAction();
showAlertDialog(this, {
title: this.hass.localize(
"ui.components.media-browser.media_browsing_error"
),
text: this._renderError(this._error),
});
} else {
return html`<div class="container error">
${this._renderError(this._error)}
</div>`;
}
}
Comment thread
bramkragten marked this conversation as resolved.

if (!this._mediaPlayerItems.length) {
return html``;
}
Expand Down Expand Up @@ -216,7 +261,11 @@ export class HaMediaPlayerBrowse extends LitElement {
`
: ""}
</div>
${currentItem.children?.length
${this._error
? html`<div class="container error">
${this._renderError(this._error)}
</div>`
: currentItem.children?.length
? hasExpandableChildren
? html`
<div class="children">
Expand Down Expand Up @@ -316,7 +365,9 @@ export class HaMediaPlayerBrowse extends LitElement {
)}
</mwc-list>
`
: this.hass.localize("ui.components.media-browser.no_items")}
: html`<div class="container">
${this.hass.localize("ui.components.media-browser.no_items")}
</div>`}
`;
}

Expand All @@ -342,6 +393,11 @@ export class HaMediaPlayerBrowse extends LitElement {
return;
}

if (changedProps.has("entityId")) {
this._error = undefined;
this._mediaPlayerItems = [];
}

this._fetchData(this.mediaContentId, this.mediaContentType).then(
(itemData) => {
if (!itemData) {
Expand Down Expand Up @@ -381,14 +437,20 @@ export class HaMediaPlayerBrowse extends LitElement {
}

private async _navigate(item: MediaPlayerItem) {
const itemData = await this._fetchData(
this._error = undefined;

const itemData = (await this._fetchData(
item.media_content_id,
item.media_content_type
);

if (!itemData) {
return;
}
)) || {
title: this.hass.localize(
"ui.components.media-browser.media_browsing_error"
),
can_expand: false,
can_play: false,
media_content_type: "",
media_content_id: "",
};

this.scrollTo(0, 0);
this._mediaPlayerItems = [...this._mediaPlayerItems, itemData];
Expand Down Expand Up @@ -417,12 +479,7 @@ export class HaMediaPlayerBrowse extends LitElement {
: compare(first.title, second.title)
);
} catch (error) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.components.media-browser.media_browsing_error"
),
text: error.message,
});
this._error = error;
}

return itemData;
Expand Down Expand Up @@ -451,8 +508,8 @@ export class HaMediaPlayerBrowse extends LitElement {
this._resizeObserver.observe(this);
}

private _hasExpandableChildren = memoizeOne((children) =>
children.find((item: MediaPlayerItem) => item.can_expand)
private _hasExpandableChildren = memoizeOne((children?: MediaPlayerItem[]) =>
children?.find((item: MediaPlayerItem) => item.can_expand)
);

private _closeDialogAction(): void {
Expand All @@ -471,6 +528,10 @@ export class HaMediaPlayerBrowse extends LitElement {
flex-direction: column;
}

.container {
padding: 16px;
}

.header {
display: flex;
justify-content: space-between;
Expand Down
8 changes: 4 additions & 4 deletions src/dialogs/generic/dialog-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {
CSSResult,
customElement,
html,
internalProperty,
LitElement,
property,
internalProperty,
TemplateResult,
} from "lit-element";
import { classMap } from "lit-html/directives/class-map";
import { fireEvent } from "../../common/dom/fire_event";
import "../../components/ha-dialog";
import "../../components/ha-switch";
import { PolymerChangedEvent } from "../../polymer-types";
import { haStyleDialog } from "../../resources/styles";
import { HomeAssistant } from "../../types";
import { DialogParams } from "./show-dialog-box";
import { fireEvent } from "../../common/dom/fire_event";

@customElement("dialog-box")
class DialogBox extends LitElement {
Expand Down Expand Up @@ -114,8 +114,8 @@ class DialogBox extends LitElement {
}

private _dismiss(): void {
if (this._params!.cancel) {
this._params!.cancel();
if (this._params?.cancel) {
this._params.cancel();
}
this._close();
}
Expand Down