/* eslint-disable react/prop-types */
import React, { useCallback, useEffect, useState } from 'react';
import {
HMSException,
selectDevices,
selectLocalAudioTrackID,
selectLocalMediaSettings,
selectTrackAudioByID,
useHMSActions,
useHMSStore,
} from '@100mslive/react-sdk';
import { MicOnIcon, SpeakerIcon, StopIcon } from '@100mslive/react-icons';
import { PermissionErrorModal } from '../Prebuilt/components/Notifications/PermissionErrorModal';
import { TestContainer, TestFooter } from './components';
import { Button } from '../Button';
import { Box, Flex } from '../Layout';
import { Progress } from '../Progress';
import { Text } from '../Text';
// @ts-ignore: No implicit any
import { DeviceSelector } from './DeviceSelector';
import { DiagnosticsStep, useDiagnostics } from './DiagnosticsContext';
import { useAudioOutputTest } from '../Prebuilt/components/hooks/useAudioOutputTest';
import { TEST_AUDIO_URL } from '../Prebuilt/common/constants';
const SelectContainer = ({ children }: { children: React.ReactNode }) => (
{children}
);
const MicTest = ({ setError }: { setError: (err?: Error) => void }) => {
const { hmsDiagnostics } = useDiagnostics();
const devices = useHMSStore(selectDevices);
const [isRecording, setIsRecording] = useState(false);
const [selectedMic, setSelectedMic] = useState(devices.audioInput[0]?.deviceId || 'default');
const trackID = useHMSStore(selectLocalAudioTrackID);
const audioLevel = useHMSStore(selectTrackAudioByID(trackID));
const { audioOutputDeviceId } = useHMSStore(selectLocalMediaSettings);
const { playing, setPlaying, audioRef } = useAudioOutputTest({
deviceId: audioOutputDeviceId || devices.audioOutput[0]?.deviceId,
});
return (
}
onChange={(deviceId: string) => {
setError(undefined);
setSelectedMic(deviceId);
hmsDiagnostics?.stopMicCheck();
}}
/>
);
};
const SpeakerTest = () => {
const actions = useHMSActions();
const devices = useHMSStore(selectDevices);
const { audioOutputDeviceId } = useHMSStore(selectLocalMediaSettings);
if (devices.audioOutput.length === 0) {
return <>>;
}
return (
}
onChange={(deviceId: string) => {
actions.setAudioOutputDevice(deviceId);
}}
/>
);
};
export const AudioTest = () => {
const { hmsDiagnostics, updateStep } = useDiagnostics();
const [error, setErrorAlone] = useState();
const setError = useCallback(
(err?: Error) => {
updateStep(DiagnosticsStep.AUDIO, { hasFailed: !!err });
setErrorAlone(err);
},
[updateStep, setErrorAlone],
);
useEffect(() => {
hmsDiagnostics?.requestPermission({ audio: true }).catch(error => setError(error));
}, [hmsDiagnostics, setError]);
return (
<>
Record a 10 second audio clip and play it back to check that your microphone and speaker are working. If they
aren't, make sure your volume is turned up, try a different speaker or microphone, or check your bluetooth
settings.
>
);
};