Skip to content

Commit

Permalink
Add icon and overflow (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Oct 16, 2022
1 parent 91ff910 commit abf68af
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 152 deletions.
186 changes: 186 additions & 0 deletions src/components/hacs-repository-owerflow-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import {
mdiAlert,
mdiAlertCircleOutline,
mdiArrowDownCircle,
mdiClose,
mdiGithub,
mdiInformation,
mdiLanguageJavascript,
mdiReload,
} from "@mdi/js";
import memoizeOne from "memoize-one";
import { mainWindow } from "../../homeassistant-frontend/src/common/dom/get_main_window";
import { navigate } from "../../homeassistant-frontend/src/common/navigate";
import "../../homeassistant-frontend/src/components/ha-icon-overflow-menu";
import { getConfigEntries } from "../../homeassistant-frontend/src/data/config_entries";
import { showConfirmationDialog } from "../../homeassistant-frontend/src/dialogs/generic/show-dialog-box";
import { RepositoryBase } from "../data/repository";
import {
deleteResource,
fetchResources,
repositoryUninstall,
repositoryUpdate,
} from "../data/websocket";
import { HacsExperimentalPanel } from "../panels/hacs-experimental-panel";
import { HacsRepositoryPanel } from "../panels/hacs-repository-panel";

export const repositoryMenuItems = memoizeOne(
(element: HacsRepositoryPanel | HacsExperimentalPanel, repository: RepositoryBase) => [
...(element.nodeName === "HACS-EXPERIMENTAL-PANEL"
? [
{
path: mdiInformation,
label: element.hacs.localize("common.show"),
action: () => navigate(`/hacs/repository/${repository.id}`),
},
]
: []),
{
path: mdiGithub,
label: element.hacs.localize("common.repository"),
action: () =>
mainWindow.open(`https://github.com/${repository.full_name}`, "_blank", "noreferrer=true"),
},
{
path: mdiArrowDownCircle,
label: element.hacs.localize("repository_card.update_information"),
action: async () => {
await repositoryUpdate(element.hass, String(repository.id));
},
},

...(repository.installed_version
? [
{
path: mdiReload,
label: element.hacs.localize("repository_card.redownload"),
action: () => _downloadRepositoryDialog(element, repository.id),
hideForUninstalled: true,
},
]
: []),
...(repository.category === "plugin" && repository.installed_version
? [
{
path: mdiLanguageJavascript,
label: element.hacs.localize("repository_card.open_source"),
action: () =>
mainWindow.open(
`/hacsfiles/${repository.local_path.split("/").pop()}/${repository.file_name}`,
"_blank",
"noreferrer=true"
),
},
]
: []),
{ divider: true },
{
path: mdiAlertCircleOutline,
label: element.hacs.localize("repository_card.open_issue"),
action: () =>
mainWindow.open(
`https://github.com/${repository.full_name}/issues`,
"_blank",
"noreferrer=true"
),
},
...(repository.id !== "172733314" && repository.installed_version
? [
{
path: mdiAlert,
label: element.hacs.localize("repository_card.report"),
action: () =>
mainWindow.open(
`https://github.com/hacs/integration/issues/new?assignees=ludeeus&labels=flag&template=removal.yml&repo=${repository.full_name}&title=Request for removal of ${repository.full_name}`,
"_blank",
"noreferrer=true"
),
warning: true,
},
{
path: mdiClose,
label: element.hacs.localize("common.remove"),
action: async () => {
if (repository.category === "integration" && repository.config_flow) {
const configFlows = (await getConfigEntries(element.hass)).some(
(entry) => entry.domain === repository.domain
);
if (configFlows) {
const ignore = await showConfirmationDialog(element, {
title: element.hacs.localize("dialog.configured.title"),
text: element.hacs.localize("dialog.configured.message", {
name: repository.name,
}),
dismissText: element.hacs.localize("common.ignore"),
confirmText: element.hacs.localize("common.navigate"),
confirm: () => {
navigate("/config/integrations", { replace: true });
},
});
if (ignore) {
return;
}
}
}
element.dispatchEvent(
new CustomEvent("hacs-dialog", {
detail: {
type: "progress",
title: element.hacs.localize("dialog.remove.title"),
confirmText: element.hacs.localize("dialog.remove.title"),
content: element.hacs.localize("dialog.remove.message", {
name: repository.name,
}),
confirm: async () => {
await _repositoryRemove(element, repository);
},
},
bubbles: true,
composed: true,
})
);
},
warning: true,
},
]
: []),
]
);

const _downloadRepositoryDialog = (
element: HacsRepositoryPanel | HacsExperimentalPanel,
repositoryId: string
) => {
element.dispatchEvent(
new CustomEvent("hacs-dialog", {
detail: {
type: "download",
repository: repositoryId,
},
bubbles: true,
composed: true,
})
);
};

const _repositoryRemove = async (
element: HacsRepositoryPanel | HacsExperimentalPanel,
repository: RepositoryBase
) => {
if (repository.category === "plugin" && element.hacs.info?.lovelace_mode !== "yaml") {
const resources = await fetchResources(element.hass);
resources
.filter((resource) =>
resource.url.startsWith(
`/hacsfiles/${repository.full_name.split("/")[1]}/${repository.file_name}`
)
)
.forEach(async (resource) => {
await deleteResource(element.hass, String(resource.id));
});
}
await repositoryUninstall(element.hass, String(repository.id));
if (element.nodeName === "HACS-REPOSITORY-PANEL") {
history.back();
}
};
1 change: 1 addition & 0 deletions src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"cancel": "Cancel",
"close": "Close",
"download": "Download",
"explore": "Explore & download repositories",
"ignore": "Ignore",
"integration_plural": "Integrations",
"integration": "Integration",
Expand Down
41 changes: 39 additions & 2 deletions src/panels/hacs-experimental-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import type {

import "../../homeassistant-frontend/src/components/ha-button-menu";
import "../../homeassistant-frontend/src/components/ha-check-list-item";
import "../../homeassistant-frontend/src/components/ha-chip";
import "../../homeassistant-frontend/src/components/ha-fab";
import "../../homeassistant-frontend/src/components/ha-menu-button";
import "../../homeassistant-frontend/src/components/ha-svg-icon";
import { haStyle } from "../../homeassistant-frontend/src/resources/styles";
import type { HomeAssistant, Route } from "../../homeassistant-frontend/src/types";
import { brandsUrl } from "../../homeassistant-frontend/src/util/brands-url";
import { showDialogAbout } from "../components/dialogs/hacs-about-dialog";
import { hacsIcon } from "../components/hacs-icon";
import { repositoryMenuItems } from "../components/hacs-repository-owerflow-menu";
import "../components/hacs-tabs-subpage-data-table";
import type { Hacs } from "../data/hacs";
import type { RepositoryBase } from "../data/repository";
Expand Down Expand Up @@ -206,7 +207,7 @@ export class HacsExperimentalPanel extends LitElement {
${this.section === "entry"
? html`
<a href="/hacs/explore" slot="fab">
<ha-fab .label=${this.hacs.localize("store.explore")} .extended=${!this.narrow}>
<ha-fab .label=${this.hacs.localize("common.explore")} .extended=${!this.narrow}>
<ha-svg-icon slot="icon" .path=${mdiPlus}></ha-svg-icon> </ha-fab
></a>
`
Expand Down Expand Up @@ -234,6 +235,26 @@ export class HacsExperimentalPanel extends LitElement {
narrow: boolean,
tableColumnsOptions: TableColumnsOptions
): DataTableColumnContainer<RepositoryBase> => ({
icon: {
title: "",
label: this.hass.localize("ui.panel.config.lovelace.dashboards.picker.headers.icon"),
hidden: this.narrow || this.section !== "entry",
type: "icon",
template: (_, repository: RepositoryBase) =>
html`
<img
style="height: 32px; width: 32px"
slot="item-icon"
src=${brandsUrl({
domain: repository.domain || "github",
type: "icon",
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
referrerpolicy="no-referrer"
/>
`,
},
name: {
...defaultKeyData,
title: this.hacs.localize("column.name"),
Expand Down Expand Up @@ -282,13 +303,29 @@ export class HacsExperimentalPanel extends LitElement {
hidden: narrow || !tableColumnsOptions[this.section].category,
sortable: true,
width: "10%",
template: (category: string) => this.hacs.localize(`common.${category}`),
},
authors: defaultKeyData,
description: defaultKeyData,
domain: defaultKeyData,
full_name: defaultKeyData,
id: defaultKeyData,
topics: defaultKeyData,
actions: {
title: "",
width: this.narrow ? undefined : "10%",
hidden: this.section !== "entry",
type: "overflow-menu",
template: (_, repository: RepositoryBase) =>
html`
<ha-icon-overflow-menu
.hass=${this.hass}
narrow
.items=${repositoryMenuItems(this, repository)}
>
</ha-icon-overflow-menu>
`,
},
})
);

Expand Down
Loading

0 comments on commit abf68af

Please sign in to comment.