From 338f8d8a69522b8bd1664f66fdff351ea154db9c Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Wed, 13 Aug 2025 10:12:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20refactor=20useRe?= =?UTF-8?q?solveDefaultDeviceId=20to=20run=20initially?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limit useResolveDefaultDeviceId hook execution to initial rendering only, considering removal due to ux concerns. Hook's purpose is to populate select fields when 'default' device option isn't available in enumerated device list. Current implementation may be too confusing for code execution. --- .../src/features/rooms/components/Join.tsx | 14 +++++++++++--- ...ce.ts => useResolveInitiallyDefaultDeviceId.ts} | 12 ++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) rename src/frontend/src/features/rooms/livekit/hooks/{useResolveDefaultDevice.ts => useResolveInitiallyDefaultDeviceId.ts} (50%) diff --git a/src/frontend/src/features/rooms/components/Join.tsx b/src/frontend/src/features/rooms/components/Join.tsx index 271ff65f..406dac21 100644 --- a/src/frontend/src/features/rooms/components/Join.tsx +++ b/src/frontend/src/features/rooms/components/Join.tsx @@ -30,7 +30,7 @@ import { useSnapshot } from 'valtio' import { openPermissionsDialog, permissionsStore } from '@/stores/permissions' import { ToggleDevice } from './join/ToggleDevice' import { SelectDevice } from './join/SelectDevice' -import { useResolveDefaultDeviceId } from '../livekit/hooks/useResolveDefaultDevice' +import { useResolveInitiallyDefaultDeviceId } from '../livekit/hooks/useResolveInitiallyDefaultDeviceId' import { isSafari } from '@/utils/livekit' import type { LocalUserChoices } from '@/stores/userChoices' @@ -163,8 +163,16 @@ export const Join = ({ // LiveKit by default populates device choices with "default" value. // Instead, use the current device id used by the preview track as a default - useResolveDefaultDeviceId(audioDeviceId, audioTrack, saveAudioInputDeviceId) - useResolveDefaultDeviceId(videoDeviceId, videoTrack, saveVideoInputDeviceId) + useResolveInitiallyDefaultDeviceId( + audioDeviceId, + audioTrack, + saveAudioInputDeviceId + ) + useResolveInitiallyDefaultDeviceId( + videoDeviceId, + videoTrack, + saveVideoInputDeviceId + ) const videoEl = useRef(null) const isVideoInitiated = useRef(false) diff --git a/src/frontend/src/features/rooms/livekit/hooks/useResolveDefaultDevice.ts b/src/frontend/src/features/rooms/livekit/hooks/useResolveInitiallyDefaultDeviceId.ts similarity index 50% rename from src/frontend/src/features/rooms/livekit/hooks/useResolveDefaultDevice.ts rename to src/frontend/src/features/rooms/livekit/hooks/useResolveInitiallyDefaultDeviceId.ts index 673b65fc..3af0fc2e 100644 --- a/src/frontend/src/features/rooms/livekit/hooks/useResolveDefaultDevice.ts +++ b/src/frontend/src/features/rooms/livekit/hooks/useResolveInitiallyDefaultDeviceId.ts @@ -1,17 +1,21 @@ -import { useEffect } from 'react' +import { useEffect, useRef } from 'react' -export const useResolveDefaultDeviceId = < +export const useResolveInitiallyDefaultDeviceId = < T extends { getDeviceId(): Promise }, >( currentId: string, track: T | undefined, save: (id: string) => void ) => { + const isInitiated = useRef(false) useEffect(() => { - if (currentId !== 'default' || !track) return + if (currentId !== 'default' || !track || isInitiated.current) return const resolveDefaultDeviceId = async () => { const actualDeviceId = await track.getDeviceId() - if (actualDeviceId && actualDeviceId !== 'default') save(actualDeviceId) + if (actualDeviceId && actualDeviceId !== 'default') { + isInitiated.current = true + save(actualDeviceId) + } } resolveDefaultDeviceId() }, [currentId, track, save])