Merge pull request #79 from daily-demos/use-screen-share
use useScreenShare hook - Daily react hooks
This commit is contained in:
commit
4f80d11309
|
|
@ -2,6 +2,7 @@ import React, { useState, useCallback } from 'react';
|
||||||
import { CallProvider } from '@custom/shared/contexts/CallProvider';
|
import { CallProvider } from '@custom/shared/contexts/CallProvider';
|
||||||
import { MediaDeviceProvider } from '@custom/shared/contexts/MediaDeviceProvider';
|
import { MediaDeviceProvider } from '@custom/shared/contexts/MediaDeviceProvider';
|
||||||
import { ParticipantsProvider } from '@custom/shared/contexts/ParticipantsProvider';
|
import { ParticipantsProvider } from '@custom/shared/contexts/ParticipantsProvider';
|
||||||
|
import { ScreenShareProvider } from '@custom/shared/contexts/ScreenShareProvider';
|
||||||
import { TracksProvider } from '@custom/shared/contexts/TracksProvider';
|
import { TracksProvider } from '@custom/shared/contexts/TracksProvider';
|
||||||
import { UIStateProvider } from '@custom/shared/contexts/UIStateProvider';
|
import { UIStateProvider } from '@custom/shared/contexts/UIStateProvider';
|
||||||
import { WaitingRoomProvider } from '@custom/shared/contexts/WaitingRoomProvider';
|
import { WaitingRoomProvider } from '@custom/shared/contexts/WaitingRoomProvider';
|
||||||
|
|
@ -125,7 +126,9 @@ export default function Index({
|
||||||
<TracksProvider>
|
<TracksProvider>
|
||||||
<MediaDeviceProvider>
|
<MediaDeviceProvider>
|
||||||
<WaitingRoomProvider>
|
<WaitingRoomProvider>
|
||||||
{customAppComponent || <App />}
|
<ScreenShareProvider>
|
||||||
|
{customAppComponent || <App />}
|
||||||
|
</ScreenShareProvider>
|
||||||
</WaitingRoomProvider>
|
</WaitingRoomProvider>
|
||||||
</MediaDeviceProvider>
|
</MediaDeviceProvider>
|
||||||
</TracksProvider>
|
</TracksProvider>
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,23 @@
|
||||||
import React, { useMemo } from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { TrayButton } from '@custom/shared/components/Tray';
|
import { TrayButton } from '@custom/shared/components/Tray';
|
||||||
import { useCallState } from '@custom/shared/contexts/CallProvider';
|
import { useCallState } from '@custom/shared/contexts/CallProvider';
|
||||||
import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider';
|
import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider';
|
||||||
|
import { useScreenShare } from '@custom/shared/contexts/ScreenShareProvider';
|
||||||
import { ReactComponent as IconShare } from '@custom/shared/icons/share-sm.svg';
|
import { ReactComponent as IconShare } from '@custom/shared/icons/share-sm.svg';
|
||||||
|
|
||||||
const MAX_SCREEN_SHARES = 2;
|
|
||||||
|
|
||||||
export const ScreenShareTray = () => {
|
export const ScreenShareTray = () => {
|
||||||
const { callObject, enableScreenShare } = useCallState();
|
const { enableScreenShare } = useCallState();
|
||||||
const { screens, participants, localParticipant } = useParticipants();
|
const { localParticipant } = useParticipants();
|
||||||
|
const {
|
||||||
const isSharingScreen = useMemo(
|
isSharingScreen,
|
||||||
() => screens.some((s) => s.isLocal),
|
isDisabled,
|
||||||
[screens]
|
startScreenShare,
|
||||||
);
|
stopScreenShare
|
||||||
|
} = useScreenShare();
|
||||||
const screensLength = useMemo(() => screens.length, [screens]);
|
|
||||||
|
|
||||||
const toggleScreenShare = () =>
|
const toggleScreenShare = () =>
|
||||||
isSharingScreen ? callObject.stopScreenShare() : callObject.startScreenShare();
|
isSharingScreen ? stopScreenShare() : startScreenShare();
|
||||||
|
|
||||||
const disabled =
|
|
||||||
participants.length &&
|
|
||||||
screensLength >= MAX_SCREEN_SHARES &&
|
|
||||||
!isSharingScreen;
|
|
||||||
|
|
||||||
if (!enableScreenShare) return null;
|
if (!enableScreenShare) return null;
|
||||||
if (!localParticipant.isOwner) return null;
|
if (!localParticipant.isOwner) return null;
|
||||||
|
|
@ -33,7 +26,7 @@ export const ScreenShareTray = () => {
|
||||||
<TrayButton
|
<TrayButton
|
||||||
label={isSharingScreen ? 'Stop': 'Share'}
|
label={isSharingScreen ? 'Stop': 'Share'}
|
||||||
orange={isSharingScreen}
|
orange={isSharingScreen}
|
||||||
disabled={disabled}
|
disabled={isDisabled}
|
||||||
onClick={toggleScreenShare}
|
onClick={toggleScreenShare}
|
||||||
>
|
>
|
||||||
<IconShare />
|
<IconShare />
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import React from 'react';
|
||||||
import { CallProvider } from '@custom/shared/contexts/CallProvider';
|
import { CallProvider } from '@custom/shared/contexts/CallProvider';
|
||||||
import { MediaDeviceProvider } from '@custom/shared/contexts/MediaDeviceProvider';
|
import { MediaDeviceProvider } from '@custom/shared/contexts/MediaDeviceProvider';
|
||||||
import { ParticipantsProvider } from '@custom/shared/contexts/ParticipantsProvider';
|
import { ParticipantsProvider } from '@custom/shared/contexts/ParticipantsProvider';
|
||||||
|
import { ScreenShareProvider } from '@custom/shared/contexts/ScreenShareProvider';
|
||||||
import { TracksProvider } from '@custom/shared/contexts/TracksProvider';
|
import { TracksProvider } from '@custom/shared/contexts/TracksProvider';
|
||||||
import { UIStateProvider } from '@custom/shared/contexts/UIStateProvider';
|
import { UIStateProvider } from '@custom/shared/contexts/UIStateProvider';
|
||||||
import { WaitingRoomProvider } from '@custom/shared/contexts/WaitingRoomProvider';
|
import { WaitingRoomProvider } from '@custom/shared/contexts/WaitingRoomProvider';
|
||||||
|
|
@ -40,7 +41,9 @@ const Room = ({
|
||||||
<TracksProvider>
|
<TracksProvider>
|
||||||
<MediaDeviceProvider>
|
<MediaDeviceProvider>
|
||||||
<WaitingRoomProvider>
|
<WaitingRoomProvider>
|
||||||
{customAppComponent || <App />}
|
<ScreenShareProvider>
|
||||||
|
{customAppComponent || <App />}
|
||||||
|
</ScreenShareProvider>
|
||||||
</WaitingRoomProvider>
|
</WaitingRoomProvider>
|
||||||
</MediaDeviceProvider>
|
</MediaDeviceProvider>
|
||||||
</TracksProvider>
|
</TracksProvider>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
import React, { createContext, useContext, useMemo } from 'react';
|
||||||
|
import { useScreenShare as useDailyScreenShare } from '@daily-co/daily-react-hooks';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
|
export const MAX_SCREEN_SHARES = 2;
|
||||||
|
|
||||||
|
const ScreenShareContext = createContext(null);
|
||||||
|
|
||||||
|
export const ScreenShareProvider = ({ children }) => {
|
||||||
|
const {
|
||||||
|
isSharingScreen,
|
||||||
|
screens,
|
||||||
|
startScreenShare,
|
||||||
|
stopScreenShare
|
||||||
|
} = useDailyScreenShare();
|
||||||
|
|
||||||
|
const isDisabled = useMemo(() => screens.length >= MAX_SCREEN_SHARES && !isSharingScreen,
|
||||||
|
[isSharingScreen, screens.length]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ScreenShareContext.Provider
|
||||||
|
value={{
|
||||||
|
isSharingScreen,
|
||||||
|
isDisabled,
|
||||||
|
screens,
|
||||||
|
startScreenShare,
|
||||||
|
stopScreenShare,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</ScreenShareContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
ScreenShareProvider.propTypes = {
|
||||||
|
children: PropTypes.node,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useScreenShare = () => useContext(ScreenShareContext);
|
||||||
Loading…
Reference in New Issue