bolt.new/packages/bolt/app/components/chat/BaseChat.tsx

201 lines
7.8 KiB
TypeScript
Raw Normal View History

import type { Message } from 'ai';
import React, { type RefCallback } from 'react';
2024-07-10 16:44:39 +00:00
import { ClientOnly } from 'remix-utils/client-only';
import { Menu } from '~/components/sidebar/Menu.client';
import { IconButton } from '~/components/ui/IconButton';
import { Workbench } from '~/components/workbench/Workbench.client';
import { classNames } from '~/utils/classNames';
import { Messages } from './Messages.client';
2024-07-10 16:44:39 +00:00
import { SendButton } from './SendButton.client';
interface BaseChatProps {
textareaRef?: React.RefObject<HTMLTextAreaElement> | undefined;
2024-07-24 13:47:48 +00:00
messageRef?: RefCallback<HTMLDivElement> | undefined;
scrollRef?: RefCallback<HTMLDivElement> | undefined;
2024-07-10 16:44:39 +00:00
chatStarted?: boolean;
isStreaming?: boolean;
messages?: Message[];
2024-07-10 16:44:39 +00:00
enhancingPrompt?: boolean;
promptEnhanced?: boolean;
input?: string;
handleStop?: () => void;
sendMessage?: (event: React.UIEvent, messageInput?: string) => void;
2024-07-10 16:44:39 +00:00
handleInputChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
enhancePrompt?: () => void;
}
const EXAMPLE_PROMPTS = [
{ text: 'Build a todo app in React using Tailwind' },
{ text: 'Build a simple blog using Astro' },
{ text: 'Create a cookie consent form using Material UI' },
{ text: 'Make a space invaders game' },
2024-08-01 15:31:18 +00:00
{ text: 'How do I center a div?' },
];
2024-07-10 16:44:39 +00:00
const TEXTAREA_MIN_HEIGHT = 72;
export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
(
{
textareaRef,
2024-07-24 13:47:48 +00:00
messageRef,
scrollRef,
2024-07-10 16:44:39 +00:00
chatStarted = false,
isStreaming = false,
2024-07-10 16:44:39 +00:00
enhancingPrompt = false,
promptEnhanced = false,
messages,
2024-07-10 16:44:39 +00:00
input = '',
sendMessage,
handleInputChange,
enhancePrompt,
handleStop,
2024-07-10 16:44:39 +00:00
},
ref,
) => {
const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200;
return (
<div ref={ref} className="relative flex h-full w-full overflow-hidden ">
2024-07-31 21:21:40 +00:00
<ClientOnly>{() => <Menu />}</ClientOnly>
2024-07-24 13:47:48 +00:00
<div ref={scrollRef} className="flex overflow-scroll w-full h-full">
<div className="flex flex-col w-full h-full px-6">
2024-07-10 16:44:39 +00:00
{!chatStarted && (
<div id="intro" className="mt-[26vh] max-w-2xl mx-auto">
<h1 className="text-5xl text-center font-bold text-slate-800 mb-2">Where ideas begin</h1>
<p className="mb-4 text-center">Bring ideas to life in seconds or get help on existing projects.</p>
2024-07-10 16:44:39 +00:00
</div>
)}
<div
className={classNames('pt-6', {
'h-full flex flex-col': chatStarted,
})}
2024-07-10 16:44:39 +00:00
>
<ClientOnly>
{() => {
return chatStarted ? (
<Messages
2024-07-24 13:47:48 +00:00
ref={messageRef}
className="flex flex-col w-full flex-1 max-w-2xl px-4 pb-10 mx-auto z-1"
messages={messages}
isStreaming={isStreaming}
/>
) : null;
}}
</ClientOnly>
<div
className={classNames('relative w-full max-w-2xl md:mx-auto z-2', {
'sticky bottom-0': chatStarted,
})}
>
<div
className={classNames(
'shadow-sm border border-gray-200 bg-white/85 backdrop-filter backdrop-blur-[8px] rounded-lg overflow-hidden',
)}
>
<textarea
ref={textareaRef}
className={`w-full pl-4 pt-4 pr-16 focus:outline-none resize-none bg-transparent`}
onKeyDown={(event) => {
if (event.key === 'Enter') {
if (event.shiftKey) {
return;
}
2024-07-10 16:44:39 +00:00
event.preventDefault();
2024-07-10 16:44:39 +00:00
sendMessage?.(event);
}
}}
value={input}
onChange={(event) => {
handleInputChange?.(event);
}}
style={{
minHeight: TEXTAREA_MIN_HEIGHT,
maxHeight: TEXTAREA_MAX_HEIGHT,
}}
placeholder="How can Bolt help you today?"
translate="no"
/>
<ClientOnly>
{() => (
<SendButton
show={input.length > 0 || isStreaming}
isStreaming={isStreaming}
onClick={(event) => {
if (isStreaming) {
handleStop?.();
return;
}
sendMessage?.(event);
}}
/>
)}
</ClientOnly>
<div className="flex justify-between text-sm p-4 pt-2">
<div className="flex gap-1 items-center">
<IconButton icon="i-ph:microphone-duotone" className="-ml-1" />
<IconButton icon="i-ph:plus-circle-duotone" />
<IconButton icon="i-ph:pencil-simple-duotone" />
<IconButton
disabled={input.length === 0 || enhancingPrompt}
className={classNames({
'opacity-100!': enhancingPrompt,
'text-accent! pr-1.5 enabled:hover:bg-accent/12!': promptEnhanced,
})}
onClick={() => enhancePrompt?.()}
>
{enhancingPrompt ? (
<>
<div className="i-svg-spinners:90-ring-with-bg text-black text-xl"></div>
<div className="ml-1.5">Enhancing prompt...</div>
</>
) : (
<>
<div className="i-blitz:stars text-xl"></div>
{promptEnhanced && <div className="ml-1.5">Prompt enhanced</div>}
</>
)}
</IconButton>
</div>
{input.length > 3 ? (
<div className="text-xs">
Use <kbd className="bg-gray-100 p-1 rounded-md">Shift</kbd> +{' '}
<kbd className="bg-gray-100 p-1 rounded-md">Return</kbd> for a new line
</div>
) : null}
2024-07-10 16:44:39 +00:00
</div>
</div>
<div className="bg-white pb-6">{/* Ghost Element */}</div>
2024-07-10 16:44:39 +00:00
</div>
</div>
{!chatStarted && (
<div id="examples" className="relative w-full max-w-2xl mx-auto text-center mt-8 flex justify-center">
<div className="flex flex-col items-center space-y-2 [mask-image:linear-gradient(to_bottom,black_0%,transparent_180%)] hover:[mask-image:none]">
{EXAMPLE_PROMPTS.map((examplePrompt, index) => {
return (
<button
key={index}
onClick={(event) => {
sendMessage?.(event, examplePrompt.text);
}}
className="group flex items-center gap-2 bg-transparent text-gray-500 hover:text-gray-1000"
>
{examplePrompt.text}
<div className="i-ph:arrow-bend-down-left" />
</button>
);
})}
</div>
</div>
)}
2024-07-10 16:44:39 +00:00
</div>
<ClientOnly>{() => <Workbench chatStarted={chatStarted} isStreaming={isStreaming} />}</ClientOnly>
2024-07-10 16:44:39 +00:00
</div>
</div>
);
},
);