mirror of
https://github.com/suitenumerique/meet.git
synced 2026-08-14 20:53:26 +00:00
48c0cb320e
Introduce a telemetry module that exposes a `reportError` helper. Under the hood it forwards errors to PostHog, but the module is the only place that knows about PostHog. Replace `console.error` calls used for error reporting with `reportError`, so the codebase now goes through a single, consistent API for telemetry. This normalizes how errors are reported and makes it straightforward to swap PostHog for another backend later on, without touching every call site.
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { type Participant, Track } from 'livekit-client'
|
|
import Source = Track.Source
|
|
import { useRoomData } from '../livekit/hooks/useRoomData'
|
|
import {
|
|
useNotifyParticipants,
|
|
NotificationType,
|
|
} from '@/features/notifications'
|
|
import { fetchApi } from '@/api/fetchApi'
|
|
import { useIsAdminOrOwner } from '../livekit/hooks/useIsAdminOrOwner'
|
|
|
|
import { useCallback } from 'react'
|
|
import { reportError } from '@/features/analytics/telemetry'
|
|
|
|
export const useMuteParticipant = () => {
|
|
const apiRoomData = useRoomData()
|
|
const { notifyParticipants } = useNotifyParticipants()
|
|
const isAdminOrOwner = useIsAdminOrOwner()
|
|
|
|
const muteParticipant = useCallback(
|
|
async (participant: Participant) => {
|
|
if (!apiRoomData?.livekit?.room) {
|
|
throw new Error('Room id is not available')
|
|
}
|
|
|
|
const trackSid = participant.getTrackPublication(
|
|
Source.Microphone
|
|
)?.trackSid
|
|
|
|
if (!trackSid) {
|
|
return
|
|
}
|
|
|
|
// Guard against undefined token for non-admin users
|
|
if (!isAdminOrOwner && !apiRoomData.livekit.token) {
|
|
reportError(
|
|
'participant_mute_api_failure',
|
|
new 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) {
|
|
reportError(
|
|
'participant_mute_api_failure',
|
|
new 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) {
|
|
reportError(
|
|
'participant_mute_api_failure',
|
|
new Error(
|
|
`Failed to notify muted participant ${participant.identity}: ${e}`
|
|
)
|
|
)
|
|
}
|
|
|
|
return response
|
|
},
|
|
[apiRoomData, isAdminOrOwner, notifyParticipants]
|
|
)
|
|
|
|
return { muteParticipant }
|
|
}
|