-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '24_2' into 24_2_remove_widget_hack
- Loading branch information
Showing
45 changed files
with
968 additions
and
451 deletions.
There are no files selected for viewing
This file contains 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 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
100 changes: 52 additions & 48 deletions
100
apps/demos/Demos/Chat/AIAndChatbotIntegration/React/Message.tsx
This file contains 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 |
---|---|---|
@@ -1,63 +1,67 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useCallback, useState, FC } from 'react'; | ||
import Button from 'devextreme-react/button'; | ||
import { unified } from 'unified'; | ||
import remarkParse from 'remark-parse'; | ||
import remarkRehype from 'remark-rehype'; | ||
import rehypeStringify from 'rehype-stringify'; | ||
import HTMLReactParser from 'html-react-parser'; | ||
|
||
import { Properties as dxButtonProperties } from 'devextreme/ui/button'; | ||
import { REGENERATION_TEXT } from './data.ts'; | ||
|
||
function convertToHtml(value: string) { | ||
const result = unified() | ||
.use(remarkParse) | ||
.use(remarkRehype) | ||
.use(rehypeStringify) | ||
.processSync(value) | ||
.toString(); | ||
function convertToHtml(value: string): string { | ||
const result = unified() | ||
.use(remarkParse) | ||
.use(remarkRehype) | ||
.use(rehypeStringify) | ||
.processSync(value) | ||
.toString(); | ||
|
||
return result; | ||
return result; | ||
} | ||
|
||
function Message({ message }, onRegenerateButtonClick) { | ||
const [icon, setIcon] = useState('copy'); | ||
|
||
if (message.text === REGENERATION_TEXT) { | ||
return <span>{REGENERATION_TEXT}</span>; | ||
} | ||
|
||
function onCopyButtonClick() { | ||
navigator.clipboard?.writeText(message.text); | ||
setIcon('check'); | ||
|
||
setTimeout(() => { | ||
setIcon('copy'); | ||
}, 2500); | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
<div | ||
className='dx-chat-messagebubble-text' | ||
> | ||
{HTMLReactParser(convertToHtml(message.text))} | ||
</div> | ||
<div className='dx-bubble-button-container'> | ||
<Button | ||
icon={icon} | ||
stylingMode='text' | ||
hint='Copy' | ||
onClick={onCopyButtonClick} | ||
/> | ||
<Button | ||
icon='refresh' | ||
stylingMode='text' | ||
hint='Regenerate' | ||
onClick={onRegenerateButtonClick} | ||
/> | ||
</div> | ||
</React.Fragment> | ||
) | ||
interface MessageProps { | ||
text: string; | ||
onRegenerateButtonClick: dxButtonProperties['onClick']; | ||
} | ||
|
||
const Message: FC<MessageProps> = ({ text, onRegenerateButtonClick }) => { | ||
const [icon, setIcon] = useState('copy'); | ||
|
||
const onCopyButtonClick = useCallback(() => { | ||
navigator.clipboard?.writeText(text); | ||
setIcon('check'); | ||
|
||
setTimeout(() => { | ||
setIcon('copy'); | ||
}, 2500); | ||
}, [text]); | ||
|
||
if (text === REGENERATION_TEXT) { | ||
return <span>{REGENERATION_TEXT}</span>; | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
<div className='dx-chat-messagebubble-text'> | ||
{HTMLReactParser(convertToHtml(text))} | ||
</div> | ||
<div className='dx-bubble-button-container'> | ||
<Button | ||
icon={icon} | ||
stylingMode='text' | ||
hint='Copy' | ||
onClick={onCopyButtonClick} | ||
/> | ||
<Button | ||
icon='refresh' | ||
stylingMode='text' | ||
hint='Regenerate' | ||
onClick={onRegenerateButtonClick} | ||
/> | ||
</div> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default Message; |
Oops, something went wrong.