Skip to content

fix: prevent re-playing when click same track.#168

Merged
roitium merged 2 commits into
devfrom
fix/issue-166-playlist-click
Jan 25, 2026
Merged

fix: prevent re-playing when click same track.#168
roitium merged 2 commits into
devfrom
fix/issue-166-playlist-click

Conversation

@roitium
Copy link
Copy Markdown
Collaborator

@roitium roitium commented Jan 25, 2026

Closes #167

Summary by CodeRabbit

发布说明

  • Bug 修复

    • 修复点击/选择当前正在播放音轨时会重新开始播放的问题,提升播放列表与远程列表的交互一致性。
  • Chores

    • 将内部依赖回退到稳定版本以提高整体稳定性与可靠性。

✏️ Tip: You can customize this high-level summary in your review settings.

…rrent track and downgrade `react-native-worklets` dependency.
@roitium roitium added the bug Something isn't working label Jan 25, 2026
@safedep
Copy link
Copy Markdown

safedep Bot commented Jan 25, 2026

SafeDep Report Summary

Green Malicious Packages Badge Green Vulnerable Packages Badge Green Risky License Badge

Package Details
Package Malware Vulnerability Risky License Report
icon react-native-worklets @ 0.7.2
pnpm-lock.yaml
ok icon
ok icon
ok icon
🔗

This report is generated by SafeDep Github App

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 25, 2026

Caution

Review failed

The pull request is closed.

Walkthrough

本次变更引入新的 useCurrentTrackId 钩子,在多个播放列表相关组件中以曲目 ID 取代原有的当前曲目追踪方式以防止重复播放,同时将 react-native-worklets 从 nightly 版本降回稳定版 0.7.2

Changes

Cohort / File(s) 变更摘要
依赖更新
package.json
react-native-worklets0.8.0-nightly-20260122-45349f895 更改为稳定版 0.7.2
新增钩子
src/hooks/player/useCurrentTrackId.ts
新增钩子:异步获取当前曲目并维护 trackId,订阅 onTrackStarted / onTrackFinished 事件,处理卸载与请求竞态,导出 useCurrentTrackId
播放队列模态
src/components/modals/PlayerQueueModal.tsx
useCurrentTrack 替换为 useCurrentTrackId,使用 currentTrackId 进行比较与索引计算,更新 switchTrackHandler 和相关依赖数组,增加 keyExtractor
本地播放钩子
src/features/playlist/local/hooks/useLocalPlaylistPlayer.ts
导入并使用 useCurrentTrackId;在 handleTrackPress 中添加对当前曲目的提前返回保护;更新回调依赖数组
远程曲目列表
src/features/playlist/remote/components/RemoteTrackList.tsx
useCurrentTrackId 替换旧钩子;引入 currentTrackIdRef(`RefObject<string

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant Component as 播放列表组件
    participant Hook as useCurrentTrackId
    participant API as Orpheus API
    participant Event as 事件系统

    rect rgba(100,200,100,0.5)
    Note over User,Hook: 初始化 / 同步当前曲目
    Component->>Hook: 调用 useCurrentTrackId()
    Hook->>API: getCurrentTrack()
    API-->>Hook: 返回 currentTrack
    Hook->>Hook: 提取并设置 trackId
    Hook-->>Component: 提供 currentTrackId
    end

    rect rgba(100,150,200,0.5)
    Note over User,Component: 用户点击曲目
    User->>Component: 点击曲目项
    Component->>Component: 比较 item.id 与 currentTrackId / currentTrackIdRef.current
    alt 不同曲目
        Component->>API: 请求播放新曲目
        API->>Event: 触发 onTrackStarted
    else 相同曲目
        Component->>Component: 提前返回,跳过重播
    end
    end

    rect rgba(200,150,100,0.5)
    Note over Event,Hook: 曲目变更事件驱动更新
    Event->>Hook: onTrackStarted / onTrackFinished
    Hook->>API: 重新获取 getCurrentTrack()
    API-->>Hook: 返回新的 currentTrack
    Hook->>Hook: 更新 trackId 并触发组件重渲染
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 我是小兔子,耳朵会摇摆,
ID 对一对,重播不再来,
钩子轻轻来,队列更安稳,
音乐不停歇,心情更愉快 🎶

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately reflects the main objective: preventing re-playing when the same track is clicked, which matches the core functionality changes across all modified files.
Linked Issues check ✅ Passed The PR successfully implements the requirement from issue #167 to prevent replaying the same track, with new useCurrentTrackId hook and guards in PlayerQueueModal, useLocalPlaylistPlayer, and RemoteTrackList components.
Out of Scope Changes check ✅ Passed The react-native-worklets dependency downgrade is a minor ancillary change not directly related to the bug fix objective, though it does not interfere with the main functionality.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/components/modals/PlayerQueueModal.tsx`:
- Around line 141-146: The switchTrackHandler can crash if queue changes and
queue[index] is undefined; modify the handler (switchTrackHandler) to first
retrieve const target = queue[index] (or check index >= 0 && index <
queue.length) and return early if target is falsy before accessing target.id or
calling Orpheus.skipTo(index), so you guard against out-of-bounds/undefined
entries and only call Orpheus.skipTo when target exists and target.id !==
currentTrackId.

In `@src/hooks/player/useCurrentTrackId.ts`:
- Around line 34-41: Add a second event listener for the 'onTrackFinished' event
in the same hook where Orpheus.addListener('onTrackStarted') is registered so
that fetchAndUpdate() runs when a track ends; create a subscription like the
existing sub for onTrackFinished, call void fetchAndUpdate() in its callback,
and ensure you remove that subscription in the cleanup along with sub and still
set isMounted = false—refer to Orpheus.addListener('onTrackStarted'),
fetchAndUpdate, and currentTrackIdRef to locate where to add and tear down the
new listener.
🧹 Nitpick comments (1)
src/hooks/player/useCurrentTrackId.ts (1)

15-23: 错误时不宜清空 trackId,避免防重播失效

getCurrentTrack 失败时直接 setTrackId(undefined),可能在播放中出现“短暂失败 → 可重新播放同一首”的回退行为。建议保留上一次成功值,仅在成功获取时更新。

♻️ 建议调整
-				if (isMounted && currentRequestId === lastRequestIdRef.current) {
-					toastAndLogError('读取当前曲目信息失败', e, 'Hooks.useCurrentTrackId')
-					setTrackId(undefined)
-				}
+				if (isMounted && currentRequestId === lastRequestIdRef.current) {
+					toastAndLogError('读取当前曲目信息失败', e, 'Hooks.useCurrentTrackId')
+					// 保留上一次成功的 trackId,避免误放开防重播
+				}

Comment thread src/components/modals/PlayerQueueModal.tsx
Comment thread src/hooks/player/useCurrentTrackId.ts
…ting on track finish, and add a null check in the player queue modal.
@roitium roitium merged commit 1eb8e8f into dev Jan 25, 2026
1 of 2 checks passed
@roitium roitium deleted the fix/issue-166-playlist-click branch January 25, 2026 10:53
roitium added a commit that referenced this pull request Jan 25, 2026
Co-authored-by: roitium <roitium@users.noreply.github.com>
Co-authored-by: Roitium. <65794453+roitium@users.noreply.github.com>
Co-authored-by: longlin li <gnulonglin@gmail.com>
fix: prevent progress bar regression & add debounce to PlayButton (#153)
fix: improve error message for failed lyric providers (BBPLAYER-5Q) (#157)
fix: update PlaySlide info after song's change (#159)
fix: prevent re-playing when click same track. (#168)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 点击同一首歌曲不应重新播放

1 participant