Spaces:
Sleeping
Sleeping
import * as React from 'react'; | |
import Textarea from 'react-textarea-autosize'; | |
import { UseChatHelpers } from 'ai/react'; | |
import { useEnterSubmit } from '@/lib/hooks/useEnterSubmit'; | |
import { cn } from '@/lib/utils'; | |
import { Button, buttonVariants } from '@/components/ui/Button'; | |
import { | |
Tooltip, | |
TooltipContent, | |
TooltipTrigger, | |
} from '@/components/ui/Tooltip'; | |
import { IconArrowElbow, IconPlus } from '@/components/ui/Icons'; | |
import { useRouter } from 'next/navigation'; | |
import Image from 'next/image'; | |
export interface PromptProps | |
extends Pick<UseChatHelpers, 'input' | 'setInput'> { | |
onSubmit: (value: string) => void; | |
isLoading: boolean; | |
url?: string; | |
} | |
export function PromptForm({ | |
onSubmit, | |
input, | |
setInput, | |
isLoading, | |
url, | |
}: PromptProps) { | |
const { formRef, onKeyDown } = useEnterSubmit(); | |
const inputRef = React.useRef<HTMLTextAreaElement>(null); | |
const router = useRouter(); | |
React.useEffect(() => { | |
if (inputRef.current) { | |
inputRef.current.focus(); | |
} | |
}, []); | |
return ( | |
<form | |
onSubmit={async e => { | |
e.preventDefault(); | |
if (!input?.trim()) { | |
return; | |
} | |
setInput(''); | |
await onSubmit(input); | |
}} | |
ref={formRef} | |
> | |
<div className="relative flex w-full px-8 pl-2 overflow-hidden max-h-60 grow bg-background sm:rounded-md sm:border sm:px-12 sm:pl-2"> | |
{url && ( | |
<Tooltip> | |
<TooltipTrigger asChild> | |
<Image | |
src={url} | |
width={60} | |
height={60} | |
alt="chosen image" | |
className="w-1/5 my-4 mx-2 rounded-md" | |
/> | |
</TooltipTrigger> | |
<TooltipContent>New Chat</TooltipContent> | |
</Tooltip> | |
)} | |
<Textarea | |
ref={inputRef} | |
tabIndex={0} | |
onKeyDown={onKeyDown} | |
rows={1} | |
value={input} | |
onChange={e => setInput(e.target.value)} | |
placeholder="Ask questions about the images." | |
spellCheck={false} | |
className="min-h-[60px] w-4/5 resize-none bg-transparent px-4 py-[1.3rem] focus-within:outline-none sm:text-sm" | |
/> | |
<div className="absolute right-0 top-4 sm:right-4"> | |
<Tooltip> | |
<TooltipTrigger asChild> | |
<Button | |
type="submit" | |
size="icon" | |
disabled={isLoading || input === ''} | |
> | |
<IconArrowElbow /> | |
<span className="sr-only">Send message</span> | |
</Button> | |
</TooltipTrigger> | |
<TooltipContent>Send message</TooltipContent> | |
</Tooltip> | |
</div> | |
</div> | |
</form> | |
); | |
} | |