-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathChat.tsx
More file actions
185 lines (177 loc) · 7.11 KB
/
Chat.tsx
File metadata and controls
185 lines (177 loc) · 7.11 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
'use client';
import { ArrowUpIcon } from '@heroicons/react/20/solid';
import {
ArrowPathRoundedSquareIcon,
ClipboardIcon,
DocumentCheckIcon,
} from '@heroicons/react/24/outline';
import {
ArrowDownIcon,
PauseCircleIcon,
XMarkIcon,
} from '@heroicons/react/24/solid';
import Skeleton from '@node-core/ui-components/Common/Skeleton';
import {
ChatInteractions,
PromptTextArea,
SlidingPanel,
} from '@orama/ui/components';
import { useScrollableContainer } from '@orama/ui/hooks/useScrollableContainer';
import Link from 'next/link';
import { useLocale, useTranslations } from 'next-intl';
import { type FC, type PropsWithChildren } from 'react';
import styles from './chat.module.css';
type SlidingChatPanelProps = PropsWithChildren & {
open: boolean;
onClose: () => void;
};
export const SlidingChatPanel: FC<SlidingChatPanelProps> = ({
open,
onClose,
}) => {
const locale = useLocale();
const t = useTranslations();
const {
containerRef,
showGoToBottomButton,
scrollToBottom,
recalculateGoToBottomButton,
} = useScrollableContainer();
return (
<>
<SlidingPanel.Wrapper open={open} onClose={onClose}>
<SlidingPanel.Backdrop />
<SlidingPanel.Content className={styles.slidingPanelContent}>
<SlidingPanel.Close
className={styles.slidingPanelCloseButton}
aria-label={t('components.search.closeChat')}
>
<XMarkIcon />
</SlidingPanel.Close>
<div className={styles.slidingPanelInner}>
<div className={styles.slidingPanelTop}>
<ChatInteractions.Wrapper
ref={containerRef}
onScroll={recalculateGoToBottomButton}
onStreaming={recalculateGoToBottomButton}
onNewInteraction={() => scrollToBottom({ animated: true })}
className={styles.chatInteractionsWrapper}
>
{(interaction, index, totalInteractions) => (
<>
<ChatInteractions.UserPrompt
className={styles.chatUserPrompt}
>
<p>{interaction.query}</p>
</ChatInteractions.UserPrompt>
{interaction.loading && !interaction.response ? (
<Skeleton className={styles.chatLoader} />
) : (
<>
<ChatInteractions.Sources
sources={
Array.isArray(interaction.sources)
? interaction.sources
: []
}
className={styles.chatSources}
itemClassName={styles.chatSourceItem}
>
{(document, index: number) => (
<div className={styles.chatSource} key={index}>
<Link
data-focus-on-arrow-nav
className={styles.chatSourceLink}
href={
(
document.siteSection as string
).toLowerCase() === 'docs'
? `/${document.path}`
: `/${locale}/${document.path}`
}
>
<span className={styles.chatSourceTitle}>
{document?.pageSectionTitle as string}
</span>
</Link>
</div>
)}
</ChatInteractions.Sources>
<div className={styles.chatAssistantMessageWrapper}>
<ChatInteractions.AssistantMessage
className={styles.chatAssistantMessage}
>
{interaction.response}
</ChatInteractions.AssistantMessage>
{!interaction.loading && (
<div className={styles.chatActionsContainer}>
<ul className={styles.chatActionsList}>
{index === totalInteractions && (
<li>
<ChatInteractions.RegenerateLatest
className={styles.chatAction}
>
<ArrowPathRoundedSquareIcon />
</ChatInteractions.RegenerateLatest>
</li>
)}
<li>
<ChatInteractions.CopyMessage
className={styles.chatAction}
interaction={interaction}
copiedContent={
<DocumentCheckIcon
className={
styles.chatActionIconSelected
}
/>
}
>
<ClipboardIcon />
</ChatInteractions.CopyMessage>
</li>
</ul>
</div>
)}
</div>
</>
)}
</>
)}
</ChatInteractions.Wrapper>
</div>
<div className={styles.slidingPanelBottom}>
{showGoToBottomButton && (
<button
onClick={() => scrollToBottom({ animated: true })}
className={styles.scrollDownButton}
aria-label={t('components.search.scrollToBottom')}
>
<ArrowDownIcon />
</button>
)}
<PromptTextArea.Wrapper className={styles.promptTextAreaWrapper}>
<PromptTextArea.Field
placeholder={t('components.search.chatPlaceholder')}
rows={1}
maxLength={500}
autoFocus
className={styles.promptTextAreaField}
/>
<PromptTextArea.Button
abortContent={<PauseCircleIcon />}
className={styles.promptTextAreaButton}
>
<ArrowUpIcon />
</PromptTextArea.Button>
</PromptTextArea.Wrapper>
<div className={styles.slidingPanelFooter}>
<small>{t('components.search.disclaimer')}</small>
</div>
</div>
</div>
</SlidingPanel.Content>
</SlidingPanel.Wrapper>
</>
);
};