-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathhacs-repository-dashboard.ts
More file actions
379 lines (348 loc) · 12.8 KB
/
hacs-repository-dashboard.ts
File metadata and controls
379 lines (348 loc) · 12.8 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import {
mdiAccount,
mdiArrowDownBold,
mdiCube,
mdiDotsVertical,
mdiDownload,
mdiExclamationThick,
mdiStar,
} from "@mdi/js";
import type { PropertyValues, TemplateResult } from "lit";
import { LitElement, css, html } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { mainWindow } from "../../homeassistant-frontend/src/common/dom/get_main_window";
import { extractSearchParamsObject } from "../../homeassistant-frontend/src/common/url/search-params";
import "../../homeassistant-frontend/src/components/chips/ha-assist-chip";
import "../../homeassistant-frontend/src/components/chips/ha-chip-set";
import "../../homeassistant-frontend/src/components/ha-alert";
import "../../homeassistant-frontend/src/components/ha-card";
import "../../homeassistant-frontend/src/components/ha-fab";
import "../../homeassistant-frontend/src/components/ha-markdown";
import "../../homeassistant-frontend/src/components/ha-menu";
import type { HaMenu } from "../../homeassistant-frontend/src/components/ha-menu";
import "../../homeassistant-frontend/src/components/ha-md-menu-item";
import { showConfirmationDialog } from "../../homeassistant-frontend/src/dialogs/generic/show-dialog-box";
import "../../homeassistant-frontend/src/layouts/hass-error-screen";
import "../../homeassistant-frontend/src/layouts/hass-loading-screen";
import "../../homeassistant-frontend/src/layouts/hass-subpage";
import type { HomeAssistant, Route } from "../../homeassistant-frontend/src/types";
import { showHacsDownloadDialog } from "../components/dialogs/show-hacs-dialog";
import { repositoryMenuItems } from "../components/hacs-repository-owerflow-menu";
import type { Hacs } from "../data/hacs";
import type { RepositoryBase, RepositoryInfo } from "../data/repository";
import { fetchRepositoryInformation } from "../data/repository";
import { getRepositories, repositoryAdd } from "../data/websocket";
import { HacsStyles } from "../styles/hacs-common-style";
import { markdownWithRepositoryContext } from "../tools/markdown";
@customElement("hacs-repository-dashboard")
export class HacsRepositoryDashboard extends LitElement {
@property({ attribute: false }) public hacs!: Hacs;
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public narrow!: boolean;
@property({ attribute: false }) public isWide!: boolean;
@property({ attribute: false }) public route!: Route;
@state() public _repository?: RepositoryInfo;
@state() private _error?: string;
@query("#overflow-menu")
private _repositoryOverflowMenu!: HaMenu;
public connectedCallback() {
super.connectedCallback();
document.body.addEventListener("keydown", this._generateMyLink);
}
public disconnectedCallback() {
super.disconnectedCallback();
document.body.removeEventListener("keydown", this._generateMyLink);
}
private _generateMyLink = (ev: KeyboardEvent) => {
if (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey) {
// Ignore if modifier keys are pressed
return;
}
if (ev.key === "m" && mainWindow.location.pathname.startsWith("/hacs/repository/")) {
if (!this._repository) {
return;
}
const myParams = new URLSearchParams({
redirect: "hacs_repository",
owner: this._repository!.full_name.split("/")[0],
repository: this._repository!.full_name.split("/")[1],
category: this._repository!.category,
});
window.open(`https://my.home-assistant.io/create-link/?${myParams.toString()}`, "_blank");
}
};
protected async firstUpdated(changedProperties: PropertyValues): Promise<void> {
super.firstUpdated(changedProperties);
const params = extractSearchParamsObject();
if (Object.entries(params).length) {
let existing: RepositoryBase | undefined;
const requestedRepository = `${params.owner}/${params.repository}`;
existing = this.hacs.repositories.find(
(repository) =>
repository.full_name.toLocaleLowerCase() === requestedRepository.toLocaleLowerCase(),
);
if (!existing && params.category) {
if (
!(await showConfirmationDialog(this, {
title: this.hacs.localize("my.add_repository_title"),
text: this.hacs.localize("my.add_repository_description", {
repository: requestedRepository,
}),
confirmText: this.hacs.localize("common.add"),
dismissText: this.hacs.localize("common.cancel"),
}))
) {
this._error = this.hacs.localize("my.repository_not_found", {
repository: requestedRepository,
});
return;
}
try {
await repositoryAdd(this.hass, requestedRepository, params.category);
this.hacs.repositories = await getRepositories(this.hass);
existing = this.hacs.repositories.find(
(repository) =>
repository.full_name.toLocaleLowerCase() === requestedRepository.toLocaleLowerCase(),
);
} catch (err: any) {
this._error = err;
return;
}
}
if (existing) {
this._fetchRepository(String(existing.id));
} else {
this._error = this.hacs.localize("my.repository_not_found", {
repository: requestedRepository,
});
}
} else {
const dividerPos = this.route.path.indexOf("/", 1);
const repositoryId = this.route.path.substr(dividerPos + 1);
if (!repositoryId) {
this._error = "Missing repositoryId from route";
return;
}
this._fetchRepository(repositoryId);
}
}
protected updated(changedProps) {
super.updated(changedProps);
if (changedProps.has("repositories") && this._repository) {
this._fetchRepository();
}
}
private async _fetchRepository(repositoryId?: string) {
try {
this._repository = await fetchRepositoryInformation(
this.hass,
repositoryId || String(this._repository!.id),
);
} catch (err: any) {
this._error = err?.message;
}
}
private _getAuthors = memoizeOne((repository: RepositoryInfo) => {
const authors: string[] = [];
if (!repository.authors) return authors;
repository.authors.forEach((author) => authors.push(author.replace("@", "")));
if (authors.length === 0) {
const author = repository.full_name.split("/")[0];
if (
["custom-cards", "custom-components", "home-assistant-community-themes"].includes(author)
) {
return authors;
}
authors.push(author);
}
return authors;
});
protected render(): TemplateResult {
if (this._error) {
return html`<hass-error-screen .error=${this._error}></hass-error-screen>`;
}
if (!this._repository) {
return html`<hass-loading-screen></hass-loading-screen>`;
}
const authors = this._getAuthors(this._repository);
return html`
<hass-subpage
.hass=${this.hass}
.narrow=${this.narrow}
.route=${this.route}
.header=${this._repository.name}
hasFab
>
<ha-icon-button
slot="toolbar-icon"
.label=${this.hass.localize("ui.common.overflow_menu") || "overflow_menu"}
.path=${mdiDotsVertical}
@click=${this._showOverflowRepositoryMenu}
></ha-icon-button>
<div class="content">
<ha-card>
<ha-chip-set>
${this._repository.installed
? html`
<ha-assist-chip
.label=${this._repository.installed_version}
title="${this.hacs.localize("dialog_info.version_installed")}"
>
<ha-svg-icon slot="icon" .path=${mdiCube}></ha-svg-icon>
</ha-assist-chip>
`
: ""}
${authors
? authors.map(
(author) =>
html`<a
href="https://github.com/${author}"
target="_blank"
rel="noreferrer noopener"
>
<ha-assist-chip
.label=${author}
title="${this.hacs.localize("dialog_info.author")}"
>
<ha-svg-icon slot="icon" .path=${mdiAccount}></ha-svg-icon>
@${author}
</ha-assist-chip>
</a>`,
)
: ""}
${this._repository.downloads
? html` <ha-assist-chip
title="${this.hacs.localize("dialog_info.downloads")}"
.label=${String(this._repository.downloads)}
>
<ha-svg-icon slot="icon" .path=${mdiArrowDownBold}></ha-svg-icon>
</ha-assist-chip>`
: ""}
<ha-assist-chip
.label=${String(this._repository.stars)}
title="${this.hacs.localize("dialog_info.stars")}"
>
<ha-svg-icon slot="icon" .path=${mdiStar}></ha-svg-icon>
${this._repository.stars}
</ha-assist-chip>
<a
href="https://github.com/${this._repository.full_name}/issues"
target="_blank"
rel="noreferrer noopener"
>
<ha-assist-chip
.label=${String(this._repository.issues)}
title="${this.hacs.localize("dialog_info.open_issues")}"
>
<ha-svg-icon slot="icon" .path=${mdiExclamationThick}></ha-svg-icon>
${this._repository.issues}
</ha-assist-chip>
</a>
</ha-chip-set>
<ha-markdown
.content=${markdownWithRepositoryContext(
this._repository.additional_info,
this._repository,
) || this.hacs.localize("dialog_info.no_info")}
></ha-markdown>
</ha-card>
</div>
${!this._repository.installed_version
? html`<ha-fab
.label=${this.hacs.localize("common.download")}
.extended=${!this.narrow}
@click=${this._downloadRepositoryDialog}
>
<ha-svg-icon slot="icon" .path=${mdiDownload}></ha-svg-icon>
</ha-fab>`
: ""}
</hass-subpage>
<ha-menu id="overflow-menu" positioning="fixed">
${repositoryMenuItems(this, this._repository, this.hacs.localize).map((entry) =>
entry.divider
? html`<li divider role="separator"></li>`
: html`
<ha-md-menu-item
class="${entry.error ? "error" : entry.warning ? "warning" : ""}"
.clickAction=${() => {
entry?.action && entry.action();
}}
>
<ha-svg-icon .path=${entry.path} slot="start"></ha-svg-icon>
<div slot="headline">${entry.label}</div>
</ha-md-menu-item>
`,
)}
</ha-menu>
`;
}
private _showOverflowRepositoryMenu = (ev: any) => {
if (
this._repositoryOverflowMenu.open &&
ev.target === this._repositoryOverflowMenu.anchorElement
) {
this._repositoryOverflowMenu.close();
return;
}
this._repositoryOverflowMenu.anchorElement = ev.target;
this._repositoryOverflowMenu.show();
};
private _downloadRepositoryDialog() {
showHacsDownloadDialog(this, {
hacs: this.hacs,
repositoryId: this._repository!.id,
repository: this._repository!,
});
}
static get styles() {
return [
HacsStyles,
css`
hass-loading-screen {
--app-header-background-color: var(--sidebar-background-color);
--app-header-text-color: var(--sidebar-text-color);
height: 100vh;
}
hass-subpage {
position: absolute;
width: 100vw;
}
ha-fab ha-svg-icon {
color: var(--hcv-text-color-on-background);
}
ha-fab {
position: fixed;
float: right;
right: calc(18px + env(safe-area-inset-right));
bottom: calc(16px + env(safe-area-inset-bottom));
z-index: 1;
}
ha-fab.rtl {
float: left;
right: auto;
left: calc(18px + env(safe-area-inset-left));
}
ha-card {
display: block;
padding: 16px;
}
.content {
margin: auto;
padding: 8px;
max-width: 1536px;
direction: ltr;
}
ha-chip-set {
padding-bottom: 8px;
}
@media all and (max-width: 500px) {
.content {
margin: 8px 4px 64px;
max-width: none;
}
}
`,
];
}
}