diff --git a/src/facets/connect/common.css b/src/facets/connect/common.css new file mode 100644 index 00000000..a4a03ad4 --- /dev/null +++ b/src/facets/connect/common.css @@ -0,0 +1,99 @@ +main { + display: flex; + align-items: center; + justify-content: center; + min-height: 100dvh; + opacity: 0; + pointer-events: none; + transition: opacity 750ms; + + &.has-loaded { + opacity: 1; + pointer-events: auto; + } +} + +wa-card { + width: min(400px, calc(100vw - 2rem)); +} + +.card-header { + display: flex; + align-items: center; + justify-content: space-between; +} + +.card-body { + display: flex; + flex-direction: column; + gap: var(--wa-space-m); +} + +.button-row { + display: flex; + gap: var(--wa-space-s); + flex-wrap: wrap; +} + +.dialog-body { + display: flex; + flex-direction: column; + gap: var(--wa-space-m); +} + +.dialog-footer { + display: flex; + gap: var(--wa-space-s); +} + +.connect-list { + list-style: none; + padding: 0; + margin: 0; + display: flex; + flex-direction: column; + gap: var(--wa-space-s); +} + +.connect-item { + display: flex; + align-items: center; + gap: var(--wa-space-s); +} + +.connect-item__info { + display: flex; + flex-direction: column; + gap: var(--wa-space-2xs); + flex: 1; + min-width: 0; +} + +.connect-item__name { + font-weight: var(--wa-font-weight-semibold); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.connect-item__detail { + font-size: var(--wa-font-size-xs); + color: var(--wa-color-text-quiet); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.connect-item__tags { + display: flex; + gap: var(--wa-space-2xs); + flex-shrink: 0; +} + +[hidden] { + display: none !important; +} + +p { + margin: 0; +} diff --git a/src/facets/connect/common.js b/src/facets/connect/common.js new file mode 100644 index 00000000..863e39ff --- /dev/null +++ b/src/facets/connect/common.js @@ -0,0 +1,154 @@ +import "@awesome.me/webawesome/dist/components/badge/badge.js"; +import "@awesome.me/webawesome/dist/components/button/button.js"; +import "@awesome.me/webawesome/dist/components/card/card.js"; +import "@awesome.me/webawesome/dist/components/dialog/dialog.js"; +import "@awesome.me/webawesome/dist/components/divider/divider.js"; +import "@awesome.me/webawesome/dist/components/icon/icon.js"; + +import { html, nothing, render as litRender } from "lit-html"; + +/** + * @import { default as WaDialog } from "@awesome.me/webawesome/dist/components/dialog/dialog.js" + * @import { TemplateResult } from "lit-html" + */ + +/** + * @typedef {{ name: string; detail: string; isInput: boolean; isOutput: boolean; onRemove: () => void }} ConnectItem + */ + +/** + * Sets up a connect facet UI: a card with "Add audio input" and + * "Use as userdata storage" buttons, a dialog with a form, and a + * reactive list of configured items below a divider. + * + * @param {Object} config + * @param {string} config.title - Card header title + * @param {TemplateResult | string} config.description - Content above the buttons + * @param {TemplateResult} config.formFields - Form body content (inputs, footnotes, etc.) + * @param {(mode: 'input' | 'output') => Promise} config.onSubmit + * + * @returns {{ setItems: (items: ConnectItem[]) => void }} + */ +export function setup({ title, description, formFields, onSubmit }) { + const main = document.querySelector("main"); + if (!main) throw new Error("No
element"); + + litRender( + html` + +
+ ${title} +
+
+ ${description} +
+ + + Add audio input + + + + Use as userdata storage + +
+ + +
+
+ + +
+ ${formFields} +
+ +
+ `, + main, + ); + + const dialog = /** @type {WaDialog} */ (main.querySelector("#connect-dialog")); + const form = /** @type {HTMLFormElement} */ (main.querySelector("#connect-form")); + const divider = /** @type {HTMLElement} */ (main.querySelector("#connect-divider")); + const list = /** @type {HTMLElement} */ (main.querySelector("#connect-list")); + + /** @type {'input' | 'output'} */ + let mode = "input"; + + /** @param {'input' | 'output'} m */ + const openDialog = (m) => { + mode = m; + dialog.label = m === "input" ? "Add audio input" : "Use as userdata storage"; + form.reset(); + dialog.open = true; + }; + + main + .querySelector("#connect-add-input-btn") + ?.addEventListener("click", () => openDialog("input")); + + main + .querySelector("#connect-add-output-btn") + ?.addEventListener("click", () => openDialog("output")); + + main.querySelector("#connect-cancel-btn")?.addEventListener("click", () => { + dialog.open = false; + }); + + form.addEventListener("submit", async (e) => { + e.preventDefault(); + await onSubmit(mode); + dialog.open = false; + }); + + return { + /** + * Updates the list of configured items below the divider. + * Call inside an effect() for reactivity. + * + * @param {ConnectItem[]} items + */ + setItems(items) { + divider.hidden = items.length === 0; + list.hidden = items.length === 0; + litRender( + html`${items.map( + ({ name, detail, isInput, isOutput, onRemove }) => html` +
  • +
    + ${name} + ${detail} +
    +
    + ${isInput + ? html`Input` + : nothing} + ${isOutput + ? html`Output` + : nothing} +
    + + + +
  • + `, + )}`, + list, + ); + }, + }; +} diff --git a/src/facets/connect/s3/index.html b/src/facets/connect/s3/index.html index 46ee972c..38aeb7d4 100644 --- a/src/facets/connect/s3/index.html +++ b/src/facets/connect/s3/index.html @@ -1,161 +1,15 @@ -
    - -
    - S3 -
    - -
    -

    Connect to an S3-compatible storage to use it as audio input or user-data storage.

    - - - -
    - - - Add audio input - - - - Use as userdata storage - -
    - - - - -
    -
    - - -
    - - - - - - -
    - -
    -
    +
    diff --git a/src/facets/connect/s3/index.inline.js b/src/facets/connect/s3/index.inline.js index 91f49fc9..8d7735a3 100644 --- a/src/facets/connect/s3/index.inline.js +++ b/src/facets/connect/s3/index.inline.js @@ -1,15 +1,9 @@ -import "@awesome.me/webawesome/dist/components/badge/badge.js"; -import "@awesome.me/webawesome/dist/components/button/button.js"; -import "@awesome.me/webawesome/dist/components/card/card.js"; -import "@awesome.me/webawesome/dist/components/dialog/dialog.js"; -import "@awesome.me/webawesome/dist/components/divider/divider.js"; -import "@awesome.me/webawesome/dist/components/icon/icon.js"; import "@awesome.me/webawesome/dist/components/input/input.js"; import "~/common/webawesome/detect-dark.js"; import "~/common/webawesome/phosphor/bold.js"; -import { html, nothing, render } from "lit-html"; +import { html } from "lit-html"; import * as IDB from "idb-keyval"; import * as TID from "@atcute/tid"; @@ -19,10 +13,11 @@ import { SCHEME as S3_SCHEME } from "~/components/input/s3/constants.js"; import foundation from "~/common/foundation.js"; import { effect, signal } from "~/common/signal.js"; +import { setup } from "~/facets/connect/common.js"; + document.title = "Connect S3 | Diffuse"; /** - * @import { default as WaDialog } from "@awesome.me/webawesome/dist/components/dialog/dialog.js" * @import { default as WaInput } from "@awesome.me/webawesome/dist/components/input/input.js" * @import { Bucket } from "~/components/input/s3/types.d.ts" */ @@ -46,50 +41,64 @@ await Promise.all([ customElements.whenDefined(sourcesOrchestrator.localName), ]); -//////////////////////////////////////////// -// STATE -//////////////////////////////////////////// - -/** @type {'input' | 'output'} */ -let dialogMode = "input"; - const $outputBucket = signal( /** @type {Bucket | undefined} */ (await IDB.get(OUTPUT_IDB_KEY)), ); //////////////////////////////////////////// -// ELEMENTS +// UI //////////////////////////////////////////// -const bucketDialog = /** @type {WaDialog} */ ( - document.querySelector("#bucket-dialog") -); - -const hostInput = /** @type {WaInput} */ (document.querySelector("#host-input")); -const bucketNameInput = /** @type {WaInput} */ ( - document.querySelector("#bucket-name-input") -); -const regionInput = /** @type {WaInput} */ ( - document.querySelector("#region-input") -); -const accessKeyInput = /** @type {WaInput} */ ( - document.querySelector("#access-key-input") -); -const secretKeyInput = /** @type {WaInput} */ ( - document.querySelector("#secret-key-input") -); -const pathInput = /** @type {WaInput} */ (document.querySelector("#path-input")); - -const bucketsDivider = /** @type {HTMLElement} */ ( - document.querySelector("#buckets-divider") -); +const { setItems } = setup({ + title: "S3", + + description: html` +

    + Connect to an S3-compatible storage to use it as audio input or user-data + storage. +

    + + `, + + formFields: html` + + + + + + + + `, + + onSubmit: (mode) => addBucket(mode), +}); -const bucketsList = /** @type {HTMLElement} */ ( - document.querySelector("#buckets-list") -); +const accessKeyInput = + /** @type {WaInput} */ (document.querySelector("#s3-access-key")); +const secretKeyInput = + /** @type {WaInput} */ (document.querySelector("#s3-secret-key")); +const bucketNameInput = + /** @type {WaInput} */ (document.querySelector("#s3-bucket-name")); +const hostInput = /** @type {WaInput} */ (document.querySelector("#s3-host")); +const regionInput = + /** @type {WaInput} */ (document.querySelector("#s3-region")); +const pathInput = /** @type {WaInput} */ (document.querySelector("#s3-path")); //////////////////////////////////////////// -// RENDER +// REACTIVE LIST //////////////////////////////////////////// effect(() => { @@ -130,40 +139,14 @@ effect(() => { } } - const hasAny = allBuckets.size > 0; - bucketsDivider.hidden = !hasAny; - bucketsList.hidden = !hasAny; - - render( - html` - ${[...allBuckets.entries()].map( - ([id, { name, host, uri, isInput, isOutput }]) => html` -
  • -
    - ${name} - ${host} -
    -
    - ${isInput - ? html`Input` - : nothing} - ${isOutput - ? html`Output` - : nothing} -
    - - - -
  • - `, - )} - `, - bucketsList, + setItems( + [...allBuckets.values()].map(({ name, host, uri, isInput, isOutput }) => ({ + name, + detail: host, + isInput, + isOutput, + onRemove: () => removeBucket(uri, isOutput), + })), ); }); @@ -171,20 +154,6 @@ effect(() => { // ACTIONS //////////////////////////////////////////// -/** @param {'input' | 'output'} mode */ -function openDialog(mode) { - dialogMode = mode; - bucketDialog.label = - mode === "input" ? "Add audio input" : "Use as userdata storage"; - hostInput.value = ""; - bucketNameInput.value = ""; - regionInput.value = ""; - accessKeyInput.value = ""; - secretKeyInput.value = ""; - pathInput.value = ""; - bucketDialog.open = true; -} - /** * @param {string | undefined} uri * @param {boolean} isOutput @@ -208,20 +177,24 @@ async function removeBucket(uri, isOutput) { } } -async function addBucket() { - const host = hostInput.value?.trim(); - const bucketName = bucketNameInput.value?.trim(); - const region = regionInput.value?.trim() || "us-east-1"; +/** @param {'input' | 'output'} mode */ +async function addBucket(mode) { const accessKey = accessKeyInput.value?.trim(); const secretKey = secretKeyInput.value?.trim(); + const bucketName = bucketNameInput.value?.trim(); + const rawHost = hostInput.value?.trim(); + const host = rawHost?.length + ? rawHost.replace(/^\w+:\/\//, "") + : "s3.amazonaws.com"; + const region = regionInput.value?.trim() || "us-east-1"; const path = pathInput.value?.trim() || "/"; - if (!host || !bucketName || !accessKey || !secretKey) return; + if (!accessKey || !secretKey || !bucketName) return; /** @type {Bucket} */ const bucket = { accessKey, bucketName, host, path, region, secretKey }; - if (dialogMode === "input") { + if (mode === "input") { const now = new Date().toISOString(); const uri = buildURI(bucket); @@ -248,24 +221,4 @@ async function addBucket() { ); if (option) await outputOrchestrator.select(option.id); } - - bucketDialog.open = false; } - -//////////////////////////////////////////// -// EVENT LISTENERS -//////////////////////////////////////////// - -document - .querySelector("#add-input-btn") - ?.addEventListener("click", () => openDialog("input")); - -document - .querySelector("#add-output-btn") - ?.addEventListener("click", () => openDialog("output")); - -document.querySelector("#cancel-btn")?.addEventListener("click", () => { - bucketDialog.open = false; -}); - -document.querySelector("#confirm-btn")?.addEventListener("click", addBucket);