Skip to content

Commit 55fcff3

Browse files
authored
Merge pull request #5582 from daily-co/eng-4927-add-hook-to-get-local-participant
ENG-4927 Implement useLocalSessionId hook
2 parents 9d33ad9 + cc91645 commit 55fcff3

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

src/hooks/useLocalSessionId.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useRecoilValue } from 'recoil';
2+
3+
import { localIdState } from '../DailyParticipants';
4+
5+
/**
6+
* Returns the local participant's session_id or null,
7+
* if the local participant doesn't exist.
8+
*/
9+
export const useLocalSessionId = () => {
10+
return useRecoilValue(localIdState) || null;
11+
};

src/hooks/useRecording.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

1616
import { useDaily } from './useDaily';
1717
import { useDailyEvent } from './useDailyEvent';
18-
import { useLocalParticipant } from './useLocalParticipant';
18+
import { useLocalSessionId } from './useLocalSessionId';
1919
import { useParticipantIds } from './useParticipantIds';
2020

2121
interface UseRecordingArgs {
@@ -85,7 +85,7 @@ export const useRecording = ({
8585
const state = useRecoilValue(recordingState);
8686
const setState = useSetRecoilState(recordingState);
8787

88-
const localParticipant = useLocalParticipant();
88+
const localSessionId = useLocalSessionId();
8989

9090
const recordingParticipantIds = useParticipantIds({
9191
filter: 'record',
@@ -96,7 +96,7 @@ export const useRecording = ({
9696
useEffect(() => {
9797
const hasRecordingParticipants = recordingParticipantIds.length > 0;
9898
const isLocalParticipantRecording = recordingParticipantIds.includes(
99-
localParticipant?.session_id ?? 'local'
99+
localSessionId ?? 'local'
100100
);
101101
setState((s) => ({
102102
...s,
@@ -119,7 +119,7 @@ export const useRecording = ({
119119
*/
120120
type: hasRecordingParticipants ? 'local' : s?.type,
121121
}));
122-
}, [localParticipant?.session_id, recordingParticipantIds, setState]);
122+
}, [localSessionId, recordingParticipantIds, setState]);
123123

124124
useDailyEvent(
125125
'recording-started',
@@ -131,9 +131,9 @@ export const useRecording = ({
131131
case 'cloud-beta':
132132
case 'cloud': {
133133
if (
134-
localParticipant &&
134+
localSessionId &&
135135
ev.layout?.preset === 'single-participant' &&
136-
ev.layout.session_id !== localParticipant?.session_id
136+
ev.layout.session_id !== localSessionId
137137
) {
138138
isLocalParticipantRecorded = false;
139139
}
@@ -153,7 +153,7 @@ export const useRecording = ({
153153
});
154154
setTimeout(() => onRecordingStarted?.(ev), 0);
155155
},
156-
[localParticipant, onRecordingStarted]
156+
[localSessionId, onRecordingStarted]
157157
)
158158
);
159159
useDailyEvent(

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { StatefulDevice, useDevices } from './hooks/useDevices';
99
export { useInputSettings } from './hooks/useInputSettings';
1010
export { useLiveStreaming } from './hooks/useLiveStreaming';
1111
export { useLocalParticipant } from './hooks/useLocalParticipant';
12+
export { useLocalSessionId } from './hooks/useLocalSessionId';
1213
export { useMediaTrack } from './hooks/useMediaTrack';
1314
export { useNetwork } from './hooks/useNetwork';
1415
export { useParticipant } from './hooks/useParticipant';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// <reference types="@types/jest" />
2+
3+
import DailyIframe, { DailyCall } from '@daily-co/daily-js';
4+
import { renderHook } from '@testing-library/react-hooks';
5+
import React from 'react';
6+
7+
import { DailyProvider } from '../../src/DailyProvider';
8+
import { useLocalSessionId } from '../../src/hooks/useLocalSessionId';
9+
10+
/**
11+
* Mock DailyRoom.
12+
* It's not required for useLocalSessionId and causes unwanted state updates.
13+
*/
14+
jest.mock('../../src/DailyRoom', () => ({
15+
DailyRoom: (({ children }) => <>{children}</>) as React.FC,
16+
}));
17+
18+
const createWrapper =
19+
(callObject: DailyCall = DailyIframe.createCallObject()): React.FC =>
20+
({ children }) =>
21+
<DailyProvider callObject={callObject}>{children}</DailyProvider>;
22+
23+
describe('useLocalSessionId', () => {
24+
it('returns null, if daily.participants() does not contain local user yet', async () => {
25+
const daily = DailyIframe.createCallObject();
26+
(daily.participants as jest.Mock).mockImplementation(() => ({}));
27+
const { result, waitFor } = renderHook(() => useLocalSessionId(), {
28+
wrapper: createWrapper(daily),
29+
});
30+
await waitFor(() => {
31+
expect(result.current).toBeNull();
32+
});
33+
});
34+
it('returns local user session_id', async () => {
35+
const daily = DailyIframe.createCallObject();
36+
(daily.participants as jest.Mock).mockImplementation(() => ({
37+
local: {
38+
local: true,
39+
session_id: 'local',
40+
user_name: '',
41+
},
42+
}));
43+
const { result, waitFor } = renderHook(() => useLocalSessionId(), {
44+
wrapper: createWrapper(daily),
45+
});
46+
await waitFor(() => {
47+
expect(result.current).toEqual('local');
48+
});
49+
});
50+
});

test/hooks/useRecording.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ jest.mock('../../src/DailyRoom', () => ({
2424

2525
const localId = faker.datatype.uuid();
2626

27-
jest.mock('../../src/hooks/useLocalParticipant', () => ({
28-
useLocalParticipant: () => ({ session_id: localId }),
27+
jest.mock('../../src/hooks/useLocalSessionId', () => ({
28+
useLocalSessionId: () => localId,
2929
}));
3030

3131
const createWrapper =

0 commit comments

Comments
 (0)