Raw
1 "use client"
2
3 import clsx from "clsx"
4 import React, {
5 Fragment,
6 useCallback,
7 useEffect,
8 useRef,
9 useState,
10 } from "react"
11 import { useAiAssistant } from "../../../providers/AiAssistant"
12 import { useIsBrowser } from "../../../providers/BrowserProvider"
13 import { AiAssistantChatWindowHeader } from "./Header"
14 import { AiAssistantSuggestions } from "../Suggestions"
15 import { AiAssistantThreadItem } from "../ThreadItem"
16 import { AiAssistantChatWindowInput } from "./Input"
17 import { useKeyboardShortcut } from "../../../hooks/use-keyboard-shortcut"
18 import { AiAssistantChatWindowFooter } from "./Footer"
19 import { useChat } from "@kapaai/react-sdk"
20 import { AiAssistantChatWindowCallout } from "./Callout"
21
22 const DEFAULT_HEIGHT = "calc(100% - 8px)"
23
24 export const AiAssistantChatWindow = () => {
25 const {
26 chatOpened,
27 setChatOpened,
28 chatType: type,
29 inputRef,
30 contentRef,
31 loading,
32 } = useAiAssistant()
33 const { conversation, error } = useChat()
34 const [height, setHeight] = useState(DEFAULT_HEIGHT)
35 const [showFade, setShowFade] = useState(false)
36 const { isBrowser } = useIsBrowser()
37 const chatWindowRef = useRef<HTMLDivElement | null>(null)
38
39 useEffect(() => {
40 if (chatOpened) {
41 inputRef.current?.focus({
42 preventScroll: true,
43 })
44 } else {
45 inputRef.current?.blur()
46 }
47 }, [chatOpened])
48
49 const getThreadItems = useCallback(() => {
50 return conversation.map((item, index) => (
51 <Fragment key={index}>
52 <AiAssistantThreadItem
53 item={{
54 type: "question",
55 content: item.question,
56 sources: item.sources,
57 question_id: item.id,
58 isGenerationAborted: item.isGenerationAborted,
59 }}
60 />
61 <AiAssistantThreadItem
62 item={{
63 type: "answer",
64 content: item.answer,
65 sources: item.sources,
66 question_id: item.id,
67 isGenerationAborted: item.isGenerationAborted,
68 }}
69 />
70 </Fragment>
71 ))
72 }, [conversation])
73
74 useKeyboardShortcut({
75 metakey: false,
76 shortcutKeys: ["escape"],
77 checkEditing: false,
78 action: () => {
79 if (!chatWindowRef.current?.contains(document.activeElement)) {
80 return
81 }
82
83 setChatOpened(false)
84 },
85 })
86
87 const checkShowFade = () => {
88 const parentElm = contentRef.current?.parentElement
89 if (!parentElm) {
90 return
91 }
92 setShowFade(
93 !loading &&
94 parentElm.offsetHeight + parentElm.scrollTop <
95 parentElm.scrollHeight - 1
96 )
97 }
98
99 useEffect(() => {
100 if (!contentRef.current?.parentElement) {
101 return
102 }
103 contentRef.current.parentElement.addEventListener("scroll", checkShowFade)
104
105 return () => {
106 contentRef.current?.parentElement?.removeEventListener(
107 "scroll",
108 checkShowFade
109 )
110 }
111 }, [contentRef.current])
112
113 useEffect(() => {
114 if (loading) {
115 setShowFade(false)
116 } else {
117 checkShowFade()
118 }
119 }, [loading])
120
121 const changeHeightForViewport = () => {
122 if (!window.visualViewport?.height) {
123 setHeight(DEFAULT_HEIGHT)
124 return
125 }
126
127 setHeight(`${window.visualViewport.height - 8}px`)
128 }
129
130 useEffect(() => {
131 if (!isBrowser) {
132 return
133 }
134
135 window.visualViewport?.addEventListener("resize", changeHeightForViewport)
136
137 return () => {
138 window.visualViewport?.removeEventListener(
139 "resize",
140 changeHeightForViewport
141 )
142 }
143 }, [isBrowser])
144
145 useEffect(() => {
146 checkShowFade()
147 }, [height])
148
149 return (
150 <>
151 <div
152 className={clsx(
153 "fixed top-0 left-0 h-screen w-screen z-50 bg-medusa-bg-overlay",
154 !chatOpened && "hidden",
155 chatOpened && "block",
156 type === "default" && "xxl:hidden"
157 )}
158 onClick={() => setChatOpened(false)}
159 />
160 <div
161 className={clsx(
162 "flex z-50 w-[calc(100%-8px)] md:w-ai-assistant transition-[right]",
163 "absolute -right-[150%] sm:-right-full top-0",
164 type === "default" && [
165 "xxl:w-0 xxl:relative xxl:transition-[right,width]",
166 "xxl:shadow-elevation-card-rest xxl:dark:shadow-elevation-card-rest-dark",
167 chatOpened && "xxl:!w-ai-assistant",
168 ],
169 "shadow-elevation-modal dark:shadow-elevation-modal-dark",
170 "bg-medusa-bg-base rounded-docs_DEFAULT overflow-x-hidden",
171 "flex-col justify-between m-docs_0.25 max-w-ai-assistant",
172 chatOpened && ["!right-0"],
173 !chatOpened && ["!fixed"]
174 )}
175 style={{
176 height,
177 }}
178 ref={chatWindowRef}
179 >
180 <AiAssistantChatWindowHeader />
181 <div className="flex flex-col flex-auto overflow-auto relative">
182 <div
183 className={clsx(
184 "overflow-y-auto flex-auto px-docs_0.5 py-docs_1.5 relative"
185 )}
186 >
187 <div ref={contentRef} className="flex flex-col gap-docs_2">
188 {!conversation.length && <AiAssistantSuggestions />}
189 {getThreadItems()}
190 {error?.length && (
191 <AiAssistantThreadItem
192 item={{
193 type: "error",
194 content: error,
195 }}
196 />
197 )}
198 </div>
199 <div className="sticky bottom-0 z-[11]">
200 <AiAssistantChatWindowCallout />
201 </div>
202 </div>
203 <span
204 className={clsx(
205 "bg-ai-assistant-bottom content-[''] absolute pointer-events-none",
206 "bottom-0 left-0 w-full h-docs_6 z-10 opacity-0 transition-opacity",
207 showFade && "opacity-100"
208 )}
209 ></span>
210 </div>
211 <AiAssistantChatWindowInput chatWindowRef={chatWindowRef} />
212 <AiAssistantChatWindowFooter />
213 </div>
214 </>
215 )
216 }