signal-desktop/ts/hooks/useActivateSpeakerViewOnPresenting.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useMemo } from 'react';
2023-08-16 20:54:39 +00:00
import type { AciString } from '../types/ServiceId';
2021-09-17 22:24:21 +00:00
import { usePrevious } from './usePrevious';
type RemoteParticipant = {
hasRemoteVideo: boolean;
presenting: boolean;
title: string;
2023-08-16 20:54:39 +00:00
aci?: AciString;
};
export function usePresenter(
remoteParticipants: ReadonlyArray<RemoteParticipant>
): AciString | undefined {
return useMemo(
() => remoteParticipants.find(participant => participant.presenting)?.aci,
[remoteParticipants]
);
}
export function useActivateSpeakerViewOnPresenting({
remoteParticipants,
switchToPresentationView,
switchFromPresentationView,
}: {
remoteParticipants: ReadonlyArray<RemoteParticipant>;
switchToPresentationView: () => void;
switchFromPresentationView: () => void;
}): void {
const presenterAci = usePresenter(remoteParticipants);
2023-08-16 20:54:39 +00:00
const prevPresenterAci = usePrevious(presenterAci, presenterAci);
useEffect(() => {
2023-08-16 20:54:39 +00:00
if (prevPresenterAci !== presenterAci && presenterAci) {
switchToPresentationView();
2023-08-16 20:54:39 +00:00
} else if (prevPresenterAci && !presenterAci) {
switchFromPresentationView();
}
}, [
2023-08-16 20:54:39 +00:00
presenterAci,
prevPresenterAci,
switchToPresentationView,
switchFromPresentationView,
]);
}