File size: 9,995 Bytes
6a059d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { useState, useRef, useEffect } from "react"
import { Button } from "@/components/ui/button"
import { Textarea } from "@/components/ui/textarea"
import { Badge } from "@/components/ui/badge"
import { Paperclip, Mic, Send, Loader2, Search, Sparkles } from "lucide-react"
import { FileUpload } from "./FileUpload"

import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { ChevronDown, MessageSquare } from "lucide-react"

interface ChatInputProps {
  input: string
  setInput: (value: string) => void
  isLoading: boolean
  isResearchMode: boolean
  useHybrid: boolean
  selectedFiles: File[]
  setSelectedFiles: React.Dispatch<React.SetStateAction<File[]>>
  onSendMessage: (content: string) => void
  enableAutocomplete?: boolean
  autocompleteSuggestions?: string[]
  showAutocomplete?: boolean
  setShowAutocomplete?: (show: boolean) => void
  onToggleResearch: () => void
  onToggleHybrid: () => void
}

export function ChatInput({

  input,

  setInput,

  isLoading,

  isResearchMode,

  useHybrid,

  selectedFiles,

  setSelectedFiles,

  onSendMessage,

  enableAutocomplete = true,

  autocompleteSuggestions = [],

  showAutocomplete = false,

  setShowAutocomplete,

  onToggleResearch,

  onToggleHybrid

}: ChatInputProps) {
  const inputRef = useRef<HTMLTextAreaElement | null>(null)
  const textareaRef = useRef<HTMLTextAreaElement | null>(null)

  // Auto-resize textarea
  useEffect(() => {
    if (textareaRef.current) {
      textareaRef.current.style.height = "auto"
      textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`
    }
  }, [input])

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault()
    if (input.trim() || selectedFiles.length > 0) {
      onSendMessage(input)
    }
  }

  const getCurrentMode = () => {
    if (isResearchMode) return { label: "Research", icon: Search, color: "text-blue-500" }
    if (useHybrid) return { label: "Hybrid", icon: Sparkles, color: "text-purple-500" }
    return { label: "Chat", icon: MessageSquare, color: "text-muted-foreground" }
  }

  const currentMode = getCurrentMode()
  const ModeIcon = currentMode.icon

  return (
    <div className="border-t border-white/5 bg-background/80 p-2 md:p-3 flex-shrink-0">

      <form onSubmit={handleSubmit} className="space-y-1.5">

        {selectedFiles.length > 0 && (

          <div className="mb-2 px-1 md:px-2">

            <FileUpload

              files={selectedFiles}

              onFilesChange={setSelectedFiles}

              maxFiles={5}

              maxSizeMB={10}

            />

          </div>

        )}

        <div className="relative group rounded-2xl">

          <div className="absolute -inset-0.5 bg-gradient-to-r from-blue-500 to-purple-600 rounded-2xl blur opacity-30 group-hover:opacity-75 transition duration-1000 group-hover:duration-200 animate-tilt"></div>

          <div className="relative rounded-2xl border border-white/10 bg-black/90 px-2 md:px-3 py-2 shadow-lg backdrop-blur-lg">

            <div className="flex items-end gap-1.5 md:gap-2">

            <div className="flex items-center gap-0.5 md:gap-1 flex-shrink-0">

              <DropdownMenu>

                <DropdownMenuTrigger asChild>

                  <Button

                    variant="ghost"

                    size="sm"

                    className="h-8 gap-1.5 px-2 text-xs font-medium text-muted-foreground hover:text-foreground"

                  >

                    <ModeIcon className={`w-3.5 h-3.5 ${currentMode.color}`} />

                    <span className="hidden sm:inline">{currentMode.label}</span>

                    <ChevronDown className="w-3 h-3 opacity-50" />

                  </Button>

                </DropdownMenuTrigger>

                <DropdownMenuContent align="start" className="w-48">

                  <DropdownMenuItem onClick={() => {

                    if (isResearchMode) onToggleResearch()

                    if (useHybrid) onToggleHybrid()

                  }}>

                    <MessageSquare className="w-4 h-4 mr-2" />

                    Standard Chat

                  </DropdownMenuItem>

                  <DropdownMenuItem onClick={() => {

                    if (isResearchMode) onToggleResearch()

                    if (!useHybrid) onToggleHybrid()

                  }}>

                    <Sparkles className="w-4 h-4 mr-2 text-purple-500" />

                    Hybrid RAG

                  </DropdownMenuItem>

                  <DropdownMenuItem onClick={() => {

                    if (!isResearchMode) onToggleResearch()

                    if (useHybrid) onToggleHybrid() // Ensure hybrid is off when research is on, though logic might handle this upstream

                  }}>

                    <Search className="w-4 h-4 mr-2 text-blue-500" />

                    Agentic Research

                  </DropdownMenuItem>

                </DropdownMenuContent>

              </DropdownMenu>



              <div className="w-px h-4 bg-white/10 mx-1" />



              <Button

                variant="ghost"

                size="icon"

                type="button"

                className="h-8 w-8 rounded-xl text-muted-foreground"

                onClick={() => {

                  const input = document.createElement("input")

                  input.type = "file"

                  input.multiple = true

                  input.accept = ".pdf,.png,.jpg,.jpeg,.txt,.md"

                  input.onchange = (e) => {

                    const files = (e.target as HTMLInputElement).files

                    if (files) {

                      setSelectedFiles(prev => [...prev, ...Array.from(files)].slice(0, 5))

                    }

                  }

                  input.click()

                }}

              >

                <Paperclip className="w-4 h-4 md:w-3.5 md:h-3.5" />

              </Button>

            </div>

            <div className="relative flex-1 min-w-0">

              <Textarea

                ref={(el) => {

                  inputRef.current = el

                  textareaRef.current = el

                }}

                value={input}

                onChange={(e) => {

                  setInput(e.target.value)

                  if (enableAutocomplete && setShowAutocomplete) {

                    setShowAutocomplete(true)

                  }

                }}

                onKeyDown={(e) => {

                  // Enter to send, Shift+Enter for new line

                  if (e.key === "Enter" && !e.shiftKey) {

                    e.preventDefault()

                    if ((input.trim() || selectedFiles.length > 0) && !isLoading) {

                      onSendMessage(input)

                    }

                  }

                }}

                onFocus={() => {

                  if (enableAutocomplete && autocompleteSuggestions.length > 0 && setShowAutocomplete) {

                    setShowAutocomplete(true)

                  }

                }}

                onBlur={() => {

                  // Delay hiding to allow clicking on suggestions

                  if (setShowAutocomplete) {

                    setTimeout(() => setShowAutocomplete(false), 200)

                  }

                }}

                placeholder={

                  isResearchMode

                    ? "Ask detailed legal research questions..."

                    : "Ask about Kenyan law..."

                }

                className="w-full border-0 bg-transparent text-sm md:text-base focus-visible:ring-0 py-2 md:py-2 resize-none min-h-[44px] max-h-[200px] overflow-y-auto [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none] text-white placeholder:text-white/50"

                disabled={isLoading}

                rows={1}

              />

              {enableAutocomplete && showAutocomplete && autocompleteSuggestions.length > 0 && (

                <div className="absolute bottom-full left-0 right-0 mb-1 rounded-xl border border-white/10 bg-white/95 dark:bg-gray-900/95 backdrop-blur-lg shadow-xl z-[100] max-h-48 md:max-h-60 overflow-y-auto [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]">

                  {autocompleteSuggestions.map((suggestion, idx) => (

                    <button

                      key={idx}

                      type="button"

                      onMouseDown={(e) => {

                        // Prevent input blur when clicking

                        e.preventDefault()

                        setInput(suggestion)

                        if (setShowAutocomplete) setShowAutocomplete(false)

                        inputRef.current?.focus()

                      }}

                      className="w-full px-3 md:px-4 py-2.5 md:py-2 text-left text-sm hover:bg-primary/10 dark:hover:bg-primary/20 transition-colors first:rounded-t-xl last:rounded-b-xl text-foreground min-h-[44px] flex items-center"

                    >

                      <div className="flex items-center gap-2">

                        <Search className="w-3.5 h-3.5 text-muted-foreground flex-shrink-0" />

                        <span className="truncate">{suggestion}</span>

                      </div>

                    </button>

                  ))}

                </div>

              )}

            </div>

            <Button type="submit" disabled={isLoading || (!input.trim() && selectedFiles.length === 0)} className="h-11 w-11 md:h-9 md:w-9 rounded-xl flex-shrink-0">

              {isLoading ? <Loader2 className="w-4 h-4 md:w-3.5 md:h-3.5 animate-spin" /> : <Send className="w-4 h-4 md:w-3.5 md:h-3.5" />}

            </Button>

          </div>  

        </div>

      </div>

      </form>

    </div>
  )
}