Skip to content

Commit c7005a7

Browse files
Chat: create storybook case for customized message (#28684)
1 parent 1ceff00 commit c7005a7

File tree

4 files changed

+328
-10
lines changed

4 files changed

+328
-10
lines changed

apps/react-storybook/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"dependencies": {
1414
"devextreme": "workspace:~",
1515
"devextreme-react": "workspace:~",
16-
"inferno": "^7.4.9"
16+
"inferno": "^7.4.9",
17+
"html-react-parser": "^5.2.2"
1718
},
1819
"devDependencies": {
1920
"@storybook/addon-essentials": "7.6.19",

apps/react-storybook/stories/chat/Chat.stories.tsx

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import React, { useState, useCallback, useEffect, useMemo } from 'react';
2-
import {Chat, ChatTypes} from 'devextreme-react/chat'
3-
import type {Meta, StoryObj} from '@storybook/react';
2+
import { Chat, ChatTypes } from 'devextreme-react/chat'
3+
import { Button } from "devextreme-react";
4+
import type { Meta, StoryObj } from '@storybook/react';
45
import DataSource from 'devextreme/data/data_source';
56
import CustomStore from 'devextreme/data/custom_store';
6-
import { firstAuthor, secondAuthor, thirdAuthor, fourthAuthor, initialMessages, longError } from './data';
7+
import {
8+
firstAuthor,
9+
secondAuthor,
10+
thirdAuthor,
11+
fourthAuthor,
12+
initialMessages,
13+
longError,
14+
REGENERATION_TEXT,
15+
assistantReplies,
16+
userRequest,
17+
regenerationMessage,
18+
assistant,
19+
} from './data';
720
import { Popup } from 'devextreme-react/popup';
21+
import HTMLReactParser from 'html-react-parser';
822

923
import './styles.css';
1024

@@ -477,3 +491,99 @@ export const TypingUsers: Story = {
477491
);
478492
}
479493
}
494+
495+
export const AIBotIntegration: Story = {
496+
args: {
497+
alerts: 'No alerts',
498+
},
499+
argTypes: {
500+
alerts: {
501+
control: 'select',
502+
defaultValue: 'No alerts',
503+
options: [
504+
'No alerts',
505+
'Limit reached',
506+
],
507+
mapping: {
508+
['No alerts']: [],
509+
['Limit reached']: [{ message: 'Request limit reached, try again in a minute.' }],
510+
},
511+
},
512+
},
513+
render: ({ alerts }) => {
514+
const [isProcessing, setIsProcessing] = useState(false);
515+
const [assistantReplyIndex, setAssistantReplyIndex] = useState<number>(0);
516+
const [copyButtonIcon, setCopyButtonIcon] = useState('copy');
517+
518+
const items = useMemo(() => {
519+
const repliesCount = assistantReplies.length;
520+
const assistantReply = isProcessing ? regenerationMessage : assistantReplies[assistantReplyIndex % repliesCount];
521+
522+
return [userRequest, assistantReply];
523+
}, [assistantReplyIndex, isProcessing]);
524+
525+
const onRegenerateButtonClick = useCallback(async (): Promise<void> => {
526+
setIsProcessing(true);
527+
528+
await new Promise((resolve) => {
529+
setTimeout(resolve, 300);
530+
});
531+
setAssistantReplyIndex((prev: number) => prev + 1);
532+
533+
setIsProcessing(false);
534+
}, []);
535+
536+
const messageRender = useCallback(({ message }: { message: ChatTypes.Message }) => {
537+
const { text = '', author } = message;
538+
539+
const onCopyButtonClick = () => {
540+
navigator.clipboard?.writeText(text);
541+
setCopyButtonIcon('check');
542+
543+
setTimeout(() => {
544+
setCopyButtonIcon('copy');
545+
}, 2500);
546+
};
547+
548+
if (text === REGENERATION_TEXT || author !== assistant) {
549+
return <span>{text}</span>;
550+
}
551+
552+
return (
553+
<>
554+
<div>
555+
{HTMLReactParser(text)}
556+
</div>
557+
<div>
558+
<Button
559+
icon={copyButtonIcon}
560+
stylingMode='text'
561+
hint='Copy'
562+
onClick={onCopyButtonClick}
563+
/>
564+
<Button
565+
icon='refresh'
566+
stylingMode='text'
567+
hint='Regenerate'
568+
onClick={onRegenerateButtonClick}
569+
/>
570+
</div>
571+
</>
572+
);
573+
}, [copyButtonIcon, onRegenerateButtonClick]);
574+
575+
return (
576+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
577+
<Chat
578+
height={900}
579+
width={500}
580+
items={items}
581+
showDayHeaders={false}
582+
user={secondAuthor}
583+
alerts={alerts}
584+
messageRender={messageRender}
585+
/>
586+
</div>
587+
);
588+
}
589+
}

