Skip to content

Commit

Permalink
Remove resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Jun 4, 2024
1 parent 3166bd5 commit 5cda9c7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 74 deletions.
10 changes: 2 additions & 8 deletions src/components/dialogs/hacs-download-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "../../data/repository";
import { repositoryBeta, websocketSubscription } from "../../data/websocket";
import { HacsStyles } from "../../styles/hacs-common-style";
import { generateFrontendResourceURL, updateFrontendResource } from "../../tools/frontend-resource";
import { generateFrontendResourceURL } from "../../tools/frontend-resource";
import type { HacsDownloadDialogParams } from "./show-hacs-dialog";
import { documentationUrl } from "../../tools/documentation";

Expand Down Expand Up @@ -177,7 +177,7 @@ export class HacsDonwloadDialog extends LitElement {
? html`
<p>${this._dialogParams.hacs.localize(`dialog_download.lovelace_instruction`)}</p>
<pre>
url: ${generateFrontendResourceURL({ repository: this._repository, skipTag: true })}
url: ${generateFrontendResourceURL({ repository: this._repository })}
type: module
</pre
>
Expand Down Expand Up @@ -281,12 +281,6 @@ export class HacsDonwloadDialog extends LitElement {
this._dialogParams!.hacs.info.lovelace_mode,
"_installRepository",
);
if (
this._repository.category === "plugin" &&
this._dialogParams!.hacs.info.lovelace_mode === "storage"
) {
await updateFrontendResource(this.hass, this._repository, selectedVersion);
}
this._installing = false;

if (this._repository.category === "plugin") {
Expand Down
18 changes: 1 addition & 17 deletions src/components/hacs-repository-owerflow-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ 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 { getConfigEntries } from "../../homeassistant-frontend/src/data/config_entries";
import {
deleteResource,
fetchResources,
} from "../../homeassistant-frontend/src/data/lovelace/resource";
import { showConfirmationDialog } from "../../homeassistant-frontend/src/dialogs/generic/show-dialog-box";
import type { RepositoryBase } from "../data/repository";
import {
Expand Down Expand Up @@ -79,7 +75,7 @@ export const repositoryMenuItems = memoizeOne(
label: element.hacs.localize("repository_card.open_source"),
action: () =>
mainWindow.open(
`/hacsfiles/${repository.local_path.split("/").pop()}/${repository.file_name}`,
`/hacsfiles/${repository.local_path.split("/").pop()}/${repository.file_name}?cachebuster=${Date.now()}`,
"_blank",
"noreferrer=true",
),
Expand Down Expand Up @@ -159,18 +155,6 @@ const _repositoryRemove = async (
element: HacsRepositoryDashboard | HacsDashboard,
repository: RepositoryBase,
) => {
if (repository.category === "plugin" && element.hacs.info?.lovelace_mode !== "yaml") {
const resources = await fetchResources(element.hass.connection);
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();
Expand Down
50 changes: 1 addition & 49 deletions src/tools/frontend-resource.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,7 @@
import {
createResource,
fetchResources,
updateResource,
} from "../../homeassistant-frontend/src/data/lovelace/resource";
import type { HomeAssistant } from "../../homeassistant-frontend/src/types";
import type { RepositoryInfo } from "../data/repository";
import { HacsLogger } from "./hacs-logger";

const generateUniqueTag = (repository: RepositoryInfo, version?: string): string =>
String(
`${repository.id}${(
version ||
repository.installed_version ||
repository.selected_tag ||
repository.available_version
).replace(/\D+/g, "")}`,
);

export async function updateFrontendResource(
hass: HomeAssistant,
repository: RepositoryInfo,
version?: string,
): Promise<void> {
const logger = new HacsLogger("updateLovelaceResources");
const resources = await fetchResources(hass.connection);
const namespace = `/hacsfiles/${repository.full_name.split("/")[1]}`;
const url = generateFrontendResourceURL({ repository, version });
const exsisting = resources.find((resource) => resource.url.includes(namespace));

logger.debug({ namespace, url, exsisting });

if (exsisting && exsisting.url !== url) {
logger.debug(`Updating exsusting resource for ${namespace}`);
await updateResource(hass, exsisting.id, {
url,
res_type: exsisting.type,
});
} else if (!resources.map((resource) => resource.url).includes(url)) {
logger.debug(`Adding ${url} to Lovelace resources`);
await createResource(hass, {
url,
res_type: "module",
});
}
}

export const generateFrontendResourceURL = (options: {
repository: RepositoryInfo;
version?: string;
skipTag?: boolean;
}): string =>
`/hacsfiles/${options.repository.full_name.split("/")[1]}/${options.repository.file_name}${
!options.skipTag ? `?hacstag=${generateUniqueTag(options.repository, options.version)}` : ""
}`;
`/hacsfiles/${options.repository.full_name.split("/")[1]}/${options.repository.file_name}`;

0 comments on commit 5cda9c7

Please sign in to comment.