Skip to content

Commit 77565d0

Browse files
committed
fix: defer mic permission request until audioInput is enabled
Previously, refreshInputDevices and watchAudioInputDevices were called on mount regardless of audioInput value, triggering mic permission prompts. Now these only run when audioInput is true, allowing apps to start in text-only mode without permission prompts. Also made setAudioInput and setAudioOutput async to properly await the underlying SDK methods.
1 parent c85a9ac commit 77565d0

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ node_modules/
1515

1616
# OS files
1717
.DS_Store
18-
Thumbs.db
18+
Thumbs.db
19+
20+
.npm-cache

src/index.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,21 @@ const useLayercodeAgent = (
148148
}, [preferredInputDeviceId]);
149149

150150
useEffect(() => {
151-
if (typeof window === 'undefined') {
151+
// Only refresh input devices if audioInput is enabled
152+
// This prevents requesting microphone permissions when audio is disabled
153+
if (typeof window === 'undefined' || !audioInput) {
152154
return;
153155
}
154156

155157
refreshInputDevices().catch((error) => {
156158
console.warn('Layercode: failed to load microphone devices', error);
157159
});
158-
}, [refreshInputDevices]);
160+
}, [refreshInputDevices, audioInput]);
159161

160162
useEffect(() => {
161-
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
163+
// Only watch for device changes if audioInput is enabled
164+
// This prevents requesting microphone permissions when audio is disabled
165+
if (typeof window === 'undefined' || typeof navigator === 'undefined' || !audioInput) {
162166
return;
163167
}
164168

@@ -176,11 +180,11 @@ const useLayercodeAgent = (
176180
return () => {
177181
unsubscribe?.();
178182
};
179-
}, []);
183+
}, [audioInput]);
180184

181185
const createClient = useCallback(
182186
(initialConversationId: string | null) => {
183-
console.log('Creating LayercodeClient instance');
187+
console.log('Creating LayercodeClient instance with audioInput:', audioInput, 'audioOutput:', audioOutput);
184188
const client = new LayercodeClient({
185189
agentId,
186190
conversationId: initialConversationId,
@@ -310,19 +314,19 @@ const useLayercodeAgent = (
310314
}, []);
311315

312316
const setAudioInput = useCallback(
313-
(state: React.SetStateAction<boolean>) => {
317+
async (state: React.SetStateAction<boolean>) => {
314318
_setAudioInput(state);
315319
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioInput) : state;
316-
clientRef.current?.setAudioInput(next);
320+
await clientRef.current?.setAudioInput(next);
317321
},
318322
[_setAudioInput, clientRef, audioInput]
319323
);
320324

321325
const setAudioOutput = useCallback(
322-
(state: React.SetStateAction<boolean>) => {
326+
async (state: React.SetStateAction<boolean>) => {
323327
_setAudioOutput(state);
324328
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioOutput) : state;
325-
clientRef.current?.setAudioOutput(next);
329+
await clientRef.current?.setAudioOutput(next);
326330
},
327331
[_setAudioOutput, clientRef, audioOutput]
328332
);

0 commit comments

Comments
 (0)