From 92851b10ccc8024ffab577230ffe92bdff484f97 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Sat, 22 Feb 2025 17:54:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20extract=20notifi?= =?UTF-8?q?cation=20decoding=20logic=20for=20reusability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract notification decoding functionality into a reusable module to improve enable consistent notification handling across the frontend application. --- .../features/notifications/MainNotificationToast.tsx | 10 ++++------ src/frontend/src/features/notifications/utils.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/frontend/src/features/notifications/MainNotificationToast.tsx b/src/frontend/src/features/notifications/MainNotificationToast.tsx index 81520967..1a9274ff 100644 --- a/src/frontend/src/features/notifications/MainNotificationToast.tsx +++ b/src/frontend/src/features/notifications/MainNotificationToast.tsx @@ -4,6 +4,7 @@ import { Participant, RemoteParticipant, RoomEvent } from 'livekit-client' import { ToastProvider, toastQueue } from './components/ToastProvider' import { NotificationType } from './NotificationType' import { NotificationDuration } from './NotificationDuration' +import { decodeNotificationDataReceived } from './utils' import { Div } from '@/primitives' import { ChatMessage, isMobileBrowser } from '@livekit/components-core' import { useNotificationSound } from '@/features/notifications/hooks/useSoundNotification' @@ -67,14 +68,11 @@ export const MainNotificationToast = () => { payload: Uint8Array, participant?: RemoteParticipant ) => { - const decoder = new TextDecoder() - const notificationPayload = JSON.parse(decoder.decode(payload)) - const notificationType = notificationPayload.type - const data = notificationPayload.data + const { type, data } = decodeNotificationDataReceived(payload) if (!participant) return - switch (notificationType) { + switch (type) { case NotificationType.ParticipantMuted: toastQueue.add( { @@ -85,7 +83,7 @@ export const MainNotificationToast = () => { ) break case NotificationType.ReactionReceived: - handleEmoji(data?.emoji, participant) + if (data?.emoji) handleEmoji(data.emoji, participant) break default: return diff --git a/src/frontend/src/features/notifications/utils.ts b/src/frontend/src/features/notifications/utils.ts index 58605f3d..2b756801 100644 --- a/src/frontend/src/features/notifications/utils.ts +++ b/src/frontend/src/features/notifications/utils.ts @@ -2,6 +2,7 @@ import { toastQueue } from './components/ToastProvider' import { NotificationType } from './NotificationType' import { NotificationDuration } from './NotificationDuration' import { Participant } from 'livekit-client' +import { NotificationPayload } from './NotificationPayload' export const showLowerHandToast = ( participant: Participant, @@ -26,3 +27,10 @@ export const closeLowerHandToasts = () => { } }) } + +export const decodeNotificationDataReceived = ( + payload: Uint8Array +): NotificationPayload => { + const decoder = new TextDecoder() + return JSON.parse(decoder.decode(payload)) +}