/
steamship-chat-prompt.tsx
149 lines (144 loc) · 5.3 KB
/
steamship-chat-prompt.tsx
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
"use client";
import { useChat } from "ai/react";
import { useRef, useMemo, ReactNode } from "react";
import {
BotIcon,
RotateCcwIcon,
SendIcon,
UserIcon,
XIcon,
} from "lucide-react";
import { v4 as uuidv4 } from "uuid";
import { Button, Input, Separator, Skeleton } from "../../ui";
import SteamshipMessage from "../../components/steamship-message";
import {
SteamshipChatLoadingMessage,
SteamshipChatMessageContainer,
SteamshipChatMessageContentsContainer,
SteamshipChatUser,
} from "../../components/steamship-chat-elements";
const SteamshipChatPrompt = ({
onClose,
children,
placeholder,
loadingText,
}: {
onClose: () => void;
children: (args: { onSubmit: (message: string) => void }) => ReactNode;
placeholder?: string;
loadingText?: string;
}) => {
const starterSubmitButtonRef = useRef<HTMLButtonElement | null>();
const chatUUID = useMemo(() => uuidv4(), []);
const {
messages,
input,
handleInputChange,
handleSubmit,
isLoading,
setInput,
setMessages,
} = useChat({
id: chatUUID,
body: { id: chatUUID },
api: "/api/steamship/chat",
});
const onSubmit = (message: string) => {
setInput(message);
setTimeout(() => {
starterSubmitButtonRef?.current?.click();
}, 100);
};
return (
<div className="steamship-widget steamship-fixed steamship-top-0 steamship-bottom-0 steamship-right-0 steamship-left-0 steamship-flex steamship-z-30 steamship-justify-center steamship-items-start steamship-py-12 md:steamship-py-24 steamship-bg-black steamship-bg-opacity-60">
<div
id="steamship-chat-modal"
className="steamship-border steamship-rounded-xl steamship-shadow-xl steamship-bg-background steamship-flex steamship-w-[650px] steamship-max-w-[100vw] steamship-max-h-[100%]"
>
{messages.length === 0 ? (
<div className="steamship-w-full">
<form
onSubmit={handleSubmit}
className="steamship-flex steamship-items-center steamship-justify-center steamship-gap-2 steamship-mt-2 steamship-px-2"
>
<Input
autoFocus
placeholder={placeholder}
value={input}
onChange={handleInputChange}
className="steamship-flex-grow steamship-border-none !steamship-ring-0 focus-visible:steamship-ring-0 focus-visible:steamship-ring-transparent focus:steamship-ring-none focus:steamship-outline-none focus:steamship-border-none steamship-text-xl"
/>
<Button
type="submit"
ref={starterSubmitButtonRef as React.Ref<HTMLButtonElement>}
>
<SendIcon className="steamship-h-6 steamship-w-6" />
</Button>
</form>
{children({ onSubmit })}
</div>
) : (
<div className="steamship-flex-grow steamship-flex steamship-flex-col steamship-justify-between">
<div className="steamship-px-4 steamship-flex steamship-justify-end steamship-gap-4 steamship-pt-2">
<Button
size="sm"
type="button"
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
setMessages([]);
setInput("");
}}
variant="outline"
>
<RotateCcwIcon className="steamship-h-4 steamship-w-4" />
</Button>
<Button size="sm" variant="outline" onClick={() => onClose()}>
<XIcon className="steamship-h-4 steamship-w-4" />
</Button>
</div>
<div className="steamship-px-4 steamship-py-2 steamship-flex-grow steamship-overflow-scroll steamship-flex steamship-flex-col-reverse">
<div>
{messages.map((m) => (
<SteamshipChatMessageContainer key={m.id}>
<SteamshipChatUser role={m.role} />
<SteamshipChatMessageContentsContainer>
<SteamshipMessage message={m.content} />
</SteamshipChatMessageContentsContainer>
</SteamshipChatMessageContainer>
))}
{isLoading && (
<div>
<SteamshipChatLoadingMessage />
{loadingText && (
<div className="steamship-flex steamship-items-center steamship-justify-center steamship-text-sm steamship-mt-2">
{loadingText}
</div>
)}
</div>
)}
</div>
</div>
<form
onSubmit={handleSubmit}
className="steamship-flex steamship-gap-2 steamship-px-4 steamship-py-2"
>
<label className="steamship-flex-grow">
<Input
placeholder={placeholder}
value={input}
onChange={handleInputChange}
className="steamship-flex-grow"
/>
</label>
<Button type="submit">
<SendIcon className="steamship-h-6 steamship-w-6" />
</Button>
</form>
</div>
)}
</div>
</div>
);
};
export default SteamshipChatPrompt;