File size: 14,113 Bytes
2db8ee1
 
 
7af9025
 
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
 
7af9025
2db8ee1
 
7af9025
 
 
 
 
 
 
 
2db8ee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8c0ee6
 
 
2db8ee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7af9025
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
 
 
7af9025
 
 
 
 
 
 
2db8ee1
7af9025
 
 
 
 
 
 
2db8ee1
 
7af9025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
7af9025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
7af9025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
7af9025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8ee1
7af9025
2db8ee1
 
d8c0ee6
 
 
 
 
 
 
 
 
 
2db8ee1
 
 
 
 
 
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { useState, useRef } from 'react'
import './Sidebar.css'

function Sidebar({
    isOpen,
    isAuthenticated,
    documents,
    conversations,
    activeConversationId,
    onUpload,
    onDeleteDocument,
    onNewChat,
    onSelectConversation,
    onDeleteConversation,
    onClearData
}) {
    const [isDragging, setIsDragging] = useState(false)
    const [uploadProgress, setUploadProgress] = useState(null)
    const [showDocs, setShowDocs] = useState(false)
    const fileInputRef = useRef(null)

    if (!isAuthenticated) {
        return (
            <aside className={`sidebar ${isOpen ? 'open' : 'closed'}`}>
                <div className="sidebar-content" />
            </aside>
        )
    }

    const handleDragOver = (e) => {
        e.preventDefault()
        setIsDragging(true)
    }

    const handleDragLeave = (e) => {
        e.preventDefault()
        setIsDragging(false)
    }

    const handleDrop = async (e) => {
        e.preventDefault()
        setIsDragging(false)

        const files = Array.from(e.dataTransfer.files)
        const validFiles = files.filter(f =>
            f.type === 'application/pdf' ||
            f.type === 'text/plain' ||
            f.name.endsWith('.docx') ||
            f.type.startsWith('audio/') ||
            ['.mp3', '.wav', '.m4a', '.ogg', '.flac'].some(ext => f.name.endsWith(ext))
        )

        for (const file of validFiles) {
            try {
                setUploadProgress({ name: file.name, percent: 0 })
                await onUpload(file)
                setUploadProgress(null)
            } catch (err) {
                console.error('Upload failed:', err)
                setUploadProgress(null)
            }
        }
    }

    const handleFileSelect = async (e) => {
        const files = Array.from(e.target.files)
        for (const file of files) {
            try {
                setUploadProgress({ name: file.name, percent: 0 })
                await onUpload(file)
                setUploadProgress(null)
            } catch (err) {
                console.error('Upload failed:', err)
                setUploadProgress(null)
            }
        }
        e.target.value = ''
    }

    const getStatusIcon = (status) => {
        switch (status) {
            case 'uploading':
                return (
                    <div className="status-icon uploading">
                        <svg className="animate-spin" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                            <path d="M21 12a9 9 0 11-6.219-8.56" />
                        </svg>
                    </div>
                )
            case 'ready':
                return (
                    <div className="status-icon ready">
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                            <path d="M20 6L9 17l-5-5" />
                        </svg>
                    </div>
                )
            case 'error':
                return (
                    <div className="status-icon error">
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                            <circle cx="12" cy="12" r="10" />
                            <path d="M12 8v4M12 16h.01" />
                        </svg>
                    </div>
                )
            default:
                return null
        }
    }

    const getFileIcon = (name) => {
        if (name.endsWith('.pdf')) {
            return (
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
                    <path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z" />
                    <path d="M14 2v6h6M9 15h6M9 11h6" />
                </svg>
            )
        }
        return (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
                <path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z" />
                <path d="M14 2v6h6" />
            </svg>
        )
    }

    const getRelativeTime = (timestamp) => {
        if (!timestamp) return ''
        const now = new Date()
        const then = new Date(timestamp)
        const diff = Math.floor((now - then) / 1000)
        if (diff < 60) return 'just now'
        if (diff < 3600) return `${Math.floor(diff / 60)}m ago`
        if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`
        if (diff < 604800) return `${Math.floor(diff / 86400)}d ago`
        return then.toLocaleDateString()
    }

    return (
        <aside className={`sidebar ${isOpen ? 'open' : 'closed'}`}>
            <div className="sidebar-content">
                {/* New Chat Button */}
                <button className="new-chat-btn" onClick={onNewChat}>
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                        <path d="M12 5v14M5 12h14" />
                    </svg>
                    New Chat
                </button>

                {/* Chat History */}
                <div className="chat-history-section">
                    <div className="section-header">
                        <h3>Conversations</h3>
                        {conversations.length > 0 && (
                            <span className="doc-count">{conversations.length}</span>
                        )}
                    </div>

                    <div className="conversation-list">
                        {conversations.length === 0 ? (
                            <div className="empty-conversations">
                                <p>No conversations yet</p>
                                <span>Start chatting to create one</span>
                            </div>
                        ) : (
                            conversations.map((conv) => (
                                <div
                                    key={conv.id}
                                    className={`conversation-item ${conv.id === activeConversationId ? 'active' : ''}`}
                                    onClick={() => onSelectConversation(conv.id)}
                                >
                                    <div className="conv-icon">
                                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
                                            <path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z" />
                                        </svg>
                                    </div>
                                    <div className="conv-info">
                                        <span className="conv-title">{conv.title}</span>
                                        <span className="conv-time">{getRelativeTime(conv.updated_at)}</span>
                                    </div>
                                    <button
                                        className="conv-delete btn btn-ghost btn-icon"
                                        onClick={(e) => {
                                            e.stopPropagation()
                                            onDeleteConversation(conv.id)
                                        }}
                                        aria-label="Delete conversation"
                                    >
                                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                                            <path d="M18 6L6 18M6 6l12 12" />
                                        </svg>
                                    </button>
                                </div>
                            ))
                        )}
                    </div>
                </div>

                {/* Documents Section - Collapsible */}
                <div className="documents-section">
                    <div
                        className="section-header clickable"
                        onClick={() => setShowDocs(!showDocs)}
                    >
                        <h3>
                            <svg
                                width="12" height="12"
                                viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" strokeWidth="2"
                                className={`chevron ${showDocs ? 'open' : ''}`}
                            >
                                <path d="M9 18l6-6-6-6" />
                            </svg>
                            Documents
                        </h3>
                        <span className="doc-count">{documents.length}</span>
                    </div>

                    {showDocs && (
                        <>
                            {/* Upload Zone */}
                            <div
                                className={`upload-zone ${isDragging ? 'dragging' : ''}`}
                                onDragOver={handleDragOver}
                                onDragLeave={handleDragLeave}
                                onDrop={handleDrop}
                                onClick={() => fileInputRef.current?.click()}
                            >
                                <input
                                    ref={fileInputRef}
                                    type="file"
                                    accept=".pdf,.txt,.docx,.mp3,.wav,.m4a,.ogg,.flac"
                                    multiple
                                    onChange={handleFileSelect}
                                    hidden
                                />
                                <div className="upload-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                                        <path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M17 8l-5-5-5 5M12 3v12" />
                                    </svg>
                                </div>
                                <p className="upload-text">
                                    {isDragging ? 'Drop files here' : 'Upload documents'}
                                </p>
                                <p className="upload-hint">PDF, TXT, DOCX, MP3, WAV, M4A</p>
                            </div>

                            {/* Upload Progress */}
                            {uploadProgress && (
                                <div className="upload-progress">
                                    <div className="progress-info">
                                        <span className="progress-name">{uploadProgress.name}</span>
                                        <span className="progress-status">Uploading...</span>
                                    </div>
                                    <div className="progress-bar">
                                        <div className="progress-fill animate-pulse" style={{ width: '100%' }} />
                                    </div>
                                </div>
                            )}

                            {/* Document List */}
                            <div className="document-list">
                                {documents.length === 0 ? (
                                    <div className="empty-docs">
                                        <p>No documents yet</p>
                                        <span>Upload files to get started</span>
                                    </div>
                                ) : (
                                    documents.map((doc) => (
                                        <div key={doc.id} className={`document-item ${doc.status}`}>
                                            <div className="doc-icon">{getFileIcon(doc.name)}</div>
                                            <div className="doc-info">
                                                <span className="doc-name">{doc.name}</span>
                                                {doc.chunks && (
                                                    <span className="doc-meta">{doc.chunks} chunks</span>
                                                )}
                                            </div>
                                            {getStatusIcon(doc.status)}
                                            <button
                                                className="doc-delete btn btn-ghost btn-icon"
                                                onClick={() => onDeleteDocument(doc.id)}
                                                aria-label="Delete document"
                                            >
                                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                                                    <path d="M18 6L6 18M6 6l12 12" />
                                                </svg>
                                            </button>
                                        </div>
                                    ))
                                )}
                            </div>
                        </>
                    )}
                </div>

                {/* Clear All Data */}
                <div className="clear-data-section">
                    <button className="btn btn-ghost btn-sm clear-data-btn" onClick={onClearData}>
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                            <path d="M3 6h18M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2" />
                        </svg>
                        Clear All Data
                    </button>
                </div>
            </div>
        </aside>
    )
}

export default Sidebar