@@ -3,7 +3,8 @@ import type { TrackReference } from '@livekit/components-core';
33import { participantTracksObservable } from '@livekit/components-core' ;
44import { useObservableState } from './internal' ;
55import type { Track } from 'livekit-client' ;
6- import { useMaybeParticipantContext , useRoomContext } from '../context' ;
6+ import { useMaybeParticipantContext } from '../context' ;
7+ import { useRemoteParticipants } from './useRemoteParticipants' ;
78
89/**
910 * `useParticipantTracks` is a custom React that allows you to get tracks of a specific participant only, by specifiying the participant's identity.
@@ -14,15 +15,22 @@ export function useParticipantTracks(
1415 sources : Track . Source [ ] ,
1516 participantIdentity ?: string ,
1617) : TrackReference [ ] {
17- const room = useRoomContext ( ) ;
1818 const participantContext = useMaybeParticipantContext ( ) ;
19- const p = participantIdentity
20- ? room . getParticipantByIdentity ( participantIdentity )
21- : participantContext ;
22- const observable = React . useMemo (
23- ( ) => ( p ? participantTracksObservable ( p , { sources } ) : undefined ) ,
24- [ p ?. sid , p ?. identity , JSON . stringify ( sources ) ] ,
25- ) ;
19+ const remoteParticipants = useRemoteParticipants ( { updateOnlyOn : [ ] } ) ;
20+
21+ const p = React . useMemo ( ( ) => {
22+ if ( participantIdentity ) {
23+ return remoteParticipants . find ( ( p ) => p . identity === participantIdentity ) ;
24+ }
25+ return participantContext ;
26+ } , [ participantIdentity , remoteParticipants , participantContext ] ) ;
27+
28+ const observable = React . useMemo ( ( ) => {
29+ if ( ! p ) {
30+ return undefined ;
31+ }
32+ return participantTracksObservable ( p , { sources } ) ;
33+ } , [ p , JSON . stringify ( sources ) ] ) ;
2634
2735 const trackRefs = useObservableState ( observable , [ ] as TrackReference [ ] ) ;
2836
0 commit comments