apps/react-storybook/stories/chat/data.ts

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { SchedulerTypes } from 'devextreme-react/scheduler';
21
import { ChatTypes } from 'devextreme-react/chat';
32

3+
export const REGENERATION_TEXT = 'Regeneration...';
4+
45
export const firstAuthor: ChatTypes.User = {
56
id: "c94c0e76-fb49-4b9b-8f07-9f93ed93b4f3",
67
name: "John Doe",
@@ -22,6 +23,12 @@ export const fourthAuthor: ChatTypes.User = {
2223
name: "Crash Bandicoot"
2324
};
2425

26+
export const assistant: ChatTypes.User = {
27+
id: 'assistant',
28+
name: 'Virtual Assistant',
29+
avatarUrl: "https://devexpress.github.io/DevExtreme/images/icons/bot.png",
30+
};
31+
2532
const todayDate = new Date();
2633

2734
const date = new Date();
@@ -86,3 +93,133 @@ export const initialMessages: ChatTypes.Message[] = [
8693
];
8794

8895
export const longError = { id: '1234', message: 'Error Message. An unexpected issue occurred while processing your request. Please check your internet connection or contact support for further assistance.' };
96+
97+
export const userRequest: ChatTypes.Message =
98+
{
99+
timestamp: todayDate,
100+
author: secondAuthor,
101+
text: "What is AI?"
102+
}
103+
;
104+
105+
export const regenerationMessage: ChatTypes.Message = {
106+
timestamp: todayDate,
107+
author: assistant,
108+
text: REGENERATION_TEXT,
109+
};
110+
111+
export const assistantReplies: ChatTypes.Message[] = [
112+
{
113+
timestamp: todayDate,
114+
author: assistant,
115+
text: `<p>Artificial Intelligence (AI) is a branch of computer science that focuses on
116+
creating systems capable of performing tasks that typically require human intelligence.
117+
These tasks include learning, reasoning, problem-solving, understanding natural
118+
language, recognizing patterns, and even making decisions.</p>
119+
<p>AI can be divided into several subfields and categories:</p>
120+
<h3><strong>Types of AI Based on Capability</strong></h3>
121+
<ul>
122+
<li><strong>Narrow AI (Weak AI):</strong> Specialized in performing a single task
123+
or a narrow range of tasks, like language translation, facial recognition,
124+
or recommendation systems. Most AI systems today fall into this category.
125+
</li>
126+
<li><strong>General AI (Strong AI):</strong> A hypothetical form of AI that possesses
127+
the ability to understand, learn, and apply knowledge across a broad range
128+
of tasks, much like a human being.
129+
</li>
130+
<li><strong>Superintelligent AI:</strong> A theoretical concept where AI surpasses
131+
human intelligence across all domains, potentially transforming or outstripping
132+
human capabilities.
133+
</li>
134+
</ul>
135+
<p>AI continues to evolve, impacting nearly every aspect of modern life while raising
136+
ethical, societal, and technological challenges.</p>`.replace(/\s{2,}/gm, ''),
137+
},
138+
{
139+
timestamp: todayDate,
140+
author: assistant,
141+
text: `<p>Artificial Intelligence (AI) is a branch of computer science that focuses on
142+
creating systems capable of performing tasks that typically require human intelligence.
143+
These tasks include learning, reasoning, problem-solving, understanding natural
144+
language, recognizing patterns, and even making decisions.</p>
145+
<p>AI can be divided into several subfields and categories:</p>
146+
<h3><strong>Types of AI Based on Functionality</strong></h3>
147+
<ol>
148+
<li><strong>Reactive Machines:</strong> AI systems that respond to specific inputs
149+
with predefined outputs, without memory or past experiences influencing their
150+
decisions (e.g., IBM's Deep Blue chess-playing computer).
151+
</li>
152+
<li><strong>Limited Memory:</strong> AI systems that can use past data for a short
153+
time to inform decisions, such as self-driving cars.
154+
</li>
155+
<li><strong>Theory of Mind:</strong> A more advanced concept where AI would
156+
understand emotions, beliefs, and intentions, allowing it to interact more
157+
naturally with humans. This remains largely in the research phase.
158+
</li>
159+
<li><strong>Self-Aware AI:</strong> A theoretical AI that has self-awareness and
160+
consciousness.
161+
</li>
162+
</ol>
163+
<p>AI continues to evolve, impacting nearly every aspect of modern life while raising
164+
ethical, societal, and technological challenges.</p>`.replace(/\s{2,}/gm, ''),
165+
},
166+
{
167+
timestamp: todayDate,
168+
author: assistant,
169+
text: `<p>Artificial Intelligence (AI) is a branch of computer science that focuses on
170+
creating systems capable of performing tasks that typically require human intelligence.
171+
These tasks include learning, reasoning, problem-solving, understanding natural
172+
language, recognizing patterns, and even making decisions.</p>
173+
<p>AI can be divided into several subfields and categories:</p>
174+
<h3><strong>Key Subfields of AI</strong></h3>
175+
<ul>
176+
<li><strong>Machine Learning (ML):</strong> Enables systems to learn and improve
177+
from data without explicit programming.
178+
</li>
179+
<li><strong>Deep Learning:</strong> A subset of ML that uses neural networks with
180+
many layers to analyze complex patterns.
181+
</li>
182+
<li><strong>Natural Language Processing (NLP):</strong> Allows AI to understand,
183+
interpret, and respond to human language.
184+
</li>
185+
<li><strong>Computer Vision:</strong> Enables AI to process and analyze visual data
186+
from the world.
187+
</li>
188+
<li><strong>Robotics:</strong> Focuses on creating AI-driven robots to perform tasks
189+
in the physical world.
190+
</li>
191+
<li><strong>Expert Systems:</strong> Mimic human decision-making using rule-based
192+
systems.
193+
</li>
194+
</ul>
195+
<p>AI continues to evolve, impacting nearly every aspect of modern life while raising
196+
ethical, societal, and technological challenges.</p>`.replace(/\s{2,}/gm, ''),
197+
},
198+
{
199+
timestamp: todayDate,
200+
author: assistant,
201+
text: `<p>Artificial Intelligence (AI) is a branch of computer science that focuses on
202+
creating systems capable of performing tasks that typically require human intelligence.
203+
These tasks include learning, reasoning, problem-solving, understanding natural
204+
language, recognizing patterns, and even making decisions.</p>
205+
<p>AI can be divided into several subfields and categories:</p>
206+
<h3><strong>Applications of AI</strong></h3>
207+
<ol>
208+
<li><strong>Healthcare:</strong> Diagnosing diseases, drug discovery, and patient
209+
care.
210+
</li>
211+
<li><strong>Finance:</strong> Fraud detection, stock market analysis, and personalized
212+
financial advice.
213+
</li>
214+
<li><strong>Transportation:</strong> Autonomous vehicles and traffic management.</li>
215+
<li><strong>Entertainment:</strong> Personalized content recommendations on platforms
216+
like Netflix or Spotify.
217+
</li>
218+
<li><strong>Customer Service:</strong> Chatbots and virtual assistants like Siri,
219+
Alexa, or Google Assistant.
220+
</li>
221+
</ol>
222+
<p>AI continues to evolve, impacting nearly every aspect of modern life while raising
223+
ethical, societal, and technological challenges.</p>`.replace(/\s{2,}/gm, ''),
224+
}
225+
];

0 commit comments

Comments
 (0)