File size: 10,278 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
232
233
234
235
236
237
238
239
"use client"

import { useState, useRef, useEffect } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Plus, MessageSquare, Trash2, X, Pencil, Check, X as XIcon } from "lucide-react"
import type { ChatSession } from "./types"

interface ChatSidebarProps {
  chatHistory: ChatSession[]
  currentSessionId: string | null
  showHistory: boolean
  onLoadSession: (sessionId: string) => void
  onDeleteSession: (sessionId: string) => void
  onRenameSession: (sessionId: string, newTitle: string) => void
  onCreateSession: () => void
  onCloseHistory: () => void
}

export function ChatSidebar({

  chatHistory,

  currentSessionId,

  showHistory,

  onLoadSession,

  onDeleteSession,

  onRenameSession,

  onCreateSession,

  onCloseHistory

}: ChatSidebarProps) {
  const [editingSessionId, setEditingSessionId] = useState<string | null>(null)
  const [editTitle, setEditTitle] = useState("")
  const editInputRef = useRef<HTMLInputElement>(null)

  useEffect(() => {
    if (editingSessionId && editInputRef.current) {
      editInputRef.current.focus()
    }
  }, [editingSessionId])

  const startEditing = (session: ChatSession, e: React.MouseEvent) => {
    e.stopPropagation()
    setEditingSessionId(session.id)
    setEditTitle(session.title || "")
  }

  const cancelEditing = (e?: React.MouseEvent) => {
    e?.stopPropagation()
    setEditingSessionId(null)
    setEditTitle("")
  }

  const saveTitle = (sessionId: string, e?: React.MouseEvent) => {
    e?.stopPropagation()
    if (editTitle.trim()) {
      onRenameSession(sessionId, editTitle.trim())
    }
    setEditingSessionId(null)
    setEditTitle("")
  }

  const handleKeyDown = (e: React.KeyboardEvent, sessionId: string) => {
    if (e.key === "Enter") {
      saveTitle(sessionId)
    } else if (e.key === "Escape") {
      cancelEditing()
    }
  }
  // Prevent body scroll when mobile sidebar is open
  useEffect(() => {
    if (showHistory) {
      document.body.style.overflow = "hidden"
    } else {
      document.body.style.overflow = "unset"
    }
    return () => {
      document.body.style.overflow = "unset"
    }
  }, [showHistory])

  return (
    <>

      {/* Desktop Sidebar */}

      <div className="hidden md:flex w-72 border-r border-white/5 bg-black/20 backdrop-blur-xl flex-col transition-all duration-300 ease-in-out h-full">

        <div className="p-4 border-b border-white/5 flex-shrink-0">

          <div className="flex items-center justify-between">

            <div>

              <p className="text-xs uppercase tracking-widest text-muted-foreground">Sessions</p>

              <h3 className="font-semibold text-sm">Chat History</h3>

            </div>

            <Button variant="ghost" size="sm" onClick={onCreateSession} className="h-8 w-8 rounded-full">

              <Plus className="w-3 h-3" />

            </Button>

          </div>

        </div>

        <div className="p-3 space-y-2 flex-1 overflow-y-auto [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]">

          {chatHistory.map((session) => (

            <div key={session.id} className="flex items-center space-x-2 p-1 group min-w-0">

              <div

                className={`flex-1 flex items-center justify-between text-left h-auto px-3 py-2 text-xs transition-all duration-200 rounded-2xl border border-transparent group-hover:border-white/10 min-w-0 cursor-pointer ${currentSessionId === session.id ? "bg-secondary text-secondary-foreground" : "hover:bg-accent hover:text-accent-foreground"}`}

                onClick={() => onLoadSession(session.id)}

              >

                <div className="flex items-center w-full min-w-0">

                  <div className="flex h-7 w-7 items-center justify-center rounded-xl bg-white/10 mr-3 flex-shrink-0">

                    <MessageSquare className="w-3.5 h-3.5" />

                  </div>

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

                    {editingSessionId === session.id ? (

                      <div className="flex items-center gap-1" onClick={(e) => e.stopPropagation()}>

                        <Input

                          ref={editInputRef}

                          value={editTitle}

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

                          onKeyDown={(e) => handleKeyDown(e, session.id)}

                          className="h-6 text-xs py-0 px-1 bg-background/50"

                        />

                        <Button

                          variant="ghost"

                          size="sm"

                          className="h-6 w-6 p-0 hover:bg-green-500/20 hover:text-green-500"

                          onClick={(e) => saveTitle(session.id, e)}

                        >

                          <Check className="w-3 h-3" />

                        </Button>

                        <Button

                          variant="ghost"

                          size="sm"

                          className="h-6 w-6 p-0 hover:bg-red-500/20 hover:text-red-500"

                          onClick={cancelEditing}

                        >

                          <XIcon className="w-3 h-3" />

                        </Button>

                      </div>

                    ) : (

                      <>

                        <div 

                          className="font-medium truncate text-xs block"

                          title={session.title || `Chat ${session.id.slice(-6)}`}

                        >

                          {session.title || `Chat ${session.id.slice(-6)}`}

                        </div>

                        <div className="text-[11px] text-muted-foreground truncate">{session.message_count} messages</div>

                      </>

                    )}

                  </div>

                </div>

              </div>

              <div className="flex flex-col gap-1 opacity-0 group-hover:opacity-100 transition-opacity duration-200">

                <Button

                  variant="ghost"

                  size="sm"

                  className="text-muted-foreground hover:text-primary h-6 w-6 rounded-full p-0"

                  onClick={(e) => startEditing(session, e)}

                >

                  <Pencil className="w-3 h-3" />

                </Button>

                <Button

                  variant="ghost"

                  size="sm"

                  className="text-muted-foreground hover:text-destructive h-6 w-6 rounded-full p-0"

                  onClick={(e) => {

                    e.stopPropagation()

                    onDeleteSession(session.id)

                  }}

                >

                  <Trash2 className="w-3 h-3" />

                </Button>

              </div>

            </div>

          ))}

        </div>

      </div>



      {/* Mobile Sidebar Overlay */}

      {showHistory && (

        <div className="md:hidden fixed inset-0 z-[60] bg-black/70 backdrop-blur-sm" onClick={onCloseHistory}>

          <div className="absolute left-0 top-0 h-full w-[85%] max-w-sm bg-background border-r border-white/10 shadow-2xl z-[61] flex flex-col" onClick={(e) => e.stopPropagation()}>

            <div className="p-3 md:p-4 border-b border-white/10 flex-shrink-0">

              <div className="flex items-center justify-between mb-2">

                <h3 className="font-semibold text-sm md:text-base">Chat History</h3>

                <Button variant="ghost" size="sm" className="h-9 w-9 rounded-full" onClick={onCloseHistory}>

                  <X className="w-4 h-4" />

                </Button>

              </div>

              <Button

                variant="outline"

                size="sm"

                className="w-full h-9 md:h-10 rounded-xl text-xs md:text-sm"

                onClick={() => {

                  onCreateSession()

                  onCloseHistory()

                }}

              >

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

                New Chat

              </Button>

            </div>

            <div className="p-2 md:p-3 space-y-1 flex-1 overflow-y-auto [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]">

              {chatHistory.map((session) => (

                <div key={session.id} className="flex items-center space-x-1.5 md:space-x-2 p-1 group min-w-0">

                  <Button

                    variant={currentSessionId === session.id ? "secondary" : "ghost"}

                    className="flex-1 justify-start text-left h-auto p-2 md:p-3 min-w-0 min-h-[44px]"

                    onClick={() => {

                      onLoadSession(session.id)

                      onCloseHistory()

                    }}

                  >

                    <MessageSquare className="w-3.5 h-3.5 md:w-4 md:h-4 mr-1.5 md:mr-2 flex-shrink-0" />

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

                      <div 

                        className="font-medium truncate block text-xs md:text-sm"

                        title={session.title || `Chat ${session.id.slice(-8)}`}

                      >

                        {session.title || `Chat ${session.id.slice(-8)}`}

                      </div>

                      <div className="text-[10px] md:text-xs text-muted-foreground truncate">{session.message_count} messages</div>

                    </div>

                  </Button>

                  <Button

                    variant="ghost"

                    size="sm"

                    className="text-muted-foreground hover:text-destructive h-9 w-9 md:h-10 md:w-10 rounded-full flex-shrink-0"

                    onClick={(e) => {

                      e.stopPropagation()

                      onDeleteSession(session.id)

                    }}

                  >

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

                  </Button>

                </div>

              ))}

            </div>

          </div>

        </div>

      )}

    </>
  )
}