// The "Furniture packs" block of the Extensions page: the user's own packs, imported from files or pasted // as JSON, and removing them. The packs that come with NextFloor are always there and are not listed. // Importing or removing fires `packs-changed`; the panel loads the packs again. import { css, html, LitElement, nothing } from "lit"; import { importPack, removePack } from "../api.ts"; import { REPO_URL } from "../features.ts"; import { translate, type I18nKey } from "../i18n.ts"; import { packLabel } from "../furniture-names.ts"; import type { FurniturePack } from "../packs.ts"; import { downloadStorePack, fetchStorePacks, storePackState, STORE_REPO, type StorePack } from "../plugins/store.ts"; /** The furniture builder on the project's site. */ const BUILDER_URL = "https://therealmrbk.github.io/NextFloor/builder/"; import { controls, tokens } from "../styles.ts"; import type { HomeAssistant } from "../types.ts"; export class PackManager extends LitElement { static properties = { hass: { attribute: false }, packs: { attribute: false }, _msg: { state: true }, _busy: { state: true }, _store: { state: true }, _storeError: { state: true }, }; declare hass: HomeAssistant | undefined; declare packs: FurniturePack[] | undefined; declare _msg: { ok: boolean; text: string } | null; declare _busy: boolean; /** The community furniture of the store, once loaded (null: not asked for yet, "loading" while it comes). */ declare _store: StorePack[] | "loading" | null; declare _storeError: string | null; constructor() { super(); this._msg = null; this._busy = false; this._store = null; this._storeError = null; } /** The store's list is fetched only when asked for (no request to GitHub unless the user wants it). */ private async loadStore(): Promise { this._store = "loading"; this._storeError = null; try { this._store = await fetchStorePacks(); } catch (err) { this._store = null; this._storeError = this.t("community_failed", { detail: (err as Error)?.message ?? String(err) }); } } private async installStore(entry: StorePack): Promise { this._busy = true; this._msg = null; try { const res = await importPack(this.hass!, await downloadStorePack(entry)); this._msg = { ok: true, text: this.t("pack_imported", { name: res.name, publisher: res.publisher, n: res.items }) }; this.changed(); } catch (err) { this._msg = { ok: false, text: this.errorText(err) }; } finally { this._busy = false; } } private renderStore() { const installed = this.packs ?? []; const store = this._store; return html`

${this.t("community_title")}

${this.t("community_intro")} ${this.t("community_builder")} · ${this.t("community_how")}

${store === null ? html`
` : store === "loading" ? html`

${this.t("loading")}

` : store.length ? html`` : html`

${this.t("community_none")}

`} ${this._storeError ? html`` : nothing}`; } private t(key: I18nKey, vars?: Record): string { return translate(this.hass, key, vars); } private changed(): void { this.dispatchEvent(new CustomEvent("packs-changed", { bubbles: true, composed: true })); } private errorText(err: unknown): string { const { code, message } = (err ?? {}) as { code?: string; message?: string }; const detail = message ?? String(err); const key = `pack_error_${code}` as I18nKey; const text = this.t(key, { detail }); return text === key ? this.t("pack_error_other", { detail }) : text; } /** Imports each text as a pack; labels name the files in the message (empty for pasted JSON). Returns how many went in. */ private async importTexts(sources: { label: string; text: string }[]): Promise { if (!this.hass || !sources.length) return 0; this._busy = true; const done: string[] = []; const failed: string[] = []; for (const { label, text } of sources) { try { const res = await importPack(this.hass, text); done.push(this.t("pack_imported", { name: res.name, publisher: res.publisher, n: res.items })); } catch (err) { failed.push(label ? `${label}: ${this.errorText(err)}` : this.errorText(err)); } } this._busy = false; if (done.length) this.changed(); const summary = sources.length > 1 ? [this.t("packs_imported_n", { n: done.length, total: sources.length })] : []; this._msg = { ok: failed.length === 0, text: [...summary, ...done, ...failed].join(" · ") }; return done.length; } private async importFiles(e: Event): Promise { const input = e.target as HTMLInputElement; const files = [...(input.files ?? [])]; input.value = ""; await this.importTexts(await Promise.all(files.map(async (f) => ({ label: f.name, text: await f.text() })))); } private async importPasted(): Promise { const area = this.renderRoot.querySelector("textarea"); const text = area?.value.trim() ?? ""; if (!area || !text) return void (this._msg = { ok: false, text: this.t("pack_paste_empty") }); try { JSON.parse(text); } catch (err) { return void (this._msg = { ok: false, text: this.t("pack_paste_not_json", { detail: err instanceof Error ? err.message : String(err) }) }); } if ((await this.importTexts([{ label: "", text }])) > 0) area.value = ""; } private async deletePack(pack: FurniturePack): Promise { if (!this.hass || !confirm(this.t("pack_remove_confirm", { name: packLabel(pack, this.hass.language ?? "de") }))) return; try { await removePack(this.hass, pack.id); this._msg = null; this.changed(); } catch (err) { this._msg = { ok: false, text: this.errorText(err) }; } } protected render() { const own = (this.packs ?? []).filter((p) => !p.builtin); const builtin = (this.packs ?? []).length - own.length; const language = this.hass?.language ?? "de"; return html`

${this.t("packs_title")}

${this.t("packs_intro", { n: builtin })} ${this.t("packs_docs")}

${own.length ? html`
    ${own.map( (p) => html`
  • ${packLabel(p, language)} ${p.id} · ${this.t("pack_by", { publisher: p.publisher, n: p.items.length })} ${p.description ? html`${p.description}` : nothing}
  • `, )}
` : html`

${this.t("packs_none")}

`}
${this._msg ? html`

${this._msg.text}

` : nothing} ${this.hass ? this.renderStore() : nothing}
`; } static styles = [ tokens, controls, css` .nf-packs { display: grid; gap: 10px; padding: 16px; border: 1px solid var(--nf-line); border-radius: 12px; } h3 { margin: 0; font-size: 17px; } .nf-sub, .nf-ok, .nf-error { margin: 0; font-size: 13px; } .nf-sub { color: var(--nf-soft); } .nf-ok { color: var(--success-color, #2e7d32); } .nf-error { color: var(--error-color, #c62828); overflow-wrap: anywhere; } h4 { margin: 14px 0 0; font-size: 15px; } ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; } li { display: flex; gap: 10px; align-items: center; } .nf-pack-main { flex: 1; min-width: 0; display: grid; gap: 2px; } .nf-detail { font-size: 12px; color: var(--nf-soft); overflow-wrap: anywhere; } .nf-file { justify-self: start; cursor: pointer; } textarea { box-sizing: border-box; width: 100%; min-height: 110px; resize: vertical; padding: 8px 10px; border: 1px solid var(--nf-line); border-radius: 8px; background: transparent; color: inherit; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; } `, ]; } if (!customElements.get("nf-pack-manager")) customElements.define("nf-pack-manager", PackManager);