Skip to content

Commit

Permalink
Add useIsRecording hook (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Jul 29, 2024
1 parent c2d5bc5 commit ddb2451
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-geese-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/components-core": patch
"@livekit/components-react": patch
---

Add useIsRecording hook
3 changes: 3 additions & 0 deletions packages/core/etc/components-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ export type ReceivedTranscriptionSegment = TranscriptionSegment & {
receivedAt: number;
};

// @public (undocumented)
export function recordingStatusObservable(room: Room): Observable<boolean>;

// @public (undocumented)
export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/observables/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,10 @@ export function encryptionStatusObservable(room: Room, participant: Participant)
),
);
}

export function recordingStatusObservable(room: Room) {
return roomEventSelector(room, RoomEvent.RecordingStatusChanged).pipe(
map(([recording]) => recording),
startWith(room.isRecording),
);
}
22 changes: 22 additions & 0 deletions packages/react/src/hooks/useIsRecording.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { recordingStatusObservable } from '@livekit/components-core';
import * as React from 'react';
import { useRoomContext } from '../context';
import { useObservableState } from './internal';
import { useConnectionState } from './useConnectionStatus';

/**
* The `useIsRecording` hook returns a `boolean` that indicates if the room is currently being recorded.
* @example
* ```tsx
* const isRecording = useIsRecording();
* ```
* @public
*/
export function useIsRecording() {
const room = useRoomContext();
const connectionState = useConnectionState(room);
const observable = React.useMemo(() => recordingStatusObservable(room), [room, connectionState]);
const isRecording = useObservableState(observable, room.isRecording);

return isRecording;
}

0 comments on commit ddb2451

Please sign in to comment.