|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { Fragment, useCallback, useState } from "react" |
|
4
|
import { Badge } from "../../Badge" |
|
5
|
import { Button } from "../../Button" |
|
6
|
import { InputText } from "../../Input/Text" |
|
7
|
import { Kbd } from "../../Kbd" |
|
8
|
import { Tooltip } from "../../Tooltip" |
|
9
|
import { Link } from "../../Link" |
|
10
|
import { useAiAssistant } from "@/providers/AiAssistant" |
|
11
|
import { useSearch } from "@/providers/Search" |
|
12
|
import { ArrowUturnLeft } from "@medusajs/icons" |
|
13
|
import clsx from "clsx" |
|
14
|
import { AiAssistantThreadItem } from "../ThreadItem" |
|
15
|
import { AiAssistantSuggestions } from "../Suggestions" |
|
16
|
import { useSearchNavigation } from "../../../hooks/use-search-navigation" |
|
17
|
import { useChat } from "@kapaai/react-sdk" |
|
18
|
|
|
19
|
export const AiAssistantSearchWindow = () => { |
|
20
|
const { conversation, submitQuery, error } = useChat() |
|
21
|
const { inputRef, contentRef, loading } = useAiAssistant() |
|
22
|
const [question, setQuestion] = useState("") |
|
23
|
const { setCommand } = useSearch() |
|
24
|
|
|
25
|
const getThreadItems = useCallback(() => { |
|
26
|
return conversation.map((item, index) => ( |
|
27
|
<Fragment key={index}> |
|
28
|
<AiAssistantThreadItem |
|
29
|
item={{ |
|
30
|
type: "question", |
|
31
|
content: item.question, |
|
32
|
sources: item.sources, |
|
33
|
question_id: item.id, |
|
34
|
}} |
|
35
|
/> |
|
36
|
<AiAssistantThreadItem |
|
37
|
item={{ |
|
38
|
type: "answer", |
|
39
|
content: item.answer, |
|
40
|
sources: item.sources, |
|
41
|
question_id: item.id, |
|
42
|
}} |
|
43
|
/> |
|
44
|
</Fragment> |
|
45
|
)) |
|
46
|
}, [conversation]) |
|
47
|
|
|
48
|
useSearchNavigation({ |
|
49
|
getInputElm: () => inputRef.current as HTMLInputElement | null, |
|
50
|
focusInput: () => inputRef.current?.focus(), |
|
51
|
handleSubmit: () => { |
|
52
|
if (question.length > 0) { |
|
53
|
submitQuery(question) |
|
54
|
} |
|
55
|
}, |
|
56
|
}) |
|
57
|
|
|
58
|
return ( |
|
59
|
<div className="h-full"> |
|
60
|
<div className={clsx("px-docs_1 pt-docs_1")}> |
|
61
|
<Tooltip |
|
62
|
tooltipChildren={ |
|
63
|
<> |
|
64
|
This site is protected by reCAPTCHA and the{" "} |
|
65
|
<Link href="https://policies.google.com/privacy"> |
|
66
|
Google Privacy Policy |
|
67
|
</Link>{" "} |
|
68
|
and <Link href="https://policies.google.com/terms">ToS</Link>{" "} |
|
69
|
apply |
|
70
|
</> |
|
71
|
} |
|
72
|
clickable={true} |
|
73
|
> |
|
74
|
<Badge variant="neutral">AI Assistant</Badge> |
|
75
|
</Tooltip> |
|
76
|
</div> |
|
77
|
<div |
|
78
|
className={clsx( |
|
79
|
"flex gap-docs_1 px-docs_1 py-docs_0.75", |
|
80
|
"h-[57px] w-full md:rounded-t-docs_xl relative border-0 border-solid", |
|
81
|
"border-b border-medusa-border-base relative" |
|
82
|
)} |
|
83
|
> |
|
84
|
<Button |
|
85
|
variant="transparent" |
|
86
|
onClick={() => setCommand(null)} |
|
87
|
className="text-medusa-fg-muted p-[6.5px]" |
|
88
|
> |
|
89
|
<ArrowUturnLeft /> |
|
90
|
</Button> |
|
91
|
<InputText |
|
92
|
value={question} |
|
93
|
onChange={(e) => setQuestion(e.target.value)} |
|
94
|
className={clsx( |
|
95
|
"bg-transparent border-0 focus:outline-none hover:!bg-transparent", |
|
96
|
"!shadow-none flex-1 text-medusa-fg-base", |
|
97
|
"disabled:!bg-transparent disabled:cursor-not-allowed" |
|
98
|
)} |
|
99
|
placeholder="Ask me a question about Medusa..." |
|
100
|
autoFocus={true} |
|
101
|
passedRef={inputRef as React.RefObject<HTMLInputElement | null>} |
|
102
|
disabled={loading} |
|
103
|
/> |
|
104
|
<span |
|
105
|
onClick={() => { |
|
106
|
setQuestion("") |
|
107
|
inputRef.current?.focus() |
|
108
|
}} |
|
109
|
className={clsx( |
|
110
|
"text-medusa-fg-muted hover:text-medusa-fg-subtle", |
|
111
|
"absolute top-docs_0.75 right-docs_1", |
|
112
|
"cursor-pointer", |
|
113
|
question.length === 0 && "hidden" |
|
114
|
)} |
|
115
|
> |
|
116
|
Clear |
|
117
|
</span> |
|
118
|
</div> |
|
119
|
<div className="h-[calc(100%-95px)] lg:max-h-[calc(100%-140px)] lg:min-h-[calc(100%-140px)] overflow-auto"> |
|
120
|
<div ref={contentRef}> |
|
121
|
{!conversation.length && ( |
|
122
|
<AiAssistantSuggestions className="mx-docs_0.5" /> |
|
123
|
)} |
|
124
|
{getThreadItems()} |
|
125
|
{error?.length && ( |
|
126
|
<AiAssistantThreadItem |
|
127
|
item={{ |
|
128
|
type: "error", |
|
129
|
content: error, |
|
130
|
}} |
|
131
|
/> |
|
132
|
)} |
|
133
|
</div> |
|
134
|
</div> |
|
135
|
<div |
|
136
|
className={clsx( |
|
137
|
"py-docs_0.75 hidden md:flex items-center justify-end px-docs_1", |
|
138
|
"border-medusa-border-base border-t", |
|
139
|
"bg-medusa-bg-field-component" |
|
140
|
)} |
|
141
|
> |
|
142
|
<div className="flex items-center gap-docs_0.75"> |
|
143
|
<div className="flex items-center gap-docs_0.5"> |
|
144
|
{conversation.length === 0 && ( |
|
145
|
<> |
|
146
|
<span |
|
147
|
className={clsx( |
|
148
|
"text-medusa-fg-subtle", |
|
149
|
"text-compact-x-small" |
|
150
|
)} |
|
151
|
> |
|
152
|
Navigate FAQ |
|
153
|
</span> |
|
154
|
<span className="gap-[5px] flex"> |
|
155
|
<Kbd |
|
156
|
className={clsx( |
|
157
|
"!bg-medusa-bg-field-component !border-medusa-border-strong", |
|
158
|
"!text-medusa-fg-subtle h-[18px] w-[18px] p-0" |
|
159
|
)} |
|
160
|
> |
|
161
|
↑ |
|
162
|
</Kbd> |
|
163
|
<Kbd |
|
164
|
className={clsx( |
|
165
|
"!bg-medusa-bg-field-component !border-medusa-border-strong", |
|
166
|
"!text-medusa-fg-subtle h-[18px] w-[18px] p-0" |
|
167
|
)} |
|
168
|
> |
|
169
|
↓ |
|
170
|
</Kbd> |
|
171
|
</span> |
|
172
|
</> |
|
173
|
)} |
|
174
|
{conversation.length > 0 && ( |
|
175
|
<span |
|
176
|
className={clsx("text-medusa-fg-muted", "text-compact-x-small")} |
|
177
|
> |
|
178
|
Chat is cleared on exit |
|
179
|
</span> |
|
180
|
)} |
|
181
|
</div> |
|
182
|
<div |
|
183
|
className={clsx("h-docs_0.75 w-px bg-medusa-border-strong")} |
|
184
|
></div> |
|
185
|
<div className="flex items-center gap-docs_0.5"> |
|
186
|
<span |
|
187
|
className={clsx("text-medusa-fg-subtle", "text-compact-x-small")} |
|
188
|
> |
|
189
|
Ask Question |
|
190
|
</span> |
|
191
|
<Kbd |
|
192
|
className={clsx( |
|
193
|
"!bg-medusa-bg-field-component !border-medusa-border-strong", |
|
194
|
"!text-medusa-fg-subtle h-[18px] w-[18px] p-0" |
|
195
|
)} |
|
196
|
> |
|
197
|
↵ |
|
198
|
</Kbd> |
|
199
|
</div> |
|
200
|
</div> |
|
201
|
</div> |
|
202
|
</div> |
|
203
|
) |
|
204
|
} |