Skip to content

Commit

Permalink
[Chat] Announce File Attachments in New Messages (#4438)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeng-ms authored Apr 9, 2024
1 parent ded48cf commit 76a858a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "patch",
"area": "fix",
"workstream": "File Sharing",
"comment": "Add Chat Attachments in Live Messages",
"packageName": "@azure/communication-react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "patch",
"area": "fix",
"workstream": "File Sharing",
"comment": "Add Chat Attachments in Live Messages",
"packageName": "@azure/communication-react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useMemo } from 'react';
import { useLocale } from '../localization';
import { _AttachmentCard } from './AttachmentCard';
import { _AttachmentCardGroup } from './AttachmentCardGroup';
/* @conditional-compile-remove(attachment-download) */
import { getAttachmentCountLiveMessage } from './ChatMessage/ChatMessageContent';
import { _formatString } from '@internal/acs-ui-common';
import { AttachmentMenuAction, AttachmentMetadata } from '../types/Attachment';
import { ChatMessage } from '../types';
Expand Down Expand Up @@ -102,15 +104,12 @@ export const _AttachmentDownloadCards = (props: _AttachmentDownloadCardsProps):

const attachmentCardGroupDescription = useMemo(
() => () => {
const attachmentGroupLocaleString =
props.strings?.attachmentCardGroupMessage ?? localeStrings.attachmentCardGroupMessage;
/* @conditional-compile-remove(attachment-download) @conditional-compile-remove(attachment-upload) */
return _formatString(attachmentGroupLocaleString, {
attachmentCount: `${attachments?.length ?? 0}`
});
return _formatString(attachmentGroupLocaleString, {
attachmentCount: `${attachments?.length ?? 0}`
});
/* @conditional-compile-remove(attachment-download) */
return getAttachmentCountLiveMessage(
attachments ?? [],
props.strings?.attachmentCardGroupMessage ?? localeStrings.attachmentCardGroupMessage
);
return '';
},
[props.strings?.attachmentCardGroupMessage, localeStrings.attachmentCardGroupMessage, attachments]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import LiveMessage from '../Announcer/LiveMessage';
/* @conditional-compile-remove(mention) */
import { defaultOnMentionRender } from './MentionRenderer';
import DOMPurify from 'dompurify';
import { _AttachmentDownloadCardsStrings } from '../AttachmentDownloadCards';
/* @conditional-compile-remove(attachment-download) */
import { AttachmentMetadata } from '../../types';

type ChatMessageContentProps = {
message: ChatMessage;
Expand Down Expand Up @@ -164,9 +167,12 @@ export const BlockedMessageContent = (props: BlockedMessageContentProps): JSX.El
};

const extractContentForAllyMessage = (props: ChatMessageContentProps): string => {
if (props.message.content) {
let attachments = undefined;
/* @conditional-compile-remove(attachment-download) */
attachments = props.message.attachments;
if (props.message.content || attachments) {
// Replace all <img> tags with 'image' for aria.
const parsedContent = DOMPurify.sanitize(props.message.content, {
const parsedContent = DOMPurify.sanitize(props.message.content ?? '', {
ALLOWED_TAGS: ['img'],
RETURN_DOM_FRAGMENT: true
});
Expand All @@ -180,6 +186,16 @@ const extractContentForAllyMessage = (props: ChatMessageContentProps): string =>
parsedContent.replaceChild(imageTextNode, child);
});

// Inject attachment names for aria.
if (attachments) {
let attachmentList = '';
/* @conditional-compile-remove(attachment-download) */
attachmentList = attachmentCardGroupDescription(props);
const attachmentTextNode = document.createElement('div');
attachmentTextNode.innerHTML = ` ${attachmentList} `;
parsedContent.appendChild(attachmentTextNode);
}

// Strip all html tags from the content for aria.
const message = DOMPurify.sanitize(parsedContent, { ALLOWED_TAGS: [] });
return message;
Expand Down Expand Up @@ -207,6 +223,29 @@ const messageContentAriaText = (props: ChatMessageContentProps): string | undefi
});
};

/* @conditional-compile-remove(attachment-download) */
const attachmentCardGroupDescription = (props: ChatMessageContentProps): string => {
const attachments = props.message.attachments;
return getAttachmentCountLiveMessage(attachments ?? [], props.strings.attachmentCardGroupMessage);
};

/* @conditional-compile-remove(attachment-download) */
/**
* @private
*/
export const getAttachmentCountLiveMessage = (
attachments: AttachmentMetadata[],
attachmentCardGroupMessage: string
): string => {
if (attachments.length === 0) {
return '';
}
return _formatString(attachmentCardGroupMessage, {
attachmentCount: `${attachments.length}`
});
return '';
};

const defaultOnRenderInlineImage = (inlineImage: InlineImage): JSX.Element => {
return (
<img
Expand Down

0 comments on commit 76a858a

Please sign in to comment.