Skip to content

Commit ab37c2e

Browse files
authored
Update and use isParticipantTrackPinned function (#141)
* add TrackParticipantPair to core types * update and use isParticipantTrackPinned function
1 parent b397999 commit ab37c2e

File tree

3 files changed

+21
-46
lines changed

3 files changed

+21
-46
lines changed

packages/core/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import type { Participant, Track } from 'livekit-client';
1+
import type { Participant, Track, TrackPublication } from 'livekit-client';
22

33
export type PinState = {
44
pinnedParticipant?: Participant;
55
pinnedSource?: Track.Source;
66
};
7+
8+
export type TrackParticipantPair = {
9+
track: TrackPublication;
10+
participant: Participant;
11+
};

packages/core/src/utils.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import {
2-
LocalParticipant,
3-
Participant,
4-
RemoteParticipant,
5-
Track,
6-
TrackPublication,
7-
} from 'livekit-client';
1+
import { LocalParticipant, Participant, RemoteParticipant, TrackPublication } from 'livekit-client';
82
import type { ClassNames } from '@livekit/components-styles/dist/types/general/styles.css';
93
import type { UnprefixedClassNames } from '@livekit/components-styles/dist/types_unprefixed/styles.scss';
104
import { cssPrefix } from './constants';
11-
import { PinState } from './types';
5+
import { PinState, TrackParticipantPair } from './types';
126
export function kebabize(str: string) {
137
return str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
148
}
@@ -43,41 +37,16 @@ export const attachIfSubscribed = (
4337
};
4438

4539
export function isParticipantTrackPinned(
46-
participant: Participant,
47-
focusState: PinState | undefined,
48-
source: Track.Source,
40+
trackParticipantPair: TrackParticipantPair,
41+
pinState: PinState | undefined,
4942
): boolean {
50-
if (focusState === undefined) {
51-
console.warn(`focusState not set: `, focusState);
52-
return false;
53-
}
54-
55-
if (focusState.pinnedParticipant === undefined || focusState.pinnedSource === undefined) {
56-
console.warn(`focusState not set: `, focusState);
57-
return false;
58-
}
43+
const { track, participant } = trackParticipantPair;
5944

60-
if (focusState.pinnedSource !== source) {
45+
if (pinState === undefined) {
6146
return false;
6247
}
6348

64-
if (focusState.pinnedParticipant.identity === participant.identity) {
65-
console.log(`Participant has same identity as pinned.`, focusState);
66-
switch (focusState.pinnedSource) {
67-
case Track.Source.Camera:
68-
return participant.isCameraEnabled;
69-
break;
70-
case Track.Source.ScreenShare:
71-
return participant.isScreenShareEnabled;
72-
break;
73-
74-
default:
75-
return false;
76-
break;
77-
}
78-
} else {
79-
return false;
80-
}
49+
return pinState.pinnedSource === track.source && pinState.pinnedParticipant === participant;
8150
}
8251

8352
/**

packages/react/src/hooks/track-hooks.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { setupMediaTrack, trackObservable } from '@livekit/components-core';
1+
import {
2+
isParticipantTrackPinned,
3+
setupMediaTrack,
4+
trackObservable,
5+
TrackParticipantPair,
6+
} from '@livekit/components-core';
27
import { Participant, Track, TrackPublication } from 'livekit-client';
38
import * as React from 'react';
49
import { useMaybePinContext } from '../contexts';
@@ -82,7 +87,6 @@ export function useTrack({ pub }: UseTrackProps) {
8287
return { publication, track };
8388
}
8489

85-
export type TrackParticipantPair = { track: TrackPublication; participant: Participant };
8690
export type TracksFilter = Parameters<TrackParticipantPair[]['filter']>['0'];
8791
type UseTracksProps = {
8892
sources: Track.Source[];
@@ -159,11 +163,8 @@ export function useTracks({
159163
});
160164

161165
if (excludePinnedTracks && pinContext) {
162-
sourceParticipantPairs = sourceParticipantPairs.filter(({ track, participant }) =>
163-
pinContext.state?.pinnedSource === track.source &&
164-
participant === pinContext.state.pinnedParticipant
165-
? false
166-
: true,
166+
sourceParticipantPairs = sourceParticipantPairs.filter((trackParticipantPair) =>
167+
isParticipantTrackPinned(trackParticipantPair, pinContext.state),
167168
);
168169
}
169170

0 commit comments

Comments
 (0)