11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { ChatMessage } from '@azure/communication-chat' ;
54import { IStackStyles , Stack } from '@fluentui/react' ;
65import { _formatString } from '@internal/acs-ui-common' ;
76import { ControlBarButtonProps } from '@internal/react-components' ;
8- import React , { useCallback , useMemo , useState } from 'react' ;
9- import { useEffect } from 'react' ;
10- import { ChatAdapter } from '../../ChatComposite' ;
7+ import React , { useCallback , useMemo } from 'react' ;
118import { CallWithChatCompositeIcon } from '../../common/icons' ;
129import { ChatButton } from './ChatButton' ;
1310import { useCallWithChatCompositeStrings } from '../hooks/useCallWithChatCompositeStrings' ;
@@ -17,27 +14,19 @@ import { NotificationIcon } from './NotificationIcon';
1714 * @private
1815 */
1916export interface ChatButtonWithUnreadMessagesBadgeProps extends ControlBarButtonProps {
20- chatAdapter : ChatAdapter ;
21- isChatPaneVisible : boolean ;
22- newMessageLabel ? : string ;
17+ unreadChatMessagesCount : number ;
18+ hideUnreadChatMessagesBadge ? : boolean ;
19+ newMessageLabel : string ;
2320}
2421
25- /**
26- * Helper function to determine if the message in the event is a valid one from a user.
27- * Display name is used since system messages will not have one.
28- */
29- const validNewChatMessage = ( message ) : boolean =>
30- ! ! message . senderDisplayName && ( message . type === 'text' || message . type === 'html' ) ;
31-
3222const filledIcon = < CallWithChatCompositeIcon iconName = { 'ControlBarChatButtonActive' } /> ;
3323const regularIcon = < CallWithChatCompositeIcon iconName = { 'ControlBarChatButtonInactive' } /> ;
3424
3525/**
3626 * @private
3727 */
3828export const ChatButtonWithUnreadMessagesBadge = ( props : ChatButtonWithUnreadMessagesBadgeProps ) : JSX . Element => {
39- const { chatAdapter, isChatPaneVisible, newMessageLabel } = props ;
40- const [ unreadChatMessagesCount , setUnreadChatMessagesCount ] = useState < number > ( 0 ) ;
29+ const { newMessageLabel, unreadChatMessagesCount, hideUnreadChatMessagesBadge } = props ;
4130
4231 const baseIcon = props . showLabel ? regularIcon : filledIcon ;
4332 const callWithChatStrings = useCallWithChatCompositeStrings ( ) ;
@@ -61,30 +50,13 @@ export const ChatButtonWithUnreadMessagesBadge = (props: ChatButtonWithUnreadMes
6150 const notificationOnIcon = useCallback ( ( ) : JSX . Element => {
6251 return (
6352 < Stack styles = { chatNotificationContainerStyles } >
64- { unreadChatMessagesCount > 0 && (
53+ { unreadChatMessagesCount > 0 && ! hideUnreadChatMessagesBadge && (
6554 < NotificationIcon chatMessagesCount = { unreadChatMessagesCount } label = { newMessageLabel } />
6655 ) }
6756 { baseIcon }
6857 </ Stack >
6958 ) ;
70- } , [ unreadChatMessagesCount , newMessageLabel , baseIcon ] ) ;
71-
72- useEffect ( ( ) => {
73- if ( isChatPaneVisible ) {
74- setUnreadChatMessagesCount ( 0 ) ;
75- return ;
76- }
77- const incrementUnreadChatMessagesCount = ( event : { message : ChatMessage } ) : void => {
78- if ( ! isChatPaneVisible && validNewChatMessage ( event . message ) ) {
79- setUnreadChatMessagesCount ( unreadChatMessagesCount + 1 ) ;
80- }
81- } ;
82- chatAdapter . on ( 'messageReceived' , incrementUnreadChatMessagesCount ) ;
83-
84- return ( ) => {
85- chatAdapter . off ( 'messageReceived' , incrementUnreadChatMessagesCount ) ;
86- } ;
87- } , [ chatAdapter , setUnreadChatMessagesCount , isChatPaneVisible , unreadChatMessagesCount ] ) ;
59+ } , [ unreadChatMessagesCount , newMessageLabel , baseIcon , hideUnreadChatMessagesBadge ] ) ;
8860
8961 return (
9062 < ChatButton
0 commit comments