File size: 5,816 Bytes
6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b |
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 |
import React from 'react'
import { Button } from '@/components/ui/button'
import { Card } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { ChatSession } from '@/types/chat'
import {
Plus,
MessageSquare,
Trash2,
Edit2,
Check,
X
} from 'lucide-react'
interface ChatSessionsProps {
sessions: ChatSession[]
currentSessionId: string | null
onSelectSession: (sessionId: string) => void
onNewSession: () => void
onDeleteSession: (sessionId: string) => void
onRenameSession: (sessionId: string, newTitle: string) => void
}
export function ChatSessions({
sessions,
currentSessionId,
onSelectSession,
onNewSession,
onDeleteSession,
onRenameSession
}: ChatSessionsProps) {
const [editingId, setEditingId] = React.useState<string | null>(null)
const [editTitle, setEditTitle] = React.useState('')
const startEditing = (session: ChatSession) => {
setEditingId(session.id)
setEditTitle(session.title)
}
const finishEditing = () => {
if (editingId && editTitle.trim()) {
onRenameSession(editingId, editTitle.trim())
}
setEditingId(null)
setEditTitle('')
}
const cancelEditing = () => {
setEditingId(null)
setEditTitle('')
}
return (
<div className="h-full flex flex-col">
{/* Header */}
<div className="p-4 border-b">
<div className="flex items-center justify-between mb-4">
<h2 className="font-semibold">Chat Sessions</h2>
<Button onClick={onNewSession} size="sm">
<Plus className="h-4 w-4 mr-1" />
New
</Button>
</div>
</div>
{/* Sessions list */}
<div className="flex-1 overflow-y-auto p-4 space-y-2">
{sessions.length === 0 ? (
<div className="text-center py-8">
<MessageSquare className="h-8 w-8 mx-auto text-muted-foreground mb-2" />
<p className="text-sm text-muted-foreground">No conversations yet</p>
<Button onClick={onNewSession} variant="outline" size="sm" className="mt-2">
Start your first chat
</Button>
</div>
) : (
sessions.map((session) => (
<Card
key={session.id}
className={`p-3 cursor-pointer transition-colors hover:bg-accent ${
currentSessionId === session.id ? 'bg-accent border-primary' : ''
}`}
onClick={() => onSelectSession(session.id)}
>
<div className="space-y-2">
{/* Title */}
{editingId === session.id ? (
<div className="flex items-center gap-1">
<input
value={editTitle}
onChange={(e) => setEditTitle(e.target.value)}
onKeyPress={(e) => {
if (e.key === 'Enter') finishEditing()
if (e.key === 'Escape') cancelEditing()
}}
className="flex-1 text-sm bg-background border rounded px-2 py-1"
autoFocus
onClick={(e) => e.stopPropagation()}
/>
<Button
size="sm"
variant="ghost"
onClick={(e) => {
e.stopPropagation()
finishEditing()
}}
>
<Check className="h-3 w-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={(e) => {
e.stopPropagation()
cancelEditing()
}}
>
<X className="h-3 w-3" />
</Button>
</div>
) : (
<div className="flex items-start justify-between">
<h3 className="font-medium text-sm line-clamp-2">
{session.title}
</h3>
<div className="flex items-center gap-1 ml-2">
<Button
size="sm"
variant="ghost"
onClick={(e) => {
e.stopPropagation()
startEditing(session)
}}
className="h-6 w-6 p-0"
>
<Edit2 className="h-3 w-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={(e) => {
e.stopPropagation()
onDeleteSession(session.id)
}}
className="h-6 w-6 p-0 text-destructive hover:text-destructive"
>
<Trash2 className="h-3 w-3" />
</Button>
</div>
</div>
)}
{/* Metadata */}
<div className="flex items-center justify-between text-xs text-muted-foreground">
<span>{session.messages.length} messages</span>
<span>{new Date(session.updatedAt).toLocaleDateString()}</span>
</div>
{/* Model info */}
{session.model && (
<Badge variant="outline" className="text-xs">
{session.model}
</Badge>
)}
</div>
</Card>
))
)}
</div>
</div>
)
}
|