"use client";
import { useEffect, useRef, useSyncExternalStore } from "react";
import { useDesktop, useWin } from "../context";
import type { AudioItem } from "../types";
export const clock = (s: number) => {
const t = Math.max(0, Math.floor(Number.isFinite(s) ? s : 0));
return `${Math.floor(t / 60)}:${String(t % 60).padStart(2, "0")}`;
};
/**
* A working player: the cover turns like a record while it plays, play/pause, a scrubber you can drag or use with
* the arrow keys, and m:ss / m:ss. The <audio> element comes from the desktop's controller, so only one window
* plays at a time and the expanded view shares the same playhead. The bar and the times are written straight to
* the DOM every frame while playing: React does not re-render for the clock.
*/
export function AudioContent({ item }: { item: AudioItem }) {
const w = useWin();
const { audio } = useDesktop();
const playing = useSyncExternalStore(audio.subscribe, () => audio.playing() === w.wkey, () => false);
const bar = useRef<HTMLInputElement>(null);
const now = useRef<HTMLSpanElement>(null);
const total = useRef<HTMLSpanElement>(null);
useEffect(() => {
const el = audio.get(w.wkey, item.src);
const length = () => (Number.isFinite(el.duration) && el.duration > 0 ? el.duration : (item.duration ?? 0));
const paint = () => {
const d = length();
const p = d ? Math.min(1, el.currentTime / d) : 0;
const b = bar.current;
if (b) {
if (document.activeElement !== b) b.value = String(Math.round(p * 1000));
b.style.setProperty("--md-p", `${p * 100}%`);
b.setAttribute("aria-valuetext", `${clock(el.currentTime)} of ${clock(d)}`);
}
if (now.current) now.current.textContent = clock(el.currentTime);
if (total.current) total.current.textContent = clock(d);
};
let raf = 0;
const loop = () => {
paint();
raf = requestAnimationFrame(loop);
};
const sync = () => {
cancelAnimationFrame(raf);
if (audio.playing() === w.wkey) raf = requestAnimationFrame(loop);
else paint();
};
paint();
sync();
el.addEventListener("loadedmetadata", paint);
el.addEventListener("seeked", paint);
const off = audio.subscribe(sync);
return () => {
cancelAnimationFrame(raf);
off();
el.removeEventListener("loadedmetadata", paint);
el.removeEventListener("seeked", paint);
};
}, [audio, w.wkey, item.src, item.duration]);
const seek = (v: number) => {
const el = audio.get(w.wkey, item.src);
const d = Number.isFinite(el.duration) && el.duration > 0 ? el.duration : (item.duration ?? 0);
if (d) el.currentTime = (v / 1000) * d;
bar.current?.style.setProperty("--md-p", `${v / 10}%`);
};
const tab = w.active ? 0 : -1;
return (
<div className="md-audio" data-playing={playing || undefined}>
<span className="md-record" aria-hidden>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={item.cover} alt="" width={64} height={64} loading={w.eager ? "eager" : "lazy"} draggable={false} />
</span>
<div className="md-audio-main">
<div className="md-audio-head">
<div className="md-audio-text">
<p className="md-track">{item.track}</p>
{item.artist && <p className="md-artist">{item.artist}</p>}
</div>
<button type="button" className="md-play" tabIndex={tab} aria-label={playing ? `Pause ${item.track}` : `Play ${item.track}`} onClick={() => (audio.get(w.wkey, item.src), audio.toggle(w.wkey))}>
<svg viewBox="0 0 16 16" aria-hidden>
{playing ? <path d="M5 3.5h2v9H5zm4 0h2v9H9z" /> : <path d="M5.5 3.2v9.6L13 8z" />}
</svg>
</button>
</div>
<input
ref={bar}
className="md-range"
type="range"
min={0}
max={1000}
step={1}
defaultValue={0}
tabIndex={tab}
aria-label={`Seek ${item.track}`}
onInput={(e) => seek(Number(e.currentTarget.value))}
/>
<p className="md-times">
<span ref={now}>0:00</span> / <span ref={total}>{clock(item.duration ?? 0)}</span>
</p>
</div>
</div>
);
}