Skip to content

Commit 7091b41

Browse files
authored
Refine visible controls based on canPublishSources permissions (#1160)
1 parent 4b7b643 commit 7091b41

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

.changeset/brown-trainers-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/components-react": patch
3+
---
4+
5+
Refine visible controls based on canPublishSources permissions

packages/react/src/prefabs/ControlBar.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ export type ControlBarControls = {
2323
settings?: boolean;
2424
};
2525

26+
const trackSourceToProtocol = (source: Track.Source) => {
27+
// NOTE: this mapping avoids importing the protocol package as that leads to a significant bundle size increase
28+
switch (source) {
29+
case Track.Source.Camera:
30+
return 1;
31+
case Track.Source.Microphone:
32+
return 2;
33+
case Track.Source.ScreenShare:
34+
return 3;
35+
default:
36+
return 0;
37+
}
38+
};
39+
2640
/** @public */
2741
export interface ControlBarProps extends React.HTMLAttributes<HTMLDivElement> {
2842
onDeviceError?: (error: { source: Track.Source; error: Error }) => void;
@@ -82,9 +96,16 @@ export function ControlBar({
8296
visibleControls.microphone = false;
8397
visibleControls.screenShare = false;
8498
} else {
85-
visibleControls.camera ??= localPermissions.canPublish;
86-
visibleControls.microphone ??= localPermissions.canPublish;
87-
visibleControls.screenShare ??= localPermissions.canPublish;
99+
const canPublishSource = (source: Track.Source) => {
100+
return (
101+
localPermissions.canPublish &&
102+
(localPermissions.canPublishSources.length === 0 ||
103+
localPermissions.canPublishSources.includes(trackSourceToProtocol(source)))
104+
);
105+
};
106+
visibleControls.camera ??= canPublishSource(Track.Source.Camera);
107+
visibleControls.microphone ??= canPublishSource(Track.Source.Microphone);
108+
visibleControls.screenShare ??= canPublishSource(Track.Source.ScreenShare);
88109
visibleControls.chat ??= localPermissions.canPublishData && controls?.chat;
89110
}
90111

0 commit comments

Comments
 (0)