Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d1b2b40ffd | |||
| 8a70f478c3 | |||
| 9c344b98c4 | |||
| 42080b37ea | |||
| a37c19fd45 | |||
| facc215450 | |||
| 98e9344261 | |||
| 1228118027 | |||
| 538cfb9c5b | |||
| 2ad3b128d6 | |||
| 55aa8933af | |||
| dacb8b9278 | |||
| bcfb36d53e | |||
| 451dbb94a8 | |||
| af0d8e7646 | |||
| f712f871f1 | |||
| a2fa425853 | |||
| ceee9b9d12 |
@@ -1,2 +1,6 @@
|
|||||||
# Default backend URL (overridable at runtime in the UI)
|
# Default backend URL (overridable at runtime in the UI)
|
||||||
PUBLIC_API_BASE_URL=http://localhost:8080/api/v1
|
PUBLIC_API_BASE_URL=http://localhost:8080/api/v1
|
||||||
|
|
||||||
|
# Show the public sign-up UI on the connect screen. Set to false to hide it.
|
||||||
|
# The backend's ALLOW_REGISTRATION is the real authority; this only gates the UI.
|
||||||
|
PUBLIC_ENABLE_REGISTRATION=true
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
name: Docker Build & Publish
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
env:
|
||||||
|
# Number of tagged (non-latest) versions to keep per image name.
|
||||||
|
KEEP_VERSIONS: "5"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
outputs:
|
||||||
|
host: ${{ steps.meta.outputs.host }}
|
||||||
|
image: ${{ steps.meta.outputs.image }}
|
||||||
|
sha: ${{ steps.meta.outputs.sha }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Resolve registry metadata
|
||||||
|
id: meta
|
||||||
|
run: |
|
||||||
|
host=$(echo "${{ gitea.server_url }}" | sed 's|https\?://||; s|/$||')
|
||||||
|
repo_lc=$(echo "${{ gitea.repository }}" | tr '[:upper:]' '[:lower:]')
|
||||||
|
echo "host=$host" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "image=$host/$repo_lc" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "sha=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Build image
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: dockerfiles/Dockerfile.prod
|
||||||
|
push: false
|
||||||
|
build-args: |
|
||||||
|
PUBLIC_API_BASE_URL=/api/v1
|
||||||
|
tags: |
|
||||||
|
${{ steps.meta.outputs.image }}:latest
|
||||||
|
${{ steps.meta.outputs.image }}:${{ steps.meta.outputs.sha }}
|
||||||
|
outputs: type=docker,dest=/tmp/image.tar
|
||||||
|
|
||||||
|
- name: Upload image artifact
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: docker-image
|
||||||
|
path: /tmp/image.tar
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
push:
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Download image artifact
|
||||||
|
uses: actions/download-artifact@v3
|
||||||
|
with:
|
||||||
|
name: docker-image
|
||||||
|
path: /tmp
|
||||||
|
|
||||||
|
- name: Load image
|
||||||
|
run: docker load < /tmp/image.tar
|
||||||
|
|
||||||
|
- name: Log in to Gitea registry
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ${{ needs.build.outputs.host }}
|
||||||
|
username: ${{ gitea.actor }}
|
||||||
|
password: ${{ secrets.PACKAGE_REGISTRY_TOKEN }}
|
||||||
|
|
||||||
|
- name: Push image
|
||||||
|
run: |
|
||||||
|
docker push ${{ needs.build.outputs.image }}:latest
|
||||||
|
docker push ${{ needs.build.outputs.image }}:${{ needs.build.outputs.sha }}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
name: Prune old image versions
|
||||||
|
needs: push
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Delete versions beyond KEEP_VERSIONS
|
||||||
|
env:
|
||||||
|
GITEA_URL: ${{ gitea.server_url }}
|
||||||
|
OWNER: ${{ gitea.repository_owner }}
|
||||||
|
IMAGE: ${{ gitea.event.repository.name }}
|
||||||
|
TOKEN: ${{ secrets.PACKAGE_REGISTRY_TOKEN }}
|
||||||
|
run: |
|
||||||
|
image=$(echo "$IMAGE" | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
|
# List all container package versions for this image (page size 50 is
|
||||||
|
# enough for typical repos; increase if you push very frequently).
|
||||||
|
response=$(curl -sf \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
"${GITEA_URL}/api/v1/packages/${OWNER}?type=container&limit=50&q=${image}")
|
||||||
|
|
||||||
|
# Keep the KEEP_VERSIONS newest SHA-tagged versions; always preserve 'latest'.
|
||||||
|
to_delete=$(printf '%s' "$response" \
|
||||||
|
| jq -r \
|
||||||
|
--arg name "$image" \
|
||||||
|
--argjson keep "$KEEP_VERSIONS" \
|
||||||
|
'[.[] | select(.name == $name and .version != "latest")]
|
||||||
|
| sort_by(.created) | reverse
|
||||||
|
| .[$keep:][].version')
|
||||||
|
|
||||||
|
if [ -z "$to_delete" ]; then
|
||||||
|
echo "Nothing to prune."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
while IFS= read -r version; do
|
||||||
|
echo "Deleting ${image}:${version}"
|
||||||
|
curl -sf -X DELETE \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
"${GITEA_URL}/api/v1/packages/${OWNER}/container/${image}/${version}" \
|
||||||
|
&& echo " ok" || echo " failed (may already be gone, continuing)"
|
||||||
|
done <<< "$to_delete"
|
||||||
Executable
+26
@@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Write the SPA's runtime operator config at container start.
|
||||||
|
#
|
||||||
|
# The nginx base image runs every /docker-entrypoint.d/*.sh before launching
|
||||||
|
# nginx, so this overwrites the build-time public/config.js stub with the
|
||||||
|
# operator's runtime config ($PUBLIC_API_BASE_URL, $PUBLIC_ENABLE_REGISTRATION).
|
||||||
|
# That lets one prebuilt image target any backend origin and toggle sign-up
|
||||||
|
# without rebuilding. Resolution + precedence live in src/config/env.ts.
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
: "${PUBLIC_API_BASE_URL:=/api/v1}"
|
||||||
|
: "${PUBLIC_ENABLE_REGISTRATION:=true}"
|
||||||
|
ROOT="${NGINX_HTML_ROOT:-/usr/share/nginx/html}"
|
||||||
|
|
||||||
|
# Anything but "false"/"0" enables the sign-up UI (mirrors parseFlag in env.ts).
|
||||||
|
if [ "$PUBLIC_ENABLE_REGISTRATION" = "false" ] || [ "$PUBLIC_ENABLE_REGISTRATION" = "0" ]; then
|
||||||
|
ENABLE_REGISTRATION=false
|
||||||
|
else
|
||||||
|
ENABLE_REGISTRATION=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf 'window.__APP_CONFIG__={"apiBaseUrl":"%s","enableRegistration":%s};\n' \
|
||||||
|
"$PUBLIC_API_BASE_URL" "$ENABLE_REGISTRATION" \
|
||||||
|
>"$ROOT/config.js"
|
||||||
|
|
||||||
|
echo "runtime-config: wrote apiBaseUrl=$PUBLIC_API_BASE_URL enableRegistration=$ENABLE_REGISTRATION to $ROOT/config.js"
|
||||||
@@ -4,14 +4,9 @@
|
|||||||
# shadows the container install. Build context = mcma-webui/.
|
# shadows the container install. Build context = mcma-webui/.
|
||||||
FROM node:22-slim
|
FROM node:22-slim
|
||||||
|
|
||||||
# `modern-sk` is a git dependency (git+https://...) — npm needs git to fetch it.
|
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y --no-install-recommends git \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json package-lock.json modern-sk-*.tgz ./
|
COPY package.json package-lock.json ./
|
||||||
RUN npm ci
|
RUN npm ci
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|||||||
@@ -8,13 +8,14 @@ FROM node:22-slim AS build
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json package-lock.json modern-sk-*.tgz ./
|
COPY package.json package-lock.json ./
|
||||||
RUN npm ci
|
RUN npm ci
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Bake the API base URL at build time (rsbuild inlines PUBLIC_* vars).
|
# Build-time default for the API base URL (rsbuild inlines PUBLIC_* vars). This
|
||||||
# Same-origin default ('/api/v1') works behind any reverse proxy.
|
# is only the *fallback* now — the real value is injected at container start by
|
||||||
|
# 30-runtime-config.sh, so the image can target any backend without a rebuild.
|
||||||
ARG PUBLIC_API_BASE_URL=/api/v1
|
ARG PUBLIC_API_BASE_URL=/api/v1
|
||||||
ENV PUBLIC_API_BASE_URL=$PUBLIC_API_BASE_URL
|
ENV PUBLIC_API_BASE_URL=$PUBLIC_API_BASE_URL
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
@@ -25,5 +26,10 @@ FROM nginx:1.27-alpine AS runtime
|
|||||||
COPY dockerfiles/nginx.conf /etc/nginx/conf.d/default.conf
|
COPY dockerfiles/nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
COPY --from=build /app/dist /usr/share/nginx/html
|
COPY --from=build /app/dist /usr/share/nginx/html
|
||||||
|
|
||||||
|
# Runtime config injection: the nginx image runs /docker-entrypoint.d/*.sh
|
||||||
|
# before starting, regenerating /config.js from $PUBLIC_API_BASE_URL.
|
||||||
|
COPY dockerfiles/30-runtime-config.sh /docker-entrypoint.d/30-runtime-config.sh
|
||||||
|
RUN chmod +x /docker-entrypoint.d/30-runtime-config.sh
|
||||||
|
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
|
|||||||
@@ -14,6 +14,13 @@ server {
|
|||||||
try_files $uri =404;
|
try_files $uri =404;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Runtime operator config — regenerated per container start, so it must
|
||||||
|
# never be cached or a redeployed backend URL would be ignored.
|
||||||
|
location = /config.js {
|
||||||
|
add_header Cache-Control "no-store";
|
||||||
|
try_files $uri =404;
|
||||||
|
}
|
||||||
|
|
||||||
# SPA: every unknown path falls back to index.html (client-side router).
|
# SPA: every unknown path falls back to index.html (client-side router).
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
|
|||||||
Generated
+4
-4
@@ -8,7 +8,7 @@
|
|||||||
"name": "mcma-webui",
|
"name": "mcma-webui",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@olly/modern-sk": "0.1.4-3",
|
"@olly/modern-sk": "^0.1.5",
|
||||||
"@phosphor-icons/react": "^2.1.10",
|
"@phosphor-icons/react": "^2.1.10",
|
||||||
"@reduxjs/toolkit": "^2.12.0",
|
"@reduxjs/toolkit": "^2.12.0",
|
||||||
"i18next": "^26.3.1",
|
"i18next": "^26.3.1",
|
||||||
@@ -693,9 +693,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@olly/modern-sk": {
|
"node_modules/@olly/modern-sk": {
|
||||||
"version": "0.1.4-3",
|
"version": "0.1.5",
|
||||||
"resolved": "https://git.ollyhearn.ru/api/packages/olly/npm/%40olly%2Fmodern-sk/-/0.1.4-3/modern-sk-0.1.4-3.tgz",
|
"resolved": "https://git.ollyhearn.ru/api/packages/olly/npm/%40olly%2Fmodern-sk/-/0.1.5/modern-sk-0.1.5.tgz",
|
||||||
"integrity": "sha512-h+d+Jd3DBr7d51V78p1Eb5rVrpN4PAskwQFnh2X4Dk7Q8oajiMVJuhZU1amx97bKHFDHgcOfhwc4cS8P4tFCmQ==",
|
"integrity": "sha512-rhKp4U2IovSZkgdfg4oZqyhF0GgB8oR5TPlPXg0iYQEuEtff5zAgRXS+uY3dOPg2tStG3ysHUJaohD9YS2ADiA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@phosphor-icons/react": "^2.1.10",
|
"@phosphor-icons/react": "^2.1.10",
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@
|
|||||||
"test:watch": "rstest --watch"
|
"test:watch": "rstest --watch"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@olly/modern-sk": "0.1.4-3",
|
"@olly/modern-sk": "^0.1.5",
|
||||||
"@phosphor-icons/react": "^2.1.10",
|
"@phosphor-icons/react": "^2.1.10",
|
||||||
"@reduxjs/toolkit": "^2.12.0",
|
"@reduxjs/toolkit": "^2.12.0",
|
||||||
"i18next": "^26.3.1",
|
"i18next": "^26.3.1",
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// Runtime operator configuration, read by the app before the bundle loads
|
||||||
|
// (see src/config/env.ts). In the PROD image this file is OVERWRITTEN at
|
||||||
|
// container start from $PUBLIC_API_BASE_URL (dockerfiles/30-runtime-config.sh),
|
||||||
|
// so one prebuilt image can target any backend origin without a rebuild.
|
||||||
|
//
|
||||||
|
// This committed stub is the local-dev / build-time default: it leaves the
|
||||||
|
// config empty so base-URL resolution falls back to the build-time env var.
|
||||||
|
window.__APP_CONFIG__ = {};
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "MCMA — Music",
|
||||||
|
"short_name": "MCMA",
|
||||||
|
"description": "Self-hosted music — control center and offline-capable player.",
|
||||||
|
"start_url": "/",
|
||||||
|
"scope": "/",
|
||||||
|
"display": "standalone",
|
||||||
|
"orientation": "any",
|
||||||
|
"background_color": "#0b0b0b",
|
||||||
|
"theme_color": "#0b0b0b",
|
||||||
|
"icons": [
|
||||||
|
{
|
||||||
|
"src": "/favicon.png",
|
||||||
|
"sizes": "128x128",
|
||||||
|
"type": "image/png",
|
||||||
|
"purpose": "any"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* Service-worker core: pure helpers with NO side effects (no `self`, no
|
||||||
|
* `caches`, no `fetch`). Split out from `sw.js` so the tricky bits — HTTP Range
|
||||||
|
* parsing, LRU eviction, cache-key normalization — can be unit-tested in Node.
|
||||||
|
*
|
||||||
|
* This is an ES module: `sw.js` imports it (the SW is registered with
|
||||||
|
* { type: 'module' }) and tests import it natively — one source, no drift.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Bump the version to invalidate every cached blob on a breaking change.
|
||||||
|
export const AUDIO_CACHE = 'mcma-audio-v1';
|
||||||
|
// Synthetic same-origin key holding the LRU index ({ key: {size, lastAccess} }).
|
||||||
|
export const INDEX_URL = '/__mcma_audio_index__';
|
||||||
|
// Soft cap on total cached audio. LRU eviction keeps us under this.
|
||||||
|
export const MAX_BYTES = 500 * 1024 * 1024; // 500 MB
|
||||||
|
|
||||||
|
// Cover art (album/artist/playlist images) gets its own cache with a simple
|
||||||
|
// count cap — covers are small and stable, so no per-byte LRU is needed.
|
||||||
|
export const COVER_CACHE = 'mcma-covers-v1';
|
||||||
|
export const MAX_COVERS = 600;
|
||||||
|
|
||||||
|
// Backend stream route: /api/v1/stream/<trackId>?token=...
|
||||||
|
const STREAM_RE = /\/stream\/([^/?#]+)/;
|
||||||
|
|
||||||
|
/** The track (content) id from a stream URL, or null if it isn't one. */
|
||||||
|
export function trackIdFromUrl(url) {
|
||||||
|
const m = STREAM_RE.exec(url);
|
||||||
|
return m ? m[1] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Canonical cache key for a stream URL: origin + path, query dropped. The auth
|
||||||
|
* token rides in the query and rotates on refresh, so keying by content path
|
||||||
|
* (origin keeps two backends from colliding) makes the cache token-stable —
|
||||||
|
* the same track is one entry regardless of which token fetched it.
|
||||||
|
*/
|
||||||
|
export function cacheKeyFor(url) {
|
||||||
|
try {
|
||||||
|
const u = new URL(url);
|
||||||
|
return u.origin + u.pathname;
|
||||||
|
} catch {
|
||||||
|
return String(url).split('?')[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse an HTTP `Range` header against a known resource size. Returns inclusive
|
||||||
|
* { start, end } byte offsets, or null for "no range / serve the whole thing".
|
||||||
|
* Handles `bytes=a-b`, `bytes=a-` (open-ended) and `bytes=-n` (last n bytes).
|
||||||
|
*/
|
||||||
|
export function parseRangeHeader(rangeHeader, size) {
|
||||||
|
if (!rangeHeader) return null;
|
||||||
|
const m = /^bytes=(\d*)-(\d*)$/.exec(String(rangeHeader).trim());
|
||||||
|
if (!m) return null;
|
||||||
|
let start = m[1] === '' ? null : parseInt(m[1], 10);
|
||||||
|
let end = m[2] === '' ? null : parseInt(m[2], 10);
|
||||||
|
if (start === null && end === null) return null;
|
||||||
|
if (start === null) {
|
||||||
|
// suffix range: the final `end` bytes
|
||||||
|
start = Math.max(0, size - end);
|
||||||
|
end = size - 1;
|
||||||
|
} else if (end === null) {
|
||||||
|
end = size - 1;
|
||||||
|
}
|
||||||
|
if (Number.isNaN(start) || Number.isNaN(end)) return null;
|
||||||
|
if (end >= size) end = size - 1;
|
||||||
|
if (start < 0 || start > end) return null;
|
||||||
|
return { start, end };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Choose which cached entries to evict (least-recently-used first) so that the
|
||||||
|
* existing total plus an incoming blob fits under `maxBytes`. Returns the list
|
||||||
|
* of keys to delete; empty when there's already room.
|
||||||
|
*
|
||||||
|
* `index` is { [key]: { size, lastAccess } }.
|
||||||
|
*/
|
||||||
|
export function selectEvictions(index, incomingSize, maxBytes) {
|
||||||
|
let total = incomingSize;
|
||||||
|
for (const k in index) total += index[k].size || 0;
|
||||||
|
if (total <= maxBytes) return [];
|
||||||
|
|
||||||
|
const entries = Object.keys(index)
|
||||||
|
.map((k) => ({
|
||||||
|
key: k,
|
||||||
|
lastAccess: index[k].lastAccess || 0,
|
||||||
|
size: index[k].size || 0,
|
||||||
|
}))
|
||||||
|
.sort((a, b) => a.lastAccess - b.lastAccess); // oldest first
|
||||||
|
|
||||||
|
const evict = [];
|
||||||
|
for (const e of entries) {
|
||||||
|
if (total <= maxBytes) break;
|
||||||
|
evict.push(e.key);
|
||||||
|
total -= e.size;
|
||||||
|
}
|
||||||
|
return evict;
|
||||||
|
}
|
||||||
+256
@@ -0,0 +1,256 @@
|
|||||||
|
/*
|
||||||
|
* MCMA service worker — Tier 3 offline support: audio blob cache.
|
||||||
|
*
|
||||||
|
* It sits between the app and the network for audio-stream requests only
|
||||||
|
* (`/stream/<id>`). The first time a track is streamed it's copied
|
||||||
|
* into the Cache API (keyed by content id, token stripped); afterwards — or
|
||||||
|
* whenever the backend is unreachable — playback is served straight from the
|
||||||
|
* cache, so already-heard tracks play with no network at all.
|
||||||
|
*
|
||||||
|
* Pure helpers (range parsing, LRU, key normalization) live in sw-core.js so
|
||||||
|
* they can be unit-tested; this file owns the side-effectful cache/network I/O.
|
||||||
|
* Registered as a module worker ({ type: 'module' }), so it uses ES imports.
|
||||||
|
*/
|
||||||
|
import {
|
||||||
|
AUDIO_CACHE,
|
||||||
|
INDEX_URL,
|
||||||
|
MAX_BYTES,
|
||||||
|
COVER_CACHE,
|
||||||
|
MAX_COVERS,
|
||||||
|
trackIdFromUrl,
|
||||||
|
cacheKeyFor,
|
||||||
|
parseRangeHeader,
|
||||||
|
selectEvictions,
|
||||||
|
} from './sw-core.js';
|
||||||
|
|
||||||
|
self.addEventListener('install', () => {
|
||||||
|
// Activate immediately on first install / update — no stale SW lingering.
|
||||||
|
self.skipWaiting();
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener('activate', (event) => {
|
||||||
|
event.waitUntil(self.clients.claim());
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener('fetch', (event) => {
|
||||||
|
const req = event.request;
|
||||||
|
if (req.method !== 'GET') return;
|
||||||
|
if (trackIdFromUrl(req.url)) {
|
||||||
|
event.respondWith(handleAudio(event)); // audio stream → range-aware cache
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (req.destination === 'image') {
|
||||||
|
event.respondWith(handleImage(event)); // cover art → stale-while-revalidate
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function handleAudio(event) {
|
||||||
|
const req = event.request;
|
||||||
|
const key = cacheKeyFor(req.url);
|
||||||
|
const range = req.headers.get('range');
|
||||||
|
const cache = await caches.open(AUDIO_CACHE);
|
||||||
|
|
||||||
|
// 1) Serve from cache when we have it (works fully offline).
|
||||||
|
const cached = await cache.match(key);
|
||||||
|
if (cached) {
|
||||||
|
event.waitUntil(touch(key));
|
||||||
|
return range ? buildRangeResponse(cached, range) : cached.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) Cache miss → fetch the WHOLE file (strip Range) so we can store a
|
||||||
|
// complete copy, then satisfy the original request (range-sliced if asked).
|
||||||
|
try {
|
||||||
|
const fullReq = new Request(req.url, { headers: withoutRange(req.headers) });
|
||||||
|
const resp = await fetch(fullReq);
|
||||||
|
if (isCacheable(resp)) {
|
||||||
|
event.waitUntil(storeInCache(key, resp.clone()));
|
||||||
|
}
|
||||||
|
return range ? buildRangeResponse(resp, range) : resp;
|
||||||
|
} catch {
|
||||||
|
// Offline and never cached — nothing we can do for this one.
|
||||||
|
return new Response('Offline and not cached', {
|
||||||
|
status: 504,
|
||||||
|
statusText: 'Offline',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cover art: stale-while-revalidate. Serve the cached image instantly (so the
|
||||||
|
* library renders offline), refresh it in the background, and cache fresh hits.
|
||||||
|
* Cover URLs are token-free and stable, and `<img>` is happy with opaque
|
||||||
|
* cross-origin responses — so unlike audio, these cache even cross-origin.
|
||||||
|
*/
|
||||||
|
async function handleImage(event) {
|
||||||
|
const req = event.request;
|
||||||
|
const cache = await caches.open(COVER_CACHE);
|
||||||
|
const cached = await cache.match(req);
|
||||||
|
|
||||||
|
const fromNetwork = fetch(req)
|
||||||
|
.then((resp) => {
|
||||||
|
if (resp && (resp.status === 200 || resp.type === 'opaque')) {
|
||||||
|
return cache
|
||||||
|
.put(req, resp.clone())
|
||||||
|
.then(() => trimCovers(cache))
|
||||||
|
.then(() => resp);
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
})
|
||||||
|
.catch(() => null);
|
||||||
|
|
||||||
|
if (cached) {
|
||||||
|
event.waitUntil(fromNetwork); // revalidate without blocking the response
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
const resp = await fromNetwork;
|
||||||
|
return resp || new Response('', { status: 504, statusText: 'Offline' });
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Keep the cover cache bounded — drop the oldest entries past the cap. */
|
||||||
|
async function trimCovers(cache) {
|
||||||
|
const keys = await cache.keys();
|
||||||
|
const excess = keys.length - MAX_COVERS;
|
||||||
|
for (let i = 0; i < excess; i++) await cache.delete(keys[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Only cache readable, complete 200 responses — opaque/partial are useless. */
|
||||||
|
function isCacheable(resp) {
|
||||||
|
return (
|
||||||
|
resp.status === 200 &&
|
||||||
|
resp.type !== 'opaque' &&
|
||||||
|
resp.type !== 'opaqueredirect'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function withoutRange(headers) {
|
||||||
|
const out = new Headers();
|
||||||
|
for (const [k, v] of headers.entries()) {
|
||||||
|
if (k.toLowerCase() !== 'range') out.set(k, v);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build a 206 Partial Content response by slicing a full cached/fetched body. */
|
||||||
|
async function buildRangeResponse(response, rangeHeader) {
|
||||||
|
const buf = await response.clone().arrayBuffer();
|
||||||
|
const size = buf.byteLength;
|
||||||
|
const r = parseRangeHeader(rangeHeader, size);
|
||||||
|
const type = response.headers.get('content-type') || 'application/octet-stream';
|
||||||
|
|
||||||
|
if (!r) {
|
||||||
|
return new Response(buf, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'content-type': type,
|
||||||
|
'content-length': String(size),
|
||||||
|
'accept-ranges': 'bytes',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const sliced = buf.slice(r.start, r.end + 1);
|
||||||
|
return new Response(sliced, {
|
||||||
|
status: 206,
|
||||||
|
statusText: 'Partial Content',
|
||||||
|
headers: {
|
||||||
|
'content-type': type,
|
||||||
|
'content-range': `bytes ${r.start}-${r.end}/${size}`,
|
||||||
|
'content-length': String(sliced.byteLength),
|
||||||
|
'accept-ranges': 'bytes',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function responseSize(resp) {
|
||||||
|
const len = resp.headers.get('content-length');
|
||||||
|
if (len) return Number(len);
|
||||||
|
const blob = await resp.blob();
|
||||||
|
return blob.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- LRU index -------------------------------------------------------------
|
||||||
|
// The index lives as a JSON entry in the same cache. All mutations go through a
|
||||||
|
// single serialized chain so concurrent fetch handlers can't clobber it.
|
||||||
|
let indexChain = Promise.resolve();
|
||||||
|
|
||||||
|
async function readIndex(cache) {
|
||||||
|
try {
|
||||||
|
const res = await cache.match(INDEX_URL);
|
||||||
|
if (!res) return {};
|
||||||
|
return (await res.json()) || {};
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeIndex(mutator) {
|
||||||
|
indexChain = indexChain
|
||||||
|
.then(async () => {
|
||||||
|
const cache = await caches.open(AUDIO_CACHE);
|
||||||
|
const index = await readIndex(cache);
|
||||||
|
await mutator(cache, index);
|
||||||
|
await cache.put(
|
||||||
|
INDEX_URL,
|
||||||
|
new Response(JSON.stringify(index), {
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
/* keep the chain alive even if one write fails */
|
||||||
|
});
|
||||||
|
return indexChain;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function storeInCache(key, resp) {
|
||||||
|
const size = await responseSize(resp.clone());
|
||||||
|
await writeIndex(async (cache, index) => {
|
||||||
|
for (const ek of selectEvictions(index, size, MAX_BYTES)) {
|
||||||
|
await cache.delete(ek);
|
||||||
|
delete index[ek];
|
||||||
|
}
|
||||||
|
await cache.put(key, resp);
|
||||||
|
index[key] = { size, lastAccess: Date.now() };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function touch(key) {
|
||||||
|
return writeIndex((_cache, index) => {
|
||||||
|
if (index[key]) index[key].lastAccess = Date.now();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- client messaging ------------------------------------------------------
|
||||||
|
// The app talks to the SW over a MessageChannel: it sends a port, we reply on
|
||||||
|
// it. Used for the offline-cache stats/controls in the UI.
|
||||||
|
self.addEventListener('message', (event) => {
|
||||||
|
const data = event.data || {};
|
||||||
|
const reply = (result) => {
|
||||||
|
if (event.ports && event.ports[0]) event.ports[0].postMessage(result);
|
||||||
|
};
|
||||||
|
event.waitUntil(
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const cache = await caches.open(AUDIO_CACHE);
|
||||||
|
const index = await readIndex(cache);
|
||||||
|
if (data.type === 'STATS') {
|
||||||
|
const keys = Object.keys(index);
|
||||||
|
const bytes = keys.reduce((s, k) => s + (index[k].size || 0), 0);
|
||||||
|
reply({ count: keys.length, bytes, maxBytes: MAX_BYTES });
|
||||||
|
} else if (data.type === 'HAS') {
|
||||||
|
reply({ cached: !!index[cacheKeyFor(data.url)] });
|
||||||
|
} else if (data.type === 'CLEAR') {
|
||||||
|
await Promise.all([
|
||||||
|
caches.delete(AUDIO_CACHE),
|
||||||
|
caches.delete(COVER_CACHE),
|
||||||
|
]);
|
||||||
|
reply({ ok: true });
|
||||||
|
} else {
|
||||||
|
reply({ error: 'unknown-message' });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
reply({ error: String(e) });
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -32,5 +32,38 @@ export default defineConfig({
|
|||||||
// no manual source.define needed. See src/config/env.ts.
|
// no manual source.define needed. See src/config/env.ts.
|
||||||
html: {
|
html: {
|
||||||
title: 'MCMA',
|
title: 'MCMA',
|
||||||
|
// PWA: link the manifest + declare theme/icon so the browser offers
|
||||||
|
// "Install app". The service worker (audio offline cache) is registered
|
||||||
|
// from src/index.tsx, not here.
|
||||||
|
tags: [
|
||||||
|
// Theme bootstrap — runs inline before first paint to kill the flash of
|
||||||
|
// white on a dark-themed load. Mirrors modern-sk's own logic exactly
|
||||||
|
// (localStorage 'modern-sk-theme' || 'dark' → data-theme on <html>), so
|
||||||
|
// there's no second flip when <ThemeProvider> mounts. Inline (not an
|
||||||
|
// external file) so it costs zero round-trips.
|
||||||
|
{
|
||||||
|
tag: 'script',
|
||||||
|
children:
|
||||||
|
"(function(){try{var t=localStorage.getItem('modern-sk-theme')||'dark';document.documentElement.setAttribute('data-theme',t);}catch(e){}})();",
|
||||||
|
head: true,
|
||||||
|
append: false,
|
||||||
|
},
|
||||||
|
// Runtime operator config. A classic (non-deferred) head script, so it
|
||||||
|
// runs before the deferred app bundle and window.__APP_CONFIG__ is set by
|
||||||
|
// the time src/config/env.ts reads it. Served from public/ in dev and
|
||||||
|
// overwritten from $PUBLIC_API_BASE_URL at container start in prod.
|
||||||
|
{
|
||||||
|
tag: 'script',
|
||||||
|
attrs: { src: '/config.js' },
|
||||||
|
head: true,
|
||||||
|
append: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag: 'link',
|
||||||
|
attrs: { rel: 'manifest', href: '/manifest.webmanifest' },
|
||||||
|
},
|
||||||
|
{ tag: 'meta', attrs: { name: 'theme-color', content: '#0b0b0b' } },
|
||||||
|
{ tag: 'link', attrs: { rel: 'apple-touch-icon', href: '/favicon.png' } },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
+9
-10
@@ -34,24 +34,23 @@ export const baseQueryWithReauth: BaseQueryFn<
|
|||||||
{
|
{
|
||||||
url: '/auth/refresh',
|
url: '/auth/refresh',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { refreshToken },
|
body: { refresh_token: refreshToken },
|
||||||
},
|
},
|
||||||
api,
|
api,
|
||||||
extraOptions,
|
extraOptions,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (refreshResult.data) {
|
if (refreshResult.data) {
|
||||||
const {
|
// Backend wire format is snake_case with no TTL (see auth.ts adapter).
|
||||||
accessToken,
|
const { access_token, refresh_token } = refreshResult.data as {
|
||||||
refreshToken: newRefresh,
|
access_token: string;
|
||||||
expiresIn,
|
refresh_token: string;
|
||||||
} = refreshResult.data as {
|
|
||||||
accessToken: string;
|
|
||||||
refreshToken: string;
|
|
||||||
expiresIn: number;
|
|
||||||
};
|
};
|
||||||
api.dispatch(
|
api.dispatch(
|
||||||
setTokens({ accessToken, refreshToken: newRefresh, expiresIn }),
|
setTokens({
|
||||||
|
accessToken: access_token,
|
||||||
|
refreshToken: refresh_token,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
result = await rawBaseQuery()(args, api, extraOptions);
|
result = await rawBaseQuery()(args, api, extraOptions);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+21
-10
@@ -1,33 +1,44 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
|
import { toUser, type RawUser } from '../mappers';
|
||||||
import type { User } from '../types';
|
import type { User } from '../types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin user management. The backend models authorization as `is_superuser` /
|
||||||
|
* `is_active` (no `role`/`email`); `toUser` maps superuser→role for the UI and
|
||||||
|
* the mutations translate role back to `is_superuser` on the way out.
|
||||||
|
*/
|
||||||
export const adminApi = api.injectEndpoints({
|
export const adminApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
getUsers: build.query<User[], void>({
|
getUsers: build.query<User[], void>({
|
||||||
query: () => '/admin/users',
|
query: () => '/admin/users',
|
||||||
|
transformResponse: (raw: RawUser[]) => raw.map(toUser),
|
||||||
providesTags: ['User'],
|
providesTags: ['User'],
|
||||||
}),
|
}),
|
||||||
createUser: build.mutation<
|
createUser: build.mutation<
|
||||||
User,
|
User,
|
||||||
{
|
{ username: string; password: string; role: 'admin' | 'user' }
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
email?: string;
|
|
||||||
role: 'admin' | 'user';
|
|
||||||
}
|
|
||||||
>({
|
>({
|
||||||
query: (body) => ({ url: '/admin/users', method: 'POST', body }),
|
query: ({ username, password, role }) => ({
|
||||||
|
url: '/admin/users',
|
||||||
|
method: 'POST',
|
||||||
|
body: { username, password, is_superuser: role === 'admin' },
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawUser) => toUser(raw),
|
||||||
invalidatesTags: ['User'],
|
invalidatesTags: ['User'],
|
||||||
}),
|
}),
|
||||||
updateUser: build.mutation<
|
updateUser: build.mutation<
|
||||||
User,
|
User,
|
||||||
{ id: string; role?: 'admin' | 'user'; email?: string }
|
{ id: string; role?: 'admin' | 'user'; isActive?: boolean }
|
||||||
>({
|
>({
|
||||||
query: ({ id, ...body }) => ({
|
query: ({ id, role, isActive }) => ({
|
||||||
url: `/admin/users/${id}`,
|
url: `/admin/users/${id}`,
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
body,
|
body: {
|
||||||
|
is_superuser: role === undefined ? undefined : role === 'admin',
|
||||||
|
is_active: isActive,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
|
transformResponse: (raw: RawUser) => toUser(raw),
|
||||||
invalidatesTags: ['User'],
|
invalidatesTags: ['User'],
|
||||||
}),
|
}),
|
||||||
deleteUser: build.mutation<void, string>({
|
deleteUser: build.mutation<void, string>({
|
||||||
|
|||||||
+95
-11
@@ -1,26 +1,110 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
import type { LoginRequest, LoginResponse } from '../types';
|
import { toUser, type RawUser } from '../mappers';
|
||||||
|
import type {
|
||||||
|
AuthTokens,
|
||||||
|
LoginRequest,
|
||||||
|
LoginResponse,
|
||||||
|
RegisterRequest,
|
||||||
|
User,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auth seam over the backend's wire format: tokens-only login + a separate
|
||||||
|
* `/auth/me` for the user. Token mapping lives here; user mapping is shared with
|
||||||
|
* the admin endpoints via `toUser` in `mappers.ts`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** `/auth/login` & `/auth/refresh` response shape. */
|
||||||
|
interface RawTokenResponse {
|
||||||
|
access_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
token_type?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const toTokens = (raw: RawTokenResponse): AuthTokens => ({
|
||||||
|
accessToken: raw.access_token,
|
||||||
|
refreshToken: raw.refresh_token,
|
||||||
|
// No TTL on the wire — expiry is 401-driven (see baseQuery reauth).
|
||||||
|
});
|
||||||
|
|
||||||
export const authApi = api.injectEndpoints({
|
export const authApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
|
// Login is a two-call flow: POST /auth/login yields tokens, then GET
|
||||||
|
// /auth/me resolves the user. A queryFn chains both so callers get the
|
||||||
|
// unified { user, tokens } the UI expects in one await.
|
||||||
login: build.mutation<LoginResponse, LoginRequest>({
|
login: build.mutation<LoginResponse, LoginRequest>({
|
||||||
query: (body) => ({ url: '/auth/login', method: 'POST', body }),
|
async queryFn(body, _api, _extra, baseQuery) {
|
||||||
|
const tokenRes = await baseQuery({
|
||||||
|
url: '/auth/login',
|
||||||
|
method: 'POST',
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
if (tokenRes.error) return { error: tokenRes.error };
|
||||||
|
const tokens = toTokens(tokenRes.data as RawTokenResponse);
|
||||||
|
|
||||||
|
// The access token isn't in the store yet, so attach it explicitly —
|
||||||
|
// baseQuery's prepareHeaders only injects what's already in auth state.
|
||||||
|
const meRes = await baseQuery({
|
||||||
|
url: '/auth/me',
|
||||||
|
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
||||||
|
});
|
||||||
|
if (meRes.error) return { error: meRes.error };
|
||||||
|
const user = toUser(meRes.data as RawUser);
|
||||||
|
|
||||||
|
return { data: { user, tokens } };
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
logout: build.mutation<void, void>({
|
// Sign-up mirrors login: POST /auth/register returns a token pair (the
|
||||||
query: () => ({ url: '/auth/logout', method: 'POST' }),
|
// backend logs the new account straight in), then GET /auth/me resolves the
|
||||||
|
// user — so the UI gets the same unified { user, tokens } as login.
|
||||||
|
register: build.mutation<LoginResponse, RegisterRequest>({
|
||||||
|
async queryFn(body, _api, _extra, baseQuery) {
|
||||||
|
const tokenRes = await baseQuery({
|
||||||
|
url: '/auth/register',
|
||||||
|
method: 'POST',
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
if (tokenRes.error) return { error: tokenRes.error };
|
||||||
|
const tokens = toTokens(tokenRes.data as RawTokenResponse);
|
||||||
|
|
||||||
|
const meRes = await baseQuery({
|
||||||
|
url: '/auth/me',
|
||||||
|
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
||||||
|
});
|
||||||
|
if (meRes.error) return { error: meRes.error };
|
||||||
|
const user = toUser(meRes.data as RawUser);
|
||||||
|
|
||||||
|
return { data: { user, tokens } };
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
refreshToken: build.mutation<
|
logout: build.mutation<void, { refreshToken: string }>({
|
||||||
{ accessToken: string; refreshToken: string; expiresIn: number },
|
query: ({ refreshToken }) => ({
|
||||||
{ refreshToken: string }
|
url: '/auth/logout',
|
||||||
>({
|
method: 'POST',
|
||||||
query: (body) => ({ url: '/auth/refresh', method: 'POST', body }),
|
body: { refresh_token: refreshToken },
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
me: build.query<import('../types').User, void>({
|
refreshToken: build.mutation<AuthTokens, { refreshToken: string }>({
|
||||||
|
query: ({ refreshToken }) => ({
|
||||||
|
url: '/auth/refresh',
|
||||||
|
method: 'POST',
|
||||||
|
body: { refresh_token: refreshToken },
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawTokenResponse) => toTokens(raw),
|
||||||
|
}),
|
||||||
|
me: build.query<User, void>({
|
||||||
query: () => '/auth/me',
|
query: () => '/auth/me',
|
||||||
|
transformResponse: (raw: RawUser) => toUser(raw),
|
||||||
providesTags: ['User'],
|
providesTags: ['User'],
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
overrideExisting: false,
|
overrideExisting: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const { useLoginMutation, useLogoutMutation, useMeQuery } = authApi;
|
export const {
|
||||||
|
useLoginMutation,
|
||||||
|
useRegisterMutation,
|
||||||
|
useLogoutMutation,
|
||||||
|
useRefreshTokenMutation,
|
||||||
|
useMeQuery,
|
||||||
|
} = authApi;
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
import type { DownloadJob } from '../types';
|
import type { DownloadJob } from '../types';
|
||||||
|
|
||||||
|
// NOTE: the backend `/downloads` routes are still unimplemented stubs (they
|
||||||
|
// return no body / no schema). The request shapes below are provisional and the
|
||||||
|
// responses will need the same snake→camel mapper treatment as library/playlists
|
||||||
|
// (see `mappers.ts`) once the backend defines DownloadJob's wire format. Do not
|
||||||
|
// wire these into the UI until then.
|
||||||
|
|
||||||
export const downloadsApi = api.injectEndpoints({
|
export const downloadsApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
getDownloads: build.query<
|
getDownloads: build.query<
|
||||||
|
|||||||
@@ -1,16 +1,61 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
|
import {
|
||||||
|
toAlbum,
|
||||||
|
toArtist,
|
||||||
|
toMetadataMatch,
|
||||||
|
toPage,
|
||||||
|
toTrack,
|
||||||
|
type RawAlbum,
|
||||||
|
type RawArtist,
|
||||||
|
type RawMetadataMatch,
|
||||||
|
type RawPaged,
|
||||||
|
type RawTrack,
|
||||||
|
} from '../mappers';
|
||||||
import type {
|
import type {
|
||||||
Track,
|
Track,
|
||||||
Album,
|
Album,
|
||||||
Artist,
|
Artist,
|
||||||
|
MetadataEdit,
|
||||||
|
MetadataMatch,
|
||||||
PaginatedResponse,
|
PaginatedResponse,
|
||||||
LibraryFilters,
|
LibraryFilters,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
|
// The backend sorts on a small allow-list; map the UI's sort keys onto it
|
||||||
|
// (album/year aren't sortable server-side yet → fall back to recency).
|
||||||
|
const SORT_BY: Record<NonNullable<LibraryFilters['sortBy']>, string> = {
|
||||||
|
title: 'title',
|
||||||
|
artist: 'artist',
|
||||||
|
album: 'created_at',
|
||||||
|
year: 'created_at',
|
||||||
|
dateAdded: 'created_at',
|
||||||
|
};
|
||||||
|
|
||||||
|
/** UI page/pageSize → backend limit/offset. */
|
||||||
|
function paging(page?: number, pageSize?: number) {
|
||||||
|
const size = pageSize ?? 50;
|
||||||
|
return { limit: size, offset: ((page ?? 1) - 1) * size };
|
||||||
|
}
|
||||||
|
|
||||||
|
function trackParams(f: LibraryFilters) {
|
||||||
|
return {
|
||||||
|
q: f.search,
|
||||||
|
artist_id: f.artistId,
|
||||||
|
album_id: f.albumId,
|
||||||
|
sort_by: f.sortBy ? SORT_BY[f.sortBy] : undefined,
|
||||||
|
order: f.sortOrder,
|
||||||
|
...paging(f.page, f.pageSize),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export const libraryApi = api.injectEndpoints({
|
export const libraryApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
getTracks: build.query<PaginatedResponse<Track>, LibraryFilters | void>({
|
getTracks: build.query<PaginatedResponse<Track>, LibraryFilters | void>({
|
||||||
query: (filters) => ({ url: '/library/tracks', params: filters ?? {} }),
|
query: (filters) => ({
|
||||||
|
url: '/tracks',
|
||||||
|
params: trackParams(filters ?? {}),
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawPaged<RawTrack>) => toPage(raw, toTrack),
|
||||||
providesTags: (result) =>
|
providesTags: (result) =>
|
||||||
result
|
result
|
||||||
? [
|
? [
|
||||||
@@ -20,7 +65,8 @@ export const libraryApi = api.injectEndpoints({
|
|||||||
: ['Track'],
|
: ['Track'],
|
||||||
}),
|
}),
|
||||||
getTrack: build.query<Track, string>({
|
getTrack: build.query<Track, string>({
|
||||||
query: (id) => `/library/tracks/${id}`,
|
query: (id) => `/tracks/${id}`,
|
||||||
|
transformResponse: (raw: RawTrack) => toTrack(raw),
|
||||||
providesTags: (_r, _e, id) => [{ type: 'Track', id }],
|
providesTags: (_r, _e, id) => [{ type: 'Track', id }],
|
||||||
}),
|
}),
|
||||||
getAlbums: build.query<
|
getAlbums: build.query<
|
||||||
@@ -32,7 +78,15 @@ export const libraryApi = api.injectEndpoints({
|
|||||||
pageSize?: number;
|
pageSize?: number;
|
||||||
} | void
|
} | void
|
||||||
>({
|
>({
|
||||||
query: (params) => ({ url: '/library/albums', params: params ?? {} }),
|
query: (p) => ({
|
||||||
|
url: '/albums',
|
||||||
|
params: {
|
||||||
|
q: p?.search,
|
||||||
|
artist_id: p?.artistId,
|
||||||
|
...paging(p?.page, p?.pageSize),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawPaged<RawAlbum>) => toPage(raw, toAlbum),
|
||||||
providesTags: (result) =>
|
providesTags: (result) =>
|
||||||
result
|
result
|
||||||
? [
|
? [
|
||||||
@@ -42,11 +96,13 @@ export const libraryApi = api.injectEndpoints({
|
|||||||
: ['Album'],
|
: ['Album'],
|
||||||
}),
|
}),
|
||||||
getAlbum: build.query<Album, string>({
|
getAlbum: build.query<Album, string>({
|
||||||
query: (id) => `/library/albums/${id}`,
|
query: (id) => `/albums/${id}`,
|
||||||
|
transformResponse: (raw: RawAlbum) => toAlbum(raw),
|
||||||
providesTags: (_r, _e, id) => [{ type: 'Album', id }],
|
providesTags: (_r, _e, id) => [{ type: 'Album', id }],
|
||||||
}),
|
}),
|
||||||
getAlbumTracks: build.query<Track[], string>({
|
getAlbumTracks: build.query<Track[], string>({
|
||||||
query: (albumId) => `/library/albums/${albumId}/tracks`,
|
query: (albumId) => `/albums/${albumId}/tracks`,
|
||||||
|
transformResponse: (raw: RawPaged<RawTrack>) => raw.items.map(toTrack),
|
||||||
providesTags: (_r, _e, albumId) => [
|
providesTags: (_r, _e, albumId) => [
|
||||||
{ type: 'Album', id: albumId },
|
{ type: 'Album', id: albumId },
|
||||||
'Track',
|
'Track',
|
||||||
@@ -56,7 +112,11 @@ export const libraryApi = api.injectEndpoints({
|
|||||||
PaginatedResponse<Artist>,
|
PaginatedResponse<Artist>,
|
||||||
{ search?: string; page?: number; pageSize?: number } | void
|
{ search?: string; page?: number; pageSize?: number } | void
|
||||||
>({
|
>({
|
||||||
query: (params) => ({ url: '/library/artists', params: params ?? {} }),
|
query: (p) => ({
|
||||||
|
url: '/artists',
|
||||||
|
params: { q: p?.search, ...paging(p?.page, p?.pageSize) },
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawPaged<RawArtist>) => toPage(raw, toArtist),
|
||||||
providesTags: (result) =>
|
providesTags: (result) =>
|
||||||
result
|
result
|
||||||
? [
|
? [
|
||||||
@@ -69,23 +129,77 @@ export const libraryApi = api.injectEndpoints({
|
|||||||
: ['Artist'],
|
: ['Artist'],
|
||||||
}),
|
}),
|
||||||
getArtist: build.query<Artist, string>({
|
getArtist: build.query<Artist, string>({
|
||||||
query: (id) => `/library/artists/${id}`,
|
query: (id) => `/artists/${id}`,
|
||||||
|
transformResponse: (raw: RawArtist) => toArtist(raw),
|
||||||
providesTags: (_r, _e, id) => [{ type: 'Artist', id }],
|
providesTags: (_r, _e, id) => [{ type: 'Artist', id }],
|
||||||
}),
|
}),
|
||||||
getArtistAlbums: build.query<Album[], string>({
|
getArtistAlbums: build.query<Album[], string>({
|
||||||
query: (artistId) => `/library/artists/${artistId}/albums`,
|
query: (artistId) => `/artists/${artistId}/albums`,
|
||||||
|
transformResponse: (raw: RawPaged<RawAlbum>) => raw.items.map(toAlbum),
|
||||||
providesTags: (_r, _e, artistId) => [
|
providesTags: (_r, _e, artistId) => [
|
||||||
{ type: 'Artist', id: artistId },
|
{ type: 'Artist', id: artistId },
|
||||||
'Album',
|
'Album',
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
getArtistTracks: build.query<Track[], string>({
|
||||||
|
query: (artistId) => `/artists/${artistId}/tracks`,
|
||||||
|
transformResponse: (raw: RawPaged<RawTrack>) => raw.items.map(toTrack),
|
||||||
|
providesTags: (_r, _e, artistId) => [
|
||||||
|
{ type: 'Artist', id: artistId },
|
||||||
|
'Track',
|
||||||
|
],
|
||||||
|
}),
|
||||||
searchLibrary: build.query<
|
searchLibrary: build.query<
|
||||||
{ tracks: Track[]; albums: Album[]; artists: Artist[] },
|
{ tracks: Track[]; albums: Album[]; artists: Artist[] },
|
||||||
string
|
string
|
||||||
>({
|
>({
|
||||||
query: (q) => ({ url: '/library/search', params: { q } }),
|
query: (q) => ({ url: '/search/library', params: { q } }),
|
||||||
|
transformResponse: (raw: {
|
||||||
|
tracks: RawTrack[];
|
||||||
|
albums: RawAlbum[];
|
||||||
|
artists: RawArtist[];
|
||||||
|
}) => ({
|
||||||
|
tracks: raw.tracks.map(toTrack),
|
||||||
|
albums: raw.albums.map(toAlbum),
|
||||||
|
artists: raw.artists.map(toArtist),
|
||||||
|
}),
|
||||||
providesTags: ['Track', 'Album', 'Artist'],
|
providesTags: ['Track', 'Album', 'Artist'],
|
||||||
}),
|
}),
|
||||||
|
getMetadataMatches: build.query<MetadataMatch[], string>({
|
||||||
|
query: (trackId) => `/tracks/${trackId}/metadata/matches`,
|
||||||
|
transformResponse: (raw: { items: RawMetadataMatch[] }) =>
|
||||||
|
raw.items.map(toMetadataMatch),
|
||||||
|
}),
|
||||||
|
applyMetadata: build.mutation<
|
||||||
|
Track,
|
||||||
|
{ trackId: string; edit: MetadataEdit }
|
||||||
|
>({
|
||||||
|
query: ({ trackId, edit }) => ({
|
||||||
|
url: `/tracks/${trackId}/metadata`,
|
||||||
|
method: 'PUT',
|
||||||
|
body: {
|
||||||
|
title: edit.title,
|
||||||
|
artist_name: edit.artistName,
|
||||||
|
album_title: edit.albumTitle,
|
||||||
|
year: edit.year,
|
||||||
|
genre: edit.genre,
|
||||||
|
track_number: edit.trackNumber,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawTrack) => toTrack(raw),
|
||||||
|
invalidatesTags: (_r, _e, { trackId }) => [
|
||||||
|
{ type: 'Track', id: trackId },
|
||||||
|
'Album',
|
||||||
|
'Artist',
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
enrichTrack: build.mutation<{ track_id: string; job_id: string }, string>({
|
||||||
|
query: (trackId) => ({
|
||||||
|
url: `/tracks/${trackId}/metadata/enrich`,
|
||||||
|
method: 'POST',
|
||||||
|
}),
|
||||||
|
invalidatesTags: (_r, _e, trackId) => [{ type: 'Track', id: trackId }],
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
overrideExisting: false,
|
overrideExisting: false,
|
||||||
});
|
});
|
||||||
@@ -99,5 +213,9 @@ export const {
|
|||||||
useGetArtistsQuery,
|
useGetArtistsQuery,
|
||||||
useGetArtistQuery,
|
useGetArtistQuery,
|
||||||
useGetArtistAlbumsQuery,
|
useGetArtistAlbumsQuery,
|
||||||
|
useGetArtistTracksQuery,
|
||||||
useSearchLibraryQuery,
|
useSearchLibraryQuery,
|
||||||
|
useLazyGetMetadataMatchesQuery,
|
||||||
|
useApplyMetadataMutation,
|
||||||
|
useEnrichTrackMutation,
|
||||||
} = libraryApi;
|
} = libraryApi;
|
||||||
|
|||||||
+58
-10
@@ -1,20 +1,68 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Likes are an append-only event-log on the backend: state changes by POSTing a
|
||||||
|
* new `{track_id, value}` event, never by PUT/DELETE on a boolean. "Unlike" is
|
||||||
|
* just a `neutral` event superseding the prior `like`.
|
||||||
|
*/
|
||||||
|
type LikeValue = 'like' | 'dislike' | 'neutral';
|
||||||
|
|
||||||
|
interface RawLikeState {
|
||||||
|
track_id: string;
|
||||||
|
value: LikeValue;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LikeState {
|
||||||
|
trackId: string;
|
||||||
|
value: LikeValue;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const toLikeState = (r: RawLikeState): LikeState => ({
|
||||||
|
trackId: r.track_id,
|
||||||
|
value: r.value,
|
||||||
|
updatedAt: r.updated_at,
|
||||||
|
});
|
||||||
|
|
||||||
export const likesApi = api.injectEndpoints({
|
export const likesApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
likeTrack: build.mutation<void, string>({
|
setLike: build.mutation<LikeState, { trackId: string; value: LikeValue }>({
|
||||||
query: (trackId) => ({ url: `/likes/tracks/${trackId}`, method: 'PUT' }),
|
query: ({ trackId, value }) => ({
|
||||||
invalidatesTags: (_r, _e, id) => ['Like', { type: 'Track', id }],
|
url: '/likes',
|
||||||
}),
|
method: 'POST',
|
||||||
unlikeTrack: build.mutation<void, string>({
|
body: { track_id: trackId, value },
|
||||||
query: (trackId) => ({
|
|
||||||
url: `/likes/tracks/${trackId}`,
|
|
||||||
method: 'DELETE',
|
|
||||||
}),
|
}),
|
||||||
invalidatesTags: (_r, _e, id) => ['Like', { type: 'Track', id }],
|
transformResponse: (raw: RawLikeState) => toLikeState(raw),
|
||||||
|
invalidatesTags: (_r, _e, { trackId }) => [
|
||||||
|
'Like',
|
||||||
|
{ type: 'Track', id: trackId },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
// Latest like state for a set of tracks (drives like buttons in lists).
|
||||||
|
getLikesState: build.query<LikeState[], string[]>({
|
||||||
|
query: (trackIds) => ({
|
||||||
|
url: '/likes/state',
|
||||||
|
params: { track_ids: trackIds.join(',') },
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawLikeState[]) => raw.map(toLikeState),
|
||||||
|
providesTags: ['Like'],
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
overrideExisting: false,
|
overrideExisting: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const { useLikeTrackMutation, useUnlikeTrackMutation } = likesApi;
|
const { useSetLikeMutation, useGetLikesStateQuery } = likesApi;
|
||||||
|
|
||||||
|
/** Convenience hook preserving the like/unlike call sites. */
|
||||||
|
export function useLikeActions() {
|
||||||
|
const [setLike, state] = useSetLikeMutation();
|
||||||
|
return {
|
||||||
|
like: (trackId: string) => setLike({ trackId, value: 'like' }),
|
||||||
|
unlike: (trackId: string) => setLike({ trackId, value: 'neutral' }),
|
||||||
|
dislike: (trackId: string) => setLike({ trackId, value: 'dislike' }),
|
||||||
|
state,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export { useSetLikeMutation, useGetLikesStateQuery };
|
||||||
|
|||||||
@@ -1,36 +1,61 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
|
import {
|
||||||
|
toPage,
|
||||||
|
toPlaylist,
|
||||||
|
toTrack,
|
||||||
|
type RawPaged,
|
||||||
|
type RawPlaylist,
|
||||||
|
type RawTrack,
|
||||||
|
} from '../mappers';
|
||||||
import type { Playlist, PlaylistTrack, PaginatedResponse } from '../types';
|
import type { Playlist, PlaylistTrack, PaginatedResponse } from '../types';
|
||||||
|
|
||||||
export const playlistsApi = api.injectEndpoints({
|
export const playlistsApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
getPlaylists: build.query<PaginatedResponse<Playlist>, void>({
|
getPlaylists: build.query<PaginatedResponse<Playlist>, void>({
|
||||||
query: () => '/playlists',
|
query: () => '/playlists',
|
||||||
|
transformResponse: (raw: RawPaged<RawPlaylist>) =>
|
||||||
|
toPage(raw, toPlaylist),
|
||||||
providesTags: ['Playlist'],
|
providesTags: ['Playlist'],
|
||||||
}),
|
}),
|
||||||
getPlaylist: build.query<Playlist, string>({
|
getPlaylist: build.query<Playlist, string>({
|
||||||
query: (id) => `/playlists/${id}`,
|
query: (id) => `/playlists/${id}`,
|
||||||
|
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||||
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }],
|
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }],
|
||||||
}),
|
}),
|
||||||
getPlaylistTracks: build.query<PlaylistTrack[], string>({
|
getPlaylistTracks: build.query<PlaylistTrack[], string>({
|
||||||
query: (id) => `/playlists/${id}/tracks`,
|
query: (id) => `/playlists/${id}/tracks`,
|
||||||
|
// The backend returns plain tracks in playlist order; position/addedAt
|
||||||
|
// aren't on TrackOut, so derive position from order and default addedAt.
|
||||||
|
transformResponse: (raw: RawPaged<RawTrack>) =>
|
||||||
|
raw.items.map((r, i) => ({
|
||||||
|
...toTrack(r),
|
||||||
|
position: i,
|
||||||
|
addedAt: r.created_at,
|
||||||
|
})),
|
||||||
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }, 'Track'],
|
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }, 'Track'],
|
||||||
}),
|
}),
|
||||||
createPlaylist: build.mutation<
|
createPlaylist: build.mutation<
|
||||||
Playlist,
|
Playlist,
|
||||||
{ name: string; description?: string; isPublic?: boolean }
|
{ name: string; description?: string }
|
||||||
>({
|
>({
|
||||||
query: (body) => ({ url: '/playlists', method: 'POST', body }),
|
query: ({ name, description }) => ({
|
||||||
|
url: '/playlists',
|
||||||
|
method: 'POST',
|
||||||
|
body: { name, description },
|
||||||
|
}),
|
||||||
|
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||||
invalidatesTags: ['Playlist'],
|
invalidatesTags: ['Playlist'],
|
||||||
}),
|
}),
|
||||||
updatePlaylist: build.mutation<
|
updatePlaylist: build.mutation<
|
||||||
Playlist,
|
Playlist,
|
||||||
{ id: string; name?: string; description?: string; isPublic?: boolean }
|
{ id: string; name?: string; description?: string }
|
||||||
>({
|
>({
|
||||||
query: ({ id, ...body }) => ({
|
query: ({ id, name, description }) => ({
|
||||||
url: `/playlists/${id}`,
|
url: `/playlists/${id}`,
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
body,
|
body: { name, description },
|
||||||
}),
|
}),
|
||||||
|
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||||
invalidatesTags: (_r, _e, { id }) => [{ type: 'Playlist', id }],
|
invalidatesTags: (_r, _e, { id }) => [{ type: 'Playlist', id }],
|
||||||
}),
|
}),
|
||||||
deletePlaylist: build.mutation<void, string>({
|
deletePlaylist: build.mutation<void, string>({
|
||||||
@@ -39,12 +64,12 @@ export const playlistsApi = api.injectEndpoints({
|
|||||||
}),
|
}),
|
||||||
addTrackToPlaylist: build.mutation<
|
addTrackToPlaylist: build.mutation<
|
||||||
void,
|
void,
|
||||||
{ playlistId: string; trackId: string }
|
{ playlistId: string; trackId: string; position?: number }
|
||||||
>({
|
>({
|
||||||
query: ({ playlistId, trackId }) => ({
|
query: ({ playlistId, trackId, position }) => ({
|
||||||
url: `/playlists/${playlistId}/tracks`,
|
url: `/playlists/${playlistId}/tracks`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { trackId },
|
body: { track_id: trackId, position },
|
||||||
}),
|
}),
|
||||||
invalidatesTags: (_r, _e, { playlistId }) => [
|
invalidatesTags: (_r, _e, { playlistId }) => [
|
||||||
{ type: 'Playlist', id: playlistId },
|
{ type: 'Playlist', id: playlistId },
|
||||||
@@ -52,10 +77,10 @@ export const playlistsApi = api.injectEndpoints({
|
|||||||
}),
|
}),
|
||||||
removeTrackFromPlaylist: build.mutation<
|
removeTrackFromPlaylist: build.mutation<
|
||||||
void,
|
void,
|
||||||
{ playlistId: string; trackId: string; position: number }
|
{ playlistId: string; trackId: string }
|
||||||
>({
|
>({
|
||||||
query: ({ playlistId, position }) => ({
|
query: ({ playlistId, trackId }) => ({
|
||||||
url: `/playlists/${playlistId}/tracks/${position}`,
|
url: `/playlists/${playlistId}/tracks/${trackId}`,
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
}),
|
}),
|
||||||
invalidatesTags: (_r, _e, { playlistId }) => [
|
invalidatesTags: (_r, _e, { playlistId }) => [
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import { api } from '../index';
|
import { api } from '../index';
|
||||||
import type { StorageStats } from '../types';
|
import type { StorageStats } from '../types';
|
||||||
|
|
||||||
|
// NOTE: the backend `/storage` routes are still unimplemented stubs (no body /
|
||||||
|
// no schema), and the real paths differ from these placeholders (`GET /storage`,
|
||||||
|
// `/storage/duplicates`, `/storage/broken`, `/storage/missing-metadata`,
|
||||||
|
// `POST /storage/cleanup`). Re-point paths and add snake→camel mappers (see
|
||||||
|
// `mappers.ts`) once the backend defines the storage response shapes; until then
|
||||||
|
// these are provisional and unused by the UI.
|
||||||
|
|
||||||
export const storageApi = api.injectEndpoints({
|
export const storageApi = api.injectEndpoints({
|
||||||
endpoints: (build) => ({
|
endpoints: (build) => ({
|
||||||
getStorageStats: build.query<StorageStats, void>({
|
getStorageStats: build.query<StorageStats, void>({
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
import { getApiBaseUrl } from '../../config/runtime-config';
|
import { getApiBaseUrl } from '../../config/runtime-config';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Audio stream URL for the `<audio>` element. The access token rides as a query
|
||||||
|
* param because `<audio>` can't send an `Authorization` header; the backend
|
||||||
|
* accepts `?token=` on `GET /stream/{id}` for exactly this reason.
|
||||||
|
*/
|
||||||
export function getStreamUrl(trackId: string, token: string): string {
|
export function getStreamUrl(trackId: string, token: string): string {
|
||||||
const base = getApiBaseUrl();
|
const base = getApiBaseUrl();
|
||||||
return `${base}/streaming/tracks/${trackId}?token=${encodeURIComponent(token)}`;
|
return `${base}/stream/${trackId}?token=${encodeURIComponent(token)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCoverUrl(artUrl: string | undefined): string | undefined {
|
export function getCoverUrl(artUrl: string | undefined): string | undefined {
|
||||||
@@ -12,3 +17,18 @@ export function getCoverUrl(artUrl: string | undefined): string | undefined {
|
|||||||
const base = getApiBaseUrl();
|
const base = getApiBaseUrl();
|
||||||
return `${base}${artUrl}`;
|
return `${base}${artUrl}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cover image URL for a track, served by `GET /tracks/{id}/cover`. Like the
|
||||||
|
* audio stream, an `<img>` can't send an `Authorization` header, so the access
|
||||||
|
* token rides as `?token=`. Returns undefined when the track has no cover.
|
||||||
|
*/
|
||||||
|
export function getTrackCoverUrl(
|
||||||
|
trackId: string,
|
||||||
|
token: string,
|
||||||
|
hasCover: boolean,
|
||||||
|
): string | undefined {
|
||||||
|
if (!hasCover) return undefined;
|
||||||
|
const base = getApiBaseUrl();
|
||||||
|
return `${base}/tracks/${trackId}/cover?token=${encodeURIComponent(token)}`;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { createApi } from '@reduxjs/toolkit/query/react';
|
import { createApi } from '@reduxjs/toolkit/query/react';
|
||||||
import { baseQueryWithReauth } from './baseQuery';
|
import { baseQueryWithReauth } from './baseQuery';
|
||||||
|
import { REHYDRATE_API, type RehydrateApiPayload } from './rehydrate';
|
||||||
|
|
||||||
export const api = createApi({
|
export const api = createApi({
|
||||||
reducerPath: 'api',
|
reducerPath: 'api',
|
||||||
@@ -14,5 +15,16 @@ export const api = createApi({
|
|||||||
'User',
|
'User',
|
||||||
'Storage',
|
'Storage',
|
||||||
],
|
],
|
||||||
|
// Tier 2 offline: seed the cache from the persisted snapshot dispatched at
|
||||||
|
// startup (see `store/rtkqPersist.ts`). Returning the saved queries/mutations
|
||||||
|
// lets the last-seen library render before — or instead of — any network call.
|
||||||
|
extractRehydrationInfo(action) {
|
||||||
|
if (action.type === REHYDRATE_API) {
|
||||||
|
// The api reducer reads `queries`/`mutations` off this and restores any
|
||||||
|
// fulfilled entries; pending/rejected ones are ignored automatically.
|
||||||
|
return action.payload as RehydrateApiPayload as never;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
endpoints: () => ({}),
|
endpoints: () => ({}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
/**
|
||||||
|
* Backend-contract adapters: the single place where the backend's wire format
|
||||||
|
* (snake_case, lean `*Out` schemas, `{items,total,limit,offset}` paging) is
|
||||||
|
* translated into the UI's internal camelCase domain types from `types.ts`.
|
||||||
|
*
|
||||||
|
* The endpoint files (`endpoints/*.ts`) own *paths and params*; this module owns
|
||||||
|
* *shape*. Swapping or mocking a backend means rewriting the mappers here —
|
||||||
|
* nothing in components, slices, or `types.ts` changes.
|
||||||
|
*
|
||||||
|
* Where the backend's lean schema omits a field the UI type carries (cover art,
|
||||||
|
* liked state, durations on albums…), the mapper fills a safe client default and
|
||||||
|
* says why inline. Those defaults are the contract's current edges, not bugs.
|
||||||
|
*/
|
||||||
|
import type {
|
||||||
|
Album,
|
||||||
|
Artist,
|
||||||
|
MetadataMatch,
|
||||||
|
MetadataStatus,
|
||||||
|
PaginatedResponse,
|
||||||
|
Playlist,
|
||||||
|
Track,
|
||||||
|
User,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
|
const METADATA_STATUSES: readonly MetadataStatus[] = [
|
||||||
|
'pending',
|
||||||
|
'enriched',
|
||||||
|
'failed',
|
||||||
|
'manual',
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Map the backend's free-form status string onto the UI union, defaulting any
|
||||||
|
* unknown value to `pending` (a safe "not yet identified" state). */
|
||||||
|
const toMetadataStatus = (raw: string): MetadataStatus =>
|
||||||
|
(METADATA_STATUSES as readonly string[]).includes(raw)
|
||||||
|
? (raw as MetadataStatus)
|
||||||
|
: 'pending';
|
||||||
|
|
||||||
|
// ---- raw wire shapes (snake_case, exactly as the backend emits) ----
|
||||||
|
|
||||||
|
export interface RawPaged<T> {
|
||||||
|
items: T[];
|
||||||
|
total: number;
|
||||||
|
limit: number;
|
||||||
|
offset: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RawUser {
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
is_superuser: boolean;
|
||||||
|
is_active: boolean;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RawTrack {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
artist_id: string;
|
||||||
|
artist_name: string;
|
||||||
|
album_id: string | null;
|
||||||
|
album_title: string | null;
|
||||||
|
duration_seconds: number | null;
|
||||||
|
file_format: string;
|
||||||
|
file_size: number;
|
||||||
|
genre: string | null;
|
||||||
|
year: number | null;
|
||||||
|
track_number: number | null;
|
||||||
|
metadata_status: string;
|
||||||
|
metadata_error: string | null;
|
||||||
|
enriched_at: string | null;
|
||||||
|
has_cover: boolean;
|
||||||
|
source: string;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One AcoustID candidate, as returned by `GET /tracks/{id}/metadata/matches`. */
|
||||||
|
export interface RawMetadataMatch {
|
||||||
|
acoustid: string;
|
||||||
|
score: number;
|
||||||
|
recording_mbid: string | null;
|
||||||
|
release_group_mbid: string | null;
|
||||||
|
title: string | null;
|
||||||
|
artist: string | null;
|
||||||
|
album: string | null;
|
||||||
|
year: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RawAlbum {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
artist_id: string;
|
||||||
|
artist_name: string;
|
||||||
|
year: number | null;
|
||||||
|
track_count: number;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RawArtist {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
album_count: number;
|
||||||
|
track_count: number;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RawPlaylist {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
description: string | null;
|
||||||
|
owner_id: string;
|
||||||
|
version: number;
|
||||||
|
track_count: number;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- mappers ----
|
||||||
|
|
||||||
|
export const toUser = (r: RawUser): User => ({
|
||||||
|
id: r.id,
|
||||||
|
username: r.username,
|
||||||
|
// MVP role model: superuser → admin, everyone else → user.
|
||||||
|
role: r.is_superuser ? 'admin' : 'user',
|
||||||
|
createdAt: r.created_at,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const toTrack = (r: RawTrack): Track => ({
|
||||||
|
id: r.id,
|
||||||
|
title: r.title,
|
||||||
|
artistId: r.artist_id,
|
||||||
|
artistName: r.artist_name,
|
||||||
|
albumId: r.album_id ?? '',
|
||||||
|
albumTitle: r.album_title ?? '',
|
||||||
|
// `has_cover` says a cover exists; the actual URL (which needs a `?token=`) is
|
||||||
|
// built in the component from the track id — see `getTrackCoverUrl`. Keep
|
||||||
|
// `albumArtUrl` undefined so callers fall back to generated tile art.
|
||||||
|
albumArtUrl: undefined,
|
||||||
|
hasCover: r.has_cover,
|
||||||
|
durationMs: (r.duration_seconds ?? 0) * 1000,
|
||||||
|
// The lean TrackOut carries no availability/like state: a track returned by
|
||||||
|
// the library is on the server, and per-track like state comes from /likes.
|
||||||
|
availability: 'server',
|
||||||
|
metadataStatus: toMetadataStatus(r.metadata_status),
|
||||||
|
metadataError: r.metadata_error ?? undefined,
|
||||||
|
genre: r.genre ?? undefined,
|
||||||
|
year: r.year ?? undefined,
|
||||||
|
trackNumber: r.track_number ?? undefined,
|
||||||
|
liked: false,
|
||||||
|
format: r.file_format,
|
||||||
|
fileSize: r.file_size,
|
||||||
|
source: r.source,
|
||||||
|
createdAt: r.created_at,
|
||||||
|
enrichedAt: r.enriched_at ?? undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const toMetadataMatch = (r: RawMetadataMatch): MetadataMatch => ({
|
||||||
|
acoustid: r.acoustid,
|
||||||
|
score: r.score,
|
||||||
|
recordingMbid: r.recording_mbid ?? undefined,
|
||||||
|
releaseGroupMbid: r.release_group_mbid ?? undefined,
|
||||||
|
title: r.title ?? undefined,
|
||||||
|
artist: r.artist ?? undefined,
|
||||||
|
album: r.album ?? undefined,
|
||||||
|
year: r.year ?? undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const toAlbum = (r: RawAlbum): Album => ({
|
||||||
|
id: r.id,
|
||||||
|
title: r.title,
|
||||||
|
artistId: r.artist_id,
|
||||||
|
artistName: r.artist_name,
|
||||||
|
artUrl: undefined,
|
||||||
|
year: r.year ?? undefined,
|
||||||
|
trackCount: r.track_count,
|
||||||
|
// AlbumOut has no aggregate duration; computed client-side from tracks when
|
||||||
|
// an album is opened.
|
||||||
|
totalDurationMs: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const toArtist = (r: RawArtist): Artist => ({
|
||||||
|
id: r.id,
|
||||||
|
name: r.name,
|
||||||
|
artUrl: undefined,
|
||||||
|
albumCount: r.album_count,
|
||||||
|
trackCount: r.track_count,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const toPlaylist = (r: RawPlaylist): Playlist => ({
|
||||||
|
id: r.id,
|
||||||
|
name: r.name,
|
||||||
|
description: r.description ?? undefined,
|
||||||
|
ownerId: r.owner_id,
|
||||||
|
trackCount: r.track_count,
|
||||||
|
totalDurationMs: 0,
|
||||||
|
// No visibility concept on the backend yet — default private.
|
||||||
|
isPublic: false,
|
||||||
|
createdAt: r.created_at,
|
||||||
|
// PlaylistOut omits updated_at; mirror created_at until it's added.
|
||||||
|
updatedAt: r.created_at,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate the backend's `{items,total,limit,offset}` envelope into the UI's
|
||||||
|
* `{items,total,page,pageSize,hasMore}`, mapping each element.
|
||||||
|
*/
|
||||||
|
export const toPage = <R, T>(
|
||||||
|
raw: RawPaged<R>,
|
||||||
|
map: (r: R) => T,
|
||||||
|
): PaginatedResponse<T> => {
|
||||||
|
const pageSize = raw.limit || raw.items.length || 1;
|
||||||
|
return {
|
||||||
|
items: raw.items.map(map),
|
||||||
|
total: raw.total,
|
||||||
|
page: Math.floor(raw.offset / pageSize) + 1,
|
||||||
|
pageSize,
|
||||||
|
hasMore: raw.offset + raw.items.length < raw.total,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { createAction } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tier 2 offline support contract. RTK Query can seed its cache from a
|
||||||
|
* persisted snapshot via `extractRehydrationInfo` (see `api/index.ts`). We use
|
||||||
|
* a single action whose payload is the previously-saved api slice state; the
|
||||||
|
* api reducer pulls `queries`/`mutations` out of it on startup so last-seen
|
||||||
|
* library data renders read-only while the backend is unreachable.
|
||||||
|
*
|
||||||
|
* The type string lives here (not in the store) so `api/index.ts` can match it
|
||||||
|
* without importing from the store layer (which would create a cycle).
|
||||||
|
*/
|
||||||
|
export const REHYDRATE_API = 'api/rehydrate';
|
||||||
|
|
||||||
|
export interface RehydrateApiPayload {
|
||||||
|
queries: Record<string, unknown>;
|
||||||
|
mutations: Record<string, unknown>;
|
||||||
|
// RTKQ's invalidation slice reads `provided.tags`/`provided.keys` during
|
||||||
|
// rehydration (it does `Object.entries(provided.tags ?? {})`), so `provided`
|
||||||
|
// must be an object — a bare `{ queries, mutations }` makes it crash on
|
||||||
|
// `provided.tags` of undefined. Always present; empty objects are valid.
|
||||||
|
provided: { tags: Record<string, unknown>; keys: Record<string, unknown> };
|
||||||
|
}
|
||||||
|
|
||||||
|
export const rehydrateApi = createAction<RehydrateApiPayload>(REHYDRATE_API);
|
||||||
+49
-1
@@ -1,5 +1,13 @@
|
|||||||
export type TrackAvailability = 'server' | 'downloading' | 'error' | 'missing';
|
export type TrackAvailability = 'server' | 'downloading' | 'error' | 'missing';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metadata-enrichment state, distinct from file `availability`. `pending` = the
|
||||||
|
* worker hasn't finished (or hasn't started); `enriched` = identity found;
|
||||||
|
* `failed` = no match / a worker error (see `metadataError`); `manual` = user-
|
||||||
|
* edited and never auto-overwritten.
|
||||||
|
*/
|
||||||
|
export type MetadataStatus = 'pending' | 'enriched' | 'failed' | 'manual';
|
||||||
|
|
||||||
export interface Track {
|
export interface Track {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
@@ -8,16 +16,26 @@ export interface Track {
|
|||||||
albumId: string;
|
albumId: string;
|
||||||
albumTitle: string;
|
albumTitle: string;
|
||||||
albumArtUrl?: string;
|
albumArtUrl?: string;
|
||||||
|
hasCover: boolean;
|
||||||
durationMs: number;
|
durationMs: number;
|
||||||
trackNumber?: number;
|
trackNumber?: number;
|
||||||
discNumber?: number;
|
discNumber?: number;
|
||||||
year?: number;
|
year?: number;
|
||||||
genre?: string;
|
genre?: string;
|
||||||
availability: TrackAvailability;
|
availability: TrackAvailability;
|
||||||
|
metadataStatus: MetadataStatus;
|
||||||
|
/** Human-readable reason the last enrichment run set `failed`; else undefined. */
|
||||||
|
metadataError?: string;
|
||||||
fileSize?: number;
|
fileSize?: number;
|
||||||
format?: string;
|
format?: string;
|
||||||
bitrate?: number;
|
bitrate?: number;
|
||||||
liked: boolean;
|
liked: boolean;
|
||||||
|
/** Where the track entered the library (e.g. `upload`, `local_folder`). */
|
||||||
|
source?: string;
|
||||||
|
/** ISO timestamp the track was added to the library. */
|
||||||
|
createdAt?: string;
|
||||||
|
/** ISO timestamp the last successful enrichment ran; undefined if never. */
|
||||||
|
enrichedAt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Album {
|
export interface Album {
|
||||||
@@ -98,7 +116,9 @@ export interface User {
|
|||||||
export interface AuthTokens {
|
export interface AuthTokens {
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
expiresIn: number;
|
// Optional: the backend's TokenResponse carries no TTL — expiry is driven by
|
||||||
|
// 401→refresh, not a client-side clock. Present only if a backend supplies it.
|
||||||
|
expiresIn?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LoginRequest {
|
export interface LoginRequest {
|
||||||
@@ -111,6 +131,11 @@ export interface LoginResponse {
|
|||||||
tokens: AuthTokens;
|
tokens: AuthTokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RegisterRequest {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface PaginatedResponse<T> {
|
export interface PaginatedResponse<T> {
|
||||||
items: T[];
|
items: T[];
|
||||||
total: number;
|
total: number;
|
||||||
@@ -136,3 +161,26 @@ export interface ApiError {
|
|||||||
message: string;
|
message: string;
|
||||||
code?: string;
|
code?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** One AcoustID candidate from `GET /tracks/{id}/metadata/matches` (§A7). */
|
||||||
|
export interface MetadataMatch {
|
||||||
|
acoustid: string;
|
||||||
|
/** Confidence 0..1. */
|
||||||
|
score: number;
|
||||||
|
recordingMbid?: string;
|
||||||
|
releaseGroupMbid?: string;
|
||||||
|
title?: string;
|
||||||
|
artist?: string;
|
||||||
|
album?: string;
|
||||||
|
year?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Manual edits / an accepted match, sent to `PUT /tracks/{id}/metadata`. */
|
||||||
|
export interface MetadataEdit {
|
||||||
|
title?: string;
|
||||||
|
artistName?: string;
|
||||||
|
albumTitle?: string;
|
||||||
|
year?: number;
|
||||||
|
genre?: string;
|
||||||
|
trackNumber?: number;
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,9 +22,15 @@ export function ConnectionStatus() {
|
|||||||
const status = useConnectionStatus();
|
const status = useConnectionStatus();
|
||||||
const baseUrl = getApiBaseUrl();
|
const baseUrl = getApiBaseUrl();
|
||||||
const label = t(STATUS_KEY[status]);
|
const label = t(STATUS_KEY[status]);
|
||||||
|
// When the backend is unreachable the UI falls back to the persisted RTKQ
|
||||||
|
// cache (Tier 2), so flag that the data on screen is last-seen, not live.
|
||||||
|
const offline = status === 'disconnected' || status === 'error';
|
||||||
|
const tip = offline
|
||||||
|
? `${label} · ${baseUrl} · ${t('conn.cached')}`
|
||||||
|
: `${label} · ${baseUrl}`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip content={`${label} · ${baseUrl}`}>
|
<Tooltip content={tip}>
|
||||||
<Badge variant={STATUS_VARIANTS[status]} dot>
|
<Badge variant={STATUS_VARIANTS[status]} dot>
|
||||||
{label}
|
{label}
|
||||||
</Badge>
|
</Badge>
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
GearSix,
|
GearSix,
|
||||||
HardDrives,
|
HardDrives,
|
||||||
Heart,
|
Heart,
|
||||||
|
Info,
|
||||||
MagnifyingGlass,
|
MagnifyingGlass,
|
||||||
Pause,
|
Pause,
|
||||||
Play,
|
Play,
|
||||||
@@ -69,6 +70,7 @@ const ICONS = {
|
|||||||
'skip-forward': SkipForward,
|
'skip-forward': SkipForward,
|
||||||
repeat: Repeat,
|
repeat: Repeat,
|
||||||
heart: Heart,
|
heart: Heart,
|
||||||
|
info: Info,
|
||||||
'thumbs-down': ThumbsDown,
|
'thumbs-down': ThumbsDown,
|
||||||
'speaker-high': SpeakerHigh,
|
'speaker-high': SpeakerHigh,
|
||||||
'speaker-x': SpeakerSimpleX,
|
'speaker-x': SpeakerSimpleX,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Suspense } from 'react';
|
|||||||
import { Sidebar } from './Sidebar';
|
import { Sidebar } from './Sidebar';
|
||||||
import { PersistentPlayer } from '../player/PersistentPlayer';
|
import { PersistentPlayer } from '../player/PersistentPlayer';
|
||||||
import { QueuePanel } from '../player/QueuePanel';
|
import { QueuePanel } from '../player/QueuePanel';
|
||||||
|
import { TrackInfoDrawer } from '../track/TrackInfoDrawer';
|
||||||
import { LoadingSkeleton } from '../common/LoadingSkeleton';
|
import { LoadingSkeleton } from '../common/LoadingSkeleton';
|
||||||
|
|
||||||
export function AppShell() {
|
export function AppShell() {
|
||||||
@@ -31,6 +32,7 @@ export function AppShell() {
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<QueuePanel />
|
<QueuePanel />
|
||||||
|
<TrackInfoDrawer />
|
||||||
</div>
|
</div>
|
||||||
<PersistentPlayer />
|
<PersistentPlayer />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -10,12 +10,14 @@ import {
|
|||||||
setVolume,
|
setVolume,
|
||||||
toggleShuffle,
|
toggleShuffle,
|
||||||
setRepeat,
|
setRepeat,
|
||||||
toggleNowPlaying,
|
|
||||||
toggleQueue,
|
toggleQueue,
|
||||||
} from '../../store/slices/player';
|
} from '../../store/slices/player';
|
||||||
|
import { openTrackInfo } from '../../store/slices/ui';
|
||||||
import { useAudioPlayer } from '../../hooks/useAudioPlayer';
|
import { useAudioPlayer } from '../../hooks/useAudioPlayer';
|
||||||
|
import { useStreamCached } from '../../hooks/useStreamCached';
|
||||||
|
import { useResolvedQueueEntry } from '../../hooks/useResolvedQueueEntry';
|
||||||
import { formatDuration } from '../../lib/format';
|
import { formatDuration } from '../../lib/format';
|
||||||
import { getCoverUrl } from '../../api/endpoints/streaming';
|
import { getCoverUrl, getTrackCoverUrl } from '../../api/endpoints/streaming';
|
||||||
|
|
||||||
export function PersistentPlayer() {
|
export function PersistentPlayer() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -23,27 +25,40 @@ export function PersistentPlayer() {
|
|||||||
const { seek, playNext, playPrev } = useAudioPlayer();
|
const { seek, playNext, playPrev } = useAudioPlayer();
|
||||||
const player = useAppSelector((s) => s.player);
|
const player = useAppSelector((s) => s.player);
|
||||||
const queue = useAppSelector((s) => s.queue);
|
const queue = useAppSelector((s) => s.queue);
|
||||||
|
const token = useAppSelector((s) => s.auth.accessToken);
|
||||||
const currentEntry = queue.entries[queue.currentIndex];
|
const currentEntry = queue.entries[queue.currentIndex];
|
||||||
|
// Read through to the live Track cache so enrichment updates reach the player,
|
||||||
|
// not just the play-time snapshot frozen in the queue slice.
|
||||||
|
const current = useResolvedQueueEntry(currentEntry);
|
||||||
|
// Source indicator: cached → playing locally, otherwise streaming.
|
||||||
|
const cached = useStreamCached(currentEntry?.trackId);
|
||||||
|
|
||||||
if (!currentEntry && !player.currentTrackId) {
|
if (!currentEntry && !player.currentTrackId) {
|
||||||
return <div className="player empty">{t('player.nothingPlaying')}</div>;
|
return <div className="player empty">{t('player.nothingPlaying')}</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const artUrl = getCoverUrl(currentEntry?.albumArtUrl);
|
const artUrl =
|
||||||
const seedLabel = currentEntry?.albumTitle ?? currentEntry?.title ?? '';
|
getCoverUrl(currentEntry?.albumArtUrl) ??
|
||||||
const onStream = true;
|
(token && current?.hasCover
|
||||||
|
? getTrackCoverUrl(current.trackId, token, true)
|
||||||
|
: undefined);
|
||||||
|
const seedLabel = current?.albumTitle ?? current?.title ?? '';
|
||||||
|
const onStream = !cached;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="player">
|
<div className="player">
|
||||||
<div
|
<div
|
||||||
className="pl-now"
|
className="pl-now"
|
||||||
onClick={() => dispatch(toggleNowPlaying())}
|
onClick={() =>
|
||||||
style={{ cursor: 'pointer' }}
|
currentEntry && dispatch(openTrackInfo(currentEntry.trackId))
|
||||||
|
}
|
||||||
|
style={{ cursor: currentEntry ? 'pointer' : 'default' }}
|
||||||
|
title={currentEntry ? t('trackInfo.open') : undefined}
|
||||||
>
|
>
|
||||||
<ArtTile seed={seedLabel} size={54} label={seedLabel} src={artUrl} />
|
<ArtTile seed={seedLabel} size={54} label={seedLabel} src={artUrl} />
|
||||||
<div className="pl-now-tt">
|
<div className="pl-now-tt">
|
||||||
<div className="t">{currentEntry?.title ?? '—'}</div>
|
<div className="t">{current?.title ?? '—'}</div>
|
||||||
<div className="a">{currentEntry?.artistName ?? ''}</div>
|
<div className="a">{current?.artistName ?? ''}</div>
|
||||||
<div
|
<div
|
||||||
className="pl-srcbadge"
|
className="pl-srcbadge"
|
||||||
style={{ color: onStream ? 'var(--fg-3)' : 'var(--lime)' }}
|
style={{ color: onStream ? 'var(--fg-3)' : 'var(--lime)' }}
|
||||||
|
|||||||
@@ -7,8 +7,11 @@ import {
|
|||||||
goToIndex,
|
goToIndex,
|
||||||
removeFromQueue,
|
removeFromQueue,
|
||||||
clearQueue,
|
clearQueue,
|
||||||
|
type QueueEntry,
|
||||||
} from '../../store/slices/queue';
|
} from '../../store/slices/queue';
|
||||||
import { toggleQueue } from '../../store/slices/player';
|
import { toggleQueue } from '../../store/slices/player';
|
||||||
|
import { openTrackInfo } from '../../store/slices/ui';
|
||||||
|
import { useResolvedQueueEntry } from '../../hooks/useResolvedQueueEntry';
|
||||||
|
|
||||||
export function QueuePanel() {
|
export function QueuePanel() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -16,8 +19,9 @@ export function QueuePanel() {
|
|||||||
const queue = useAppSelector((s) => s.queue);
|
const queue = useAppSelector((s) => s.queue);
|
||||||
const isOpen = useAppSelector((s) => s.player.isQueueOpen);
|
const isOpen = useAppSelector((s) => s.player.isQueueOpen);
|
||||||
|
|
||||||
const now =
|
const nowEntry =
|
||||||
queue.currentIndex >= 0 ? queue.entries[queue.currentIndex] : undefined;
|
queue.currentIndex >= 0 ? queue.entries[queue.currentIndex] : undefined;
|
||||||
|
const now = useResolvedQueueEntry(nowEntry);
|
||||||
const upNext = queue.entries
|
const upNext = queue.entries
|
||||||
.map((entry, index) => ({ entry, index }))
|
.map((entry, index) => ({ entry, index }))
|
||||||
.filter(({ index }) => index > queue.currentIndex);
|
.filter(({ index }) => index > queue.currentIndex);
|
||||||
@@ -82,14 +86,27 @@ export function QueuePanel() {
|
|||||||
<div className="t">{now.title}</div>
|
<div className="t">{now.title}</div>
|
||||||
<div className="r">{now.artistName}</div>
|
<div className="r">{now.artistName}</div>
|
||||||
</div>
|
</div>
|
||||||
<Icon name="cloud" style={{ color: 'var(--fg-3)' }} />
|
<button
|
||||||
|
type="button"
|
||||||
|
className="iconbtn sm"
|
||||||
|
onClick={() => dispatch(openTrackInfo(now.trackId))}
|
||||||
|
title={t('trackInfo.open')}
|
||||||
|
>
|
||||||
|
<Icon name="info" />
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{isRadio && (
|
{isRadio && (
|
||||||
<div className="qd-radio">
|
<div className="qd-radio">
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<Icon name="radio" />
|
<Icon name="radio" />
|
||||||
<span style={{ fontSize: 13, fontWeight: 600, color: 'var(--fg-1)' }}>
|
<span
|
||||||
|
style={{
|
||||||
|
fontSize: 13,
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--fg-1)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
{t('queue.radioActive')}
|
{t('queue.radioActive')}
|
||||||
</span>
|
</span>
|
||||||
<div style={{ flex: 1 }} />
|
<div style={{ flex: 1 }} />
|
||||||
@@ -120,33 +137,12 @@ export function QueuePanel() {
|
|||||||
<div className="qd-empty">{t('queue.nothingNext')}</div>
|
<div className="qd-empty">{t('queue.nothingNext')}</div>
|
||||||
) : (
|
) : (
|
||||||
upNext.map(({ entry, index }) => (
|
upNext.map(({ entry, index }) => (
|
||||||
<div
|
<QueueRow
|
||||||
key={`${entry.trackId}-${index}`}
|
key={`${entry.trackId}-${index}`}
|
||||||
className="qrow"
|
entry={entry}
|
||||||
onDoubleClick={() => dispatch(goToIndex(index))}
|
onPlay={() => dispatch(goToIndex(index))}
|
||||||
title={t('queue.doubleClickPlay')}
|
onRemove={() => dispatch(removeFromQueue(index))}
|
||||||
>
|
/>
|
||||||
<span className="grip">
|
|
||||||
<Icon name="dots-six-vertical" />
|
|
||||||
</span>
|
|
||||||
<ArtTile
|
|
||||||
seed={entry.albumTitle}
|
|
||||||
size={36}
|
|
||||||
label={entry.albumTitle}
|
|
||||||
/>
|
|
||||||
<div className="qt">
|
|
||||||
<div className="t">{entry.title}</div>
|
|
||||||
<div className="r">{entry.artistName}</div>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="iconbtn sm"
|
|
||||||
onClick={() => dispatch(removeFromQueue(index))}
|
|
||||||
title={t('queue.removeFromQueue')}
|
|
||||||
>
|
|
||||||
<Icon name="x" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -162,3 +158,44 @@ export function QueuePanel() {
|
|||||||
</aside>
|
</aside>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** An "up next" row, resolving its display fields against the live Track cache
|
||||||
|
* (same read-through as the now-playing entry) so enrichment updates show. */
|
||||||
|
function QueueRow({
|
||||||
|
entry,
|
||||||
|
onPlay,
|
||||||
|
onRemove,
|
||||||
|
}: {
|
||||||
|
entry: QueueEntry;
|
||||||
|
onPlay: () => void;
|
||||||
|
onRemove: () => void;
|
||||||
|
}) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const resolved = useResolvedQueueEntry(entry);
|
||||||
|
const albumTitle = resolved?.albumTitle ?? entry.albumTitle;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="qrow"
|
||||||
|
onDoubleClick={onPlay}
|
||||||
|
title={t('queue.doubleClickPlay')}
|
||||||
|
>
|
||||||
|
<span className="grip">
|
||||||
|
<Icon name="dots-six-vertical" />
|
||||||
|
</span>
|
||||||
|
<ArtTile seed={albumTitle} size={36} label={albumTitle} />
|
||||||
|
<div className="qt">
|
||||||
|
<div className="t">{resolved?.title ?? entry.title}</div>
|
||||||
|
<div className="r">{resolved?.artistName ?? entry.artistName}</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="iconbtn sm"
|
||||||
|
onClick={onRemove}
|
||||||
|
title={t('queue.removeFromQueue')}
|
||||||
|
>
|
||||||
|
<Icon name="x" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { Badge, Spinner, Tooltip } from '@olly/modern-sk';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import type { MetadataStatus } from '../../api/types';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
status: MetadataStatus;
|
||||||
|
/** Reason shown in the tooltip for a `failed` status. */
|
||||||
|
error?: string;
|
||||||
|
/** When true, render nothing for the normal `enriched` state (keeps dense
|
||||||
|
* track lists quiet; the upload screen sets this false to confirm success). */
|
||||||
|
hideWhenEnriched?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Variant = 'lime' | 'ember' | 'neutral' | 'outline';
|
||||||
|
|
||||||
|
const VARIANT: Record<MetadataStatus, Variant> = {
|
||||||
|
pending: 'neutral',
|
||||||
|
enriched: 'lime',
|
||||||
|
failed: 'ember',
|
||||||
|
manual: 'outline',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows a track's metadata-enrichment state (distinct from file availability).
|
||||||
|
* `pending` carries a spinner; `failed` exposes the backend reason on hover.
|
||||||
|
*/
|
||||||
|
export function MetadataStatusBadge({
|
||||||
|
status,
|
||||||
|
error,
|
||||||
|
hideWhenEnriched = true,
|
||||||
|
}: Props) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
if (status === 'enriched' && hideWhenEnriched) return null;
|
||||||
|
|
||||||
|
const label = t(`metadata.status.${status}`);
|
||||||
|
const tooltip =
|
||||||
|
status === 'failed' && error ? error : t(`metadata.statusHint.${status}`);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Tooltip content={tooltip}>
|
||||||
|
<Badge variant={VARIANT[status]} dot={status !== 'pending'}>
|
||||||
|
{status === 'pending' ? <Spinner size="sm" /> : null}
|
||||||
|
{label}
|
||||||
|
</Badge>
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { useAppDispatch } from '../../hooks/useAppDispatch';
|
import { useAppDispatch } from '../../hooks/useAppDispatch';
|
||||||
import { addToQueue, addNextInQueue } from '../../store/slices/queue';
|
import { addToQueue, addNextInQueue } from '../../store/slices/queue';
|
||||||
import { play } from '../../store/slices/player';
|
import { play } from '../../store/slices/player';
|
||||||
|
import { openTrackInfo } from '../../store/slices/ui';
|
||||||
import type { Track } from '../../api/types';
|
import type { Track } from '../../api/types';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -42,21 +43,45 @@ export function TrackContextMenu({
|
|||||||
return (
|
return (
|
||||||
<Menu>
|
<Menu>
|
||||||
<MenuTrigger asChild>
|
<MenuTrigger asChild>
|
||||||
<IconButton variant="ghost" size="sm" aria-label={t('track.menu.options')}>
|
<IconButton
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
aria-label={t('track.menu.options')}
|
||||||
|
>
|
||||||
⋯
|
⋯
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</MenuTrigger>
|
</MenuTrigger>
|
||||||
<MenuContent>
|
<MenuContent>
|
||||||
<MenuItem onSelect={() => { dispatch(play(track.id)); }}>
|
<MenuItem
|
||||||
|
onSelect={() => {
|
||||||
|
dispatch(play(track.id));
|
||||||
|
}}
|
||||||
|
>
|
||||||
{t('track.menu.playNow')}
|
{t('track.menu.playNow')}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onSelect={() => { dispatch(addNextInQueue(entry)); }}>
|
<MenuItem
|
||||||
|
onSelect={() => {
|
||||||
|
dispatch(addNextInQueue(entry));
|
||||||
|
}}
|
||||||
|
>
|
||||||
{t('track.menu.playNext')}
|
{t('track.menu.playNext')}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onSelect={() => { dispatch(addToQueue(entry)); }}>
|
<MenuItem
|
||||||
|
onSelect={() => {
|
||||||
|
dispatch(addToQueue(entry));
|
||||||
|
}}
|
||||||
|
>
|
||||||
{t('track.menu.addToQueue')}
|
{t('track.menu.addToQueue')}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuSeparator />
|
<MenuSeparator />
|
||||||
|
<MenuItem
|
||||||
|
onSelect={() => {
|
||||||
|
dispatch(openTrackInfo(track.id));
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t('track.menu.info')}
|
||||||
|
</MenuItem>
|
||||||
|
<MenuSeparator />
|
||||||
{onAddToPlaylist && (
|
{onAddToPlaylist && (
|
||||||
<MenuItem onSelect={() => onAddToPlaylist(track)}>
|
<MenuItem onSelect={() => onAddToPlaylist(track)}>
|
||||||
{t('track.menu.addToPlaylist')}
|
{t('track.menu.addToPlaylist')}
|
||||||
|
|||||||
@@ -0,0 +1,319 @@
|
|||||||
|
import type { ReactNode } from 'react';
|
||||||
|
import { Badge, Button } from '@olly/modern-sk';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Link, useNavigate } from 'react-router';
|
||||||
|
import { skipToken } from '@reduxjs/toolkit/query';
|
||||||
|
import { Icon } from '../common/Icon';
|
||||||
|
import { ArtTile } from '../common/ArtTile';
|
||||||
|
import { AvailabilityBadge } from './AvailabilityBadge';
|
||||||
|
import { MetadataStatusBadge } from './MetadataStatusBadge';
|
||||||
|
import { LoadingSkeleton } from '../common/LoadingSkeleton';
|
||||||
|
import { ErrorState } from '../common/ErrorState';
|
||||||
|
import { EmptyState } from '../common/EmptyState';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../hooks/useAppDispatch';
|
||||||
|
import { closeTrackInfo } from '../../store/slices/ui';
|
||||||
|
import { play } from '../../store/slices/player';
|
||||||
|
import { addToQueue } from '../../store/slices/queue';
|
||||||
|
import {
|
||||||
|
useGetTrackQuery,
|
||||||
|
useGetAlbumQuery,
|
||||||
|
} from '../../api/endpoints/library';
|
||||||
|
import { getTrackCoverUrl } from '../../api/endpoints/streaming';
|
||||||
|
import {
|
||||||
|
formatDuration,
|
||||||
|
formatFileSize,
|
||||||
|
formatDateTime,
|
||||||
|
} from '../../lib/format';
|
||||||
|
import type { Track } from '../../api/types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Right-side "Get Info"-style drawer for a single track. Rendered after the
|
||||||
|
* QueuePanel in AppShell so it sits to the *right* of the queue when both are
|
||||||
|
* open. Open state lives in `ui.trackInfoId`; it reads the live Track (and its
|
||||||
|
* album) from the RTKQ cache so enrichment updates stay in sync.
|
||||||
|
*/
|
||||||
|
export function TrackInfoDrawer() {
|
||||||
|
const trackId = useAppSelector((s) => s.ui.trackInfoId);
|
||||||
|
const isOpen = trackId !== null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<aside className={`tid${isOpen ? '' : ' closed'}`} aria-hidden={!isOpen}>
|
||||||
|
<div className="tid-inner">
|
||||||
|
{trackId ? <TrackInfoContent trackId={trackId} /> : null}
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TrackInfoContent({ trackId }: { trackId: string }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const token = useAppSelector((s) => s.auth.accessToken);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: track,
|
||||||
|
isLoading,
|
||||||
|
isError,
|
||||||
|
refetch,
|
||||||
|
} = useGetTrackQuery(trackId);
|
||||||
|
// Album record fills in fields the lean TrackOut omits (year especially).
|
||||||
|
const { data: album } = useGetAlbumQuery(track?.albumId ?? skipToken);
|
||||||
|
|
||||||
|
const close = () => dispatch(closeTrackInfo());
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="tid-head">
|
||||||
|
<h3>{t('trackInfo.title')}</h3>
|
||||||
|
<div style={{ flex: 1 }} />
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="iconbtn sm"
|
||||||
|
onClick={close}
|
||||||
|
title={t('trackInfo.close')}
|
||||||
|
>
|
||||||
|
<Icon name="x" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="tid-scroll">
|
||||||
|
{isLoading ? (
|
||||||
|
<LoadingSkeleton rows={6} />
|
||||||
|
) : isError ? (
|
||||||
|
<ErrorState onRetry={refetch} />
|
||||||
|
) : !track ? (
|
||||||
|
<EmptyState title={t('trackInfo.notFound')} />
|
||||||
|
) : (
|
||||||
|
<TrackInfoBody
|
||||||
|
track={track}
|
||||||
|
albumYear={album?.year}
|
||||||
|
albumTrackCount={album?.trackCount}
|
||||||
|
coverUrl={
|
||||||
|
token
|
||||||
|
? getTrackCoverUrl(track.id, token, track.hasCover)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
onPlay={() => dispatch(play(track.id))}
|
||||||
|
onQueue={() =>
|
||||||
|
dispatch(
|
||||||
|
addToQueue({
|
||||||
|
trackId: track.id,
|
||||||
|
title: track.title,
|
||||||
|
artistName: track.artistName,
|
||||||
|
albumTitle: track.albumTitle,
|
||||||
|
durationMs: track.durationMs,
|
||||||
|
albumArtUrl: track.albumArtUrl,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
onEdit={() => {
|
||||||
|
navigate(`/tracks/${track.id}/metadata`);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TrackInfoBody({
|
||||||
|
track,
|
||||||
|
albumYear,
|
||||||
|
albumTrackCount,
|
||||||
|
coverUrl,
|
||||||
|
onPlay,
|
||||||
|
onQueue,
|
||||||
|
onEdit,
|
||||||
|
}: {
|
||||||
|
track: Track;
|
||||||
|
albumYear?: number;
|
||||||
|
albumTrackCount?: number;
|
||||||
|
coverUrl?: string;
|
||||||
|
onPlay: () => void;
|
||||||
|
onQueue: () => void;
|
||||||
|
onEdit: () => void;
|
||||||
|
}) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const seedLabel = track.albumTitle || track.title;
|
||||||
|
const year = track.year ?? albumYear;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="tid-cover">
|
||||||
|
{coverUrl ? (
|
||||||
|
<img src={coverUrl} alt={track.albumTitle} />
|
||||||
|
) : (
|
||||||
|
<ArtTile seed={seedLabel} size={256} label={seedLabel} radius={12} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2 className="tid-title">{track.title}</h2>
|
||||||
|
<Link className="tid-sub" to={`/artists/${track.artistId}`}>
|
||||||
|
{track.artistName}
|
||||||
|
</Link>
|
||||||
|
{track.albumId && (
|
||||||
|
<Link className="tid-sub tid-album" to={`/albums/${track.albumId}`}>
|
||||||
|
<Icon name="vinyl-record" />
|
||||||
|
{track.albumTitle}
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="tid-actions">
|
||||||
|
<Button variant="primary" size="sm" onClick={onPlay}>
|
||||||
|
<Icon name="play" fill /> {t('trackInfo.play')}
|
||||||
|
</Button>
|
||||||
|
<Button variant="ghost" size="sm" onClick={onQueue}>
|
||||||
|
<Icon name="queue" /> {t('trackInfo.addToQueue')}
|
||||||
|
</Button>
|
||||||
|
<Button variant="ghost" size="sm" onClick={onEdit}>
|
||||||
|
{t('trackInfo.editMetadata')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<InfoSection title={t('trackInfo.sections.status')}>
|
||||||
|
<div className="tid-status">
|
||||||
|
<AvailabilityBadge availability={track.availability} />
|
||||||
|
<MetadataStatusBadge
|
||||||
|
status={track.metadataStatus}
|
||||||
|
error={track.metadataError}
|
||||||
|
hideWhenEnriched={false}
|
||||||
|
/>
|
||||||
|
{track.liked && (
|
||||||
|
<Badge variant="lime" dot>
|
||||||
|
{t('trackInfo.liked')}
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{track.metadataStatus === 'failed' && track.metadataError && (
|
||||||
|
<p className="tid-error">{track.metadataError}</p>
|
||||||
|
)}
|
||||||
|
</InfoSection>
|
||||||
|
|
||||||
|
<InfoSection title={t('trackInfo.sections.general')}>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.artist')}
|
||||||
|
value={track.artistName}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.album')}
|
||||||
|
value={track.albumTitle || undefined}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.trackNumber')}
|
||||||
|
value={
|
||||||
|
track.trackNumber !== undefined
|
||||||
|
? albumTrackCount
|
||||||
|
? t('trackInfo.trackOf', {
|
||||||
|
n: track.trackNumber,
|
||||||
|
total: albumTrackCount,
|
||||||
|
})
|
||||||
|
: String(track.trackNumber)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.disc')}
|
||||||
|
value={
|
||||||
|
track.discNumber !== undefined
|
||||||
|
? String(track.discNumber)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.year')}
|
||||||
|
value={year !== undefined ? String(year) : undefined}
|
||||||
|
/>
|
||||||
|
<InfoRow label={t('trackInfo.fields.genre')} value={track.genre} />
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.duration')}
|
||||||
|
value={formatDuration(track.durationMs)}
|
||||||
|
/>
|
||||||
|
</InfoSection>
|
||||||
|
|
||||||
|
<InfoSection title={t('trackInfo.sections.file')}>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.format')}
|
||||||
|
value={track.format?.toUpperCase()}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.bitrate')}
|
||||||
|
value={
|
||||||
|
track.bitrate !== undefined
|
||||||
|
? t('trackInfo.kbps', { n: track.bitrate })
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.size')}
|
||||||
|
value={
|
||||||
|
track.fileSize !== undefined
|
||||||
|
? formatFileSize(track.fileSize)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.source')}
|
||||||
|
value={track.source}
|
||||||
|
mono
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.added')}
|
||||||
|
value={formatDateTime(track.createdAt)}
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.enriched')}
|
||||||
|
value={formatDateTime(track.enrichedAt)}
|
||||||
|
/>
|
||||||
|
</InfoSection>
|
||||||
|
|
||||||
|
<InfoSection title={t('trackInfo.sections.identifiers')}>
|
||||||
|
<InfoRow label={t('trackInfo.fields.trackId')} value={track.id} mono />
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.albumId')}
|
||||||
|
value={track.albumId || undefined}
|
||||||
|
mono
|
||||||
|
/>
|
||||||
|
<InfoRow
|
||||||
|
label={t('trackInfo.fields.artistId')}
|
||||||
|
value={track.artistId}
|
||||||
|
mono
|
||||||
|
/>
|
||||||
|
</InfoSection>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function InfoSection({
|
||||||
|
title,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
children: ReactNode;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<section className="tid-section">
|
||||||
|
<span className="msk-label tid-section-label">{title}</span>
|
||||||
|
{children}
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A label/value row; renders nothing when the value is empty (Finder-style). */
|
||||||
|
function InfoRow({
|
||||||
|
label,
|
||||||
|
value,
|
||||||
|
mono,
|
||||||
|
}: {
|
||||||
|
label: string;
|
||||||
|
value?: string;
|
||||||
|
mono?: boolean;
|
||||||
|
}) {
|
||||||
|
if (!value) return null;
|
||||||
|
return (
|
||||||
|
<div className="tid-row">
|
||||||
|
<span className="tid-row-k">{label}</span>
|
||||||
|
<span className={`tid-row-v${mono ? ' mono' : ''}`}>{value}</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import { Row } from '@olly/modern-sk';
|
import { Row } from '@olly/modern-sk';
|
||||||
import { TrackContextMenu } from './TrackContextMenu';
|
import { TrackContextMenu } from './TrackContextMenu';
|
||||||
import { AvailabilityBadge } from './AvailabilityBadge';
|
import { AvailabilityBadge } from './AvailabilityBadge';
|
||||||
|
import { MetadataStatusBadge } from './MetadataStatusBadge';
|
||||||
import { formatDuration } from '../../lib/format';
|
import { formatDuration } from '../../lib/format';
|
||||||
import { useAppDispatch, useAppSelector } from '../../hooks/useAppDispatch';
|
import { useAppDispatch, useAppSelector } from '../../hooks/useAppDispatch';
|
||||||
import { play } from '../../store/slices/player';
|
import { play } from '../../store/slices/player';
|
||||||
import type { Track } from '../../api/types';
|
import type { Track } from '../../api/types';
|
||||||
import { getCoverUrl } from '../../api/endpoints/streaming';
|
import { getCoverUrl, getTrackCoverUrl } from '../../api/endpoints/streaming';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
track: Track;
|
track: Track;
|
||||||
@@ -27,8 +28,13 @@ export function TrackRow({
|
|||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const currentTrackId = useAppSelector((s) => s.player.currentTrackId);
|
const currentTrackId = useAppSelector((s) => s.player.currentTrackId);
|
||||||
const isPlaying = useAppSelector((s) => s.player.isPlaying);
|
const isPlaying = useAppSelector((s) => s.player.isPlaying);
|
||||||
|
const token = useAppSelector((s) => s.auth.accessToken);
|
||||||
const isActive = currentTrackId === track.id;
|
const isActive = currentTrackId === track.id;
|
||||||
const artUrl = getCoverUrl(track.albumArtUrl);
|
// Prefer an explicit album art URL; otherwise serve the track's own cover
|
||||||
|
// (needs the token in the query string — `<img>` can't send a header).
|
||||||
|
const artUrl =
|
||||||
|
getCoverUrl(track.albumArtUrl) ??
|
||||||
|
(token ? getTrackCoverUrl(track.id, token, track.hasCover) : undefined);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row
|
<Row
|
||||||
@@ -95,7 +101,13 @@ export function TrackRow({
|
|||||||
{showAlbum && ` · ${track.albumTitle}`}
|
{showAlbum && ` · ${track.albumTitle}`}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<AvailabilityBadge availability={track.availability} />
|
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
|
||||||
|
<MetadataStatusBadge
|
||||||
|
status={track.metadataStatus}
|
||||||
|
error={track.metadataError}
|
||||||
|
/>
|
||||||
|
<AvailabilityBadge availability={track.availability} />
|
||||||
|
</div>
|
||||||
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
|
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
|
||||||
<span
|
<span
|
||||||
style={{
|
style={{
|
||||||
|
|||||||
+40
-1
@@ -1,2 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Default backend base URL — the operator-set fallback used when no specific
|
||||||
|
* instance is active. Resolution order:
|
||||||
|
*
|
||||||
|
* 1. window.__APP_CONFIG__.apiBaseUrl — runtime, injected by the container
|
||||||
|
* at start from $PUBLIC_API_BASE_URL (see public/config.js). Lets one
|
||||||
|
* prebuilt image point at any backend origin without rebuilding.
|
||||||
|
* 2. import.meta.env.PUBLIC_API_BASE_URL — build-time default (rsbuild inlines
|
||||||
|
* PUBLIC_* vars). Used in local dev and as a baked fallback.
|
||||||
|
* 3. '/api/v1' — same-origin relative path (works behind a reverse proxy).
|
||||||
|
*
|
||||||
|
* The user's chosen instance still wins over all of these — see
|
||||||
|
* runtime-config.ts / instances.ts.
|
||||||
|
*/
|
||||||
|
function runtimeApiBaseUrl(): string | undefined {
|
||||||
|
if (typeof window === 'undefined') return undefined;
|
||||||
|
const value = window.__APP_CONFIG__?.apiBaseUrl;
|
||||||
|
return value ? value : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export const DEFAULT_API_BASE_URL =
|
export const DEFAULT_API_BASE_URL =
|
||||||
import.meta.env.PUBLIC_API_BASE_URL ?? '/api/v1';
|
runtimeApiBaseUrl() ?? import.meta.env.PUBLIC_API_BASE_URL ?? '/api/v1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the public sign-up UI is shown. Same precedence as the base URL:
|
||||||
|
* runtime operator config (injected into `window.__APP_CONFIG__` at container
|
||||||
|
* start) wins over the build-time `PUBLIC_ENABLE_REGISTRATION` env, which
|
||||||
|
* defaults to enabled. This only gates the *UI*; the backend independently
|
||||||
|
* enforces `ALLOW_REGISTRATION` and is the real authority.
|
||||||
|
*/
|
||||||
|
function parseFlag(value: string | undefined): boolean | undefined {
|
||||||
|
if (value == null || value === '') return undefined;
|
||||||
|
return value !== 'false' && value !== '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
export const REGISTRATION_ENABLED: boolean =
|
||||||
|
(typeof window !== 'undefined'
|
||||||
|
? window.__APP_CONFIG__?.enableRegistration
|
||||||
|
: undefined) ??
|
||||||
|
parseFlag(import.meta.env.PUBLIC_ENABLE_REGISTRATION) ??
|
||||||
|
true;
|
||||||
|
|||||||
+11
-1
@@ -29,8 +29,13 @@ const ACTIVE_KEY = 'mcma:activeInstance';
|
|||||||
const LEGACY_URL_KEY = 'mcma_api_base_url';
|
const LEGACY_URL_KEY = 'mcma_api_base_url';
|
||||||
const LEGACY_AUTH_KEY = 'mcma_auth';
|
const LEGACY_AUTH_KEY = 'mcma_auth';
|
||||||
|
|
||||||
|
// The UI always talks to the `/api/v1` contract, so users only enter the
|
||||||
|
// origin (and optional reverse-proxy prefix). We append the contract path
|
||||||
|
// here, the single choke point for both the base URL and the instance id, so
|
||||||
|
// `domain.com`, `domain.com/`, and `domain.com/api/v1` all converge.
|
||||||
function normalizeUrl(url: string): string {
|
function normalizeUrl(url: string): string {
|
||||||
return url.trim().replace(/\/+$/, '');
|
const trimmed = url.trim().replace(/\/+$/, '');
|
||||||
|
return /\/api\/v1$/.test(trimmed) ? trimmed : `${trimmed}/api/v1`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Stable, readable id from a base URL — also serves as the storage namespace. */
|
/** Stable, readable id from a base URL — also serves as the storage namespace. */
|
||||||
@@ -93,6 +98,11 @@ export function upsertInstance(url: string, name?: string): Instance {
|
|||||||
return inst;
|
return inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clear a backend's stored session without forgetting the instance itself. */
|
||||||
|
export function clearInstanceAuth(id: string): void {
|
||||||
|
localStorage.removeItem(scopedKey('auth', id));
|
||||||
|
}
|
||||||
|
|
||||||
/** Remove a backend and wipe every scoped key it owns. */
|
/** Remove a backend and wipe every scoped key it owns. */
|
||||||
export function removeInstance(id: string): void {
|
export function removeInstance(id: string): void {
|
||||||
writeRegistry(readRegistry().filter((i) => i.id !== id));
|
writeRegistry(readRegistry().filter((i) => i.id !== id));
|
||||||
|
|||||||
Vendored
+10
@@ -1,7 +1,17 @@
|
|||||||
/// <reference types="@rsbuild/core/types" />
|
/// <reference types="@rsbuild/core/types" />
|
||||||
interface ImportMetaEnv {
|
interface ImportMetaEnv {
|
||||||
readonly PUBLIC_API_BASE_URL?: string;
|
readonly PUBLIC_API_BASE_URL?: string;
|
||||||
|
readonly PUBLIC_ENABLE_REGISTRATION?: string;
|
||||||
}
|
}
|
||||||
interface ImportMeta {
|
interface ImportMeta {
|
||||||
readonly env: ImportMetaEnv;
|
readonly env: ImportMetaEnv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Runtime operator config injected by /config.js before the app bundle loads
|
||||||
|
// (written from $PUBLIC_API_BASE_URL at container start). See src/config/env.ts.
|
||||||
|
interface Window {
|
||||||
|
__APP_CONFIG__?: {
|
||||||
|
apiBaseUrl?: string;
|
||||||
|
enableRegistration?: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,18 +1,191 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useNavigate } from 'react-router';
|
import { useNavigate } from 'react-router';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Card, TextField, Button, Callout, Badge } from '@olly/modern-sk';
|
import type { FetchBaseQueryError } from '@reduxjs/toolkit/query';
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
TextField,
|
||||||
|
Button,
|
||||||
|
Callout,
|
||||||
|
Badge,
|
||||||
|
Dialog,
|
||||||
|
IconButton,
|
||||||
|
} from '@olly/modern-sk';
|
||||||
import { Icon } from '../../components/common/Icon';
|
import { Icon } from '../../components/common/Icon';
|
||||||
import { useAppDispatch } from '../../hooks/useAppDispatch';
|
import { useAppDispatch } from '../../hooks/useAppDispatch';
|
||||||
|
import { useConnectionStatus } from '../../hooks/useConnectionStatus';
|
||||||
import { setTokens, setUser } from '../../store/slices/auth';
|
import { setTokens, setUser } from '../../store/slices/auth';
|
||||||
import { setApiBaseUrl } from '../../config/runtime-config';
|
import {
|
||||||
|
useLoginMutation,
|
||||||
|
useRegisterMutation,
|
||||||
|
} from '../../api/endpoints/auth';
|
||||||
|
import { REGISTRATION_ENABLED } from '../../config/env';
|
||||||
import {
|
import {
|
||||||
listInstances,
|
listInstances,
|
||||||
getActiveInstanceId,
|
getActiveInstanceId,
|
||||||
setActiveInstanceId,
|
setActiveInstanceId,
|
||||||
removeInstance,
|
removeInstance,
|
||||||
|
clearInstanceAuth,
|
||||||
|
upsertInstance,
|
||||||
|
type Instance,
|
||||||
} from '../../config/instances';
|
} from '../../config/instances';
|
||||||
import type { User } from '../../api/types';
|
|
||||||
|
type Mode = 'login' | 'register';
|
||||||
|
|
||||||
|
const HEALTH_VARIANTS = {
|
||||||
|
connected: 'lime',
|
||||||
|
connecting: 'neutral',
|
||||||
|
disconnected: 'ember',
|
||||||
|
error: 'ember',
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
const HEALTH_KEY = {
|
||||||
|
connected: 'conn.connected',
|
||||||
|
connecting: 'conn.connecting',
|
||||||
|
disconnected: 'conn.disconnected',
|
||||||
|
error: 'conn.error',
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
/** Map an RTKQ login failure to a user-facing i18n key. */
|
||||||
|
function resolveLoginError(err: unknown): string {
|
||||||
|
const e = err as FetchBaseQueryError | undefined;
|
||||||
|
if (e && 'status' in e) {
|
||||||
|
if (e.status === 'FETCH_ERROR') return 'connect.errors.unreachable';
|
||||||
|
if (e.status === 401) return 'connect.errors.badCredentials';
|
||||||
|
}
|
||||||
|
return 'connect.errors.generic';
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Map an RTKQ register failure to a user-facing i18n key. */
|
||||||
|
function resolveRegisterError(err: unknown): string {
|
||||||
|
const e = err as FetchBaseQueryError | undefined;
|
||||||
|
if (e && 'status' in e) {
|
||||||
|
if (e.status === 'FETCH_ERROR') return 'connect.errors.unreachable';
|
||||||
|
if (e.status === 409) return 'connect.errors.usernameTaken';
|
||||||
|
if (e.status === 422) return 'connect.errors.passwordTooShort';
|
||||||
|
if (e.status === 403) return 'connect.errors.registrationDisabled';
|
||||||
|
}
|
||||||
|
return 'connect.errors.registerFailed';
|
||||||
|
}
|
||||||
|
|
||||||
|
function InstanceRow({
|
||||||
|
inst,
|
||||||
|
selected,
|
||||||
|
onSelect,
|
||||||
|
onLogout,
|
||||||
|
onRemove,
|
||||||
|
}: {
|
||||||
|
inst: Instance;
|
||||||
|
selected: boolean;
|
||||||
|
onSelect: () => void;
|
||||||
|
onLogout: () => void;
|
||||||
|
onRemove: () => void;
|
||||||
|
}) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const status = useConnectionStatus(inst.baseUrl);
|
||||||
|
const [dialogOpen, setDialogOpen] = useState(false);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '0.625rem',
|
||||||
|
padding: '0.375rem 0',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Badge variant={HEALTH_VARIANTS[status]} dot>
|
||||||
|
{t(HEALTH_KEY[status])}
|
||||||
|
</Badge>
|
||||||
|
<div style={{ minWidth: 0, flex: 1 }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: '0.875rem',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--color-text-1)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{inst.name}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: '0.75rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{inst.baseUrl}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{selected ? (
|
||||||
|
<Badge variant="outline">{t('connect.domains.selected')}</Badge>
|
||||||
|
) : (
|
||||||
|
<Button variant="ghost" size="sm" onClick={onSelect}>
|
||||||
|
{t('connect.domains.use')}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<Dialog
|
||||||
|
open={dialogOpen}
|
||||||
|
onOpenChange={setDialogOpen}
|
||||||
|
title={t('connect.removeDialog.title')}
|
||||||
|
description={t('connect.removeDialog.description', {
|
||||||
|
name: inst.name,
|
||||||
|
})}
|
||||||
|
trigger={
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="iconbtn sm"
|
||||||
|
title={t('connect.domains.forgetTitle')}
|
||||||
|
>
|
||||||
|
<Icon name="trash" />
|
||||||
|
</button>
|
||||||
|
}
|
||||||
|
footer={
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'flex-end',
|
||||||
|
gap: '0.5rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => setDialogOpen(false)}
|
||||||
|
>
|
||||||
|
{t('connect.removeDialog.cancel')}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => {
|
||||||
|
setDialogOpen(false);
|
||||||
|
onLogout();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t('connect.removeDialog.logout')}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ember"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => {
|
||||||
|
setDialogOpen(false);
|
||||||
|
onRemove();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t('connect.removeDialog.removeAndLogout')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function ConnectPage() {
|
export function ConnectPage() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -21,41 +194,80 @@ export function ConnectPage() {
|
|||||||
|
|
||||||
const [rev, setRev] = useState(0);
|
const [rev, setRev] = useState(0);
|
||||||
const instances = listInstances();
|
const instances = listInstances();
|
||||||
const activeId = getActiveInstanceId();
|
|
||||||
|
|
||||||
const [apiUrl, setApiUrl] = useState('https://');
|
const [selectedId, setSelectedId] = useState<string | null>(() =>
|
||||||
|
getActiveInstanceId(),
|
||||||
|
);
|
||||||
|
const selectedInstance = instances.find((i) => i.id === selectedId) ?? null;
|
||||||
|
const [instanceAddShown, setInstanceAddShown] = useState(false);
|
||||||
|
|
||||||
|
const [addUrl, setAddUrl] = useState('');
|
||||||
|
|
||||||
|
const [mode, setMode] = useState<Mode>('login');
|
||||||
const [username, setUsername] = useState('');
|
const [username, setUsername] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
const switchTo = (id: string) => {
|
const [login, { isLoading: isLoggingIn }] = useLoginMutation();
|
||||||
|
const [register, { isLoading: isRegistering }] = useRegisterMutation();
|
||||||
|
const isLoading = isLoggingIn || isRegistering;
|
||||||
|
|
||||||
|
const switchMode = (next: Mode) => {
|
||||||
|
setMode(next);
|
||||||
|
setError(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Switching the active instance and reloading lets the app pick the saved
|
||||||
|
// session for that instance back up (if any); if it has none, ProtectedRoute
|
||||||
|
// bounces back here and `selectedId` defaults to it, surfacing the login card.
|
||||||
|
const selectInstance = (id: string) => {
|
||||||
setActiveInstanceId(id);
|
setActiveInstanceId(id);
|
||||||
window.location.assign('/');
|
window.location.assign('/');
|
||||||
};
|
};
|
||||||
|
|
||||||
const forget = (id: string) => {
|
const handleAdd = (e: React.FormEvent) => {
|
||||||
removeInstance(id);
|
e.preventDefault();
|
||||||
|
const url = addUrl.trim();
|
||||||
|
if (!url || url === 'https://') return;
|
||||||
|
const inst = upsertInstance(url);
|
||||||
|
setActiveInstanceId(inst.id);
|
||||||
|
setAddUrl('https://');
|
||||||
|
setSelectedId(inst.id);
|
||||||
setRev((r) => r + 1);
|
setRev((r) => r + 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleLogout = (id: string) => {
|
||||||
e.preventDefault();
|
clearInstanceAuth(id);
|
||||||
setApiBaseUrl(apiUrl);
|
setRev((r) => r + 1);
|
||||||
|
};
|
||||||
|
|
||||||
const fakeUser: User = {
|
const handleRemove = (id: string) => {
|
||||||
id: 'dev-user',
|
removeInstance(id);
|
||||||
username: username || 'dev',
|
if (selectedId === id) setSelectedId(getActiveInstanceId());
|
||||||
role: 'admin',
|
setRev((r) => r + 1);
|
||||||
createdAt: new Date().toISOString(),
|
};
|
||||||
};
|
|
||||||
dispatch(
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
setTokens({
|
e.preventDefault();
|
||||||
accessToken: 'dev-token',
|
if (!selectedInstance) return;
|
||||||
refreshToken: 'dev-refresh',
|
setError(null);
|
||||||
expiresIn: 3600,
|
|
||||||
}),
|
try {
|
||||||
);
|
const action =
|
||||||
dispatch(setUser(fakeUser));
|
mode === 'register'
|
||||||
void navigate('/');
|
? register({ username, password })
|
||||||
|
: login({ username, password });
|
||||||
|
const { user, tokens } = await action.unwrap();
|
||||||
|
dispatch(setTokens(tokens));
|
||||||
|
dispatch(setUser(user));
|
||||||
|
void navigate('/');
|
||||||
|
} catch (err) {
|
||||||
|
setError(
|
||||||
|
mode === 'register'
|
||||||
|
? resolveRegisterError(err)
|
||||||
|
: resolveLoginError(err),
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const labelStyle: React.CSSProperties = {
|
const labelStyle: React.CSSProperties = {
|
||||||
@@ -100,145 +312,172 @@ export function ConnectPage() {
|
|||||||
<Icon name="vinyl-record" fill /> MCMA
|
<Icon name="vinyl-record" fill /> MCMA
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
{instances.length > 0 && (
|
|
||||||
<Card>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
padding: '1.25rem 1.5rem',
|
|
||||||
display: 'flex',
|
|
||||||
flexDirection: 'column',
|
|
||||||
gap: '0.5rem',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<span className="msk-label" style={{ marginBottom: '0.25rem' }}>
|
|
||||||
{t('connect.savedInstances')}
|
|
||||||
</span>
|
|
||||||
{instances.map((inst) => (
|
|
||||||
<div
|
|
||||||
key={inst.id}
|
|
||||||
style={{
|
|
||||||
display: 'flex',
|
|
||||||
alignItems: 'center',
|
|
||||||
gap: '0.625rem',
|
|
||||||
padding: '0.375rem 0',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
className={`led ${inst.id === activeId ? 'online' : 'offline'}`}
|
|
||||||
style={{
|
|
||||||
width: 8,
|
|
||||||
height: 8,
|
|
||||||
borderRadius: '50%',
|
|
||||||
background:
|
|
||||||
inst.id === activeId ? 'var(--lime)' : 'var(--fg-3)',
|
|
||||||
boxShadow:
|
|
||||||
inst.id === activeId ? '0 0 6px var(--lime)' : 'none',
|
|
||||||
flexShrink: 0,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<div style={{ minWidth: 0, flex: 1 }}>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
fontSize: '0.875rem',
|
|
||||||
fontWeight: 600,
|
|
||||||
color: 'var(--color-text-1)',
|
|
||||||
overflow: 'hidden',
|
|
||||||
textOverflow: 'ellipsis',
|
|
||||||
whiteSpace: 'nowrap',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{inst.name}
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
fontSize: '0.75rem',
|
|
||||||
color: 'var(--color-text-3)',
|
|
||||||
overflow: 'hidden',
|
|
||||||
textOverflow: 'ellipsis',
|
|
||||||
whiteSpace: 'nowrap',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{inst.baseUrl}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{inst.id === activeId ? (
|
|
||||||
<Badge variant="lime">{t('connect.active')}</Badge>
|
|
||||||
) : (
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => switchTo(inst.id)}
|
|
||||||
>
|
|
||||||
{t('connect.use')}
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="iconbtn sm"
|
|
||||||
onClick={() => forget(inst.id)}
|
|
||||||
title={t('connect.forgetTitle')}
|
|
||||||
>
|
|
||||||
<Icon name="trash" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</Card>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Card>
|
<Card>
|
||||||
<form
|
<div
|
||||||
onSubmit={handleSubmit}
|
|
||||||
style={{
|
style={{
|
||||||
|
padding: '1.25rem 1.5rem',
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
flexDirection: 'column',
|
flexDirection: 'column',
|
||||||
gap: '1rem',
|
gap: '0.5rem',
|
||||||
padding: '1.5rem',
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span className="msk-label">{t('connect.form.title')}</span>
|
{instances.length > 0 && (
|
||||||
<div>
|
<>
|
||||||
<label style={labelStyle}>{t('connect.form.serverUrl')}</label>
|
<span className="msk-label" style={{ marginBottom: '0.25rem' }}>
|
||||||
<TextField
|
{t('connect.domains.title')}
|
||||||
value={apiUrl}
|
</span>
|
||||||
onChange={(e) => setApiUrl(e.target.value)}
|
{instances.map((inst) => (
|
||||||
placeholder="https://your-server.example.com/api/v1"
|
<InstanceRow
|
||||||
required
|
key={inst.id}
|
||||||
/>
|
inst={inst}
|
||||||
</div>
|
selected={inst.id === selectedId}
|
||||||
<div>
|
onSelect={() => selectInstance(inst.id)}
|
||||||
<label style={labelStyle}>{t('connect.form.username')}</label>
|
onLogout={() => handleLogout(inst.id)}
|
||||||
<TextField
|
onRemove={() => handleRemove(inst.id)}
|
||||||
value={username}
|
/>
|
||||||
onChange={(e) => setUsername(e.target.value)}
|
))}
|
||||||
placeholder="username"
|
</>
|
||||||
autoComplete="username"
|
)}
|
||||||
required
|
<form
|
||||||
/>
|
onSubmit={handleAdd}
|
||||||
</div>
|
style={{
|
||||||
<div>
|
display: 'flex',
|
||||||
<label style={labelStyle}>{t('connect.form.password')}</label>
|
gap: '0.5rem',
|
||||||
<TextField
|
marginTop: instances.length > 0 ? '0.5rem' : 0,
|
||||||
type="password"
|
}}
|
||||||
value={password}
|
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
|
||||||
placeholder="password"
|
|
||||||
autoComplete="current-password"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<Callout variant="warning">
|
|
||||||
{t('connect.form.stubNote')}
|
|
||||||
</Callout>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
variant="primary"
|
|
||||||
style={{ marginTop: '0.5rem' }}
|
|
||||||
>
|
>
|
||||||
{t('connect.form.submit')}
|
{instanceAddShown ? (
|
||||||
</Button>
|
<>
|
||||||
</form>
|
<TextField
|
||||||
|
value={addUrl}
|
||||||
|
onChange={(e) => setAddUrl(e.target.value)}
|
||||||
|
placeholder={t('connect.domains.addPlaceholder')}
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
/>
|
||||||
|
<IconButton type="submit" variant="primary">
|
||||||
|
<Icon name="plus" />
|
||||||
|
</IconButton>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
onClick={() => setInstanceAddShown(true)}
|
||||||
|
style={{ width: '100%' }}
|
||||||
|
variant="ghost"
|
||||||
|
>
|
||||||
|
<Icon name="plus" /> {t('connect.domains.addButton')}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{selectedInstance && (
|
||||||
|
<Card>
|
||||||
|
<form
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: '1rem',
|
||||||
|
padding: '1.5rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span className="msk-label">
|
||||||
|
{mode === 'register'
|
||||||
|
? t('connect.login.registerTitle', {
|
||||||
|
name: selectedInstance.name,
|
||||||
|
})
|
||||||
|
: t('connect.login.title', { name: selectedInstance.name })}
|
||||||
|
</span>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('connect.login.username')}</label>
|
||||||
|
<TextField
|
||||||
|
value={username}
|
||||||
|
onChange={(e) => setUsername(e.target.value)}
|
||||||
|
placeholder="username"
|
||||||
|
autoComplete="username"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('connect.login.password')}</label>
|
||||||
|
<TextField
|
||||||
|
type="password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
placeholder="password"
|
||||||
|
autoComplete={
|
||||||
|
mode === 'register' ? 'new-password' : 'current-password'
|
||||||
|
}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
{mode === 'register' && (
|
||||||
|
<span
|
||||||
|
style={{
|
||||||
|
display: 'block',
|
||||||
|
fontSize: '0.75rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
marginTop: '0.375rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t('connect.login.passwordHint')}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{error && <Callout variant="danger">{t(error)}</Callout>}
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
variant="primary"
|
||||||
|
disabled={isLoading}
|
||||||
|
style={{ marginTop: '0.5rem' }}
|
||||||
|
>
|
||||||
|
{isLoading
|
||||||
|
? mode === 'register'
|
||||||
|
? t('connect.login.registering')
|
||||||
|
: t('connect.login.submitting')
|
||||||
|
: mode === 'register'
|
||||||
|
? t('connect.login.registerSubmit')
|
||||||
|
: t('connect.login.submit')}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
{REGISTRATION_ENABLED && (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
textAlign: 'center',
|
||||||
|
fontSize: '0.8125rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{mode === 'register' ? (
|
||||||
|
<>
|
||||||
|
{t('connect.login.haveAccount')}{' '}
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => switchMode('login')}
|
||||||
|
>
|
||||||
|
{t('connect.login.signInLink')}
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{t('connect.login.noAccount')}{' '}
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => switchMode('register')}
|
||||||
|
>
|
||||||
|
{t('connect.login.registerLink')}
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,4 +1,16 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useParams, useNavigate } from 'react-router';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Badge, Button, Callout, Card, IconButton, ScrollArea, Spinner, TextField } from '@olly/modern-sk';
|
||||||
|
import {
|
||||||
|
useApplyMetadataMutation,
|
||||||
|
useEnrichTrackMutation,
|
||||||
|
useGetTrackQuery,
|
||||||
|
useLazyGetMetadataMatchesQuery,
|
||||||
|
} from '../../api/endpoints/library';
|
||||||
|
import type { MetadataMatch } from '../../api/types';
|
||||||
|
import { LoadingSkeleton } from '../../components/common/LoadingSkeleton';
|
||||||
|
import { ErrorState } from '../../components/common/ErrorState';
|
||||||
import { Placeholder } from '../../components/common/Placeholder';
|
import { Placeholder } from '../../components/common/Placeholder';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -6,13 +18,500 @@ interface Props {
|
|||||||
batch?: boolean;
|
batch?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Editable fields, kept as strings while in the form — parsed on save. */
|
||||||
|
interface FormState {
|
||||||
|
title: string;
|
||||||
|
artistName: string;
|
||||||
|
albumTitle: string;
|
||||||
|
year: string;
|
||||||
|
genre: string;
|
||||||
|
trackNumber: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EMPTY_FORM: FormState = {
|
||||||
|
title: '',
|
||||||
|
artistName: '',
|
||||||
|
albumTitle: '',
|
||||||
|
year: '',
|
||||||
|
genre: '',
|
||||||
|
trackNumber: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const labelStyle: React.CSSProperties = {
|
||||||
|
display: 'block',
|
||||||
|
fontSize: '0.8125rem',
|
||||||
|
fontWeight: 500,
|
||||||
|
marginBottom: '0.375rem',
|
||||||
|
color: 'var(--color-text-2)',
|
||||||
|
};
|
||||||
|
|
||||||
|
function fieldStyle(): React.CSSProperties {
|
||||||
|
return { width: '100%' };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `/tracks/:trackId/metadata` (single) and `/metadata/batch` (bulk) — A7
|
* `/tracks/:trackId/metadata` — A7 metadata editor: manual edits + AcoustID
|
||||||
* metadata editor with auto-enrichment / diff view. Scaffold only.
|
* match picker with a current-vs-proposed diff. `/metadata/batch` is deferred.
|
||||||
*/
|
*/
|
||||||
export function MetadataEditorPage({ batch = false }: Props) {
|
export function MetadataEditorPage({ batch = false }: Props) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
if (batch) {
|
||||||
|
return <Placeholder title={t('pages.metadataBatch')} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <SingleTrackEditor />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function SingleTrackEditor() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { trackId } = useParams<{ trackId: string }>();
|
||||||
|
|
||||||
|
const trackQuery = useGetTrackQuery(trackId ?? '', { skip: !trackId });
|
||||||
|
const [findMatches, matchesResult] = useLazyGetMetadataMatchesQuery();
|
||||||
|
const [applyMetadata, applyResult] = useApplyMetadataMutation();
|
||||||
|
const [enrichTrack, enrichResult] = useEnrichTrackMutation();
|
||||||
|
|
||||||
|
const [form, setForm] = useState<FormState>(EMPTY_FORM);
|
||||||
|
const [initialized, setInitialized] = useState(false);
|
||||||
|
const [selectedMatch, setSelectedMatch] = useState<MetadataMatch | null>(null);
|
||||||
|
|
||||||
|
// Seed the form from the loaded track exactly once — afterwards it's the
|
||||||
|
// user's edit buffer and shouldn't be clobbered by refetches.
|
||||||
|
useEffect(() => {
|
||||||
|
if (initialized || !trackQuery.data) return;
|
||||||
|
const track = trackQuery.data;
|
||||||
|
setForm({
|
||||||
|
title: track.title,
|
||||||
|
artistName: track.artistName,
|
||||||
|
albumTitle: track.albumTitle,
|
||||||
|
year: track.year != null ? String(track.year) : '',
|
||||||
|
genre: track.genre ?? '',
|
||||||
|
trackNumber: track.trackNumber != null ? String(track.trackNumber) : '',
|
||||||
|
});
|
||||||
|
setInitialized(true);
|
||||||
|
}, [initialized, trackQuery.data]);
|
||||||
|
|
||||||
|
if (!trackId) {
|
||||||
|
return <ErrorState message={t('metadataEditor.error')} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trackQuery.isLoading || !initialized) {
|
||||||
|
return (
|
||||||
|
<div style={{ padding: '1.5rem' }}>
|
||||||
|
<LoadingSkeleton rows={6} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trackQuery.isError || !trackQuery.data) {
|
||||||
|
return (
|
||||||
|
<ErrorState
|
||||||
|
message={t('metadataEditor.error')}
|
||||||
|
onRetry={() => trackQuery.refetch()}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const track = trackQuery.data;
|
||||||
|
|
||||||
|
const updateField = (key: keyof FormState) => (value: string) =>
|
||||||
|
setForm((prev) => ({ ...prev, [key]: value }));
|
||||||
|
|
||||||
|
const applyMatch = (match: MetadataMatch) => {
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
title: match.title ?? prev.title,
|
||||||
|
artistName: match.artist ?? prev.artistName,
|
||||||
|
albumTitle: match.album ?? prev.albumTitle,
|
||||||
|
year: match.year != null ? String(match.year) : prev.year,
|
||||||
|
}));
|
||||||
|
setSelectedMatch(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
await applyMetadata({
|
||||||
|
trackId,
|
||||||
|
edit: {
|
||||||
|
title: form.title.trim() || undefined,
|
||||||
|
artistName: form.artistName.trim() || undefined,
|
||||||
|
albumTitle: form.albumTitle.trim() || undefined,
|
||||||
|
year: form.year.trim() ? Number(form.year) : undefined,
|
||||||
|
genre: form.genre.trim() || undefined,
|
||||||
|
trackNumber: form.trackNumber.trim() ? Number(form.trackNumber) : undefined,
|
||||||
|
},
|
||||||
|
}).unwrap();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Placeholder title={batch ? t('pages.metadataBatch') : t('pages.metadata')} />
|
<div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: '1.25rem 1.5rem',
|
||||||
|
borderBottom: '1px solid var(--color-border)',
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '1rem',
|
||||||
|
flexShrink: 0,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<IconButton
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => navigate(-1)}
|
||||||
|
aria-label={t('common.back')}
|
||||||
|
>
|
||||||
|
←
|
||||||
|
</IconButton>
|
||||||
|
<div style={{ flex: 1, minWidth: 0 }}>
|
||||||
|
<h2 style={{ margin: 0, fontSize: '1.125rem', fontWeight: 700 }}>
|
||||||
|
{t('pages.metadata')}
|
||||||
|
</h2>
|
||||||
|
<p
|
||||||
|
style={{
|
||||||
|
margin: '0.125rem 0 0',
|
||||||
|
fontSize: '0.8125rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{track.artistName} · {track.title}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ScrollArea style={{ flex: 1 }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: '1.5rem',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: '1.25rem',
|
||||||
|
maxWidth: 640,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{applyResult.isSuccess && (
|
||||||
|
<Callout variant="success">{t('metadataEditor.saved')}</Callout>
|
||||||
|
)}
|
||||||
|
{applyResult.isError && (
|
||||||
|
<Callout variant="danger">{t('metadataEditor.saveError')}</Callout>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: '1.25rem',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: '1rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.title')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
value={form.title}
|
||||||
|
onChange={(e) => updateField('title')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.artist')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
value={form.artistName}
|
||||||
|
onChange={(e) => updateField('artistName')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.album')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
value={form.albumTitle}
|
||||||
|
onChange={(e) => updateField('albumTitle')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ display: 'flex', gap: '1rem' }}>
|
||||||
|
<div style={{ flex: 1 }}>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.year')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
type="number"
|
||||||
|
value={form.year}
|
||||||
|
onChange={(e) => updateField('year')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ flex: 1 }}>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.trackNumber')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
type="number"
|
||||||
|
value={form.trackNumber}
|
||||||
|
onChange={(e) => updateField('trackNumber')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label style={labelStyle}>{t('metadataEditor.fields.genre')}</label>
|
||||||
|
<TextField
|
||||||
|
style={fieldStyle()}
|
||||||
|
value={form.genre}
|
||||||
|
onChange={(e) => updateField('genre')(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
|
||||||
|
<Button
|
||||||
|
variant="primary"
|
||||||
|
onClick={() => void handleSave()}
|
||||||
|
disabled={applyResult.isLoading}
|
||||||
|
>
|
||||||
|
{applyResult.isLoading ? <Spinner size="sm" /> : t('metadataEditor.save')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: '1.25rem',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: '1rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
gap: '0.75rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<div style={{ fontWeight: 600, fontSize: '0.9375rem' }}>
|
||||||
|
{t('metadataEditor.autoEnrich.title')}
|
||||||
|
</div>
|
||||||
|
<div style={{ fontSize: '0.8125rem', color: 'var(--color-text-3)' }}>
|
||||||
|
{t('metadataEditor.autoEnrich.hint')}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div style={{ display: 'flex', gap: '0.5rem', flexShrink: 0 }}>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => void enrichTrack(trackId)}
|
||||||
|
disabled={enrichResult.isLoading}
|
||||||
|
>
|
||||||
|
{enrichResult.isLoading ? (
|
||||||
|
<Spinner size="sm" />
|
||||||
|
) : (
|
||||||
|
t('metadataEditor.autoEnrich.reEnrich')
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="primary"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => void findMatches(trackId)}
|
||||||
|
disabled={matchesResult.isFetching}
|
||||||
|
>
|
||||||
|
{matchesResult.isFetching ? (
|
||||||
|
<Spinner size="sm" />
|
||||||
|
) : (
|
||||||
|
t('metadataEditor.autoEnrich.findMatches')
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{enrichResult.isSuccess && (
|
||||||
|
<Callout variant="info">{t('metadataEditor.autoEnrich.enqueued')}</Callout>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{matchesResult.isError && (
|
||||||
|
<Callout variant="danger">{t('metadataEditor.autoEnrich.error')}</Callout>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{matchesResult.isSuccess && matchesResult.data && (
|
||||||
|
matchesResult.data.length === 0 ? (
|
||||||
|
<Callout variant="warning">{t('metadataEditor.autoEnrich.noMatches')}</Callout>
|
||||||
|
) : (
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
|
||||||
|
{matchesResult.data.map((match) => (
|
||||||
|
<MatchRow
|
||||||
|
key={match.acoustid}
|
||||||
|
match={match}
|
||||||
|
onUse={() => setSelectedMatch(match)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
|
||||||
|
{selectedMatch && (
|
||||||
|
<DiffView
|
||||||
|
current={form}
|
||||||
|
proposed={selectedMatch}
|
||||||
|
onApply={() => applyMatch(selectedMatch)}
|
||||||
|
onCancel={() => setSelectedMatch(null)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</ScrollArea>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function MatchRow({ match, onUse }: { match: MetadataMatch; onUse: () => void }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const pct = Math.round(match.score * 100);
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '0.75rem',
|
||||||
|
padding: '0.625rem 0.75rem',
|
||||||
|
border: '1px solid var(--color-border)',
|
||||||
|
borderRadius: 8,
|
||||||
|
background: 'var(--color-surface-1)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Badge variant={pct >= 80 ? 'lime' : pct >= 50 ? 'outline' : 'neutral'}>
|
||||||
|
{pct}%
|
||||||
|
</Badge>
|
||||||
|
<div style={{ flex: 1, minWidth: 0 }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: '0.875rem',
|
||||||
|
fontWeight: 500,
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{match.title ?? t('metadataEditor.matches.unknownTitle')}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: '0.8125rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{[match.artist, match.album, match.year]
|
||||||
|
.filter((v) => v !== undefined && v !== null && v !== '')
|
||||||
|
.join(' · ')}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Button variant="ghost" size="sm" onClick={onUse}>
|
||||||
|
{t('metadataEditor.matches.use')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiffRowDef {
|
||||||
|
key: 'title' | 'artistName' | 'albumTitle' | 'year';
|
||||||
|
label: string;
|
||||||
|
current: string;
|
||||||
|
proposed?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function DiffView({
|
||||||
|
current,
|
||||||
|
proposed,
|
||||||
|
onApply,
|
||||||
|
onCancel,
|
||||||
|
}: {
|
||||||
|
current: FormState;
|
||||||
|
proposed: MetadataMatch;
|
||||||
|
onApply: () => void;
|
||||||
|
onCancel: () => void;
|
||||||
|
}) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const rows: DiffRowDef[] = [
|
||||||
|
{
|
||||||
|
key: 'title',
|
||||||
|
label: t('metadataEditor.fields.title'),
|
||||||
|
current: current.title,
|
||||||
|
proposed: proposed.title,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'artistName',
|
||||||
|
label: t('metadataEditor.fields.artist'),
|
||||||
|
current: current.artistName,
|
||||||
|
proposed: proposed.artist,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'albumTitle',
|
||||||
|
label: t('metadataEditor.fields.album'),
|
||||||
|
current: current.albumTitle,
|
||||||
|
proposed: proposed.album,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'year',
|
||||||
|
label: t('metadataEditor.fields.year'),
|
||||||
|
current: current.year,
|
||||||
|
proposed: proposed.year != null ? String(proposed.year) : undefined,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const changed = rows.filter(
|
||||||
|
(row) => row.proposed !== undefined && row.proposed !== row.current,
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
border: '1px solid var(--color-border)',
|
||||||
|
borderRadius: 8,
|
||||||
|
padding: '0.875rem 1rem',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'column',
|
||||||
|
gap: '0.625rem',
|
||||||
|
background: 'var(--color-surface-1)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div style={{ fontWeight: 600, fontSize: '0.875rem' }}>
|
||||||
|
{t('metadataEditor.diff.title')}
|
||||||
|
</div>
|
||||||
|
{changed.length === 0 ? (
|
||||||
|
<div style={{ fontSize: '0.8125rem', color: 'var(--color-text-3)' }}>
|
||||||
|
{t('metadataEditor.diff.noChanges')}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.375rem' }}>
|
||||||
|
{changed.map((row) => (
|
||||||
|
<div key={row.key} style={{ fontSize: '0.8125rem' }}>
|
||||||
|
<span style={{ color: 'var(--color-text-3)' }}>{row.label}: </span>
|
||||||
|
<span style={{ textDecoration: 'line-through', color: 'var(--color-text-3)' }}>
|
||||||
|
{row.current || '—'}
|
||||||
|
</span>
|
||||||
|
{' → '}
|
||||||
|
<span style={{ color: 'var(--color-accent)', fontWeight: 600 }}>
|
||||||
|
{row.proposed || '—'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'flex-end' }}>
|
||||||
|
<Button variant="ghost" size="sm" onClick={onCancel}>
|
||||||
|
{t('metadataEditor.diff.cancel')}
|
||||||
|
</Button>
|
||||||
|
<Button variant="primary" size="sm" onClick={onApply} disabled={changed.length === 0}>
|
||||||
|
{t('metadataEditor.diff.apply')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useNavigate } from 'react-router';
|
import { useNavigate } from 'react-router';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Badge, Button, Callout, ScrollArea, Spinner } from '@olly/modern-sk';
|
import { Badge, Button, Callout, ScrollArea, Spinner } from '@olly/modern-sk';
|
||||||
@@ -6,6 +6,8 @@ import {
|
|||||||
buildUploadFormData,
|
buildUploadFormData,
|
||||||
useUploadTrackMutation,
|
useUploadTrackMutation,
|
||||||
} from '../../api/endpoints/upload';
|
} from '../../api/endpoints/upload';
|
||||||
|
import { useGetTrackQuery } from '../../api/endpoints/library';
|
||||||
|
import { MetadataStatusBadge } from '../../components/track/MetadataStatusBadge';
|
||||||
|
|
||||||
/** Pure client-side state — this is a transient upload queue, never server data. */
|
/** Pure client-side state — this is a transient upload queue, never server data. */
|
||||||
type ItemStatus = 'queued' | 'uploading' | 'done' | 'duplicate' | 'error';
|
type ItemStatus = 'queued' | 'uploading' | 'done' | 'duplicate' | 'error';
|
||||||
@@ -273,11 +275,7 @@ function UploadRow({
|
|||||||
{item.error}
|
{item.error}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{done && (
|
{done && item.trackId && <EnrichmentStatus trackId={item.trackId} />}
|
||||||
<div style={{ fontSize: '0.75rem', color: 'var(--color-text-3)' }}>
|
|
||||||
{t('upload.unknownArtist')}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<StatusBadge status={item.status} />
|
<StatusBadge status={item.status} />
|
||||||
@@ -301,6 +299,59 @@ function UploadRow({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polls a just-uploaded track until enrichment settles, then shows the outcome.
|
||||||
|
* Metadata enrichment runs asynchronously in a worker after the upload response
|
||||||
|
* returns, so without polling the row would never reflect the resolved title/
|
||||||
|
* artist or a failure reason. Polling stops (interval → 0) once the status
|
||||||
|
* leaves `pending`.
|
||||||
|
*/
|
||||||
|
function EnrichmentStatus({ trackId }: { trackId: string }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [pollMs, setPollMs] = useState(2500);
|
||||||
|
const { data } = useGetTrackQuery(trackId, { pollingInterval: pollMs });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (data && data.metadataStatus !== 'pending') setPollMs(0);
|
||||||
|
}, [data]);
|
||||||
|
|
||||||
|
const status = data?.metadataStatus ?? 'pending';
|
||||||
|
const resolved =
|
||||||
|
data && data.metadataStatus === 'enriched'
|
||||||
|
? `${data.artistName} · ${data.title}`
|
||||||
|
: t(`metadata.statusHint.${status}`);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '0.5rem',
|
||||||
|
marginTop: '0.25rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<MetadataStatusBadge
|
||||||
|
status={status}
|
||||||
|
error={data?.metadataError}
|
||||||
|
hideWhenEnriched={false}
|
||||||
|
/>
|
||||||
|
<span
|
||||||
|
style={{
|
||||||
|
fontSize: '0.75rem',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
textOverflow: 'ellipsis',
|
||||||
|
whiteSpace: 'nowrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{status === 'failed' && data?.metadataError
|
||||||
|
? data.metadataError
|
||||||
|
: resolved}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function StatusBadge({ status }: { status: ItemStatus }) {
|
function StatusBadge({ status }: { status: ItemStatus }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
if (status === 'uploading') {
|
if (status === 'uploading') {
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ import { getApiBaseUrl } from '../config/runtime-config';
|
|||||||
|
|
||||||
type ConnectionStatus = 'connected' | 'connecting' | 'disconnected' | 'error';
|
type ConnectionStatus = 'connected' | 'connecting' | 'disconnected' | 'error';
|
||||||
|
|
||||||
export function useConnectionStatus() {
|
/** Pings `${baseUrl}/health` (defaults to the active instance's base URL). */
|
||||||
|
export function useConnectionStatus(baseUrl?: string) {
|
||||||
|
const url = baseUrl ?? getApiBaseUrl();
|
||||||
const [status, setStatus] = useState<ConnectionStatus>('connecting');
|
const [status, setStatus] = useState<ConnectionStatus>('connecting');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -13,7 +15,7 @@ export function useConnectionStatus() {
|
|||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
setStatus('connecting');
|
setStatus('connecting');
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${getApiBaseUrl()}/health`, {
|
const res = await fetch(`${url}/health`, {
|
||||||
signal: AbortSignal.timeout(5000),
|
signal: AbortSignal.timeout(5000),
|
||||||
});
|
});
|
||||||
if (!cancelled) setStatus(res.ok ? 'connected' : 'error');
|
if (!cancelled) setStatus(res.ok ? 'connected' : 'error');
|
||||||
@@ -30,7 +32,7 @@ export function useConnectionStatus() {
|
|||||||
cancelled = true;
|
cancelled = true;
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [url]);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { skipToken } from '@reduxjs/toolkit/query';
|
||||||
|
import { useGetTrackQuery } from '../api/endpoints/library';
|
||||||
|
import type { QueueEntry } from '../store/slices/queue';
|
||||||
|
|
||||||
|
export interface ResolvedQueueEntry {
|
||||||
|
trackId: string;
|
||||||
|
title: string;
|
||||||
|
artistName: string;
|
||||||
|
albumTitle: string;
|
||||||
|
durationMs: number;
|
||||||
|
hasCover: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge a queue entry's play-time snapshot with the live `Track` cache.
|
||||||
|
*
|
||||||
|
* The queue slice stores denormalized display fields (title/artist/…) captured
|
||||||
|
* when a track was queued, so they go stale after metadata enrichment updates
|
||||||
|
* the track. This reads through to the RTKQ `Track` cache — invalidated by the
|
||||||
|
* same tags that refresh the library — and prefers its fresh values, falling
|
||||||
|
* back to the snapshot for instant render and offline. Returns undefined when
|
||||||
|
* there is no current entry.
|
||||||
|
*/
|
||||||
|
export function useResolvedQueueEntry(
|
||||||
|
entry: QueueEntry | undefined,
|
||||||
|
): ResolvedQueueEntry | undefined {
|
||||||
|
const { data } = useGetTrackQuery(entry?.trackId ?? skipToken);
|
||||||
|
if (!entry) return undefined;
|
||||||
|
return {
|
||||||
|
trackId: entry.trackId,
|
||||||
|
title: data?.title ?? entry.title,
|
||||||
|
artistName: data?.artistName ?? entry.artistName,
|
||||||
|
albumTitle: data?.albumTitle ?? entry.albumTitle,
|
||||||
|
durationMs: data?.durationMs ?? entry.durationMs,
|
||||||
|
hasCover: data?.hasCover ?? false,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useAppSelector } from './useAppDispatch';
|
||||||
|
import { isStreamCached } from '../lib/sw';
|
||||||
|
import { getStreamUrl } from '../api/endpoints/streaming';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the given track is available from the offline audio cache (Tier 3).
|
||||||
|
* Drives the player-bar source indicator (local vs streaming). Returns false
|
||||||
|
* until the service worker is controlling and confirms a hit.
|
||||||
|
*/
|
||||||
|
export function useStreamCached(trackId: string | undefined): boolean {
|
||||||
|
const token = useAppSelector((s) => s.auth.accessToken);
|
||||||
|
const [cached, setCached] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!trackId || !token) {
|
||||||
|
setCached(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let active = true;
|
||||||
|
void isStreamCached(getStreamUrl(trackId, token)).then((hit) => {
|
||||||
|
if (active) setCached(hit);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
active = false;
|
||||||
|
};
|
||||||
|
}, [trackId, token]);
|
||||||
|
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
+126
-11
@@ -17,6 +17,7 @@ const en = {
|
|||||||
disconnected: 'Offline',
|
disconnected: 'Offline',
|
||||||
error: 'Unreachable',
|
error: 'Unreachable',
|
||||||
manage: 'Connection — manage instances',
|
manage: 'Connection — manage instances',
|
||||||
|
cached: 'Showing last-seen data',
|
||||||
},
|
},
|
||||||
user: {
|
user: {
|
||||||
online: 'online',
|
online: 'online',
|
||||||
@@ -24,18 +25,46 @@ const en = {
|
|||||||
signOut: 'Sign out',
|
signOut: 'Sign out',
|
||||||
},
|
},
|
||||||
connect: {
|
connect: {
|
||||||
savedInstances: 'Saved instances',
|
domains: {
|
||||||
active: 'active',
|
title: 'Saved instances',
|
||||||
use: 'Use',
|
addPlaceholder: 'https://your-server.example.com',
|
||||||
forgetTitle: 'Forget this instance',
|
addButton: 'Add instance',
|
||||||
form: {
|
selected: 'Selected',
|
||||||
title: 'Connect to a backend',
|
use: 'Use',
|
||||||
serverUrl: 'Server URL',
|
forgetTitle: 'Remove this instance',
|
||||||
|
},
|
||||||
|
removeDialog: {
|
||||||
|
title: 'Remove cached data?',
|
||||||
|
description:
|
||||||
|
'This removes "{{name}}" from your saved instances and clears its cached data on this device.',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
logout: 'Just log out',
|
||||||
|
removeAndLogout: 'Remove data & log out',
|
||||||
|
},
|
||||||
|
login: {
|
||||||
|
title: 'Log in to {{name}}',
|
||||||
|
registerTitle: 'Sign up for {{name}}',
|
||||||
username: 'Username',
|
username: 'Username',
|
||||||
password: 'Password',
|
password: 'Password',
|
||||||
submit: 'Connect',
|
passwordHint: 'At least 8 characters.',
|
||||||
stubNote:
|
submit: 'Log in',
|
||||||
'Stub mode — backend not wired. Connect signs in with a fake admin session, scoped to this instance.',
|
submitting: 'Logging in…',
|
||||||
|
registerSubmit: 'Sign up',
|
||||||
|
registering: 'Signing up…',
|
||||||
|
noAccount: "Don't have an account?",
|
||||||
|
registerLink: 'Sign up',
|
||||||
|
haveAccount: 'Already have an account?',
|
||||||
|
signInLink: 'Log in',
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
unreachable:
|
||||||
|
"Can't reach this server. Check the URL and that it's online.",
|
||||||
|
badCredentials: 'Incorrect username or password.',
|
||||||
|
generic: 'Sign-in failed. Please try again.',
|
||||||
|
usernameTaken: 'That username is already taken.',
|
||||||
|
passwordTooShort: 'Password must be at least 8 characters.',
|
||||||
|
registrationDisabled: 'Registration is disabled on this server.',
|
||||||
|
registerFailed: 'Could not create the account. Please try again.',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
library: {
|
library: {
|
||||||
@@ -127,12 +156,49 @@ const en = {
|
|||||||
playNow: 'Play now',
|
playNow: 'Play now',
|
||||||
playNext: 'Play next',
|
playNext: 'Play next',
|
||||||
addToQueue: 'Add to queue',
|
addToQueue: 'Add to queue',
|
||||||
|
info: 'Track info',
|
||||||
addToPlaylist: 'Add to playlist…',
|
addToPlaylist: 'Add to playlist…',
|
||||||
editMetadata: 'Edit metadata',
|
editMetadata: 'Edit metadata',
|
||||||
download: 'Download',
|
download: 'Download',
|
||||||
delete: 'Delete',
|
delete: 'Delete',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
trackInfo: {
|
||||||
|
title: 'Track info',
|
||||||
|
open: 'View track info',
|
||||||
|
close: 'Close',
|
||||||
|
notFound: 'Track not found',
|
||||||
|
play: 'Play',
|
||||||
|
addToQueue: 'Queue',
|
||||||
|
editMetadata: 'Edit metadata',
|
||||||
|
liked: 'Liked',
|
||||||
|
trackOf: 'No. {{n}} of {{total}}',
|
||||||
|
kbps: '{{n}} kbps',
|
||||||
|
sections: {
|
||||||
|
status: 'Status',
|
||||||
|
general: 'General',
|
||||||
|
file: 'File',
|
||||||
|
identifiers: 'Identifiers',
|
||||||
|
},
|
||||||
|
fields: {
|
||||||
|
artist: 'Artist',
|
||||||
|
album: 'Album',
|
||||||
|
trackNumber: 'Track',
|
||||||
|
disc: 'Disc',
|
||||||
|
year: 'Year',
|
||||||
|
genre: 'Genre',
|
||||||
|
duration: 'Duration',
|
||||||
|
format: 'Format',
|
||||||
|
bitrate: 'Bitrate',
|
||||||
|
size: 'Size',
|
||||||
|
source: 'Source',
|
||||||
|
added: 'Added',
|
||||||
|
enriched: 'Enriched',
|
||||||
|
trackId: 'Track ID',
|
||||||
|
albumId: 'Album ID',
|
||||||
|
artistId: 'Artist ID',
|
||||||
|
},
|
||||||
|
},
|
||||||
common: {
|
common: {
|
||||||
error: 'Something went wrong',
|
error: 'Something went wrong',
|
||||||
retry: 'Retry',
|
retry: 'Retry',
|
||||||
@@ -201,11 +267,60 @@ const en = {
|
|||||||
error: 'Failed',
|
error: 'Failed',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
metadata: {
|
||||||
|
status: {
|
||||||
|
pending: 'Enriching…',
|
||||||
|
enriched: 'Enriched',
|
||||||
|
failed: 'No match',
|
||||||
|
manual: 'Manual',
|
||||||
|
},
|
||||||
|
statusHint: {
|
||||||
|
pending: 'Identifying metadata…',
|
||||||
|
enriched: 'Metadata identified',
|
||||||
|
failed: 'Metadata could not be identified',
|
||||||
|
manual: 'Edited manually — not auto-updated',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
metadataEditor: {
|
||||||
|
error: 'Failed to load track',
|
||||||
|
saved: 'Metadata saved.',
|
||||||
|
saveError: 'Failed to save metadata.',
|
||||||
|
save: 'Save',
|
||||||
|
fields: {
|
||||||
|
title: 'Title',
|
||||||
|
artist: 'Artist',
|
||||||
|
album: 'Album',
|
||||||
|
year: 'Year',
|
||||||
|
genre: 'Genre',
|
||||||
|
trackNumber: 'Track number',
|
||||||
|
},
|
||||||
|
autoEnrich: {
|
||||||
|
title: 'AcoustID lookup',
|
||||||
|
hint: 'Identify this track by audio fingerprint.',
|
||||||
|
findMatches: 'Find matches',
|
||||||
|
reEnrich: 'Re-run enrichment',
|
||||||
|
enqueued: 'Enrichment queued — refresh in a moment.',
|
||||||
|
error: 'Could not look up matches.',
|
||||||
|
noMatches: 'No matches found.',
|
||||||
|
},
|
||||||
|
matches: {
|
||||||
|
use: 'Use',
|
||||||
|
unknownTitle: 'Unknown title',
|
||||||
|
},
|
||||||
|
diff: {
|
||||||
|
title: 'Apply this match?',
|
||||||
|
noChanges: 'No changes from current values.',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
apply: 'Apply',
|
||||||
|
},
|
||||||
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export default en;
|
export default en;
|
||||||
|
|
||||||
type DeepString<T> = {
|
type DeepString<T> = {
|
||||||
[K in keyof T]: T[K] extends Record<string, unknown> ? DeepString<T[K]> : string;
|
[K in keyof T]: T[K] extends Record<string, unknown>
|
||||||
|
? DeepString<T[K]>
|
||||||
|
: string;
|
||||||
};
|
};
|
||||||
export type Translations = DeepString<typeof en>;
|
export type Translations = DeepString<typeof en>;
|
||||||
|
|||||||
+123
-10
@@ -19,6 +19,7 @@ const ru: Translations = {
|
|||||||
disconnected: 'Нет связи',
|
disconnected: 'Нет связи',
|
||||||
error: 'Недоступно',
|
error: 'Недоступно',
|
||||||
manage: 'Соединение — управление экземплярами',
|
manage: 'Соединение — управление экземплярами',
|
||||||
|
cached: 'Показаны последние данные',
|
||||||
},
|
},
|
||||||
user: {
|
user: {
|
||||||
online: 'онлайн',
|
online: 'онлайн',
|
||||||
@@ -26,18 +27,46 @@ const ru: Translations = {
|
|||||||
signOut: 'Выйти',
|
signOut: 'Выйти',
|
||||||
},
|
},
|
||||||
connect: {
|
connect: {
|
||||||
savedInstances: 'Сохранённые серверы',
|
domains: {
|
||||||
active: 'активный',
|
title: 'Сохранённые серверы',
|
||||||
use: 'Выбрать',
|
addPlaceholder: 'https://your-server.example.com',
|
||||||
forgetTitle: 'Забыть этот сервер',
|
addButton: 'Добавить сервер',
|
||||||
form: {
|
selected: 'Выбран',
|
||||||
title: 'Подключиться к серверу',
|
use: 'Выбрать',
|
||||||
serverUrl: 'URL сервера',
|
forgetTitle: 'Удалить этот сервер',
|
||||||
|
},
|
||||||
|
removeDialog: {
|
||||||
|
title: 'Удалить локальные данные?',
|
||||||
|
description:
|
||||||
|
'Сервер «{{name}}» будет удалён из сохранённых, а его данные на этом устройстве — очищены.',
|
||||||
|
cancel: 'Отмена',
|
||||||
|
logout: 'Просто выйти',
|
||||||
|
removeAndLogout: 'Удалить данные и выйти',
|
||||||
|
},
|
||||||
|
login: {
|
||||||
|
title: 'Вход в {{name}}',
|
||||||
|
registerTitle: 'Регистрация на {{name}}',
|
||||||
username: 'Имя пользователя',
|
username: 'Имя пользователя',
|
||||||
password: 'Пароль',
|
password: 'Пароль',
|
||||||
submit: 'Подключиться',
|
passwordHint: 'Не менее 8 символов.',
|
||||||
stubNote:
|
submit: 'Войти',
|
||||||
'Режим заглушки — сервер не подключён. Создаётся фиктивная сессия администратора для этого экземпляра.',
|
submitting: 'Вход…',
|
||||||
|
registerSubmit: 'Зарегистрироваться',
|
||||||
|
registering: 'Регистрация…',
|
||||||
|
noAccount: 'Нет аккаунта?',
|
||||||
|
registerLink: 'Зарегистрироваться',
|
||||||
|
haveAccount: 'Уже есть аккаунт?',
|
||||||
|
signInLink: 'Войти',
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
unreachable:
|
||||||
|
'Не удаётся подключиться к серверу. Проверьте URL и доступность.',
|
||||||
|
badCredentials: 'Неверное имя пользователя или пароль.',
|
||||||
|
generic: 'Не удалось войти. Попробуйте ещё раз.',
|
||||||
|
usernameTaken: 'Это имя пользователя уже занято.',
|
||||||
|
passwordTooShort: 'Пароль должен содержать не менее 8 символов.',
|
||||||
|
registrationDisabled: 'Регистрация на этом сервере отключена.',
|
||||||
|
registerFailed: 'Не удалось создать аккаунт. Попробуйте ещё раз.',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
library: {
|
library: {
|
||||||
@@ -129,12 +158,49 @@ const ru: Translations = {
|
|||||||
playNow: 'Играть сейчас',
|
playNow: 'Играть сейчас',
|
||||||
playNext: 'Следующим',
|
playNext: 'Следующим',
|
||||||
addToQueue: 'Добавить в очередь',
|
addToQueue: 'Добавить в очередь',
|
||||||
|
info: 'Информация о треке',
|
||||||
addToPlaylist: 'Добавить в плейлист…',
|
addToPlaylist: 'Добавить в плейлист…',
|
||||||
editMetadata: 'Редактировать метаданные',
|
editMetadata: 'Редактировать метаданные',
|
||||||
download: 'Скачать',
|
download: 'Скачать',
|
||||||
delete: 'Удалить',
|
delete: 'Удалить',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
trackInfo: {
|
||||||
|
title: 'О треке',
|
||||||
|
open: 'Информация о треке',
|
||||||
|
close: 'Закрыть',
|
||||||
|
notFound: 'Трек не найден',
|
||||||
|
play: 'Играть',
|
||||||
|
addToQueue: 'В очередь',
|
||||||
|
editMetadata: 'Метаданные',
|
||||||
|
liked: 'В избранном',
|
||||||
|
trackOf: '№ {{n}} из {{total}}',
|
||||||
|
kbps: '{{n}} кбит/с',
|
||||||
|
sections: {
|
||||||
|
status: 'Статус',
|
||||||
|
general: 'Основное',
|
||||||
|
file: 'Файл',
|
||||||
|
identifiers: 'Идентификаторы',
|
||||||
|
},
|
||||||
|
fields: {
|
||||||
|
artist: 'Исполнитель',
|
||||||
|
album: 'Альбом',
|
||||||
|
trackNumber: 'Трек',
|
||||||
|
disc: 'Диск',
|
||||||
|
year: 'Год',
|
||||||
|
genre: 'Жанр',
|
||||||
|
duration: 'Длительность',
|
||||||
|
format: 'Формат',
|
||||||
|
bitrate: 'Битрейт',
|
||||||
|
size: 'Размер',
|
||||||
|
source: 'Источник',
|
||||||
|
added: 'Добавлен',
|
||||||
|
enriched: 'Обогащён',
|
||||||
|
trackId: 'ID трека',
|
||||||
|
albumId: 'ID альбома',
|
||||||
|
artistId: 'ID исполнителя',
|
||||||
|
},
|
||||||
|
},
|
||||||
common: {
|
common: {
|
||||||
error: 'Что-то пошло не так',
|
error: 'Что-то пошло не так',
|
||||||
retry: 'Повторить',
|
retry: 'Повторить',
|
||||||
@@ -203,6 +269,53 @@ const ru: Translations = {
|
|||||||
error: 'Ошибка',
|
error: 'Ошибка',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
metadata: {
|
||||||
|
status: {
|
||||||
|
pending: 'Обработка…',
|
||||||
|
enriched: 'Готово',
|
||||||
|
failed: 'Нет совпадения',
|
||||||
|
manual: 'Вручную',
|
||||||
|
},
|
||||||
|
statusHint: {
|
||||||
|
pending: 'Определяем метаданные…',
|
||||||
|
enriched: 'Метаданные определены',
|
||||||
|
failed: 'Не удалось определить метаданные',
|
||||||
|
manual: 'Изменено вручную — не обновляется автоматически',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
metadataEditor: {
|
||||||
|
error: 'Не удалось загрузить трек',
|
||||||
|
saved: 'Метаданные сохранены.',
|
||||||
|
saveError: 'Не удалось сохранить метаданные.',
|
||||||
|
save: 'Сохранить',
|
||||||
|
fields: {
|
||||||
|
title: 'Название',
|
||||||
|
artist: 'Исполнитель',
|
||||||
|
album: 'Альбом',
|
||||||
|
year: 'Год',
|
||||||
|
genre: 'Жанр',
|
||||||
|
trackNumber: 'Номер трека',
|
||||||
|
},
|
||||||
|
autoEnrich: {
|
||||||
|
title: 'Поиск по AcoustID',
|
||||||
|
hint: 'Определить трек по аудио-отпечатку.',
|
||||||
|
findMatches: 'Найти совпадения',
|
||||||
|
reEnrich: 'Повторить обогащение',
|
||||||
|
enqueued: 'Обогащение запущено — обновите через момент.',
|
||||||
|
error: 'Не удалось найти совпадения.',
|
||||||
|
noMatches: 'Совпадений не найдено.',
|
||||||
|
},
|
||||||
|
matches: {
|
||||||
|
use: 'Использовать',
|
||||||
|
unknownTitle: 'Неизвестное название',
|
||||||
|
},
|
||||||
|
diff: {
|
||||||
|
title: 'Применить это совпадение?',
|
||||||
|
noChanges: 'Нет изменений относительно текущих значений.',
|
||||||
|
cancel: 'Отмена',
|
||||||
|
apply: 'Применить',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ru;
|
export default ru;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { BrowserRouter } from 'react-router';
|
|||||||
import { ThemeProvider, TooltipProvider } from '@olly/modern-sk';
|
import { ThemeProvider, TooltipProvider } from '@olly/modern-sk';
|
||||||
import { store } from './store';
|
import { store } from './store';
|
||||||
import { AppRoutes } from './routes';
|
import { AppRoutes } from './routes';
|
||||||
|
import { registerServiceWorker } from './lib/sw';
|
||||||
|
|
||||||
// Import all endpoint injections to ensure they are registered
|
// Import all endpoint injections to ensure they are registered
|
||||||
import './api/endpoints/auth';
|
import './api/endpoints/auth';
|
||||||
@@ -21,6 +22,10 @@ import './api/endpoints/storage';
|
|||||||
import './api/endpoints/admin';
|
import './api/endpoints/admin';
|
||||||
import './api/endpoints/upload';
|
import './api/endpoints/upload';
|
||||||
|
|
||||||
|
// Tier 3 offline: register the audio-caching service worker (no-op if the
|
||||||
|
// browser/origin doesn't support it).
|
||||||
|
registerServiceWorker();
|
||||||
|
|
||||||
const rootEl = document.getElementById('root');
|
const rootEl = document.getElementById('root');
|
||||||
if (rootEl) {
|
if (rootEl) {
|
||||||
// grained black-ish background + base text color from modern-sk
|
// grained black-ish background + base text color from modern-sk
|
||||||
|
|||||||
@@ -16,6 +16,16 @@ export function formatFileSize(bytes: number): string {
|
|||||||
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatDateTime(iso: string | undefined): string | undefined {
|
||||||
|
if (!iso) return undefined;
|
||||||
|
const d = new Date(iso);
|
||||||
|
if (Number.isNaN(d.getTime())) return undefined;
|
||||||
|
return new Intl.DateTimeFormat(undefined, {
|
||||||
|
dateStyle: 'medium',
|
||||||
|
timeStyle: 'short',
|
||||||
|
}).format(d);
|
||||||
|
}
|
||||||
|
|
||||||
export function formatCount(n: number): string {
|
export function formatCount(n: number): string {
|
||||||
if (n < 1000) return String(n);
|
if (n < 1000) return String(n);
|
||||||
if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`;
|
if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`;
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Service-worker client: registration + a typed bridge to the audio offline
|
||||||
|
* cache (Tier 3). The SW itself lives in `public/sw.js`; this is the app side.
|
||||||
|
*
|
||||||
|
* Messaging uses a MessageChannel — we hand the SW a port and await its reply —
|
||||||
|
* so each call resolves with that request's result rather than a global event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface AudioCacheStats {
|
||||||
|
count: number;
|
||||||
|
bytes: number;
|
||||||
|
maxBytes: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Register the service worker. No-op when unsupported (e.g. plain http host). */
|
||||||
|
export function registerServiceWorker(): void {
|
||||||
|
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.addEventListener('load', () => {
|
||||||
|
// Module worker: sw.js uses ES imports (see public/sw.js + sw-core.js).
|
||||||
|
navigator.serviceWorker.register('/sw.js', { type: 'module' }).catch(() => {
|
||||||
|
/* SW unavailable (insecure origin, blocked, …) — app still works online */
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function controller(): ServiceWorker | null {
|
||||||
|
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return navigator.serviceWorker.controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Round-trip a message to the SW; rejects if no controlling SW is present. */
|
||||||
|
function send<T>(message: Record<string, unknown>): Promise<T> {
|
||||||
|
const sw = controller();
|
||||||
|
if (!sw) return Promise.reject(new Error('no-service-worker'));
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
const channel = new MessageChannel();
|
||||||
|
channel.port1.onmessage = (event) => resolve(event.data as T);
|
||||||
|
try {
|
||||||
|
sw.postMessage(message, [channel.port2]);
|
||||||
|
} catch (err) {
|
||||||
|
reject(err as Error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Total size + count of cached audio, or null when the SW isn't controlling. */
|
||||||
|
export async function getAudioCacheStats(): Promise<AudioCacheStats | null> {
|
||||||
|
try {
|
||||||
|
return await send<AudioCacheStats>({ type: 'STATS' });
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether a given stream URL is already cached for offline playback. */
|
||||||
|
export async function isStreamCached(streamUrl: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const { cached } = await send<{ cached: boolean }>({
|
||||||
|
type: 'HAS',
|
||||||
|
url: streamUrl,
|
||||||
|
});
|
||||||
|
return cached;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Drop the entire audio cache. Resolves false if the SW isn't controlling. */
|
||||||
|
export async function clearAudioCache(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const { ok } = await send<{ ok: boolean }>({ type: 'CLEAR' });
|
||||||
|
return ok;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ import authReducer from './slices/auth';
|
|||||||
import playerReducer from './slices/player';
|
import playerReducer from './slices/player';
|
||||||
import queueReducer from './slices/queue';
|
import queueReducer from './slices/queue';
|
||||||
import uiReducer from './slices/ui';
|
import uiReducer from './slices/ui';
|
||||||
|
import { loadPlayerState, loadQueueState, startPersistence } from './persist';
|
||||||
|
import { rehydrateApiCache, startApiPersistence } from './rtkqPersist';
|
||||||
|
|
||||||
export const store = configureStore({
|
export const store = configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
@@ -13,9 +15,23 @@ export const store = configureStore({
|
|||||||
queue: queueReducer,
|
queue: queueReducer,
|
||||||
ui: uiReducer,
|
ui: uiReducer,
|
||||||
},
|
},
|
||||||
|
// Tier 1 offline: rehydrate queue/player from the active backend's namespace
|
||||||
|
// so a reload (even with no backend) restores exactly where the user left off.
|
||||||
|
preloadedState: {
|
||||||
|
queue: loadQueueState(),
|
||||||
|
player: loadPlayerState(),
|
||||||
|
},
|
||||||
middleware: (getDefaultMiddleware) =>
|
middleware: (getDefaultMiddleware) =>
|
||||||
getDefaultMiddleware().concat(api.middleware),
|
getDefaultMiddleware().concat(api.middleware),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Flush queue/player changes back to localStorage (throttled).
|
||||||
|
startPersistence(store);
|
||||||
|
|
||||||
|
// Tier 2 offline: replay the last-seen RTKQ cache, then keep snapshotting it.
|
||||||
|
// Rehydrate first so cached server data is present before any component mounts.
|
||||||
|
rehydrateApiCache(store.dispatch);
|
||||||
|
startApiPersistence(store);
|
||||||
|
|
||||||
export type RootState = ReturnType<typeof store.getState>;
|
export type RootState = ReturnType<typeof store.getState>;
|
||||||
export type AppDispatch = typeof store.dispatch;
|
export type AppDispatch = typeof store.dispatch;
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* Tier 1 offline support: persist client state (queue + player) to the active
|
||||||
|
* backend's localStorage namespace, mirroring the auth slice. This is what lets
|
||||||
|
* the UI come back exactly as the user left it after a reload with no backend
|
||||||
|
* reachable — no server data is duplicated (the queue stores track IDs + minimal
|
||||||
|
* display fields only; full track records still live in the RTKQ cache).
|
||||||
|
*
|
||||||
|
* Server data (library/albums/…) is Tier 2 (see `rtkqPersist.ts`).
|
||||||
|
*/
|
||||||
|
import { instanceStorage } from '../config/instances';
|
||||||
|
import {
|
||||||
|
queueInitialState,
|
||||||
|
type QueueState,
|
||||||
|
} from './slices/queue';
|
||||||
|
import {
|
||||||
|
playerInitialState,
|
||||||
|
type PlayerState,
|
||||||
|
} from './slices/player';
|
||||||
|
import type { RootState } from './index';
|
||||||
|
|
||||||
|
const QUEUE_KEY = 'queue';
|
||||||
|
const PLAYER_KEY = 'player';
|
||||||
|
|
||||||
|
// Only persist fields that make sense to restore. `duration`/`isPlaying` are
|
||||||
|
// derived from the <audio> element on next load, and the panel toggles are
|
||||||
|
// transient UI, so they are intentionally left out.
|
||||||
|
type PersistedQueue = Pick<
|
||||||
|
QueueState,
|
||||||
|
'entries' | 'currentIndex' | 'source' | 'sourceId' | 'sourceName'
|
||||||
|
>;
|
||||||
|
type PersistedPlayer = Pick<
|
||||||
|
PlayerState,
|
||||||
|
'currentTrackId' | 'position' | 'volume' | 'muted' | 'repeat' | 'shuffle'
|
||||||
|
>;
|
||||||
|
|
||||||
|
function pickQueue(state: QueueState): PersistedQueue {
|
||||||
|
return {
|
||||||
|
entries: state.entries,
|
||||||
|
currentIndex: state.currentIndex,
|
||||||
|
source: state.source,
|
||||||
|
sourceId: state.sourceId,
|
||||||
|
sourceName: state.sourceName,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function pickPlayer(state: PlayerState): PersistedPlayer {
|
||||||
|
return {
|
||||||
|
currentTrackId: state.currentTrackId,
|
||||||
|
position: state.position,
|
||||||
|
volume: state.volume,
|
||||||
|
muted: state.muted,
|
||||||
|
repeat: state.repeat,
|
||||||
|
shuffle: state.shuffle,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function read<T>(key: string): Partial<T> | null {
|
||||||
|
try {
|
||||||
|
const raw = instanceStorage.get(key);
|
||||||
|
return raw ? (JSON.parse(raw) as Partial<T>) : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build the queue slice's initial state, restoring any persisted queue. */
|
||||||
|
export function loadQueueState(): QueueState {
|
||||||
|
const persisted = read<PersistedQueue>(QUEUE_KEY);
|
||||||
|
if (!persisted) return queueInitialState;
|
||||||
|
const merged: QueueState = { ...queueInitialState, ...persisted };
|
||||||
|
// Guard the index against a corrupted/short entries array.
|
||||||
|
if (
|
||||||
|
merged.currentIndex >= merged.entries.length ||
|
||||||
|
merged.currentIndex < -1
|
||||||
|
) {
|
||||||
|
merged.currentIndex = merged.entries.length ? 0 : -1;
|
||||||
|
}
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build the player slice's initial state, restoring any persisted player. */
|
||||||
|
export function loadPlayerState(): PlayerState {
|
||||||
|
const persisted = read<PersistedPlayer>(PLAYER_KEY);
|
||||||
|
if (!persisted) return playerInitialState;
|
||||||
|
// Never auto-resume playback on load: browsers block autoplay and the
|
||||||
|
// <audio> element starts paused regardless. isPlaying stays false.
|
||||||
|
return { ...playerInitialState, ...persisted, isPlaying: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe a store so queue/player changes are flushed to localStorage. The
|
||||||
|
* write is throttled because `setPosition` fires several times a second during
|
||||||
|
* playback — without throttling we'd hammer localStorage on every tick.
|
||||||
|
*/
|
||||||
|
export function startPersistence(store: {
|
||||||
|
getState: () => RootState;
|
||||||
|
subscribe: (listener: () => void) => () => void;
|
||||||
|
}): () => void {
|
||||||
|
const initial = store.getState();
|
||||||
|
let lastQueue = JSON.stringify(pickQueue(initial.queue));
|
||||||
|
let lastPlayer = JSON.stringify(pickPlayer(initial.player));
|
||||||
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
|
const flush = () => {
|
||||||
|
timer = null;
|
||||||
|
const state = store.getState();
|
||||||
|
const queueSnapshot = JSON.stringify(pickQueue(state.queue));
|
||||||
|
if (queueSnapshot !== lastQueue) {
|
||||||
|
instanceStorage.set(QUEUE_KEY, queueSnapshot);
|
||||||
|
lastQueue = queueSnapshot;
|
||||||
|
}
|
||||||
|
const playerSnapshot = JSON.stringify(pickPlayer(state.player));
|
||||||
|
if (playerSnapshot !== lastPlayer) {
|
||||||
|
instanceStorage.set(PLAYER_KEY, playerSnapshot);
|
||||||
|
lastPlayer = playerSnapshot;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return store.subscribe(() => {
|
||||||
|
if (timer) return;
|
||||||
|
timer = setTimeout(flush, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Tier 2 offline support: persist the RTK Query cache (last-seen server data —
|
||||||
|
* library/albums/artists/…) to the active backend's localStorage namespace and
|
||||||
|
* replay it into the cache on startup. With the backend down, components keep
|
||||||
|
* rendering the last-known data read-only instead of an error state.
|
||||||
|
*
|
||||||
|
* Per the architecture invariant, server data is NOT copied into a slice: this
|
||||||
|
* snapshots the RTKQ cache itself and feeds it back through RTKQ's own
|
||||||
|
* `extractRehydrationInfo` mechanism (see `api/index.ts`).
|
||||||
|
*/
|
||||||
|
import { instanceStorage } from '../config/instances';
|
||||||
|
import { rehydrateApi, type RehydrateApiPayload } from '../api/rehydrate';
|
||||||
|
import type { RootState } from './index';
|
||||||
|
|
||||||
|
const CACHE_KEY = 'rtkq';
|
||||||
|
|
||||||
|
type ApiState = RootState['api'];
|
||||||
|
type QueryEntry = ApiState['queries'][string];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep only successfully-fulfilled query results — pending/rejected entries
|
||||||
|
* carry no usable data and subscriptions are rebuilt by components on mount.
|
||||||
|
* Mutation results are never restored.
|
||||||
|
*/
|
||||||
|
const EMPTY_PROVIDED = { tags: {}, keys: {} };
|
||||||
|
|
||||||
|
function snapshot(apiState: ApiState): RehydrateApiPayload {
|
||||||
|
const queries: Record<string, unknown> = {};
|
||||||
|
for (const [key, entry] of Object.entries(apiState.queries)) {
|
||||||
|
const q = entry as QueryEntry | undefined;
|
||||||
|
if (q && q.status === 'fulfilled') queries[key] = q;
|
||||||
|
}
|
||||||
|
// Carry `provided` along so RTKQ can re-register invalidation tags for the
|
||||||
|
// restored entries; it is also required structurally (see RehydrateApiPayload).
|
||||||
|
return { queries, mutations: {}, provided: apiState.provided ?? EMPTY_PROVIDED };
|
||||||
|
}
|
||||||
|
|
||||||
|
function load(): RehydrateApiPayload | null {
|
||||||
|
try {
|
||||||
|
const raw = instanceStorage.get(CACHE_KEY);
|
||||||
|
if (!raw) return null;
|
||||||
|
const parsed = JSON.parse(raw) as Partial<RehydrateApiPayload>;
|
||||||
|
if (!parsed.queries) return null;
|
||||||
|
// `provided` may be absent in snapshots written before this field existed —
|
||||||
|
// default it so the invalidation slice doesn't crash on `provided.tags`.
|
||||||
|
return {
|
||||||
|
queries: parsed.queries,
|
||||||
|
mutations: {},
|
||||||
|
provided: parsed.provided ?? EMPTY_PROVIDED,
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Replay the persisted cache into RTKQ. Call once after the store is created. */
|
||||||
|
export function rehydrateApiCache(dispatch: (action: unknown) => void): void {
|
||||||
|
const cached = load();
|
||||||
|
if (cached) dispatch(rehydrateApi(cached));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe a store so the RTKQ cache is flushed to localStorage. Throttled,
|
||||||
|
* since cache state churns on every in-flight query transition.
|
||||||
|
*/
|
||||||
|
export function startApiPersistence(store: {
|
||||||
|
getState: () => RootState;
|
||||||
|
subscribe: (listener: () => void) => () => void;
|
||||||
|
}): () => void {
|
||||||
|
let last = '';
|
||||||
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
|
const flush = () => {
|
||||||
|
timer = null;
|
||||||
|
const snap = JSON.stringify(snapshot(store.getState().api));
|
||||||
|
if (snap !== last) {
|
||||||
|
instanceStorage.set(CACHE_KEY, snap);
|
||||||
|
last = snap;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return store.subscribe(() => {
|
||||||
|
if (timer) return;
|
||||||
|
timer = setTimeout(flush, 2000);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -38,12 +38,16 @@ export const authSlice = createSlice({
|
|||||||
action: PayloadAction<{
|
action: PayloadAction<{
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
expiresIn: number;
|
expiresIn?: number;
|
||||||
}>,
|
}>,
|
||||||
) {
|
) {
|
||||||
state.accessToken = action.payload.accessToken;
|
state.accessToken = action.payload.accessToken;
|
||||||
state.refreshToken = action.payload.refreshToken;
|
state.refreshToken = action.payload.refreshToken;
|
||||||
state.expiresAt = Date.now() + action.payload.expiresIn * 1000;
|
// Backends that omit a TTL leave expiresAt null — reauth is 401-driven.
|
||||||
|
state.expiresAt =
|
||||||
|
action.payload.expiresIn != null
|
||||||
|
? Date.now() + action.payload.expiresIn * 1000
|
||||||
|
: null;
|
||||||
persistAuth(state);
|
persistAuth(state);
|
||||||
},
|
},
|
||||||
setUser(state, action: PayloadAction<User>) {
|
setUser(state, action: PayloadAction<User>) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
type RepeatMode = 'none' | 'one' | 'all';
|
export type RepeatMode = 'none' | 'one' | 'all';
|
||||||
|
|
||||||
interface PlayerState {
|
export interface PlayerState {
|
||||||
currentTrackId: string | null;
|
currentTrackId: string | null;
|
||||||
isPlaying: boolean;
|
isPlaying: boolean;
|
||||||
position: number;
|
position: number;
|
||||||
@@ -11,11 +11,10 @@ interface PlayerState {
|
|||||||
muted: boolean;
|
muted: boolean;
|
||||||
repeat: RepeatMode;
|
repeat: RepeatMode;
|
||||||
shuffle: boolean;
|
shuffle: boolean;
|
||||||
isNowPlayingOpen: boolean;
|
|
||||||
isQueueOpen: boolean;
|
isQueueOpen: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: PlayerState = {
|
export const playerInitialState: PlayerState = {
|
||||||
currentTrackId: null,
|
currentTrackId: null,
|
||||||
isPlaying: false,
|
isPlaying: false,
|
||||||
position: 0,
|
position: 0,
|
||||||
@@ -24,15 +23,12 @@ const initialState: PlayerState = {
|
|||||||
muted: false,
|
muted: false,
|
||||||
repeat: 'none',
|
repeat: 'none',
|
||||||
shuffle: false,
|
shuffle: false,
|
||||||
isNowPlayingOpen: false,
|
isQueueOpen: false,
|
||||||
// STUB: open by default so the queue drawer look is visible before a backend
|
|
||||||
// exists (pairs with DEMO_QUEUE). Default to false once real playback lands.
|
|
||||||
isQueueOpen: true,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const playerSlice = createSlice({
|
export const playerSlice = createSlice({
|
||||||
name: 'player',
|
name: 'player',
|
||||||
initialState,
|
initialState: playerInitialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
play(state, action: PayloadAction<string>) {
|
play(state, action: PayloadAction<string>) {
|
||||||
state.currentTrackId = action.payload;
|
state.currentTrackId = action.payload;
|
||||||
@@ -68,9 +64,6 @@ export const playerSlice = createSlice({
|
|||||||
toggleShuffle(state) {
|
toggleShuffle(state) {
|
||||||
state.shuffle = !state.shuffle;
|
state.shuffle = !state.shuffle;
|
||||||
},
|
},
|
||||||
toggleNowPlaying(state) {
|
|
||||||
state.isNowPlayingOpen = !state.isNowPlayingOpen;
|
|
||||||
},
|
|
||||||
toggleQueue(state) {
|
toggleQueue(state) {
|
||||||
state.isQueueOpen = !state.isQueueOpen;
|
state.isQueueOpen = !state.isQueueOpen;
|
||||||
},
|
},
|
||||||
@@ -88,7 +81,6 @@ export const {
|
|||||||
toggleMute,
|
toggleMute,
|
||||||
setRepeat,
|
setRepeat,
|
||||||
toggleShuffle,
|
toggleShuffle,
|
||||||
toggleNowPlaying,
|
|
||||||
toggleQueue,
|
toggleQueue,
|
||||||
} = playerSlice.actions;
|
} = playerSlice.actions;
|
||||||
export default playerSlice.reducer;
|
export default playerSlice.reducer;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
type QueueSource =
|
export type QueueSource =
|
||||||
| 'manual'
|
| 'manual'
|
||||||
| 'album'
|
| 'album'
|
||||||
| 'playlist'
|
| 'playlist'
|
||||||
@@ -8,7 +8,7 @@ type QueueSource =
|
|||||||
| 'search'
|
| 'search'
|
||||||
| 'radio';
|
| 'radio';
|
||||||
|
|
||||||
interface QueueEntry {
|
export interface QueueEntry {
|
||||||
trackId: string;
|
trackId: string;
|
||||||
title: string;
|
title: string;
|
||||||
artistName: string;
|
artistName: string;
|
||||||
@@ -17,7 +17,7 @@ interface QueueEntry {
|
|||||||
albumArtUrl?: string;
|
albumArtUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueueState {
|
export interface QueueState {
|
||||||
entries: QueueEntry[];
|
entries: QueueEntry[];
|
||||||
currentIndex: number;
|
currentIndex: number;
|
||||||
source: QueueSource;
|
source: QueueSource;
|
||||||
@@ -25,52 +25,17 @@ interface QueueState {
|
|||||||
sourceName: string | null;
|
sourceName: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// STUB demo queue — purely client-side display data so the player bar and
|
export const queueInitialState: QueueState = {
|
||||||
// queue drawer render with content before the backend exists. Delete this
|
entries: [],
|
||||||
// block (reset entries/currentIndex/source to the empty values) once real
|
currentIndex: -1,
|
||||||
// playback wires tracks into the queue.
|
source: 'manual',
|
||||||
const DEMO_QUEUE: QueueEntry[] = [
|
|
||||||
{
|
|
||||||
trackId: 'd1',
|
|
||||||
title: 'Quiet Storage',
|
|
||||||
artistName: 'Cyan Atlas',
|
|
||||||
albumTitle: 'Night Index',
|
|
||||||
durationMs: 312_000,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
trackId: 'd2',
|
|
||||||
title: 'Magnetic North',
|
|
||||||
artistName: 'Tidal Bloom',
|
|
||||||
albumTitle: 'Ferric Coast',
|
|
||||||
durationMs: 243_000,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
trackId: 'd3',
|
|
||||||
title: 'Ambergris',
|
|
||||||
artistName: 'Møller',
|
|
||||||
albumTitle: 'Warm Static',
|
|
||||||
durationMs: 201_000,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
trackId: 'd4',
|
|
||||||
title: 'Slow Carrier',
|
|
||||||
artistName: 'Tidal Bloom',
|
|
||||||
albumTitle: 'Ferric Coast',
|
|
||||||
durationMs: 301_000,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const initialState: QueueState = {
|
|
||||||
entries: DEMO_QUEUE,
|
|
||||||
currentIndex: 0,
|
|
||||||
source: 'radio',
|
|
||||||
sourceId: null,
|
sourceId: null,
|
||||||
sourceName: 'My radio',
|
sourceName: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const queueSlice = createSlice({
|
export const queueSlice = createSlice({
|
||||||
name: 'queue',
|
name: 'queue',
|
||||||
initialState,
|
initialState: queueInitialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
setQueue(
|
setQueue(
|
||||||
state,
|
state,
|
||||||
|
|||||||
@@ -4,12 +4,15 @@ interface UiState {
|
|||||||
sidebarCollapsed: boolean;
|
sidebarCollapsed: boolean;
|
||||||
activeModal: string | null;
|
activeModal: string | null;
|
||||||
activeTrackContextMenuId: string | null;
|
activeTrackContextMenuId: string | null;
|
||||||
|
/** Track whose info drawer is open (rightmost drawer); null = closed. */
|
||||||
|
trackInfoId: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: UiState = {
|
const initialState: UiState = {
|
||||||
sidebarCollapsed: false,
|
sidebarCollapsed: false,
|
||||||
activeModal: null,
|
activeModal: null,
|
||||||
activeTrackContextMenuId: null,
|
activeTrackContextMenuId: null,
|
||||||
|
trackInfoId: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uiSlice = createSlice({
|
export const uiSlice = createSlice({
|
||||||
@@ -31,6 +34,12 @@ export const uiSlice = createSlice({
|
|||||||
setActiveContextMenu(state, action: PayloadAction<string | null>) {
|
setActiveContextMenu(state, action: PayloadAction<string | null>) {
|
||||||
state.activeTrackContextMenuId = action.payload;
|
state.activeTrackContextMenuId = action.payload;
|
||||||
},
|
},
|
||||||
|
openTrackInfo(state, action: PayloadAction<string>) {
|
||||||
|
state.trackInfoId = action.payload;
|
||||||
|
},
|
||||||
|
closeTrackInfo(state) {
|
||||||
|
state.trackInfoId = null;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -40,5 +49,7 @@ export const {
|
|||||||
openModal,
|
openModal,
|
||||||
closeModal,
|
closeModal,
|
||||||
setActiveContextMenu,
|
setActiveContextMenu,
|
||||||
|
openTrackInfo,
|
||||||
|
closeTrackInfo,
|
||||||
} = uiSlice.actions;
|
} = uiSlice.actions;
|
||||||
export default uiSlice.reducer;
|
export default uiSlice.reducer;
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ body {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
font-family: var(--font-sans);
|
font-family: var(--font-sans);
|
||||||
color: var(--fg-1);
|
color: var(--fg-1);
|
||||||
|
/* Paint the themed background immediately. The inline theme script in
|
||||||
|
index.html (see rsbuild.config.ts) sets [data-theme] before first paint, so
|
||||||
|
--color-bg resolves to the right value here before React mounts #root and
|
||||||
|
layers the .modern-sk-felt grain on top — no flash of white. */
|
||||||
|
background: var(--color-bg);
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -724,6 +724,164 @@
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ============================================================
|
||||||
|
TRACK INFO DRAWER (rightmost — sits right of the queue drawer)
|
||||||
|
============================================================ */
|
||||||
|
/* Same width-collapse pattern as .qd. Rendered after QueuePanel in AppShell so
|
||||||
|
when both are open this is the rightmost panel. */
|
||||||
|
.tid {
|
||||||
|
width: 360px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
border-left: 1px solid var(--hair);
|
||||||
|
background: linear-gradient(180deg, rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.24));
|
||||||
|
transition:
|
||||||
|
width 0.24s var(--ease-out),
|
||||||
|
border-left-color 0.24s var(--ease-out);
|
||||||
|
}
|
||||||
|
.tid.closed {
|
||||||
|
width: 0;
|
||||||
|
border-left-color: transparent;
|
||||||
|
}
|
||||||
|
.tid-inner {
|
||||||
|
width: 360px;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
.tid-head {
|
||||||
|
flex-shrink: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 16px 18px 12px;
|
||||||
|
border-bottom: 1px solid var(--hair);
|
||||||
|
}
|
||||||
|
.tid-head h3 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 15px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--fg-1);
|
||||||
|
}
|
||||||
|
.tid-scroll {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
overflow-y: auto;
|
||||||
|
padding: 18px;
|
||||||
|
}
|
||||||
|
.tid-cover {
|
||||||
|
width: 100%;
|
||||||
|
aspect-ratio: 1 / 1;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
border-radius: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
background: var(--steel-900);
|
||||||
|
box-shadow: var(--shadow-raised, 0 8px 24px rgba(0, 0, 0, 0.4));
|
||||||
|
}
|
||||||
|
.tid-cover img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: cover;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.tid-title {
|
||||||
|
margin: 0 0 4px;
|
||||||
|
font-size: 19px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--fg-1);
|
||||||
|
line-height: 1.25;
|
||||||
|
}
|
||||||
|
.tid-sub {
|
||||||
|
display: block;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--fg-2);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.tid-sub:hover {
|
||||||
|
color: var(--lime);
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
.tid-album {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
margin-top: 3px;
|
||||||
|
color: var(--fg-3);
|
||||||
|
}
|
||||||
|
.tid-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 8px;
|
||||||
|
margin: 16px 0 4px;
|
||||||
|
}
|
||||||
|
.tid-section {
|
||||||
|
margin-top: 18px;
|
||||||
|
padding-top: 14px;
|
||||||
|
border-top: 1px solid var(--hair);
|
||||||
|
}
|
||||||
|
.tid-section-label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.tid-status {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
.tid-error {
|
||||||
|
margin: 8px 0 0;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--ember, #e9572b);
|
||||||
|
}
|
||||||
|
.tid-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 5px 0;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
.tid-row-k {
|
||||||
|
flex-shrink: 0;
|
||||||
|
width: 96px;
|
||||||
|
color: var(--fg-3);
|
||||||
|
}
|
||||||
|
.tid-row-v {
|
||||||
|
min-width: 0;
|
||||||
|
flex: 1;
|
||||||
|
color: var(--fg-1);
|
||||||
|
text-align: right;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
.tid-row-v.mono {
|
||||||
|
font-family: var(--font-mono, ui-monospace, monospace);
|
||||||
|
font-size: 11px;
|
||||||
|
color: var(--fg-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* On narrower viewports the drawer overlays the content instead of pushing it,
|
||||||
|
so the queue + info drawers don't squeeze the main screen. */
|
||||||
|
@media (max-width: 1180px) {
|
||||||
|
.app-body {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.tid {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
width: 360px;
|
||||||
|
z-index: 30;
|
||||||
|
box-shadow: -16px 0 40px rgba(0, 0, 0, 0.5);
|
||||||
|
transition: transform 0.24s var(--ease-out);
|
||||||
|
}
|
||||||
|
.tid.closed {
|
||||||
|
width: 360px;
|
||||||
|
transform: translateX(100%);
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ============================================================
|
/* ============================================================
|
||||||
PAGE HEADER + SECONDARY NAV (Settings, Admin)
|
PAGE HEADER + SECONDARY NAV (Settings, Admin)
|
||||||
============================================================ */
|
============================================================ */
|
||||||
|
|||||||
@@ -0,0 +1,120 @@
|
|||||||
|
// @rstest-environment jsdom
|
||||||
|
import { expect, test, beforeEach, rstest } from '@rstest/core';
|
||||||
|
import {
|
||||||
|
loadQueueState,
|
||||||
|
loadPlayerState,
|
||||||
|
startPersistence,
|
||||||
|
} from '../src/store/persist';
|
||||||
|
import { queueInitialState, type QueueState } from '../src/store/slices/queue';
|
||||||
|
import {
|
||||||
|
playerInitialState,
|
||||||
|
type PlayerState,
|
||||||
|
} from '../src/store/slices/player';
|
||||||
|
import {
|
||||||
|
upsertInstance,
|
||||||
|
setActiveInstanceId,
|
||||||
|
instanceStorage,
|
||||||
|
} from '../src/config/instances';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
localStorage.clear();
|
||||||
|
const inst = upsertInstance('http://test.local');
|
||||||
|
setActiveInstanceId(inst.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
const sampleQueue: QueueState = {
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
trackId: 't1',
|
||||||
|
title: 'A',
|
||||||
|
artistName: 'X',
|
||||||
|
albumTitle: 'Alb',
|
||||||
|
durationMs: 1000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trackId: 't2',
|
||||||
|
title: 'B',
|
||||||
|
artistName: 'Y',
|
||||||
|
albumTitle: 'Alb',
|
||||||
|
durationMs: 2000,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
currentIndex: 1,
|
||||||
|
source: 'album',
|
||||||
|
sourceId: 'alb-1',
|
||||||
|
sourceName: 'My Album',
|
||||||
|
};
|
||||||
|
|
||||||
|
test('loaders fall back to initial state with nothing persisted', () => {
|
||||||
|
expect(loadQueueState()).toEqual(queueInitialState);
|
||||||
|
expect(loadPlayerState()).toEqual(playerInitialState);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('loadQueueState restores a persisted queue', () => {
|
||||||
|
instanceStorage.set('queue', JSON.stringify(sampleQueue));
|
||||||
|
expect(loadQueueState()).toEqual(sampleQueue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('loadQueueState guards a currentIndex past the entries array', () => {
|
||||||
|
instanceStorage.set(
|
||||||
|
'queue',
|
||||||
|
JSON.stringify({ ...sampleQueue, currentIndex: 99 }),
|
||||||
|
);
|
||||||
|
expect(loadQueueState().currentIndex).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('loadPlayerState restores fields but never auto-resumes playback', () => {
|
||||||
|
instanceStorage.set(
|
||||||
|
'player',
|
||||||
|
JSON.stringify({
|
||||||
|
currentTrackId: 't2',
|
||||||
|
position: 42,
|
||||||
|
volume: 0.5,
|
||||||
|
muted: true,
|
||||||
|
repeat: 'all',
|
||||||
|
shuffle: true,
|
||||||
|
// a stale isPlaying:true must not survive a reload
|
||||||
|
isPlaying: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const loaded = loadPlayerState();
|
||||||
|
expect(loaded.currentTrackId).toBe('t2');
|
||||||
|
expect(loaded.position).toBe(42);
|
||||||
|
expect(loaded.volume).toBe(0.5);
|
||||||
|
expect(loaded.repeat).toBe('all');
|
||||||
|
expect(loaded.isPlaying).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('corrupt JSON falls back to initial state', () => {
|
||||||
|
instanceStorage.set('queue', '{not json');
|
||||||
|
expect(loadQueueState()).toEqual(queueInitialState);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('startPersistence flushes changed state to storage after throttle', () => {
|
||||||
|
rstest.useFakeTimers();
|
||||||
|
let state = {
|
||||||
|
queue: queueInitialState,
|
||||||
|
player: playerInitialState,
|
||||||
|
} as { queue: QueueState; player: PlayerState };
|
||||||
|
let listener: (() => void) | null = null;
|
||||||
|
const store = {
|
||||||
|
getState: () => state as never,
|
||||||
|
subscribe: (l: () => void) => {
|
||||||
|
listener = l;
|
||||||
|
return () => {};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
startPersistence(store);
|
||||||
|
|
||||||
|
// mutate + notify
|
||||||
|
state = { ...state, queue: sampleQueue };
|
||||||
|
listener!();
|
||||||
|
// nothing written before the throttle window elapses
|
||||||
|
expect(instanceStorage.get('queue')).toBeNull();
|
||||||
|
|
||||||
|
rstest.advanceTimersByTime(1000);
|
||||||
|
expect(JSON.parse(instanceStorage.get('queue')!).currentIndex).toBe(1);
|
||||||
|
|
||||||
|
rstest.useRealTimers();
|
||||||
|
});
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
// @rstest-environment jsdom
|
||||||
|
import { expect, test, beforeEach, rstest } from '@rstest/core';
|
||||||
|
import {
|
||||||
|
rehydrateApiCache,
|
||||||
|
startApiPersistence,
|
||||||
|
} from '../src/store/rtkqPersist';
|
||||||
|
import { REHYDRATE_API } from '../src/api/rehydrate';
|
||||||
|
import {
|
||||||
|
upsertInstance,
|
||||||
|
setActiveInstanceId,
|
||||||
|
instanceStorage,
|
||||||
|
} from '../src/config/instances';
|
||||||
|
import type { RootState } from '../src/store/index';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
localStorage.clear();
|
||||||
|
const inst = upsertInstance('http://test.local');
|
||||||
|
setActiveInstanceId(inst.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
function apiStateWith(queries: Record<string, unknown>) {
|
||||||
|
return {
|
||||||
|
api: { queries, mutations: {}, provided: {}, subscriptions: {}, config: {} },
|
||||||
|
} as unknown as RootState;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('rehydrateApiCache dispatches nothing when no cache is stored', () => {
|
||||||
|
const dispatched: unknown[] = [];
|
||||||
|
rehydrateApiCache((a) => dispatched.push(a));
|
||||||
|
expect(dispatched).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rehydrateApiCache replays a stored cache as a rehydrate action', () => {
|
||||||
|
instanceStorage.set(
|
||||||
|
'rtkq',
|
||||||
|
JSON.stringify({
|
||||||
|
queries: { 'getLibrary(undefined)': { status: 'fulfilled', data: [1] } },
|
||||||
|
mutations: {},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const dispatched: Array<{ type: string; payload: unknown }> = [];
|
||||||
|
rehydrateApiCache((a) =>
|
||||||
|
dispatched.push(a as { type: string; payload: unknown }),
|
||||||
|
);
|
||||||
|
expect(dispatched).toHaveLength(1);
|
||||||
|
expect(dispatched[0].type).toBe(REHYDRATE_API);
|
||||||
|
expect(dispatched[0].payload).toMatchObject({
|
||||||
|
queries: { 'getLibrary(undefined)': { status: 'fulfilled' } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rehydrate payload always carries `provided` (regression: RTKQ reads provided.tags)', () => {
|
||||||
|
// A snapshot persisted before `provided` existed must not crash RTKQ's
|
||||||
|
// invalidation slice, which does `Object.entries(provided.tags ?? {})`.
|
||||||
|
instanceStorage.set(
|
||||||
|
'rtkq',
|
||||||
|
JSON.stringify({
|
||||||
|
queries: { 'getLibrary(undefined)': { status: 'fulfilled', data: [] } },
|
||||||
|
mutations: {},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const dispatched: Array<{ payload: { provided?: unknown } }> = [];
|
||||||
|
rehydrateApiCache((a) =>
|
||||||
|
dispatched.push(a as { payload: { provided?: unknown } }),
|
||||||
|
);
|
||||||
|
expect(dispatched[0].payload.provided).toEqual({ tags: {}, keys: {} });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('startApiPersistence saves only fulfilled queries after throttle', () => {
|
||||||
|
rstest.useFakeTimers();
|
||||||
|
let state = apiStateWith({});
|
||||||
|
let listener: (() => void) | null = null;
|
||||||
|
const store = {
|
||||||
|
getState: () => state,
|
||||||
|
subscribe: (l: () => void) => {
|
||||||
|
listener = l;
|
||||||
|
return () => {};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
startApiPersistence(store);
|
||||||
|
|
||||||
|
state = apiStateWith({
|
||||||
|
'getAlbums(undefined)': { status: 'fulfilled', data: ['a'] },
|
||||||
|
'getArtists(undefined)': { status: 'pending' },
|
||||||
|
'getTracks(undefined)': { status: 'rejected', error: 'boom' },
|
||||||
|
});
|
||||||
|
listener!();
|
||||||
|
|
||||||
|
// throttled — nothing yet
|
||||||
|
expect(instanceStorage.get('rtkq')).toBeNull();
|
||||||
|
|
||||||
|
rstest.advanceTimersByTime(2000);
|
||||||
|
const saved = JSON.parse(instanceStorage.get('rtkq')!);
|
||||||
|
expect(Object.keys(saved.queries)).toEqual(['getAlbums(undefined)']);
|
||||||
|
expect(saved.mutations).toEqual({});
|
||||||
|
|
||||||
|
rstest.useRealTimers();
|
||||||
|
});
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import { expect, test } from '@rstest/core';
|
||||||
|
import {
|
||||||
|
trackIdFromUrl,
|
||||||
|
cacheKeyFor,
|
||||||
|
parseRangeHeader,
|
||||||
|
selectEvictions,
|
||||||
|
} from '../public/sw-core.js';
|
||||||
|
|
||||||
|
test('trackIdFromUrl extracts the content id from a stream URL', () => {
|
||||||
|
expect(
|
||||||
|
trackIdFromUrl('https://host/api/v1/stream/abc123?token=xyz'),
|
||||||
|
).toBe('abc123');
|
||||||
|
expect(trackIdFromUrl('https://host/api/v1/library/albums')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cacheKeyFor strips the token so the key is token-stable', () => {
|
||||||
|
const a = cacheKeyFor('https://host/api/v1/stream/t1?token=AAA');
|
||||||
|
const b = cacheKeyFor('https://host/api/v1/stream/t1?token=BBB');
|
||||||
|
expect(a).toBe(b);
|
||||||
|
expect(a).toBe('https://host/api/v1/stream/t1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cacheKeyFor keeps different origins distinct', () => {
|
||||||
|
expect(cacheKeyFor('https://a/stream/t1?token=x')).not.toBe(
|
||||||
|
cacheKeyFor('https://b/stream/t1?token=x'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseRangeHeader: closed range', () => {
|
||||||
|
expect(parseRangeHeader('bytes=0-99', 1000)).toEqual({ start: 0, end: 99 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseRangeHeader: open-ended range clamps to size', () => {
|
||||||
|
expect(parseRangeHeader('bytes=500-', 1000)).toEqual({
|
||||||
|
start: 500,
|
||||||
|
end: 999,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseRangeHeader: suffix range (last N bytes)', () => {
|
||||||
|
expect(parseRangeHeader('bytes=-200', 1000)).toEqual({
|
||||||
|
start: 800,
|
||||||
|
end: 999,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseRangeHeader: end past size is clamped', () => {
|
||||||
|
expect(parseRangeHeader('bytes=900-5000', 1000)).toEqual({
|
||||||
|
start: 900,
|
||||||
|
end: 999,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseRangeHeader: invalid / no range returns null', () => {
|
||||||
|
expect(parseRangeHeader('', 1000)).toBeNull();
|
||||||
|
expect(parseRangeHeader('items=0-1', 1000)).toBeNull();
|
||||||
|
expect(parseRangeHeader('bytes=500-100', 1000)).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selectEvictions: nothing evicted when under cap', () => {
|
||||||
|
const index = {
|
||||||
|
a: { size: 100, lastAccess: 1 },
|
||||||
|
b: { size: 100, lastAccess: 2 },
|
||||||
|
};
|
||||||
|
expect(selectEvictions(index, 100, 1000)).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selectEvictions: evicts least-recently-used first until it fits', () => {
|
||||||
|
const index = {
|
||||||
|
a: { size: 400, lastAccess: 10 }, // oldest
|
||||||
|
b: { size: 400, lastAccess: 30 },
|
||||||
|
c: { size: 400, lastAccess: 20 },
|
||||||
|
};
|
||||||
|
// total 1200 + incoming 400 = 1600, cap 1000 → must free >=600.
|
||||||
|
// LRU order: a (10), c (20). Evict a (1200→800... wait incl incoming)
|
||||||
|
const evicted = selectEvictions(index, 400, 1000);
|
||||||
|
// total with incoming = 1600; evict a → 1200; evict c → 800 <= 1000.
|
||||||
|
expect(evicted).toEqual(['a', 'c']);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user