Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
164 lines
4.9 KiB
TypeScript
164 lines
4.9 KiB
TypeScript
import { useStableCallback } from '@rocket.chat/fuselage-hooks';
|
|
import type { Device, DeviceContextValue } from '@rocket.chat/ui-contexts';
|
|
import { DeviceContext } from '@rocket.chat/ui-contexts';
|
|
import { useQuery, useQueryClient, keepPreviousData } from '@tanstack/react-query';
|
|
import type { ReactNode } from 'react';
|
|
import { useEffect, useState, useMemo } from 'react';
|
|
|
|
import { isSetSinkIdAvailable } from './lib/isSetSinkIdAvailable';
|
|
|
|
export type DeviceProviderProps = {
|
|
children?: ReactNode | undefined;
|
|
};
|
|
|
|
const defaultDevices = {
|
|
audioInput: [],
|
|
audioOutput: [],
|
|
defaultAudioOutputDevice: {
|
|
id: '',
|
|
label: '',
|
|
type: 'audiooutput',
|
|
},
|
|
defaultAudioInputDevice: {
|
|
id: '',
|
|
label: '',
|
|
type: 'audioinput',
|
|
},
|
|
};
|
|
|
|
const devicesQueryKey = ['media-devices-list'];
|
|
|
|
export const DeviceProvider = ({ children }: DeviceProviderProps) => {
|
|
const [enabled] = useState(typeof isSecureContext && isSecureContext);
|
|
const [selectedAudioOutputDevice, setSelectedAudioOutputDevice] = useState<Device | undefined>(undefined);
|
|
const [selectedAudioInputDevice, setSelectedAudioInputDevice] = useState<Device | undefined>(undefined);
|
|
|
|
const setAudioInputDevice = (device: Device): void => {
|
|
if (!isSecureContext) {
|
|
throw new Error('Device Changes are not available on insecure contexts');
|
|
}
|
|
setSelectedAudioInputDevice(device);
|
|
};
|
|
|
|
const setAudioOutputDevice = useStableCallback(
|
|
({ outputDevice, HTMLAudioElement }: { outputDevice: Device; HTMLAudioElement: HTMLAudioElement }): void => {
|
|
if (!isSetSinkIdAvailable()) {
|
|
throw new Error('setSinkId is not available in this browser');
|
|
}
|
|
if (!enabled) {
|
|
throw new Error('Device Changes are not available on insecure contexts');
|
|
}
|
|
setSelectedAudioOutputDevice(outputDevice);
|
|
HTMLAudioElement.setSinkId(outputDevice.id);
|
|
},
|
|
);
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const { data } = useQuery({
|
|
queryKey: devicesQueryKey,
|
|
enabled,
|
|
queryFn: async () => {
|
|
const devices = await navigator.mediaDevices?.enumerateDevices();
|
|
if (!devices || devices.length === 0) {
|
|
return defaultDevices;
|
|
}
|
|
|
|
const mappedDevices: Device[] = devices.map((device) => ({
|
|
id: device.deviceId,
|
|
label: device.label,
|
|
type: device.kind,
|
|
}));
|
|
|
|
const filteredInput = mappedDevices.filter((device) => device.type === 'audioinput');
|
|
|
|
const filteredOutput = mappedDevices.filter((device) => device.type === 'audiooutput');
|
|
|
|
const audioInput = filteredInput.length > 0 ? filteredInput : [defaultDevices.defaultAudioInputDevice];
|
|
const audioOutput = filteredOutput.length > 0 ? filteredOutput : [defaultDevices.defaultAudioOutputDevice];
|
|
|
|
return {
|
|
audioInput,
|
|
audioOutput,
|
|
defaultAudioOutputDevice: audioOutput[0],
|
|
defaultAudioInputDevice: audioInput[0],
|
|
};
|
|
},
|
|
initialData: defaultDevices,
|
|
placeholderData: keepPreviousData,
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: true,
|
|
staleTime: 0,
|
|
});
|
|
|
|
const { data: permissionStatus } = useQuery({
|
|
queryKey: [...devicesQueryKey, 'permission-status'],
|
|
queryFn: async () => {
|
|
if (!navigator.permissions) {
|
|
return;
|
|
}
|
|
const result = await navigator.permissions.query({ name: 'microphone' as PermissionName });
|
|
return result;
|
|
},
|
|
initialData: undefined,
|
|
placeholderData: undefined,
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: true,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!permissionStatus) {
|
|
return;
|
|
}
|
|
const invalidateQueries = (): void => {
|
|
queryClient.invalidateQueries({ queryKey: devicesQueryKey });
|
|
};
|
|
|
|
permissionStatus.addEventListener('change', invalidateQueries);
|
|
|
|
return (): void => {
|
|
permissionStatus.removeEventListener('change', invalidateQueries);
|
|
};
|
|
}, [permissionStatus, queryClient]);
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !navigator.mediaDevices) {
|
|
return;
|
|
}
|
|
|
|
const invalidateQuery = (): void => {
|
|
queryClient.invalidateQueries({ queryKey: devicesQueryKey, exact: true });
|
|
};
|
|
|
|
navigator.mediaDevices.addEventListener('devicechange', invalidateQuery);
|
|
|
|
return (): void => {
|
|
navigator.mediaDevices.removeEventListener('devicechange', invalidateQuery);
|
|
};
|
|
}, [enabled, queryClient]);
|
|
|
|
const contextValue = useMemo((): DeviceContextValue => {
|
|
if (!enabled) {
|
|
return {
|
|
enabled,
|
|
};
|
|
}
|
|
const { audioInput, audioOutput, defaultAudioOutputDevice, defaultAudioInputDevice } = data;
|
|
|
|
return {
|
|
enabled,
|
|
permissionStatus,
|
|
availableAudioOutputDevices: audioOutput,
|
|
availableAudioInputDevices: audioInput,
|
|
selectedAudioOutputDevice: selectedAudioOutputDevice || defaultAudioOutputDevice,
|
|
selectedAudioInputDevice: selectedAudioInputDevice || defaultAudioInputDevice,
|
|
setAudioOutputDevice,
|
|
setAudioInputDevice,
|
|
};
|
|
}, [enabled, data, permissionStatus, selectedAudioOutputDevice, selectedAudioInputDevice, setAudioOutputDevice]);
|
|
|
|
return <DeviceContext.Provider value={contextValue}>{children}</DeviceContext.Provider>;
|
|
};
|