Skip to content

Commit 2e22d4d

Browse files
committed
add useLocalSessionId
1 parent 5afc68c commit 2e22d4d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
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/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+
});

0 commit comments

Comments
 (0)