fix play now

This commit is contained in:
2026-02-10 01:48:02 -08:00
parent c8220eec02
commit 9f0234e00e

View File

@@ -109,11 +109,16 @@ class PlayerStore {
if (existingIdx !== -1) {
if (playNow) {
this.playNext(track);
this.playId(track.id);
}
return;
}
if (playNow) {
this.playNext(track);
this.playId(track.id);
} else {
// Add to end
this.queue.push(track);
@@ -121,12 +126,11 @@ class PlayerStore {
this.shuffledIndices.push(this.queue.length - 1);
}
if (playNow) {
this.playId(track.id);
} else if (this.queue.length === 1 && !this.currentId) {
if (this.queue.length === 1 && !this.currentId) {
this.currentId = track.id;
}
}
}
playNext(track: Track) {
const existingIdx = this.queue.findIndex((t) => t.id === track.id);
@@ -248,23 +252,35 @@ class PlayerStore {
}
prev() {
// If history has > 1 item, go back
if (this.history.length > 1) {
// Pop current
this.history.pop();
const prevId = this.history[this.history.length - 1];
if (this.hasTrack(prevId)) {
this.currentId = prevId;
} else {
// Track removed? fallback
this.history.pop(); // Remove invalid
this.prev(); // Recurse
if (this.queue.length === 0) return;
let prevIdxInQueue: number | null = null;
const currentIdx = this.currentIndex;
if (this.isShuffled) {
const currentShufflePos = this.shuffledIndices.indexOf(currentIdx);
if (currentShufflePos > 0) {
prevIdxInQueue = this.shuffledIndices[currentShufflePos - 1];
} else if (this.repeatMode === "all" && this.shuffledIndices.length > 0) {
// Wrap to end? Or just stop.
// For now let's stop at start if not wrapping.
// If repeat all, wrap to end?
prevIdxInQueue = this.shuffledIndices[this.shuffledIndices.length - 1];
}
} else {
// Restart current song?
// Handled by UI usually (calling audio.currentTime = 0),
// but here we just seek to 0 if we could.
// Store doesn't control audio element directly.
if (currentIdx > 0) {
prevIdxInQueue = currentIdx - 1;
} else if (this.repeatMode === "all" && this.queue.length > 0) {
prevIdxInQueue = this.queue.length - 1;
}
}
if (prevIdxInQueue !== null) {
this.playId(this.queue[prevIdxInQueue]?.id);
} else {
// At start of queue.
// Just seek to 0? Store doesn't control audio.
// If we can't go back, we do nothing (UI likely handles the seek-to-0 comparison).
}
}