-
Notifications
You must be signed in to change notification settings - Fork 52
/
show-api-key-dialog.ts
115 lines (102 loc) · 2.96 KB
/
show-api-key-dialog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { LitElement, html, css, PropertyValues } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import "@material/mwc-button";
import "@material/mwc-dialog";
import { esphomeDialogStyles } from "../styles";
import { copyToClipboard } from "../util/copy-clipboard";
import { openEditDialog } from "../editor";
import { getConfigurationApiKey } from "../api/configuration";
@customElement("esphome-show-api-key-dialog")
class ESPHomeShowApiKeyDialogDialog extends LitElement {
@property() public configuration!: string;
@state() private _apiKey?: string | null;
@state() private _showCopied = false;
protected render() {
return html`
<mwc-dialog
open
heading=${`API key for ${this.configuration}`}
scrimClickAction
@closed=${this._handleClose}
>
${this._apiKey === undefined
? "Loading…"
: this._apiKey === null
? html`Unable to automatically extract API key. Open the configuration
and look for <code>api:</code>.`
: html`
<div class="key" @click=${this._copyApiKey}>
<code>${this._apiKey}</code>
<mwc-button ?disabled=${this._showCopied}
>${this._showCopied ? "Copied!" : "Copy"}</mwc-button
>
</div>
`}
${this._apiKey === null
? html`
<mwc-button
@click=${this._editConfig}
no-attention
slot="secondaryAction"
dialogAction="close"
label="Open configuration"
></mwc-button>
`
: ""}
<mwc-button
no-attention
slot="primaryAction"
dialogAction="close"
label="Close"
></mwc-button>
</mwc-dialog>
`;
}
protected firstUpdated(changedProps: PropertyValues): void {
super.firstUpdated(changedProps);
getConfigurationApiKey(this.configuration).then((key) => {
this._apiKey = key;
});
}
private _copyApiKey() {
copyToClipboard(this._apiKey!);
this._showCopied = true;
setTimeout(() => (this._showCopied = false), 2000);
}
private _editConfig() {
openEditDialog(this.configuration);
}
private _handleClose() {
this.parentNode!.removeChild(this);
}
static styles = [
esphomeDialogStyles,
css`
.key {
position: relative;
display: flex;
align-items: center;
}
code {
word-break: break-all;
}
.key mwc-button {
margin-left: 16px;
}
.copied {
font-weight: bold;
color: var(--alert-success-color);
position: absolute;
background-color: var(--mdc-theme-surface, #fff);
padding: 10px;
right: 0;
font-size: 1.2em;
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
"esphome-show-api-key-dialog": ESPHomeShowApiKeyDialogDialog;
}
}