- 
                Notifications
    
You must be signed in to change notification settings  - Fork 355
 
feat: render adaptive cards #2746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            musale
  wants to merge
  40
  commits into
  next/mgt-chat
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
adaptive-cards
  
      
      
   
  
    
  
  
  
 
  
      
    base: next/mgt-chat
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 11 commits
      Commits
    
    
            Show all changes
          
          
            40 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3427a73
              
                Add adaptive cards and markdownit to render md
              
              
                musale 69abaed
              
                Render adaptive cards in a chat
              
              
                musale 2134f17
              
                Render only Action.OpenUrl type of cards
              
              
                musale a6c449d
              
                Add types for markdown-it
              
              
                musale 84b898d
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale 7594315
              
                WIP: redo rendering the cards from the state to the chat component
              
              
                musale b04905b
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale c56bc11
              
                Use hooks to render the adaptive card
              
              
                musale 39d26a5
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale 9e44636
              
                Merge chat.tsx to onRenderMessage.tsx'
              
              
                musale c6ac564
              
                Fine tune the adaptive cards rendered
              
              
                musale 6c137c4
              
                Remove unnecessary assertions
              
              
                musale 4379e2f
              
                Fix naming and imports issues
              
              
                musale 03da770
              
                Update the artifacts
              
              
                musale 992c356
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale deb0051
              
                Render multiple cards in a message and clear div before append
              
              
                musale d18846d
              
                Add react-components to the mgt-chat package
              
              
                musale 062cc3c
              
                Add ChatMessage and ChatMyMessage as wrappers around adaptive card me…
              
              
                musale 0789365
              
                Add the chat message header props with styling
              
              
                musale 726cd3d
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale dd13a48
              
                Add copyright header text on new files
              
              
                musale bfe432d
              
                Update the string descriptions and fix tests
              
              
                musale b12f680
              
                Add adaptivecards-designer
              
              
                musale 8a20450
              
                Add teams light as the default CSS styling
              
              
                musale a38da05
              
                Add styling for my chat messages
              
              
                musale 47af024
              
                Add message details prop to the container
              
              
                musale 28d56e9
              
                WIP: set icons for status
              
              
                musale afebe03
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                musale 740d3dd
              
                Fix tests for displayDates
              
              
                musale 30e12d7
              
                Move Mgt adaptive cards component to it's pwn file
              
              
                musale 3b7562f
              
                Move message container to it's own file
              
              
                musale 87d3f0a
              
                WIP: find icons
              
              
                musale a84a063
              
                Revert to default rendering for the uncustomized messages
              
              
                musale cad5fc7
              
                Remove displayDates util and use utils in mgt-component
              
              
                musale 5f13cc3
              
                Add tests for getRelativeDisplayDate
              
              
                musale 7f7dab1
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                sebastienlevert cd86198
              
                Move MgtMessageContainer to it's own file
              
              
                musale ea55ed4
              
                WIP: render emojis
              
              
                musale fe88388
              
                Merge branch 'adaptive-cards' of github.com:microsoftgraph/microsoft-…
              
              
                musale ce90d31
              
                Merge branch 'next/mgt-chat' into adaptive-cards
              
              
                gavinbarron File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
            Binary file not shown.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -162,5 +162,8 @@ | |
