'use client';
import * as React from 'react';
import {
AudioLinesIcon,
FlaskConicalIcon,
MicIcon,
PaperclipIcon,
PlusIcon,
SendIcon,
SparklesIcon,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Textarea } from '@/components/ui/textarea';
import { cn } from '@/lib/utils';
/**
* Assistant composer with voice dictation.
* A pill-shaped prompt input that expands into a card once the message grows.
*/
export default function AiChat01() {
const [message, setMessage] = React.useState('');
const [isExpanded, setIsExpanded] = React.useState(false);
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
const fileInputRef = React.useRef<HTMLInputElement>(null);
const send = () => {
if (!message.trim()) return;
setMessage('');
setIsExpanded(false);
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
send();
};
const handleTextareaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setMessage(e.target.value);
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
}
setIsExpanded(e.target.value.length > 100 || e.target.value.includes('\n'));
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
send();
}
};
return (
<div className="w-full px-4">
<h1 className="mx-auto mb-6 max-w-2xl text-balance text-center font-semibold text-3xl text-foreground leading-tight tracking-tight">
What are we working on today?
</h1>
<form className="group/composer w-full" onSubmit={handleSubmit}>
<input className="sr-only" multiple ref={fileInputRef} type="file" />
<div
className={cn(
'mx-auto grid w-full max-w-2xl cursor-text overflow-clip border border-border bg-background bg-clip-padding p-2.5 shadow-sm dark:bg-muted/50',
isExpanded ? 'rounded-xl' : 'rounded-full'
)}
style={
isExpanded
? {
gridTemplateAreas: "'header' 'primary' 'footer'",
gridTemplateColumns: '1fr',
gridTemplateRows: 'auto 1fr auto',
}
: {
gridTemplateAreas: "'header header header' 'leading primary trailing' '. footer .'",
gridTemplateColumns: 'auto 1fr auto',
gridTemplateRows: 'auto 1fr auto',
}
}
>
<div
className={cn(
'flex overflow-x-hidden',
isExpanded ? 'px-2 pt-1.5 pb-2' : '-my-2.5 min-h-14 items-center px-1.5'
)}
style={{ gridArea: 'primary' }}
>
<div className="max-h-52 flex-1 overflow-auto">
<Textarea
className="min-h-0 resize-none rounded-none border-0 p-0 text-base shadow-none placeholder:text-muted-foreground focus-visible:ring-0 focus-visible:ring-offset-0 md:text-base dark:bg-transparent"
onChange={handleTextareaChange}
onKeyDown={handleKeyDown}
placeholder="Describe a task or ask a question"
ref={textareaRef}
rows={1}
value={message}
/>
</div>
</div>
<div className={cn('flex items-center', { hidden: isExpanded })} style={{ gridArea: 'leading' }}>
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button
aria-label="Add attachments"
className="rounded-full transition-[scale,background-color] duration-150 ease-out active:scale-[0.96]"
size="icon-lg"
type="button"
variant="ghost"
/>
}
>
<PlusIcon className="size-5 text-muted-foreground" strokeWidth={1.5} />
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-64 rounded-xl p-1.5">
<DropdownMenuGroup className="space-y-1">
<DropdownMenuItem className="rounded-lg px-2.5 py-2" onClick={() => fileInputRef.current?.click()}>
<PaperclipIcon className="opacity-60" />
Upload files or images
</DropdownMenuItem>
<DropdownMenuItem className="rounded-lg px-2.5 py-2">
<SparklesIcon className="opacity-60" />
Autopilot mode
</DropdownMenuItem>
<DropdownMenuItem className="rounded-lg px-2.5 py-2">
<FlaskConicalIcon className="opacity-60" />
Extended analysis
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="flex items-center gap-2" style={{ gridArea: isExpanded ? 'footer' : 'trailing' }}>
<div className="ms-auto flex items-center gap-1.5">
<Button
aria-label="Dictate a message"
className="rounded-full transition-[scale,background-color] duration-150 ease-out active:scale-[0.96]"
size="icon-lg"
type="button"
variant="ghost"
>
<MicIcon className="size-5 text-muted-foreground" strokeWidth={1.5} />
</Button>
<Button
aria-label="Start a voice session"
className="rounded-full transition-[scale,background-color] duration-150 ease-out active:scale-[0.96]"
size="icon-lg"
type="button"
variant="ghost"
>
<AudioLinesIcon className="size-5 text-muted-foreground" strokeWidth={1.5} />
</Button>
<Button
aria-label="Send message"
className="rounded-full transition-[scale,opacity,background-color] duration-150 ease-out active:scale-[0.96] disabled:opacity-40"
disabled={!message.trim()}
size="icon-lg"
type="submit"
>
<SendIcon className="size-4.5" strokeWidth={1.5} />
</Button>
</div>
</div>
</div>
</form>
</div>
);
}