From 5e030c2a07317e073b2323650db32c5516685ef6 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Sun, 17 May 2026 18:43:46 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20refactor=20useMu?= =?UTF-8?q?teParticipant=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix unreachable code when notifying participants that they were muted. Prevent unnecessary function re-creations when props remain unchanged. Also guard against missing tokens by logging an error and returning early when the token is undefined. --- .../src/features/rooms/api/muteParticipant.ts | 101 ++++++++++-------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/src/frontend/src/features/rooms/api/muteParticipant.ts b/src/frontend/src/features/rooms/api/muteParticipant.ts index 3b8418d4..66f8dd35 100644 --- a/src/frontend/src/features/rooms/api/muteParticipant.ts +++ b/src/frontend/src/features/rooms/api/muteParticipant.ts @@ -8,59 +8,72 @@ import { import { fetchApi } from '@/api/fetchApi' import { useIsAdminOrOwner } from '../livekit/hooks/useIsAdminOrOwner' +import { useCallback } from 'react' + export const useMuteParticipant = () => { const apiRoomData = useRoomData() const { notifyParticipants } = useNotifyParticipants() const isAdminOrOwner = useIsAdminOrOwner() - const muteParticipant = async (participant: Participant) => { - if (!apiRoomData?.livekit?.room) { - throw new Error('Room id is not available') - } - const trackSid = participant.getTrackPublication( - Source.Microphone - )?.trackSid + const muteParticipant = useCallback( + async (participant: Participant) => { + if (!apiRoomData?.livekit?.room) { + throw new Error('Room id is not available') + } - if (!trackSid) { - return - } + const trackSid = participant.getTrackPublication( + Source.Microphone + )?.trackSid - const headers = !isAdminOrOwner - ? { - Authorization: `Bearer ${apiRoomData?.livekit?.token}`, - } - : undefined + if (!trackSid) { + return + } + + // Guard against undefined token for non-admin users + if (!isAdminOrOwner && !apiRoomData.livekit.token) { + console.error('Cannot mute participant: missing auth token') + return + } + + const headers = !isAdminOrOwner + ? { Authorization: `Bearer ${apiRoomData.livekit.token}` } + : undefined + + let response + try { + response = await fetchApi( + `rooms/${apiRoomData.livekit.room}/mute-participant/`, + { + method: 'POST', + headers, + body: JSON.stringify({ + participant_identity: participant.identity, + track_sid: trackSid, + }), + } + ) + } catch (error) { + console.error( + `Failed to mute participant ${participant.identity}: ${error instanceof Error ? error.message : 'Unknown error'}` + ) + return + } + + try { + await notifyParticipants({ + type: NotificationType.ParticipantMuted, + destinationIdentities: [participant.identity], + }) + } catch (e) { + console.error( + `Failed to notify muted participant ${participant.identity}: ${e}` + ) + } - try { - const response = fetchApi( - `rooms/${apiRoomData?.livekit?.room}/mute-participant/`, - { - method: 'POST', - headers, - body: JSON.stringify({ - participant_identity: participant.identity, - track_sid: trackSid, - }), - } - ) return response - } catch (error) { - console.error( - `Failed to mute participant ${participant.identity}: ${error instanceof Error ? error.message : 'Unknown error'}` - ) - return - } + }, + [apiRoomData, isAdminOrOwner, notifyParticipants] + ) - try { - await notifyParticipants({ - type: NotificationType.ParticipantMuted, - destinationIdentities: [participant.identity], - }) - } catch (e) { - console.error( - `Failed to notify muted participant ${participant.identity}: ${e}` - ) - } - } return { muteParticipant } }