diff --git a/src/components/configurator/output/element.js b/src/components/configurator/output/element.js index 70551c15..873306fe 100644 --- a/src/components/configurator/output/element.js +++ b/src/components/configurator/output/element.js @@ -2,11 +2,16 @@ import { DiffuseElement } from "@common/element.js"; import { computed, signal } from "@common/signal.js"; /** - * @import {ProxiedActions} from "@common/worker.d.ts" * @import {Track} from "@definitions/types.d.ts" * @import {OutputManager, OutputElement} from "@components/output/types.d.ts" */ +/** + * @typedef {OutputElement} Output + */ + +const STORAGE_PREFIX = "diffuse/configurator/output"; + //////////////////////////////////////////// // ELEMENT //////////////////////////////////////////// @@ -16,7 +21,6 @@ import { computed, signal } from "@common/signal.js"; */ class OutputConfigurator extends DiffuseElement { static NAME = "diffuse/configurator/output"; - static WORKER_URL = "components/configurator/output/worker.js"; constructor() { super(); @@ -25,13 +29,25 @@ class OutputConfigurator extends DiffuseElement { const manager = { tracks: { collection: computed(() => { + const out = this.#selectedOutput.value; + if (out) return out.tracks.collection(); return this.#memory.tracks.value; }), - reload: async () => {}, + reload: () => { + const out = this.#selectedOutput.value; + if (out) return out.tracks.reload(); + return Promise.resolve(); + }, save: async (newTracks) => { + const out = this.#selectedOutput.value; + if (out) return await out.tracks.save(newTracks); this.#memory.tracks.value = newTracks; }, - state: () => "loaded", + state: computed(() => { + const out = this.#selectedOutput.value; + if (out) return out.tracks.state(); + return out === undefined ? "loading" : "loaded"; + }), }, }; @@ -45,6 +61,10 @@ class OutputConfigurator extends DiffuseElement { tracks: signal(/** @type {Track[]} */ ([])), }; + #selectedOutput = signal( + /** @type {Output | null | undefined} */ (undefined), + ); + // LIFECYCLE /** @@ -52,6 +72,68 @@ class OutputConfigurator extends DiffuseElement { */ async connectedCallback() { super.connectedCallback(); + this.#selectedOutput.value = await this.#findSelectedOutput(); + + this.effect(() => { + console.log("selectedOutput changed", this.#selectedOutput.value); + }); + + this.effect(() => { + console.log("collection changed", this.tracks.collection()); + }); + + this.effect(() => { + console.log("state changed", this.tracks.state()); + }); + } + + // MISC + + async #findSelectedOutput() { + const id = localStorage.getItem(`${STORAGE_PREFIX}/selected/id`); + const el = id ? this.root().querySelector(`#${id}`) : null; + + if (!el) return null; + + await customElements.whenDefined(el.localName); + + if ( + "nameWithGroup" in el === false || + "tracks" in el === false + ) { + return null; + } + + return /** @type {Output} */ (/** @type {unknown} */ (el)); + } + + /** + * @override + */ + dependencies() { + return Object.fromEntries( + Array.from(this.children).flatMap((element) => { + if (element.hasAttribute("id") === false) { + console.warn( + "Missing `id` for output configurator child element with `localName` '" + + element.localName + "'", + ); + return []; + } + + const d = /** @type {DiffuseElement} */ (element); + return [[d.id, d]]; + }), + ); + } + + // ADDITIONAL ACTIONS + + /** + * @param {string} id + */ + selectOutput(id) { + localStorage.setItem(`${STORAGE_PREFIX}/selected/id`, id); } } diff --git a/src/components/output/polymorphic/indexed-db/constants.js b/src/components/output/polymorphic/indexed-db/constants.js index 6e78669b..f40032da 100644 --- a/src/components/output/polymorphic/indexed-db/constants.js +++ b/src/components/output/polymorphic/indexed-db/constants.js @@ -1 +1 @@ -export const IDB_PREFIX = "@components/output/polymorphic/indexed-db"; +export const IDB_PREFIX = "diffuse/output/polymorphic/indexed-db"; diff --git a/src/themes/webamp/index.js b/src/themes/webamp/index.js index b154e1eb..601f2e9e 100644 --- a/src/themes/webamp/index.js +++ b/src/themes/webamp/index.js @@ -1,7 +1,8 @@ -import "@components/orchestrator/process-tracks/element.js"; -import "@components/orchestrator/queue-tracks/element.js"; +import "@components/configurator/output/element.js"; import "@components/input/opensubsonic/element.js"; import "@components/input/s3/element.js"; +import "@components/orchestrator/process-tracks/element.js"; +import "@components/orchestrator/queue-tracks/element.js"; import "@components/output/polymorphic/indexed-db/element.js"; import "@components/processor/metadata/element.js"; import "@components/transformer/output/string/json/element.js"; @@ -22,6 +23,7 @@ const input = component(Input); const queue = component(Queue); globalThis.queue = queue; +globalThis.output = document.querySelector("#output"); //////////////////////////////////////////// // 📡 diff --git a/src/themes/webamp/index.vto b/src/themes/webamp/index.vto index 442c3218..86c6e2b8 100644 --- a/src/themes/webamp/index.vto +++ b/src/themes/webamp/index.vto @@ -77,18 +77,23 @@ --> - - + + - - - + + + + + + + +