Skip to content

Commit d4a1b39

Browse files
committed
fix: preserve device selection after joining call
This PR fixes an issue where currentMic, currentCam, and currentSpeaker from useDevices() become undefined after joining a call, despite micState remaining 'granted' and tracks working correctly. Root cause: The mapDevice function in DailyDevices.tsx was not handling the case where daily.getInputDevices() returns empty objects (during race conditions or timing issues). This caused all devices to lose their selected state. Solution: Modified mapDevice to preserve previous device selection when getInputDevices() returns empty objects, while maintaining normal behavior when valid device info is returned. Changes: - Modified mapDevice function in DailyDevices.tsx to check and preserve previous selection state - Maintains backward compatibility with existing functionality - Ensures stable device state throughout the call lifecycle
1 parent abacd15 commit d4a1b39

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/DailyDevices.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ export const DailyDevices: React.FC<React.PropsWithChildren<unknown>> = ({
9191
device: {} | MediaDeviceInfo,
9292
d: MediaDeviceInfo,
9393
prevDevices: StatefulDevice[]
94-
) => ({
95-
device: d,
96-
selected: 'deviceId' in device && d.deviceId === device.deviceId,
97-
state:
98-
prevDevices.find((p) => p.device.deviceId === d.deviceId)
99-
?.state ?? 'granted',
100-
});
94+
) => {
95+
const prevDevice = prevDevices.find(
96+
(p) => p.device.deviceId === d.deviceId
97+
);
98+
// Determine if this device is selected
99+
// First, check if getInputDevices() returned a valid deviceId
100+
const isSelectedByDeviceId =
101+
'deviceId' in device && d.deviceId === device.deviceId;
102+
// If getInputDevices() returned an empty object, preserve the previous selection
103+
const isSelectedByPrevious =
104+
!('deviceId' in device) && prevDevice?.selected;
105+
106+
return {
107+
device: d,
108+
selected: isSelectedByDeviceId || Boolean(isSelectedByPrevious),
109+
state: prevDevice?.state ?? 'granted',
110+
};
111+
};
101112
const sortDeviceByLabel = (a: StatefulDevice, b: StatefulDevice) => {
102113
if (a.device.deviceId === 'default') return -1;
103114
if (b.device.deviceId === 'default') return 1;

0 commit comments

Comments
 (0)