Skip to content

Commit

Permalink
Merge pull request #5582 from daily-co/eng-4927-add-hook-to-get-local…
Browse files Browse the repository at this point in the history
…-participant

ENG-4927 Implement useLocalSessionId hook
  • Loading branch information
Regaddi authored Jul 13, 2022
2 parents 9d33ad9 + cc91645 commit 55fcff3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/hooks/useLocalSessionId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRecoilValue } from 'recoil';

import { localIdState } from '../DailyParticipants';

/**
* Returns the local participant's session_id or null,
* if the local participant doesn't exist.
*/
export const useLocalSessionId = () => {
return useRecoilValue(localIdState) || null;
};
14 changes: 7 additions & 7 deletions src/hooks/useRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import { useDaily } from './useDaily';
import { useDailyEvent } from './useDailyEvent';
import { useLocalParticipant } from './useLocalParticipant';
import { useLocalSessionId } from './useLocalSessionId';
import { useParticipantIds } from './useParticipantIds';

interface UseRecordingArgs {
Expand Down Expand Up @@ -85,7 +85,7 @@ export const useRecording = ({
const state = useRecoilValue(recordingState);
const setState = useSetRecoilState(recordingState);

const localParticipant = useLocalParticipant();
const localSessionId = useLocalSessionId();

const recordingParticipantIds = useParticipantIds({
filter: 'record',
Expand All @@ -96,7 +96,7 @@ export const useRecording = ({
useEffect(() => {
const hasRecordingParticipants = recordingParticipantIds.length > 0;
const isLocalParticipantRecording = recordingParticipantIds.includes(
localParticipant?.session_id ?? 'local'
localSessionId ?? 'local'
);
setState((s) => ({
...s,
Expand All @@ -119,7 +119,7 @@ export const useRecording = ({
*/
type: hasRecordingParticipants ? 'local' : s?.type,
}));
}, [localParticipant?.session_id, recordingParticipantIds, setState]);
}, [localSessionId, recordingParticipantIds, setState]);

useDailyEvent(
'recording-started',
Expand All @@ -131,9 +131,9 @@ export const useRecording = ({
case 'cloud-beta':
case 'cloud': {
if (
localParticipant &&
localSessionId &&
ev.layout?.preset === 'single-participant' &&
ev.layout.session_id !== localParticipant?.session_id
ev.layout.session_id !== localSessionId
) {
isLocalParticipantRecorded = false;
}
Expand All @@ -153,7 +153,7 @@ export const useRecording = ({
});
setTimeout(() => onRecordingStarted?.(ev), 0);
},
[localParticipant, onRecordingStarted]
[localSessionId, onRecordingStarted]
)
);
useDailyEvent(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { StatefulDevice, useDevices } from './hooks/useDevices';
export { useInputSettings } from './hooks/useInputSettings';
export { useLiveStreaming } from './hooks/useLiveStreaming';
export { useLocalParticipant } from './hooks/useLocalParticipant';
export { useLocalSessionId } from './hooks/useLocalSessionId';
export { useMediaTrack } from './hooks/useMediaTrack';
export { useNetwork } from './hooks/useNetwork';
export { useParticipant } from './hooks/useParticipant';
Expand Down
50 changes: 50 additions & 0 deletions test/hooks/useLocalSessionId.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// <reference types="@types/jest" />

import DailyIframe, { DailyCall } from '@daily-co/daily-js';
import { renderHook } from '@testing-library/react-hooks';
import React from 'react';

import { DailyProvider } from '../../src/DailyProvider';
import { useLocalSessionId } from '../../src/hooks/useLocalSessionId';

/**
* Mock DailyRoom.
* It's not required for useLocalSessionId and causes unwanted state updates.
*/
jest.mock('../../src/DailyRoom', () => ({
DailyRoom: (({ children }) => <>{children}</>) as React.FC,
}));

const createWrapper =
(callObject: DailyCall = DailyIframe.createCallObject()): React.FC =>
({ children }) =>
<DailyProvider callObject={callObject}>{children}</DailyProvider>;

describe('useLocalSessionId', () => {
it('returns null, if daily.participants() does not contain local user yet', async () => {
const daily = DailyIframe.createCallObject();
(daily.participants as jest.Mock).mockImplementation(() => ({}));
const { result, waitFor } = renderHook(() => useLocalSessionId(), {
wrapper: createWrapper(daily),
});
await waitFor(() => {
expect(result.current).toBeNull();
});
});
it('returns local user session_id', async () => {
const daily = DailyIframe.createCallObject();
(daily.participants as jest.Mock).mockImplementation(() => ({
local: {
local: true,
session_id: 'local',
user_name: '',
},
}));
const { result, waitFor } = renderHook(() => useLocalSessionId(), {
wrapper: createWrapper(daily),
});
await waitFor(() => {
expect(result.current).toEqual('local');
});
});
});
4 changes: 2 additions & 2 deletions test/hooks/useRecording.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jest.mock('../../src/DailyRoom', () => ({

const localId = faker.datatype.uuid();

jest.mock('../../src/hooks/useLocalParticipant', () => ({
useLocalParticipant: () => ({ session_id: localId }),
jest.mock('../../src/hooks/useLocalSessionId', () => ({
useLocalSessionId: () => localId,
}));

const createWrapper =
Expand Down

0 comments on commit 55fcff3

Please sign in to comment.