@@ -5,6 +5,7 @@ import { getKickBuffer } from "@/components/dashboard/Metronome";
55import { IS_DEMO_MODE } from "@/lib/demo" ;
66import { IS_P2P_MODE } from "@/lib/p2p" ;
77import { getLocalTrack } from "@/p2p/audio/localTracks" ;
8+ import { reconcileP2PAudioSources } from "@/p2p/audio/availableSources" ;
89import { loadP2PTrackArrayBuffer } from "@/p2p/audio/transfer" ;
910import { isP2PTrackUrl , parseP2PTrackId } from "@/p2p/audio/urls" ;
1011import { getApiUrl } from "@/lib/urls" ;
@@ -1492,6 +1493,16 @@ export const useGlobalStore = create<GlobalState>((set, get) => {
14921493 await initializationMutex . waitForUnlock ( ) ;
14931494 }
14941495
1496+ let playlistSources = sources ;
1497+ if ( IS_P2P_MODE ) {
1498+ playlistSources = await reconcileP2PAudioSources ( sources , "room" ) ;
1499+ }
1500+
1501+ let activeSource = currentAudioSource ;
1502+ if ( activeSource && ! playlistSources . some ( ( s ) => s . url === activeSource ) ) {
1503+ activeSource = undefined ;
1504+ }
1505+
14951506 const state = get ( ) ;
14961507
14971508 // Clean up buffer access queue - remove URLs that are no longer in the playlist
@@ -1504,22 +1515,22 @@ export const useGlobalStore = create<GlobalState>((set, get) => {
15041515 const newQueue : string [ ] = [ ] ;
15051516
15061517 // Add current/selected track first (highest priority)
1507- if ( currentAudioSource && sources . some ( ( s ) => s . url === currentAudioSource ) ) {
1508- newQueue . push ( currentAudioSource ) ;
1509- } else if ( state . selectedAudioUrl && sources . some ( ( s ) => s . url === state . selectedAudioUrl ) ) {
1518+ if ( activeSource && playlistSources . some ( ( s ) => s . url === activeSource ) ) {
1519+ newQueue . push ( activeSource ) ;
1520+ } else if ( state . selectedAudioUrl && playlistSources . some ( ( s ) => s . url === state . selectedAudioUrl ) ) {
15101521 newQueue . push ( state . selectedAudioUrl ) ;
15111522 }
15121523
15131524 // Add remaining tracks in playlist order
1514- sources . forEach ( ( source ) => {
1525+ playlistSources . forEach ( ( source ) => {
15151526 if ( ! newQueue . includes ( source . url ) ) {
15161527 newQueue . push ( source . url ) ;
15171528 }
15181529 } ) ;
15191530
15201531 // Create new audioSources array (preserving loaded buffers for tracks still in playlist)
15211532 const existingByUrl = new Map ( state . audioSources . map ( ( as ) => [ as . source . url , as ] ) ) ;
1522- const newAudioSources : AudioSourceState [ ] = sources . map ( ( source ) => {
1533+ const newAudioSources : AudioSourceState [ ] = playlistSources . map ( ( source ) => {
15231534 const existing = existingByUrl . get ( source . url ) ;
15241535 if ( existing ) {
15251536 return existing ;
@@ -1534,15 +1545,15 @@ export const useGlobalStore = create<GlobalState>((set, get) => {
15341545 set ( { audioSources : newAudioSources , bufferAccessQueue : newQueue } ) ;
15351546
15361547 // If currentAudioSource is provided from server, update selectedAudioUrl and start loading it
1537- if ( currentAudioSource ) {
1538- set ( { selectedAudioUrl : currentAudioSource } ) ;
1539- loadAudioSource ( currentAudioSource ) ;
1548+ if ( activeSource ) {
1549+ set ( { selectedAudioUrl : activeSource } ) ;
1550+ loadAudioSource ( activeSource ) ;
15401551 }
15411552
15421553 // In demo mode, eagerly load remaining sources if sync is done.
15431554 // In prod, sources load on-demand when the user selects them.
15441555 if ( IS_DEMO_MODE && get ( ) . isSynced ) {
1545- eagerLoadIdleSources ( { skip : currentAudioSource } ) ;
1556+ eagerLoadIdleSources ( { skip : activeSource } ) ;
15461557 }
15471558
15481559 // Check if the currently selected/playing track was removed
0 commit comments