forked from esphome/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dialog to choose firmware download type (esphome#469)
- Loading branch information
Showing
9 changed files
with
208 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { fetchApiJson } from "."; | ||
|
||
export type DownloadType = { | ||
title: string; | ||
description: string; | ||
file: string; | ||
download: string; | ||
}; | ||
|
||
export const getDownloadTypes = (configuration: string) => | ||
fetchApiJson<DownloadType[]>( | ||
`./downloads?configuration=${encodeURIComponent(configuration)}` | ||
); | ||
|
||
export const getDownloadUrl = (configuration: string, type: DownloadType) => | ||
`./download.bin?configuration=${encodeURIComponent( | ||
configuration | ||
)}&file=${encodeURIComponent(type.file)}&download=${encodeURIComponent( | ||
type.download | ||
)}`; | ||
|
||
export const getFactoryDownloadUrl = (configuration: string) => | ||
`./download.bin?configuration=${encodeURIComponent( | ||
configuration | ||
)}&file=firmware-factory.bin`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { LitElement, html, PropertyValues, css } from "lit"; | ||
import { customElement, property, state } from "lit/decorators.js"; | ||
import "@material/mwc-dialog"; | ||
import "@material/mwc-list/mwc-list-item.js"; | ||
import "@material/mwc-circular-progress"; | ||
import "@material/mwc-button"; | ||
import { metaChevronRight } from "../const"; | ||
import { esphomeDialogStyles, esphomeSvgStyles } from "../styles"; | ||
import "../components/esphome-alert"; | ||
import { | ||
DownloadType, | ||
getDownloadTypes, | ||
getDownloadUrl, | ||
} from "../api/download"; | ||
|
||
@customElement("esphome-download-type-dialog") | ||
class ESPHomeDownloadTypeDialog extends LitElement { | ||
@property() public configuration!: string; | ||
@property() public platformSupportsWebSerial!: boolean; | ||
|
||
@state() private _error?: string; | ||
@state() private _downloadTypes?: DownloadType[]; | ||
|
||
protected render() { | ||
let heading; | ||
let content; | ||
|
||
heading = "What version do you want to download?"; | ||
content = html` | ||
${this._error ? html`<div class="error">${this._error}</div>` : ""} | ||
${!this._downloadTypes | ||
? html`<div>Checking files to download...</div>` | ||
: html` | ||
${this._downloadTypes.map( | ||
(type) => html` | ||
<mwc-list-item | ||
twoline | ||
hasMeta | ||
dialogAction="close" | ||
@click=${() => this._handleDownload(type)} | ||
> | ||
<span>${type.title}</span> | ||
<span slot="secondary">${type.description}</span> | ||
${metaChevronRight} | ||
</mwc-list-item> | ||
` | ||
)} | ||
`} | ||
`; | ||
|
||
return html` | ||
<mwc-dialog open heading=${heading} scrimClickAction> | ||
${content} | ||
${this.platformSupportsWebSerial | ||
? html` | ||
<a | ||
href="https://web.esphome.io" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
class="bottom-left" | ||
>Open ESPHome Web</a | ||
> | ||
` | ||
: ""} | ||
<mwc-button | ||
no-attention | ||
slot="primaryAction" | ||
dialogAction="close" | ||
label="Cancel" | ||
></mwc-button> | ||
</mwc-dialog> | ||
`; | ||
} | ||
|
||
protected firstUpdated(changedProps: PropertyValues) { | ||
super.firstUpdated(changedProps); | ||
getDownloadTypes(this.configuration) | ||
.then((types) => { | ||
if (types.length == 1) { | ||
this._handleDownload(types[0]); | ||
this._close(); | ||
return; | ||
} | ||
this._downloadTypes = types; | ||
}) | ||
.catch((err: any) => { | ||
this._error = err.message || err; | ||
}); | ||
} | ||
|
||
private _handleDownload(type: DownloadType) { | ||
const link = document.createElement("a"); | ||
link.download = type.download; | ||
link.href = getDownloadUrl(this.configuration, type); | ||
document.body.appendChild(link); | ||
link.click(); | ||
link.remove(); | ||
} | ||
|
||
private _close() { | ||
this.shadowRoot!.querySelector("mwc-dialog")!.close(); | ||
} | ||
|
||
static styles = [ | ||
esphomeDialogStyles, | ||
esphomeSvgStyles, | ||
css` | ||
mwc-list-item { | ||
margin: 0 -20px; | ||
} | ||
`, | ||
]; | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"esphome-download-type-dialog": ESPHomeDownloadTypeDialog; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const preload = () => import("./download-type-dialog"); | ||
|
||
export const openDownloadTypeDialog = ( | ||
configuration: string, | ||
platformSupportsWebSerial: boolean | ||
) => { | ||
preload(); | ||
const dialog = document.createElement("esphome-download-type-dialog"); | ||
dialog.configuration = configuration; | ||
dialog.platformSupportsWebSerial = platformSupportsWebSerial; | ||
document.body.append(dialog); | ||
}; |
Oops, something went wrong.