| "resolutions": { | ||
| "responselike": "2.0.0" | ||
| }, | ||
| "packageManager": "[email protected]" | ||
| "packageManager": "[email protected]", | ||
| "dependencies": { | ||
| "@types/markdown-it": "^13.0.2" | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * ------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
| * See License in the project root for license information. | ||
| * ------------------------------------------------------------------------------------------- | ||
| */ | ||
| 
     | 
||
| import { MessageProps, MessageRenderer } from '@azure/communication-react'; | ||
| import * as AdaptiveCards from 'adaptivecards'; | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| import MarkdownIt from 'markdown-it'; | ||
| import React, { useEffect, useRef } from 'react'; | ||
| import { isGraphChatMessage, isActionOpenUrl, isChatMessage } from './types'; | ||
| import { | ||
| IAdaptiveCard, | ||
| ISubmitAction, | ||
| IOpenUrlAction, | ||
| IShowCardAction, | ||
| IExecuteAction | ||
| } from 'adaptivecards/lib/schema'; | ||
| import { ChatMessageAttachment } from '@microsoft/microsoft-graph-types'; | ||
| import produce from 'immer'; | ||
| import { renderToString } from 'react-dom/server'; | ||
| import UnsupportedContent from '../components/UnsupportedContent/UnsupportedContent'; | ||
| 
     | 
||
| type IAction = ISubmitAction | IOpenUrlAction | IShowCardAction | IExecuteAction; | ||
| 
     | 
||
| /** | ||
| * Props for an adaptive card message.s | ||
| */ | ||
| interface MGTAdaptiveCardProps { | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| attachments: ChatMessageAttachment[]; | ||
| defaultOnRender?: (props: MessageProps) => JSX.Element; | ||
| messageProps: MessageProps; | ||
| } | ||
| 
     | 
||
| /** | ||
| * Render an adaptive card from the attachments | ||
| */ | ||
| const MGTAdaptiveCard = (msg: MGTAdaptiveCardProps) => { | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| const cardRef = useRef<HTMLDivElement | null>(null); | ||
| const attachments = msg.attachments; | ||
| const adaptiveCardAttachment = getAdaptiveCardAttachment(attachments); | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| useEffect(() => { | ||
| if (adaptiveCardAttachment) { | ||
| const cardHtmlElement = getHtmlElementFromAttachment(adaptiveCardAttachment); | ||
| cardRef?.current?.appendChild(cardHtmlElement!); | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| }, [cardRef, adaptiveCardAttachment]); | ||
| const defaultOnRender = msg?.defaultOnRender; | ||
| const messageProps = msg.messageProps; | ||
| const defaultRender = defaultOnRender ? defaultOnRender(messageProps) : <></>; | ||
| return adaptiveCardAttachment ? <div ref={cardRef}></div> : defaultRender; | ||
| }; | ||
| 
     | 
||
| /** | ||
| * Renders the preferred content depending on whether it is supported. | ||
| * | ||
| * @param messageProps final message values from the state. | ||
| * @param defaultOnRender default component to render content. | ||
| * @returns | ||
| */ | ||
| const onRenderMessage = (messageProps: MessageProps, defaultOnRender?: MessageRenderer) => { | ||
| const message = messageProps?.message; | ||
| if (isGraphChatMessage(message)) { | ||
| const attachments = message?.attachments ?? []; | ||
| 
     | 
||
| if (message?.hasUnsupportedContent) { | ||
| const unsupportedContentComponent = <UnsupportedContent targetUrl={message.rawChatUrl} />; | ||
| messageProps = produce(messageProps, (draft: MessageProps) => { | ||
| if (isChatMessage(draft.message)) { | ||
| draft.message.content = renderToString(unsupportedContentComponent); | ||
| } | ||
| }); | ||
| } else if (attachments.length) { | ||
| return ( | ||
| <MGTAdaptiveCard attachments={attachments} defaultOnRender={defaultOnRender} messageProps={messageProps} /> | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| ); | ||
| } | ||
| } | ||
| return defaultOnRender ? defaultOnRender(messageProps) : <></>; | ||
| }; | ||
| 
     | 
||
| /** | ||
| * Filters out the adaptive card attachments. | ||
| * @param attachments | ||
| * @returns | ||
| */ | ||
| const getAdaptiveCardAttachment = (attachments: ChatMessageAttachment[]): ChatMessageAttachment | undefined => { | ||
| for (const att of attachments) { | ||
| const contentType = att?.contentType ?? ''; | ||
| if (contentType === 'application/vnd.microsoft.card.adaptive') { | ||
| return att; | ||
| } | ||
| } | ||
| return; | ||
| }; | ||
| 
     | 
||
| /** | ||
| * Process the attachment object and return an HTMLElement or nothing. | ||
| * @param attachment | ||
| * @returns | ||
| */ | ||
| const getHtmlElementFromAttachment = (attachment: ChatMessageAttachment | undefined): HTMLElement | undefined => { | ||
| /* eslint-disable | ||
| @typescript-eslint/no-unsafe-assignment, | ||
| @typescript-eslint/no-unsafe-member-access, | ||
| @typescript-eslint/no-unsafe-call, react-hooks/rules-of-hooks */ | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| const adaptiveCard = new AdaptiveCards.AdaptiveCard(); | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| const adaptiveCardContentString: string = attachment?.content ?? ''; | ||
| const adaptiveCardContent = JSON.parse(adaptiveCardContentString) as IAdaptiveCard; | ||
| 
     | 
||
| // Check if the actions property has OpenUrl actions only | ||
| const actions = adaptiveCardContent?.actions?.filter(ac => ac.type === 'Action.OpenUrl'); | ||
| if (actions) { | ||
| // Update actions to only Action.OpenUrl actions. | ||
| adaptiveCardContent.actions = actions; | ||
| } | ||
| 
     | 
||
| // Check if the body has actionSet actions and filter for OpenUrl only | ||
| const actionSetArray = adaptiveCardContent?.body?.filter(ac => Object.values(ac).includes('ActionSet')); | ||
| if (actionSetArray) { | ||
| const finalInnerActions = []; | ||
| for (const actionSet of actionSetArray) { | ||
| const innerActions = actionSet?.actions as IAction[]; | ||
| const valid = innerActions?.filter(ac => ac?.type === 'Action.OpenUrl'); | ||
| if (valid) finalInnerActions.push(...valid); | ||
| } | ||
| 
     | 
||
| for (const b of adaptiveCardContent?.body ?? []) { | ||
| if (Object.values(b).includes('ActionSet')) { | ||
| b.actions = finalInnerActions; | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| // markdown support | ||
| AdaptiveCards.AdaptiveCard.onProcessMarkdown = (text: string, result: AdaptiveCards.IMarkdownProcessingResult) => { | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| const md = new MarkdownIt(); | ||
| result.outputHtml = md.render(text); | ||
| result.didProcess = true; | ||
| }; | ||
| 
     | 
||
| adaptiveCard.parse(adaptiveCardContent); | ||
| adaptiveCard.onExecuteAction = (action: AdaptiveCards.Action) => { | ||
                
      
                  musale marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if (isActionOpenUrl(action)) { | ||
| const url: string = action?.url ?? ''; | ||
| window.open(url, '_blank', 'noopener,noreferrer'); | ||
| } | ||
| }; | ||
| return adaptiveCard.render(); | ||
| }; | ||
| 
     | 
||
| export { onRenderMessage }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.