From 89d4cb737f3bfb8613d170e155e8d047dc0041d3 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Fri, 6 Mar 2026 18:52:22 +0100 Subject: [PATCH] feat: path-collections orchestrator --- .../orchestrator/path-collections/element.js | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/components/orchestrator/path-collections/element.js diff --git a/src/components/orchestrator/path-collections/element.js b/src/components/orchestrator/path-collections/element.js new file mode 100644 index 00000000..fb352507 --- /dev/null +++ b/src/components/orchestrator/path-collections/element.js @@ -0,0 +1,101 @@ +import { computed } from "~/common/signal.js"; +import { OutputTransformer } from "~/components/transformer/output/base.js"; + +/** + * @import {PlaylistItem, Track} from "~/definitions/types.d.ts" + */ + +//////////////////////////////////////////// +// ELEMENT +//////////////////////////////////////////// + +class PathCollectionsOrchestrator extends OutputTransformer { + static NAME = "diffuse/orchestrator/path-collections"; + + constructor() { + super(); + + const base = this.base(); + + const ephemeralItems = computed(() => { + return createEphemeralItems(base.tracks.collection() ?? []); + }); + + this.facets = base.facets; + this.playlistItems = { + ...base.playlistItems, + collection: computed(() => { + const stored = base.playlistItems.collection() ?? []; + return [...stored, ...ephemeralItems()]; + }), + /** @type {(typeof base.playlistItems.save)} */ + save: async (items) => { + await base.playlistItems.save( + (items || []).filter((p) => !p.ephemeral), + ); + }, + }; + this.themes = base.themes; + this.tracks = base.tracks; + + this.ready = base.ready; + } +} + +/** + * @param {Track[]} tracks + * @returns {PlaylistItem[]} + */ +function createEphemeralItems(tracks) { + /** @type {PlaylistItem[]} */ + const items = []; + + /** @type {Set} */ + const segmentsAdded = new Set(); + + tracks.forEach((track) => { + const segment = pathSegment(track.uri); + if (!segment) return; + + if (segmentsAdded.has(segment)) return; + + /** @type {PlaylistItem} */ + const item = { + $type: "sh.diffuse.output.playlistItem", + id: `path-collections/${track.id}`, + playlist: segment, + criteria: [{ field: "uri", value: /** @type {any} */ (track.uri) }], + ephemeral: true, + }; + + items.push(item); + segmentsAdded.add(segment); + }); + + return items; +} + +/** + * @param {string} uri + * @returns {string | undefined} + */ +function pathSegment(uri) { + try { + const url = new URL(uri); + const segment = url.pathname.split("/").filter(Boolean)[0]; + return segment || undefined; + } catch { + return undefined; + } +} + +export default PathCollectionsOrchestrator; + +//////////////////////////////////////////// +// REGISTER +//////////////////////////////////////////// + +export const CLASS = PathCollectionsOrchestrator; +export const NAME = "do-path-collections"; + +customElements.define(NAME, CLASS); -- knotmirror 0.0.1