-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathChatBar.tsx
395 lines (372 loc) · 13 KB
/
ChatBar.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"use client";
import { MultiNetworkSelector } from "@/components/blocks/NetworkSelectors";
import { Spinner } from "@/components/ui/Spinner/Spinner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Skeleton } from "@/components/ui/skeleton";
import { AutoResizeTextarea } from "@/components/ui/textarea";
import { cn } from "@/lib/utils";
import { ChainIconClient } from "components/icons/ChainIcon";
import { useAllChainsData } from "hooks/chains/allChains";
import {
ArrowUpIcon,
CheckIcon,
ChevronDown,
ChevronDownIcon,
CircleStopIcon,
CopyIcon,
} from "lucide-react";
import { useState } from "react";
import type { ThirdwebClient } from "thirdweb";
import {
AccountAvatar,
AccountBlobbie,
AccountName,
AccountProvider,
WalletName,
WalletProvider,
} from "thirdweb/react";
import { shortenAddress } from "thirdweb/utils";
import type { Wallet } from "thirdweb/wallets";
import type { NebulaContext } from "../api/chat";
export type WalletMeta = {
walletId: Wallet["id"];
address: string;
};
export function ChatBar(props: {
sendMessage: (message: string) => void;
isChatStreaming: boolean;
abortChatStream: () => void;
prefillMessage: string | undefined;
className?: string;
context: NebulaContext | undefined;
setContext: (context: NebulaContext | undefined) => void;
showContextSelector: boolean;
client: ThirdwebClient;
connectedWallets: WalletMeta[];
activeAccountAddress: string | undefined;
setActiveWallet: (wallet: WalletMeta) => void;
isConnectingWallet: boolean;
}) {
const [message, setMessage] = useState(props.prefillMessage || "");
const selectedChainIds = props.context?.chainIds?.map((x) => Number(x)) || [];
const firstChainId = selectedChainIds[0];
return (
<div
className={cn(
"rounded-2xl border border-border bg-card p-2",
props.className,
)}
>
<div className="max-h-[200px] overflow-y-auto">
<AutoResizeTextarea
placeholder={"Ask Nebula"}
value={message}
onChange={(e) => setMessage(e.target.value)}
onKeyDown={(e) => {
// ignore if shift key is pressed to allow entering new lines
if (e.shiftKey) {
return;
}
if (e.key === "Enter" && !props.isChatStreaming) {
e.preventDefault();
setMessage("");
props.sendMessage(message);
}
}}
className="min-h-[60px] resize-none border-none bg-transparent pt-2 leading-relaxed focus-visible:ring-0 focus-visible:ring-offset-0"
disabled={props.isChatStreaming}
/>
</div>
<div className="flex items-end justify-between gap-3 px-2 pb-2">
{/* left */}
<div className="grow">
{props.showContextSelector && (
<div className="flex flex-wrap gap-2 [&>*]:w-auto">
<MultiNetworkSelector
client={props.client}
hideTestnets
disableChainId
selectedChainIds={selectedChainIds}
popoverContentClassName="!w-[calc(100vw-80px)] lg:!w-[320px]"
align="start"
side="top"
showSelectedValuesInModal={true}
customTrigger={
<Button
variant="ghost"
className="h-auto w-full p-0 hover:bg-transparent "
>
{selectedChainIds.length > 0 && firstChainId && (
<ChainBadge
chainId={firstChainId}
plusMore={selectedChainIds.length - 1}
client={props.client}
/>
)}
{selectedChainIds.length === 0 && (
<Badge
variant="outline"
className="flex h-auto gap-1 px-2 py-1.5 hover:bg-accent"
>
Select Chains
<ChevronDownIcon className="size-3 text-muted-foreground" />
</Badge>
)}
</Button>
}
onChange={(values) => {
props.setContext({
walletAddress: props.context?.walletAddress || null,
chainIds: values.map((x) => x.toString()),
networks: props.context?.networks || null,
});
}}
priorityChains={[
1, // ethereum
56, // bnb smart chain mainnet (bsc)
42161, // arbitrum one mainnet
8453, // base mainnet
43114, // avalanche mainnet
146, // sonic
137, // polygon
80094, // berachain mainnet
10, // optimism
]}
/>
{props.connectedWallets.length > 1 &&
!props.isConnectingWallet && (
<WalletSelector
client={props.client}
wallets={props.connectedWallets}
activeAccountAddress={props.activeAccountAddress}
onClick={(walletMeta) => {
props.setActiveWallet(walletMeta);
props.setContext({
walletAddress: walletMeta.address,
chainIds: props.context?.chainIds || [],
networks: props.context?.networks || null,
});
}}
/>
)}
{props.isConnectingWallet && (
<Badge
variant="outline"
className="h-auto w-auto shrink-0 gap-1.5 px-2 py-1.5"
>
<Spinner className="size-3" />
<span>Connecting Wallet</span>
</Badge>
)}
</div>
)}
</div>
{/* Send / Stop */}
{props.isChatStreaming ? (
<Button
variant="default"
className="!h-auto w-auto shrink-0 gap-2 p-2"
onClick={() => {
props.abortChatStream();
}}
>
<CircleStopIcon className="size-4" />
Stop
</Button>
) : (
<Button
aria-label="Send"
disabled={message.trim() === "" || props.isConnectingWallet}
className="!h-auto w-auto border border-nebula-pink-foreground p-2 disabled:opacity-100"
variant="pink"
onClick={() => {
if (message.trim() === "") return;
setMessage("");
props.sendMessage(message);
}}
>
<ArrowUpIcon className="size-4" />
</Button>
)}
</div>
</div>
);
}
function ChainBadge(props: {
chainId: number;
plusMore: number;
client: ThirdwebClient;
}) {
const { idToChain } = useAllChainsData();
const chain = idToChain.get(props.chainId);
return (
<Badge
variant="outline"
className="flex h-auto gap-1.5 px-1.5 py-1.5 hover:bg-accent"
>
<ChainIconClient
src={chain?.icon?.url}
client={props.client}
className="size-3.5"
/>
{chain?.name ? shortenChainName(chain.name) : `Chain #${props.chainId}`}
{props.plusMore > 0 && (
<span className="text-foreground"> +{props.plusMore}</span>
)}
<ChevronDownIcon className="size-3 text-muted-foreground/70" />
</Badge>
);
}
function shortenChainName(chainName: string) {
return chainName.replace("Mainnet", "").trim();
}
function WalletSelector(props: {
wallets: WalletMeta[];
onClick: (wallet: WalletMeta) => void;
client: ThirdwebClient;
activeAccountAddress: string | undefined;
}) {
const [open, setOpen] = useState(false);
if (!props.activeAccountAddress) {
return null;
}
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
aria-expanded={open}
className="flex h-auto items-center gap-1 rounded-full px-2 py-1.5 text-xs"
>
<AccountProvider
address={props.activeAccountAddress}
client={props.client}
>
<AccountAvatar
className="size-4 rounded-full"
loadingComponent={
<Skeleton className="size-3 rounded-full border" />
}
fallbackComponent={
<AccountBlobbie className="size-3 rounded-full" />
}
/>
{shortenAddress(props.activeAccountAddress)}
<ChevronDown className="size-3 text-muted-foreground/70" />
</AccountProvider>
</Button>
</PopoverTrigger>
<PopoverContent
className="w-[350px] overflow-hidden border p-0 shadow-xl lg:w-[350px]"
sideOffset={10}
>
<div className="border-b p-3 py-4">
<p className="text-muted-foreground text-sm">
Select account to use for executing transactions
</p>
</div>
<div className="[&>*:not(:last-child)]:border-b">
{props.wallets.map((wallet) => (
// biome-ignore lint/a11y/useKeyWithClickEvents: <explanation>
<div
key={wallet.address}
className={cn(
"flex cursor-pointer items-center justify-between px-3 py-4 hover:bg-accent/50",
props.activeAccountAddress === wallet.address && "bg-accent/50",
)}
onClick={() => {
setOpen(false);
props.onClick(wallet);
}}
>
<div className="flex items-center gap-2">
<AccountProvider address={wallet.address} client={props.client}>
<WalletProvider id={wallet.walletId}>
<div className="flex items-center gap-2">
<AccountAvatar
className="size-8 rounded-full"
loadingComponent={
<Skeleton className="size-8 rounded-full border" />
}
fallbackComponent={
<AccountBlobbie className="size-8 rounded-full" />
}
/>
<div>
<div className="flex items-center gap-1">
<AccountName
className="text-sm"
loadingComponent={
<span className="font-mono text-sm">
{shortenAddress(wallet.address)}
</span>
}
fallbackComponent={
<span className="font-mono text-sm">
{shortenAddress(wallet.address)}
</span>
}
/>
<CopyButton address={wallet.address} />
{wallet.walletId === "smart" && (
<Badge variant="outline" className="bg-card px-2">
Gasless
</Badge>
)}
</div>
<div className="text-muted-foreground text-xs">
{wallet.walletId === "smart" ? (
"Smart Account"
) : (
<WalletName
fallbackComponent={<span> Your Account </span>}
loadingComponent={
<Skeleton className="h-3.5 w-40" />
}
/>
)}
</div>
</div>
</div>
</WalletProvider>
</AccountProvider>
</div>
{props.activeAccountAddress === wallet.address && (
<CheckIcon className="size-4 text-foreground" />
)}
</div>
))}
</div>
</PopoverContent>
</Popover>
);
}
function CopyButton(props: { address: string }) {
const [copied, setCopied] = useState(false);
return (
<Button
variant="ghost"
className="h-auto w-auto p-1.5 text-muted-foreground"
onClick={(e) => {
e.stopPropagation();
navigator.clipboard.writeText(props.address);
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 1000);
}}
>
{copied ? (
<CheckIcon className="size-3 text-green-500" />
) : (
<CopyIcon className="size-3" />
)}
</Button>
);
}