Continuous Integration / build_web (push) Failing after 1s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
- the incoming sleeve no longer fades in from zero while the old one fades out, so the artwork does not dim mid-transition
674 lines
19 KiB
TypeScript
674 lines
19 KiB
TypeScript
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogTitle,
|
|
IconButton,
|
|
ListItemIcon,
|
|
Menu,
|
|
MenuItem,
|
|
Slider,
|
|
Tooltip,
|
|
} from "@mui/material";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
|
|
import { AnimatePresence, motion } from "motion/react";
|
|
import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
import CastButton from "@/components/buttons/castButton";
|
|
import LikeButton from "@/components/buttons/likeButton";
|
|
import LyricsPanel, {
|
|
getLyricsQueryOptions,
|
|
} from "@/components/playback/audioPlayer/components/LyricsPanel";
|
|
import PlayerVolume from "@/components/playback/audioPlayer/components/PlayerVolume";
|
|
import StatsPanel from "@/components/playback/audioPlayer/components/StatsPanel";
|
|
import WindowControls from "@/components/windowControls";
|
|
import { getRuntimeMusic, secToTicks, ticksToSec } from "@/utils/date/time";
|
|
import { getCoverPalette } from "@/utils/methods/coverColor";
|
|
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
|
|
import { getItemQueryOptions } from "@/utils/queries/items";
|
|
import { useApiInContext } from "@/utils/store/api";
|
|
import {
|
|
setIsMuted,
|
|
setVolume,
|
|
useAudioPlayback,
|
|
} from "@/utils/store/audioPlayback";
|
|
import { useCentralStore } from "@/utils/store/central";
|
|
import { playItemFromQueue } from "@/utils/store/playback";
|
|
import useQueue, {
|
|
cycleRepeatMode,
|
|
type RepeatMode,
|
|
shuffleUpcoming,
|
|
} from "@/utils/store/queue";
|
|
|
|
import "./audio.scss";
|
|
|
|
export const Route = createFileRoute("/_api/player/audio")({
|
|
component: AudioPlayerRoute,
|
|
});
|
|
|
|
const repeatLabels: Record<RepeatMode, string> = {
|
|
off: "Повтор выкл.",
|
|
all: "Повтор всех",
|
|
one: "Повтор одного",
|
|
};
|
|
|
|
const QueueRow = ({
|
|
item,
|
|
index,
|
|
isCurrent,
|
|
coverUrl,
|
|
onPlay,
|
|
queryKey,
|
|
userId,
|
|
}: {
|
|
item: BaseItemDto;
|
|
index: number;
|
|
isCurrent: boolean;
|
|
coverUrl?: string;
|
|
onPlay: (index: number) => void;
|
|
queryKey: string[];
|
|
userId?: string;
|
|
}) => (
|
|
// biome-ignore lint/a11y/useSemanticElements: the row contains its own like button
|
|
<div
|
|
className={`music-player-queue-row${isCurrent ? " is-current" : ""}`}
|
|
onClick={() => {
|
|
if (!isCurrent) onPlay(index);
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (isCurrent) return;
|
|
if (event.key === "Enter" || event.key === " ") {
|
|
event.preventDefault();
|
|
onPlay(index);
|
|
}
|
|
}}
|
|
role="button"
|
|
tabIndex={0}
|
|
>
|
|
<span className="music-player-queue-cover">
|
|
{coverUrl ? (
|
|
<img src={coverUrl} alt="" />
|
|
) : (
|
|
<span className="material-symbols-rounded">music_note</span>
|
|
)}
|
|
{isCurrent && (
|
|
<span className="music-player-queue-playing">
|
|
<span className="material-symbols-rounded">play_arrow</span>
|
|
</span>
|
|
)}
|
|
</span>
|
|
<span className="music-player-queue-text">
|
|
<span className="music-player-queue-title">{item.Name}</span>
|
|
<span className="music-player-queue-artist">
|
|
{item.Artists?.join(", ") ?? item.AlbumArtist ?? item.Album}
|
|
</span>
|
|
</span>
|
|
<span
|
|
className="music-player-queue-like"
|
|
onClick={(event) => event.stopPropagation()}
|
|
onKeyDown={(event) => event.stopPropagation()}
|
|
role="presentation"
|
|
>
|
|
<LikeButton
|
|
itemId={item.Id}
|
|
itemName={item.Name}
|
|
isFavorite={item.UserData?.IsFavorite}
|
|
queryKey={queryKey}
|
|
userId={userId}
|
|
/>
|
|
</span>
|
|
<span className="music-player-queue-time">
|
|
{getRuntimeMusic(item.RunTimeTicks ?? 0)}
|
|
</span>
|
|
</div>
|
|
);
|
|
|
|
const COVER_FADE_MS = 600;
|
|
|
|
/**
|
|
* Sleeve that crossfades into the next track: the previous cover stays on top
|
|
* while the next one loads, then simply dissolves over it.
|
|
*/
|
|
const CoverArt = ({ src, alt }: { src?: string; alt: string }) => {
|
|
const [current, setCurrent] = useState<string | undefined>(src);
|
|
const [previous, setPrevious] = useState<string | undefined>();
|
|
const currentRef = useRef<string | undefined>(src);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!src || src === currentRef.current) return;
|
|
let cancelled = false;
|
|
let begun = false;
|
|
|
|
const start = () => {
|
|
if (cancelled || begun) return;
|
|
begun = true;
|
|
setPrevious(currentRef.current);
|
|
currentRef.current = src;
|
|
setCurrent(src);
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(
|
|
() => setPrevious(undefined),
|
|
COVER_FADE_MS + 40,
|
|
);
|
|
};
|
|
|
|
const image = new Image();
|
|
image.onload = start;
|
|
image.onerror = start;
|
|
image.src = src;
|
|
if (image.complete && image.naturalWidth > 0) start();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
};
|
|
}, [src]);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
},
|
|
[],
|
|
);
|
|
|
|
return (
|
|
<div className="music-player-art">
|
|
{previous && (
|
|
<img
|
|
key={previous}
|
|
className="music-player-art-prev"
|
|
src={previous}
|
|
alt=""
|
|
/>
|
|
)}
|
|
{current ? (
|
|
<img
|
|
key={current}
|
|
className={`music-player-art-current${
|
|
previous ? " is-static" : ""
|
|
}`}
|
|
src={current}
|
|
alt={alt}
|
|
/>
|
|
) : (
|
|
<span className="material-symbols-rounded">music_note</span>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
function AudioPlayerRoute() {
|
|
const api = useApiInContext((s) => s.api);
|
|
const user = useCentralStore((s) => s.currentUser);
|
|
const navigate = useNavigate();
|
|
const [queue, currentTrack, repeatMode] = useQueue((s) => [
|
|
s.tracks,
|
|
s.currentItemIndex,
|
|
s.repeatMode,
|
|
]);
|
|
const [item, audioPlayerRef, volume, isMuted] = useAudioPlayback((s) => [
|
|
s.item,
|
|
s.player.ref,
|
|
s.player.volume,
|
|
s.player.isMuted,
|
|
]);
|
|
|
|
const [progress, setProgress] = useState(0);
|
|
const [playing, setPlaying] = useState(false);
|
|
const [panelOpen, setPanelOpen] = useState(false);
|
|
const [panelTab, setPanelTab] = useState<"queue" | "lyrics">("queue");
|
|
const [menuAnchor, setMenuAnchor] = useState<HTMLElement | null>(null);
|
|
const [statsOpen, setStatsOpen] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const player = audioPlayerRef?.current;
|
|
if (!player) return;
|
|
|
|
const updateProgress = () => setProgress(player.currentTime);
|
|
const updatePlaying = () => setPlaying(!player.paused);
|
|
|
|
player.addEventListener("timeupdate", updateProgress);
|
|
player.addEventListener("play", updatePlaying);
|
|
player.addEventListener("pause", updatePlaying);
|
|
|
|
setProgress(player.currentTime);
|
|
setPlaying(!player.paused);
|
|
|
|
return () => {
|
|
player.removeEventListener("timeupdate", updateProgress);
|
|
player.removeEventListener("play", updatePlaying);
|
|
player.removeEventListener("pause", updatePlaying);
|
|
};
|
|
}, [audioPlayerRef?.current, item?.Id]);
|
|
|
|
// Fresh user data so the like button reflects changes immediately.
|
|
const itemDetails = useQuery({
|
|
...getItemQueryOptions(item?.Id ?? "", api, user?.Id),
|
|
enabled: Boolean(item?.Id && api && user?.Id),
|
|
});
|
|
const lyrics = useQuery(getLyricsQueryOptions(item, api));
|
|
const hasLyrics = (lyrics.data?.Lyrics?.length ?? 0) > 0;
|
|
|
|
// Wall colour derived from the sleeve.
|
|
const palette = useMemo(() => getCoverPalette(item), [item]);
|
|
|
|
const handlePlayPause = () => {
|
|
const player = audioPlayerRef?.current;
|
|
if (!player) return;
|
|
if (player.paused) {
|
|
player.play();
|
|
} else {
|
|
player.pause();
|
|
}
|
|
};
|
|
|
|
const handleSeekCommit = (value: number) => {
|
|
const player = audioPlayerRef?.current;
|
|
if (player) {
|
|
player.currentTime = ticksToSec(value);
|
|
setProgress(ticksToSec(value));
|
|
}
|
|
};
|
|
|
|
const imageUrl = useMemo(
|
|
() =>
|
|
item?.ImageTags?.Primary
|
|
? api &&
|
|
getImageUrlsApi(api).getItemImageUrlById(item.Id ?? "", "Primary", {
|
|
tag: item.ImageTags.Primary,
|
|
})
|
|
: undefined,
|
|
[item?.Id, item?.ImageTags?.Primary, api],
|
|
);
|
|
|
|
const coverFor = (track: BaseItemDto) =>
|
|
api && track.ImageTags?.Primary
|
|
? getImageUrlsApi(api).getItemImageUrlById(
|
|
track.AlbumId ?? track.Id ?? "",
|
|
"Primary",
|
|
{ quality: 70, fillHeight: 96, fillWidth: 96 },
|
|
)
|
|
: undefined;
|
|
|
|
const hasUpcoming = (queue?.length ?? 0) > currentTrack + 1;
|
|
const isFavorite =
|
|
itemDetails.data?.UserData?.IsFavorite ?? item?.UserData?.IsFavorite;
|
|
const artistId = item?.ArtistItems?.[0]?.Id ?? item?.AlbumArtists?.[0]?.Id;
|
|
const tracks = queue ?? [];
|
|
const upcoming = tracks.slice(currentTrack + 1);
|
|
|
|
const openPanel = (tab: "queue" | "lyrics") => {
|
|
if (panelOpen && panelTab === tab) {
|
|
setPanelOpen(false);
|
|
return;
|
|
}
|
|
setPanelTab(tab);
|
|
setPanelOpen(true);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div
|
|
className={`music-player${panelOpen ? " has-panel" : ""}`}
|
|
>
|
|
<AnimatePresence initial={false} mode="sync">
|
|
<motion.div
|
|
key={item?.Id ?? "no-item"}
|
|
className="music-player-wall"
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
exit={{ opacity: 0 }}
|
|
transition={{
|
|
duration: 2.2,
|
|
ease: [0.22, 1, 0.36, 1],
|
|
}}
|
|
style={{
|
|
background: `radial-gradient(90% 70% at 50% -10%, rgb(255 255 255 / 0.1), transparent 60%), linear-gradient(180deg, ${palette.base}, ${palette.deep})`,
|
|
}}
|
|
/>
|
|
</AnimatePresence>
|
|
<header className="music-player-chrome" data-tauri-drag-region>
|
|
<div className="music-player-chrome-end">
|
|
<WindowControls />
|
|
</div>
|
|
</header>
|
|
|
|
<Tooltip title="Свернуть плеер">
|
|
<button
|
|
type="button"
|
|
className="music-player-collapse"
|
|
onClick={() => window.history.back()}
|
|
aria-label="Свернуть плеер"
|
|
>
|
|
<span className="material-symbols-rounded">expand_more</span>
|
|
</button>
|
|
</Tooltip>
|
|
|
|
<div className="music-player-stage">
|
|
<div className="music-player-row">
|
|
<div className="music-player-main">
|
|
<CoverArt
|
|
src={imageUrl}
|
|
alt={item?.Name ?? "Обложка"}
|
|
/>
|
|
|
|
<div className="music-player-meta">
|
|
<h1 className="music-player-title">{item?.Name}</h1>
|
|
<div className="music-player-subtitle">
|
|
{item?.ArtistItems?.length
|
|
? item.ArtistItems.map((artist, index) => (
|
|
<React.Fragment key={artist.Id ?? artist.Name}>
|
|
{index > 0 && ", "}
|
|
{artist.Id ? (
|
|
<Link
|
|
to="/music/artist/$id"
|
|
params={{ id: artist.Id }}
|
|
>
|
|
{artist.Name}
|
|
</Link>
|
|
) : (
|
|
artist.Name
|
|
)}
|
|
</React.Fragment>
|
|
))
|
|
: item?.Artists?.join(", ")}
|
|
{item?.Album && (
|
|
<>
|
|
{(item?.ArtistItems?.length || item?.Artists?.length) &&
|
|
" · "}
|
|
{item.AlbumId ? (
|
|
<Link
|
|
to="/music/album/$id"
|
|
params={{ id: item.AlbumId }}
|
|
>
|
|
{item.Album}
|
|
</Link>
|
|
) : (
|
|
item.Album
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
<div className="music-player-seek">
|
|
<Slider
|
|
value={Math.min(
|
|
secToTicks(progress),
|
|
item?.RunTimeTicks ?? 0,
|
|
)}
|
|
max={item?.RunTimeTicks ?? 1}
|
|
step={1}
|
|
size="small"
|
|
aria-label="Перемотка"
|
|
onChange={(_, value) => {
|
|
const next = Array.isArray(value) ? value[0] : value;
|
|
setProgress(ticksToSec(next));
|
|
}}
|
|
onChangeCommitted={(_, value) => {
|
|
const next = Array.isArray(value) ? value[0] : value;
|
|
handleSeekCommit(next);
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<AnimatePresence initial={false}>
|
|
{panelOpen && (
|
|
<motion.aside
|
|
key="panel"
|
|
className="music-player-panel"
|
|
initial={{ x: -240, opacity: 0 }}
|
|
animate={{ x: 0, opacity: 1 }}
|
|
exit={{ x: -240, opacity: 0 }}
|
|
transition={{ duration: 0.32, ease: [0.2, 0, 0, 1] }}
|
|
>
|
|
<div className="music-player-panel-inner">
|
|
{panelTab === "lyrics" ? (
|
|
<>
|
|
<div className="music-player-panel-head">
|
|
<span className="music-player-panel-source">
|
|
Текст песни
|
|
</span>
|
|
<IconButton
|
|
size="small"
|
|
aria-label="Показать очередь"
|
|
onClick={() => setPanelTab("queue")}
|
|
>
|
|
<span className="material-symbols-rounded">close</span>
|
|
</IconButton>
|
|
</div>
|
|
<div className="music-player-panel-body music-player-panel-lyrics">
|
|
{hasLyrics ? (
|
|
<LyricsPanel
|
|
item={item}
|
|
api={api}
|
|
currentTime={progress}
|
|
onSeek={handleSeekCommit}
|
|
/>
|
|
) : (
|
|
<p className="music-player-panel-empty">
|
|
Для этого трека нет текста.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</>
|
|
) : (
|
|
<div className="music-player-panel-body">
|
|
{upcoming.length === 0 ? (
|
|
<p className="music-player-panel-empty">
|
|
Очередь пуста. Запустите альбом или плейлист,
|
|
чтобы она наполнилась.
|
|
</p>
|
|
) : (
|
|
<div className="music-player-queue-list">
|
|
{upcoming.map((track, offset) => (
|
|
<QueueRow
|
|
key={track.Id}
|
|
item={track}
|
|
index={currentTrack + 1 + offset}
|
|
isCurrent={false}
|
|
coverUrl={coverFor(track)}
|
|
onPlay={(index) =>
|
|
playItemFromQueue(index, user?.Id, api)
|
|
}
|
|
queryKey={["queue-upcoming"]}
|
|
userId={user?.Id}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</motion.aside>
|
|
)}
|
|
</AnimatePresence>
|
|
</div>
|
|
</div>
|
|
|
|
<footer className="music-player-controls">
|
|
<div className="music-player-controls-side">
|
|
<LikeButton
|
|
itemId={item?.Id ?? undefined}
|
|
itemName={item?.Name}
|
|
isFavorite={isFavorite}
|
|
queryKey={["item", item?.Id ?? ""]}
|
|
userId={user?.Id}
|
|
/>
|
|
{item && (
|
|
<CastButton
|
|
item={item}
|
|
transfer={{
|
|
kind: "audio",
|
|
positionTicks: secToTicks(progress),
|
|
queue: queue ?? [],
|
|
startIndex: currentTrack,
|
|
}}
|
|
/>
|
|
)}
|
|
<IconButton
|
|
onClick={(e) => setMenuAnchor(e.currentTarget)}
|
|
aria-label="Ещё"
|
|
title="Ещё"
|
|
>
|
|
<span className="material-symbols-rounded">more_horiz</span>
|
|
</IconButton>
|
|
<Menu
|
|
anchorEl={menuAnchor}
|
|
open={Boolean(menuAnchor)}
|
|
onClose={() => setMenuAnchor(null)}
|
|
anchorOrigin={{ vertical: "top", horizontal: "left" }}
|
|
transformOrigin={{ vertical: "bottom", horizontal: "left" }}
|
|
>
|
|
<MenuItem
|
|
disabled={!item?.AlbumId}
|
|
onClick={() => {
|
|
setMenuAnchor(null);
|
|
if (item?.AlbumId) {
|
|
navigate({
|
|
to: "/music/album/$id",
|
|
params: { id: item.AlbumId },
|
|
});
|
|
}
|
|
}}
|
|
>
|
|
<ListItemIcon>
|
|
<span className="material-symbols-rounded">album</span>
|
|
</ListItemIcon>
|
|
Перейти к альбому
|
|
</MenuItem>
|
|
<MenuItem
|
|
disabled={!artistId}
|
|
onClick={() => {
|
|
setMenuAnchor(null);
|
|
if (artistId) {
|
|
navigate({
|
|
to: "/music/artist/$id",
|
|
params: { id: artistId },
|
|
});
|
|
}
|
|
}}
|
|
>
|
|
<ListItemIcon>
|
|
<span className="material-symbols-rounded">person</span>
|
|
</ListItemIcon>
|
|
Перейти к исполнителю
|
|
</MenuItem>
|
|
<MenuItem
|
|
onClick={() => {
|
|
setMenuAnchor(null);
|
|
setStatsOpen(true);
|
|
}}
|
|
>
|
|
<ListItemIcon>
|
|
<span className="material-symbols-rounded">query_stats</span>
|
|
</ListItemIcon>
|
|
Статистика
|
|
</MenuItem>
|
|
</Menu>
|
|
</div>
|
|
|
|
<div className="music-player-transport">
|
|
<Tooltip title="Перемешать следующие">
|
|
<span>
|
|
<IconButton
|
|
onClick={shuffleUpcoming}
|
|
disabled={!hasUpcoming}
|
|
aria-label="Перемешать следующие"
|
|
>
|
|
<span className="material-symbols-rounded">shuffle</span>
|
|
</IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<IconButton
|
|
onClick={() => playItemFromQueue("previous", user?.Id, api)}
|
|
aria-label="Предыдущий"
|
|
title="Предыдущий"
|
|
>
|
|
<span className="material-symbols-rounded">skip_previous</span>
|
|
</IconButton>
|
|
<button
|
|
type="button"
|
|
className="music-player-play"
|
|
onClick={handlePlayPause}
|
|
aria-label={playing ? "Пауза" : "Играть"}
|
|
>
|
|
<span className="material-symbols-rounded">
|
|
{playing ? "pause" : "play_arrow"}
|
|
</span>
|
|
</button>
|
|
<IconButton
|
|
onClick={() => playItemFromQueue("next", user?.Id, api)}
|
|
aria-label="Следующий"
|
|
title="Следующий"
|
|
>
|
|
<span className="material-symbols-rounded">skip_next</span>
|
|
</IconButton>
|
|
<Tooltip title={repeatLabels[repeatMode]}>
|
|
<IconButton
|
|
onClick={cycleRepeatMode}
|
|
aria-label={repeatLabels[repeatMode]}
|
|
className={repeatMode !== "off" ? "is-active" : undefined}
|
|
>
|
|
<span className="material-symbols-rounded">
|
|
{repeatMode === "one" ? "repeat_one" : "repeat"}
|
|
</span>
|
|
</IconButton>
|
|
</Tooltip>
|
|
</div>
|
|
|
|
<div className="music-player-controls-side is-end">
|
|
<div className="music-player-volume">
|
|
<PlayerVolume
|
|
volume={volume}
|
|
isMuted={isMuted}
|
|
onVolumeChange={setVolume}
|
|
onMuteToggle={() => setIsMuted(!isMuted)}
|
|
/>
|
|
</div>
|
|
<Tooltip title="Текст песни">
|
|
<span>
|
|
<IconButton
|
|
onClick={() => openPanel("lyrics")}
|
|
aria-label="Текст песни"
|
|
className={
|
|
panelOpen && panelTab === "lyrics" ? "is-active" : undefined
|
|
}
|
|
disabled={!hasLyrics}
|
|
>
|
|
<span className="material-symbols-rounded">lyrics</span>
|
|
</IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Очередь">
|
|
<IconButton
|
|
onClick={() => openPanel("queue")}
|
|
aria-label="Очередь"
|
|
className={
|
|
panelOpen && panelTab === "queue" ? "is-active" : undefined
|
|
}
|
|
>
|
|
<span className="material-symbols-rounded">queue_music</span>
|
|
</IconButton>
|
|
</Tooltip>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
|
|
<Dialog
|
|
open={statsOpen}
|
|
onClose={() => setStatsOpen(false)}
|
|
maxWidth="sm"
|
|
fullWidth
|
|
>
|
|
<DialogTitle>Статистика</DialogTitle>
|
|
<DialogContent>
|
|
<StatsPanel item={item} audioRef={audioPlayerRef} />
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|