-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathChat.tsx
More file actions
193 lines (183 loc) · 5.82 KB
/
Chat.tsx
File metadata and controls
193 lines (183 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/** @jsx createElement */
/** @jsxFrag Fragment */
import { cx } from '../../lib';
import { createChatHeaderComponent } from './ChatHeader';
import { createChatMessagesComponent } from './ChatMessages';
import { createChatPromptComponent } from './ChatPrompt';
import { createChatPromptSuggestionsComponent } from './ChatPromptSuggestions';
import { createChatToggleButtonComponent } from './ChatToggleButton';
import type { Renderer, ComponentProps } from '../../types';
import type { ChatHeaderProps, ChatHeaderOwnProps } from './ChatHeader';
import type { ChatMessagesProps } from './ChatMessages';
import type { ChatPromptProps, ChatPromptOwnProps } from './ChatPrompt';
import type { ChatPromptSuggestionsOwnProps } from './ChatPromptSuggestions';
import type {
ChatToggleButtonOwnProps,
ChatToggleButtonProps,
} from './ChatToggleButton';
export type ChatClassNames = {
root?: string | string[];
container?: string | string[];
header?: ChatHeaderProps['classNames'];
messages?: ChatMessagesProps['classNames'];
message?: ChatMessagesProps['messageClassNames'];
prompt?: ChatPromptProps['classNames'];
toggleButton?: ChatToggleButtonProps['classNames'];
suggestions?: ChatPromptSuggestionsOwnProps['classNames'];
};
export type ChatMode = 'overlay' | 'side-panel' | 'inline';
export type ChatProps = Omit<ComponentProps<'div'>, 'onError' | 'title'> & {
/*
* The display mode of the chat widget.
* - 'overlay' (default): Fixed bottom-right overlay
* - 'side-panel': Fixed right-edge full-height panel
* - 'inline': Flows within the page layout
*/
mode?: ChatMode;
/*
* Whether the chat is open or closed.
*/
open: boolean;
/*
* Whether the chat is maximized or not.
*/
maximized?: boolean;
/*
* Props for the ChatHeader component.
*/
headerProps: ChatHeaderProps;
/*
* Props for the ChatToggleButton component.
*/
toggleButtonProps: ChatToggleButtonProps;
/*
* Props for the ChatMessages component.
*/
messagesProps: ChatMessagesProps;
/*
* Props for the ChatPrompt component.
*/
promptProps: ChatPromptProps;
/*
* Props for the ChatPromptSuggestions component.
*/
suggestionsProps: ChatPromptSuggestionsOwnProps;
/**
* Optional class names for elements
*/
classNames?: Partial<ChatClassNames>;
/**
* Optional title for the chat
*/
title?: string;
/**
* Optional header component for the chat
*/
headerComponent?: (props: ChatHeaderOwnProps) => JSX.Element;
/**
* Optional prompt component for the chat
*/
promptComponent?: (props: ChatPromptOwnProps) => JSX.Element;
/**
* Optional toggle button component for the chat
*/
toggleButtonComponent?: (props: ChatToggleButtonOwnProps) => JSX.Element;
/**
* Optional suggestions component for the chat
*/
suggestionsComponent?: (props: ChatPromptSuggestionsOwnProps) => JSX.Element;
};
export function createChatComponent({ createElement, Fragment }: Renderer) {
const ChatToggleButton = createChatToggleButtonComponent({
createElement,
Fragment,
});
const ChatHeader = createChatHeaderComponent({ createElement, Fragment });
const ChatMessages = createChatMessagesComponent({ createElement, Fragment });
const ChatPrompt = createChatPromptComponent({ createElement, Fragment });
const ChatPromptSuggestions = createChatPromptSuggestionsComponent({
createElement,
Fragment,
});
return function Chat(userProps: ChatProps) {
const {
mode = 'overlay',
open,
maximized = false,
headerProps,
toggleButtonProps,
messagesProps,
suggestionsProps,
promptProps = {},
headerComponent: HeaderComponent,
promptComponent: PromptComponent,
toggleButtonComponent: ToggleButtonComponent,
suggestionsComponent: SuggestionsComponent,
classNames = {},
className,
...props
} = userProps;
// In inline mode, the chat is always open and has no toggle button
const effectiveOpen = mode === 'inline' ? true : open;
const showToggleButton = mode !== 'inline';
return (
<div
{...props}
className={cx(
'ais-Chat',
mode !== 'overlay' && `ais-Chat--${mode}`,
effectiveOpen && 'ais-Chat--open',
maximized && 'ais-Chat--maximized',
classNames.root,
className
)}
>
<div
className={cx(
'ais-Chat-container',
effectiveOpen && 'ais-Chat-container--open',
maximized && 'ais-Chat-container--maximized',
classNames.container
)}
>
{createElement(HeaderComponent || ChatHeader, {
...headerProps,
classNames: classNames.header,
maximized,
})}
<ChatMessages
{...messagesProps}
classNames={classNames.messages}
messageClassNames={classNames.message}
{...(mode === 'inline' && { visibleMessageCount: 2 })}
suggestionsElement={createElement(
SuggestionsComponent || ChatPromptSuggestions,
{
...suggestionsProps,
classNames: classNames.suggestions,
}
)}
/>
{createElement(PromptComponent || ChatPrompt, {
...promptProps,
classNames: classNames.prompt,
})}
</div>
{showToggleButton && (
<div className="ais-Chat-toggleButtonWrapper">
{createElement(ToggleButtonComponent || ChatToggleButton, {
...toggleButtonProps,
classNames: classNames.toggleButton,
onClick: () => {
toggleButtonProps.onClick?.();
if (!open) {
promptProps.promptRef?.current?.focus();
}
},
})}
</div>
)}
</div>
);
};
}