| 22 | * Chat input component with submit and stop buttons |
| 23 | */ |
| 24 | export function ChatInput({ |
| 25 | input, |
| 26 | setInput, |
| 27 | onSubmit, |
| 28 | isLoading, |
| 29 | onStop, |
| 30 | disabled = false, |
| 31 | placeholder = "What are we surfing today?", |
| 32 | className, |
| 33 | }: ChatInputProps) { |
| 34 | const isInputEmpty = useMemo(() => input.trim() === "", [input]); |
| 35 | |
| 36 | return ( |
| 37 | <form onSubmit={onSubmit} className={cn(className)}> |
| 38 | <div className="flex items-center"> |
| 39 | <div className="relative flex-1 flex items-center gap-2"> |
| 40 | <Input |
| 41 | placeholder={placeholder} |
| 42 | value={input} |
| 43 | onChange={(e) => setInput(e.target.value)} |
| 44 | autoFocus |
| 45 | required |
| 46 | disabled={disabled} |
| 47 | className="w-full pr-16" |
| 48 | /> |
| 49 | <div className="absolute right-2 top-1/2 -translate-y-1/2 flex items-center gap-2"> |
| 50 | {isLoading ? ( |
| 51 | <Button |
| 52 | type="button" |
| 53 | onClick={onStop} |
| 54 | variant="error" |
| 55 | size="iconLg" |
| 56 | disabled={disabled} |
| 57 | title="Stop generating" |
| 58 | > |
| 59 | <StopCircle className="w-5 h-5" /> |
| 60 | </Button> |
| 61 | ) : ( |
| 62 | <Button |
| 63 | type="submit" |
| 64 | variant="accent" |
| 65 | size="iconLg" |
| 66 | disabled={disabled || isInputEmpty} |
| 67 | title="Send message" |
| 68 | > |
| 69 | <motion.span |
| 70 | animate={{ |
| 71 | rotate: isInputEmpty ? 0 : -90, |
| 72 | }} |
| 73 | transition={{ |
| 74 | type: "spring", |
| 75 | stiffness: 500, |
| 76 | damping: 20, |
| 77 | }} |
| 78 | > |
| 79 | <ChevronsRight className="w-5 h-5" /> |
| 80 | </motion.span> |
| 81 | </Button> |