File size: 2,382 Bytes
9853b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client'

import { useState, FormEvent, KeyboardEvent } from 'react'
import { Button } from 'components/ui/button'

interface ChatInputProps {
  onSend: (message: string) => Promise<void>
  disabled?: boolean
  placeholder?: string
}

export function ChatInput({ onSend, disabled, placeholder = "Napište zprávu..." }: ChatInputProps) {
  const [message, setMessage] = useState('')
  const [sending, setSending] = useState(false)

  const handleSubmit = async (e: FormEvent) => {
    e.preventDefault()

    const trimmed = message.trim()
    if (!trimmed || sending || disabled) return

    setSending(true)
    try {
      await onSend(trimmed)
      setMessage('')
    } catch (error) {
      console.error('Failed to send message:', error)
    } finally {
      setSending(false)
    }
  }

  const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault()
      handleSubmit(e as unknown as FormEvent)
    }
  }

  return (
    <form onSubmit={handleSubmit} className="p-4 border-t bg-white">

      <div className="flex items-end gap-3">

        <div className="flex-1 relative">

          <textarea

            value={message}

            onChange={(e) => setMessage(e.target.value)}

            onKeyDown={handleKeyDown}

            placeholder={placeholder}

            disabled={disabled || sending}

            className="w-full resize-none rounded-2xl border border-gray-300 px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500 disabled:opacity-50"

            rows={1}

            style={{ minHeight: '44px', maxHeight: '120px' }}

          />

        </div>



        <Button

          type="submit"

          size="sm"

          disabled={!message.trim() || disabled || sending}

          className="rounded-full h-11 w-11 p-0"

        >

          {sending ? (

            <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin" />

          ) : (

            <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">

              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" />

            </svg>

          )}

        </Button>

      </div>

    </form>
  )
}