nextjs-chatbot-ui 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,650 +1,701 @@
1
- 'use client';
2
-
3
- import React, { useState } from 'react';
4
- import { DatabaseType, DatabaseConnection, ColumnSelection, DatabaseConfig, AdminSetupProps } from '../types/admin';
5
- import clsx from 'clsx';
6
-
7
- const AdminSetup: React.FC<AdminSetupProps> = ({
8
- onSave,
9
- onTestConnection,
10
- onFetchColumns,
11
- }) => {
12
- const [isModalOpen, setIsModalOpen] = useState(false);
13
- const [currentStep, setCurrentStep] = useState<'connection' | 'columns'>('connection');
14
- const [isConnecting, setIsConnecting] = useState(false);
15
- const [connectionError, setConnectionError] = useState<string | null>(null);
16
- const [connectionSuccess, setConnectionSuccess] = useState(false);
17
- const [availableColumns, setAvailableColumns] = useState<string[]>([]);
18
- const [isLoadingColumns, setIsLoadingColumns] = useState(false);
19
-
20
- const [dbType, setDbType] = useState<DatabaseType>('mongodb');
21
- const [connection, setConnection] = useState<DatabaseConnection>({
22
- type: 'mongodb',
23
- host: '',
24
- port: 27017,
25
- database: '',
26
- username: '',
27
- password: '',
28
- connectionString: '',
29
- ssl: false,
30
- });
31
-
32
- const [columnSelection, setColumnSelection] = useState<ColumnSelection>({
33
- embeddingColumns: [],
34
- llmColumns: [],
35
- chromaColumns: [],
36
- });
37
-
38
- const handleDbTypeChange = (type: DatabaseType) => {
39
- setDbType(type);
40
- setConnection({
41
- ...connection,
42
- type,
43
- port: type === 'mongodb' ? 27017 : 5432,
44
- });
45
- setConnectionError(null);
46
- };
47
-
48
- const handleConnectionChange = (field: keyof DatabaseConnection, value: any) => {
49
- setConnection({
50
- ...connection,
51
- [field]: value,
52
- });
53
- setConnectionError(null);
54
- };
55
-
56
- const handleTestConnection = async (): Promise<boolean> => {
57
- if (!onTestConnection) {
58
- // Default test - just validate fields
59
- if (!connection.host || !connection.database) {
60
- setConnectionError('Please fill in all required fields');
61
- setConnectionSuccess(false);
62
- return false;
63
- }
64
- setConnectionError(null);
65
- setConnectionSuccess(true);
66
- return true;
67
- }
68
-
69
- setIsConnecting(true);
70
- setConnectionError(null);
71
- setConnectionSuccess(false);
72
-
73
- try {
74
- const isValid = await onTestConnection(connection);
75
- if (isValid) {
76
- setConnectionError(null);
77
- setConnectionSuccess(true);
78
- return true;
79
- } else {
80
- setConnectionError('Connection failed. Please check your credentials.');
81
- setConnectionSuccess(false);
82
- return false;
83
- }
84
- } catch (error: any) {
85
- setConnectionError(error.message || 'Connection failed. Please try again.');
86
- setConnectionSuccess(false);
87
- return false;
88
- } finally {
89
- setIsConnecting(false);
90
- }
91
- };
92
-
93
- const handleFetchColumns = async (): Promise<string[]> => {
94
- if (!onFetchColumns) {
95
- // Mock columns for demo
96
- const mockColumns = ['id', 'title', 'content', 'description', 'category', 'tags', 'created_at', 'updated_at'];
97
- setAvailableColumns(mockColumns);
98
- setIsLoadingColumns(false);
99
- return mockColumns;
100
- }
101
-
102
- setIsLoadingColumns(true);
103
- setConnectionError(null);
104
- try {
105
- const columns = await onFetchColumns(connection);
106
- if (columns && columns.length > 0) {
107
- setAvailableColumns(columns);
108
- setIsLoadingColumns(false);
109
- return columns;
110
- } else {
111
- setConnectionError('No columns found in the database.');
112
- setIsLoadingColumns(false);
113
- return [];
114
- }
115
- } catch (error: any) {
116
- setConnectionError(error.message || 'Failed to fetch columns');
117
- setIsLoadingColumns(false);
118
- return [];
119
- }
120
- };
121
-
122
- const handleConnectAndNext = async () => {
123
- // Validate connection fields
124
- if (dbType === 'mongodb') {
125
- if (!connection.connectionString && (!connection.host || !connection.database)) {
126
- setConnectionError('Please provide connection string or host and database');
127
- setConnectionSuccess(false);
128
- return;
129
- }
130
- } else {
131
- if (!connection.host || !connection.database || !connection.username || !connection.password) {
132
- setConnectionError('Please fill in all required fields');
133
- setConnectionSuccess(false);
134
- return;
135
- }
136
- }
137
-
138
- // Clear previous errors
139
- setConnectionError(null);
140
- setConnectionSuccess(false);
141
-
142
- // Test connection first
143
- const connectionSuccess = await handleTestConnection();
144
-
145
- if (!connectionSuccess) {
146
- // Connection failed, don't proceed
147
- return;
148
- }
149
-
150
- // If connection successful, fetch columns
151
- const fetchedColumns = await handleFetchColumns();
152
-
153
- if (fetchedColumns && fetchedColumns.length > 0) {
154
- // Successfully fetched columns, move to next step
155
- setCurrentStep('columns');
156
- setConnectionError(null);
157
- } else {
158
- // Column fetching failed, show error but keep connection success
159
- // Error is already set in handleFetchColumns
160
- }
161
- };
162
-
163
- const handleColumnToggle = (column: string, category: keyof ColumnSelection) => {
164
- setColumnSelection((prev) => {
165
- const currentColumns = prev[category];
166
- const isSelected = currentColumns.includes(column);
167
-
168
- return {
169
- ...prev,
170
- [category]: isSelected
171
- ? currentColumns.filter((c) => c !== column)
172
- : [...currentColumns, column],
173
- };
174
- });
175
- };
176
-
177
- const handleSave = () => {
178
- const config: DatabaseConfig = {
179
- connection,
180
- columnSelection,
181
- };
182
-
183
- if (onSave) {
184
- onSave(config);
185
- }
186
-
187
- // Close modal and reset
188
- setIsModalOpen(false);
189
- setCurrentStep('connection');
190
- setConnectionError(null);
191
- };
192
-
193
- const handleClose = () => {
194
- setIsModalOpen(false);
195
- setCurrentStep('connection');
196
- setConnectionError(null);
197
- setConnectionSuccess(false);
198
- setAvailableColumns([]);
199
- setColumnSelection({
200
- embeddingColumns: [],
201
- llmColumns: [],
202
- chromaColumns: [],
203
- });
204
- };
205
-
206
- return (
207
- <>
208
- {/* Sidebar Button/Item - This can be integrated into admin sidebar */}
209
- <button
210
- onClick={() => setIsModalOpen(true)}
211
- className="w-full flex items-center gap-3 px-4 py-2.5 text-left hover:bg-gray-100 rounded-lg transition-colors"
212
- >
213
- <svg
214
- xmlns="http://www.w3.org/2000/svg"
215
- className="h-5 w-5 text-gray-600"
216
- fill="none"
217
- viewBox="0 0 24 24"
218
- stroke="currentColor"
219
- >
220
- <path
221
- strokeLinecap="round"
222
- strokeLinejoin="round"
223
- strokeWidth={2}
224
- d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"
225
- />
226
- </svg>
227
- <span className="text-sm font-medium text-gray-700">Database Setup</span>
228
- </button>
229
-
230
- {/* Modal */}
231
- {isModalOpen && (
232
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50">
233
- <div className="bg-white rounded-lg shadow-xl w-full max-w-2xl max-h-[90vh] overflow-hidden flex flex-col">
234
- {/* Modal Header */}
235
- <div className="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
236
- <h2 className="text-xl font-semibold text-gray-900">
237
- {currentStep === 'connection' ? 'Database Connection' : 'Select Columns'}
238
- </h2>
239
- <button
240
- onClick={handleClose}
241
- className="text-gray-400 hover:text-gray-600 transition-colors"
242
- >
243
- <svg
244
- xmlns="http://www.w3.org/2000/svg"
245
- className="h-6 w-6"
246
- fill="none"
247
- viewBox="0 0 24 24"
248
- stroke="currentColor"
249
- >
250
- <path
251
- strokeLinecap="round"
252
- strokeLinejoin="round"
253
- strokeWidth={2}
254
- d="M6 18L18 6M6 6l12 12"
255
- />
256
- </svg>
257
- </button>
258
- </div>
259
-
260
- {/* Modal Content */}
261
- <div className="flex-1 overflow-y-auto px-6 py-4">
262
- {currentStep === 'connection' ? (
263
- <div className="space-y-6">
264
- {/* Database Type Selection */}
265
- <div>
266
- <label className="block text-sm font-medium text-gray-700 mb-3">
267
- Database Type
268
- </label>
269
- <div className="grid grid-cols-2 gap-4">
270
- <button
271
- onClick={() => handleDbTypeChange('mongodb')}
272
- className={clsx(
273
- 'p-4 border-2 rounded-lg transition-all text-left',
274
- dbType === 'mongodb'
275
- ? 'border-blue-500 bg-blue-50'
276
- : 'border-gray-200 hover:border-gray-300'
277
- )}
278
- >
279
- <div className="flex items-center gap-3">
280
- <div className={clsx(
281
- 'w-5 h-5 rounded-full border-2 flex items-center justify-center',
282
- dbType === 'mongodb' ? 'border-blue-500' : 'border-gray-300'
283
- )}>
284
- {dbType === 'mongodb' && (
285
- <div className="w-3 h-3 rounded-full bg-blue-500" />
286
- )}
287
- </div>
288
- <div>
289
- <div className="font-semibold text-gray-900">MongoDB</div>
290
- <div className="text-xs text-gray-500">NoSQL Database</div>
291
- </div>
292
- </div>
293
- </button>
294
- <button
295
- onClick={() => handleDbTypeChange('postgres')}
296
- className={clsx(
297
- 'p-4 border-2 rounded-lg transition-all text-left',
298
- dbType === 'postgres'
299
- ? 'border-blue-500 bg-blue-50'
300
- : 'border-gray-200 hover:border-gray-300'
301
- )}
302
- >
303
- <div className="flex items-center gap-3">
304
- <div className={clsx(
305
- 'w-5 h-5 rounded-full border-2 flex items-center justify-center',
306
- dbType === 'postgres' ? 'border-blue-500' : 'border-gray-300'
307
- )}>
308
- {dbType === 'postgres' && (
309
- <div className="w-3 h-3 rounded-full bg-blue-500" />
310
- )}
311
- </div>
312
- <div>
313
- <div className="font-semibold text-gray-900">PostgreSQL</div>
314
- <div className="text-xs text-gray-500">SQL Database</div>
315
- </div>
316
- </div>
317
- </button>
318
- </div>
319
- </div>
320
-
321
- {/* Connection Fields */}
322
- {dbType === 'mongodb' ? (
323
- <div className="space-y-4">
324
- <div>
325
- <label className="block text-sm font-medium text-gray-700 mb-2">
326
- Connection String (Recommended)
327
- </label>
328
- <input
329
- type="text"
330
- value={connection.connectionString || ''}
331
- onChange={(e) => handleConnectionChange('connectionString', e.target.value)}
332
- placeholder="mongodb://username:password@host:port/database"
333
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
334
- />
335
- <p className="mt-1 text-xs text-gray-500">
336
- Or fill individual fields below
337
- </p>
338
- </div>
339
- <div className="border-t border-gray-200 pt-4">
340
- <div className="grid grid-cols-2 gap-4">
341
- <div>
342
- <label className="block text-sm font-medium text-gray-700 mb-2">
343
- Host *
344
- </label>
345
- <input
346
- type="text"
347
- value={connection.host}
348
- onChange={(e) => handleConnectionChange('host', e.target.value)}
349
- placeholder="localhost"
350
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
351
- />
352
- </div>
353
- <div>
354
- <label className="block text-sm font-medium text-gray-700 mb-2">
355
- Port *
356
- </label>
357
- <input
358
- type="number"
359
- value={connection.port}
360
- onChange={(e) => handleConnectionChange('port', parseInt(e.target.value) || 27017)}
361
- placeholder="27017"
362
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
363
- />
364
- </div>
365
- </div>
366
- <div className="mt-4">
367
- <label className="block text-sm font-medium text-gray-700 mb-2">
368
- Database Name *
369
- </label>
370
- <input
371
- type="text"
372
- value={connection.database}
373
- onChange={(e) => handleConnectionChange('database', e.target.value)}
374
- placeholder="my_database"
375
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
376
- />
377
- </div>
378
- <div className="mt-4">
379
- <label className="flex items-center gap-2">
380
- <input
381
- type="checkbox"
382
- checked={connection.ssl || false}
383
- onChange={(e) => handleConnectionChange('ssl', e.target.checked)}
384
- className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
385
- />
386
- <span className="text-sm text-gray-700">Enable SSL</span>
387
- </label>
388
- </div>
389
- </div>
390
- </div>
391
- ) : (
392
- <div className="space-y-4">
393
- <div className="grid grid-cols-2 gap-4">
394
- <div>
395
- <label className="block text-sm font-medium text-gray-700 mb-2">
396
- Host *
397
- </label>
398
- <input
399
- type="text"
400
- value={connection.host}
401
- onChange={(e) => handleConnectionChange('host', e.target.value)}
402
- placeholder="localhost"
403
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
404
- />
405
- </div>
406
- <div>
407
- <label className="block text-sm font-medium text-gray-700 mb-2">
408
- Port *
409
- </label>
410
- <input
411
- type="number"
412
- value={connection.port}
413
- onChange={(e) => handleConnectionChange('port', parseInt(e.target.value) || 5432)}
414
- placeholder="5432"
415
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
416
- />
417
- </div>
418
- </div>
419
- <div>
420
- <label className="block text-sm font-medium text-gray-700 mb-2">
421
- Database Name *
422
- </label>
423
- <input
424
- type="text"
425
- value={connection.database}
426
- onChange={(e) => handleConnectionChange('database', e.target.value)}
427
- placeholder="my_database"
428
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
429
- />
430
- </div>
431
- <div className="grid grid-cols-2 gap-4">
432
- <div>
433
- <label className="block text-sm font-medium text-gray-700 mb-2">
434
- Username *
435
- </label>
436
- <input
437
- type="text"
438
- value={connection.username || ''}
439
- onChange={(e) => handleConnectionChange('username', e.target.value)}
440
- placeholder="postgres"
441
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
442
- />
443
- </div>
444
- <div>
445
- <label className="block text-sm font-medium text-gray-700 mb-2">
446
- Password *
447
- </label>
448
- <input
449
- type="password"
450
- value={connection.password || ''}
451
- onChange={(e) => handleConnectionChange('password', e.target.value)}
452
- placeholder="••••••••"
453
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
454
- />
455
- </div>
456
- </div>
457
- <div>
458
- <label className="flex items-center gap-2">
459
- <input
460
- type="checkbox"
461
- checked={connection.ssl || false}
462
- onChange={(e) => handleConnectionChange('ssl', e.target.checked)}
463
- className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
464
- />
465
- <span className="text-sm text-gray-700">Enable SSL</span>
466
- </label>
467
- </div>
468
- </div>
469
- )}
470
-
471
- {connectionError && (
472
- <div className="bg-red-50 border border-red-200 rounded-lg p-4">
473
- <p className="text-sm text-red-800">{connectionError}</p>
474
- </div>
475
- )}
476
- {connectionSuccess && !connectionError && !isConnecting && (
477
- <div className="bg-green-50 border border-green-200 rounded-lg p-4">
478
- <p className="text-sm text-green-800">✓ Connection successful!</p>
479
- </div>
480
- )}
481
- </div>
482
- ) : (
483
- <div className="space-y-6">
484
- {isLoadingColumns ? (
485
- <div className="flex flex-col items-center justify-center py-8">
486
- <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mb-3"></div>
487
- <p className="text-sm text-gray-600">Loading columns...</p>
488
- </div>
489
- ) : availableColumns.length === 0 ? (
490
- <div className="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
491
- <p className="text-sm text-yellow-800">No columns available. Please go back and check your connection.</p>
492
- </div>
493
- ) : (
494
- <>
495
- <div>
496
- <p className="text-sm text-gray-600 mb-2">
497
- Select which columns to use for <strong>Embeddings</strong>, <strong>LLM processing</strong>, and <strong>ChromaDB storage</strong>.
498
- </p>
499
- <p className="text-xs text-gray-500 mb-4">
500
- Found {availableColumns.length} column{availableColumns.length !== 1 ? 's' : ''} in your database.
501
- </p>
502
- </div>
503
-
504
- {/* Embedding Columns */}
505
- <div>
506
- <label className="block text-sm font-medium text-gray-700 mb-2">
507
- Works with Embeddings
508
- </label>
509
- <p className="text-xs text-gray-500 mb-3">Select columns that will be used for embedding generation</p>
510
- <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
511
- {availableColumns.length === 0 ? (
512
- <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
513
- ) : (
514
- availableColumns.map((column) => (
515
- <label
516
- key={`embedding-${column}`}
517
- className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
518
- >
519
- <input
520
- type="checkbox"
521
- checked={columnSelection.embeddingColumns.includes(column)}
522
- onChange={() => handleColumnToggle(column, 'embeddingColumns')}
523
- className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
524
- />
525
- <span className="text-sm text-gray-700">{column}</span>
526
- </label>
527
- ))
528
- )}
529
- </div>
530
- {columnSelection.embeddingColumns.length > 0 && (
531
- <p className="text-xs text-green-600 mt-1">
532
- {columnSelection.embeddingColumns.length} column{columnSelection.embeddingColumns.length !== 1 ? 's' : ''} selected
533
- </p>
534
- )}
535
- </div>
536
-
537
- {/* LLM Columns */}
538
- <div>
539
- <label className="block text-sm font-medium text-gray-700 mb-2">
540
- Works with LLM
541
- </label>
542
- <p className="text-xs text-gray-500 mb-3">Select columns that will be processed by the LLM</p>
543
- <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
544
- {availableColumns.length === 0 ? (
545
- <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
546
- ) : (
547
- availableColumns.map((column) => (
548
- <label
549
- key={`llm-${column}`}
550
- className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
551
- >
552
- <input
553
- type="checkbox"
554
- checked={columnSelection.llmColumns.includes(column)}
555
- onChange={() => handleColumnToggle(column, 'llmColumns')}
556
- className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
557
- />
558
- <span className="text-sm text-gray-700">{column}</span>
559
- </label>
560
- ))
561
- )}
562
- </div>
563
- {columnSelection.llmColumns.length > 0 && (
564
- <p className="text-xs text-green-600 mt-1">
565
- {columnSelection.llmColumns.length} column{columnSelection.llmColumns.length !== 1 ? 's' : ''} selected
566
- </p>
567
- )}
568
- </div>
569
-
570
- {/* ChromaDB Columns */}
571
- <div>
572
- <label className="block text-sm font-medium text-gray-700 mb-2">
573
- Works with ChromaDB
574
- </label>
575
- <p className="text-xs text-gray-500 mb-3">Select columns that will be stored in ChromaDB</p>
576
- <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
577
- {availableColumns.length === 0 ? (
578
- <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
579
- ) : (
580
- availableColumns.map((column) => (
581
- <label
582
- key={`chroma-${column}`}
583
- className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
584
- >
585
- <input
586
- type="checkbox"
587
- checked={columnSelection.chromaColumns.includes(column)}
588
- onChange={() => handleColumnToggle(column, 'chromaColumns')}
589
- className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
590
- />
591
- <span className="text-sm text-gray-700">{column}</span>
592
- </label>
593
- ))
594
- )}
595
- </div>
596
- {columnSelection.chromaColumns.length > 0 && (
597
- <p className="text-xs text-green-600 mt-1">
598
- {columnSelection.chromaColumns.length} column{columnSelection.chromaColumns.length !== 1 ? 's' : ''} selected
599
- </p>
600
- )}
601
- </div>
602
- </>
603
- )}
604
- </div>
605
- )}
606
- </div>
607
-
608
- {/* Modal Footer */}
609
- <div className="px-6 py-4 border-t border-gray-200 flex items-center justify-between">
610
- <button
611
- onClick={currentStep === 'columns' ? () => setCurrentStep('connection') : handleClose}
612
- className="px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 rounded-lg transition-colors"
613
- >
614
- {currentStep === 'columns' ? 'Back' : 'Cancel'}
615
- </button>
616
- <div className="flex gap-3">
617
- {currentStep === 'connection' ? (
618
- <button
619
- onClick={handleConnectAndNext}
620
- disabled={isConnecting || isLoadingColumns}
621
- className="px-6 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors flex items-center gap-2"
622
- >
623
- {isConnecting && (
624
- <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
625
- )}
626
- {isLoadingColumns ? 'Loading Columns...' : isConnecting ? 'Connecting...' : 'Connect & Next'}
627
- </button>
628
- ) : (
629
- <button
630
- onClick={handleSave}
631
- disabled={
632
- columnSelection.embeddingColumns.length === 0 &&
633
- columnSelection.llmColumns.length === 0 &&
634
- columnSelection.chromaColumns.length === 0
635
- }
636
- className="px-6 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
637
- >
638
- Save Configuration
639
- </button>
640
- )}
641
- </div>
642
- </div>
643
- </div>
644
- </div>
645
- )}
646
- </>
647
- );
648
- };
649
-
650
- export default AdminSetup;
1
+ 'use client';
2
+
3
+ import React, { useState } from 'react';
4
+ import { DatabaseType, DatabaseConnection, ColumnSelection, DatabaseConfig, AdminSetupProps } from '../types/admin';
5
+ import clsx from 'clsx';
6
+
7
+ const AdminSetup: React.FC<AdminSetupProps> = ({
8
+ onSave,
9
+ onTestConnection,
10
+ onFetchColumns,
11
+ }) => {
12
+ const [isModalOpen, setIsModalOpen] = useState(false);
13
+ const [currentStep, setCurrentStep] = useState<'connection' | 'columns'>('connection');
14
+ const [isConnecting, setIsConnecting] = useState(false);
15
+ const [connectionError, setConnectionError] = useState<string | null>(null);
16
+ const [connectionSuccess, setConnectionSuccess] = useState(false);
17
+ const [availableColumns, setAvailableColumns] = useState<string[]>([]);
18
+ const [isLoadingColumns, setIsLoadingColumns] = useState(false);
19
+
20
+ const [dbType, setDbType] = useState<DatabaseType>('mongodb');
21
+ const [connection, setConnection] = useState<DatabaseConnection>({
22
+ type: 'mongodb',
23
+ host: '',
24
+ port: 27017,
25
+ database: '',
26
+ username: '',
27
+ password: '',
28
+ connectionString: '',
29
+ ssl: false,
30
+ });
31
+
32
+ const [columnSelection, setColumnSelection] = useState<ColumnSelection>({
33
+ embeddingColumns: [],
34
+ llmColumns: [],
35
+ chromaColumns: [],
36
+ });
37
+
38
+ const handleDbTypeChange = (type: DatabaseType) => {
39
+ setDbType(type);
40
+ setConnection({
41
+ ...connection,
42
+ type,
43
+ port: type === 'mongodb' ? 27017 : 5432,
44
+ });
45
+ setConnectionError(null);
46
+ };
47
+
48
+ const handleConnectionChange = (field: keyof DatabaseConnection, value: any) => {
49
+ setConnection({
50
+ ...connection,
51
+ [field]: value,
52
+ });
53
+ setConnectionError(null);
54
+ };
55
+
56
+ const handleTestConnection = async (): Promise<boolean> => {
57
+ setIsConnecting(true);
58
+ setConnectionError(null);
59
+ setConnectionSuccess(false);
60
+
61
+ try {
62
+ let isValid: boolean;
63
+
64
+ if (onTestConnection) {
65
+ // Use provided handler
66
+ isValid = await onTestConnection(connection);
67
+ } else {
68
+ // Default: Try to call backend API
69
+ try {
70
+ const response = await fetch('/api/database/test', {
71
+ method: 'POST',
72
+ headers: {
73
+ 'Content-Type': 'application/json',
74
+ },
75
+ body: JSON.stringify(connection),
76
+ });
77
+
78
+ if (!response.ok) {
79
+ const errorData = await response.json().catch(() => ({}));
80
+ throw new Error(errorData.message || `HTTP ${response.status}: ${response.statusText}`);
81
+ }
82
+
83
+ const data = await response.json();
84
+ isValid = data.success === true || response.ok;
85
+ } catch (fetchError: any) {
86
+ // If API endpoint doesn't exist, show helpful error
87
+ if (fetchError.message?.includes('Failed to fetch') || fetchError.message?.includes('404')) {
88
+ throw new Error(
89
+ 'Backend API endpoint not found. Please implement POST /api/database/test endpoint or provide onTestConnection handler.'
90
+ );
91
+ }
92
+ throw fetchError;
93
+ }
94
+ }
95
+
96
+ if (isValid) {
97
+ setConnectionError(null);
98
+ setConnectionSuccess(true);
99
+ return true;
100
+ } else {
101
+ setConnectionError('Connection failed. Please check your credentials and try again.');
102
+ setConnectionSuccess(false);
103
+ return false;
104
+ }
105
+ } catch (error: any) {
106
+ const errorMessage = error.message || 'Connection failed. Please try again.';
107
+ setConnectionError(errorMessage);
108
+ setConnectionSuccess(false);
109
+ console.error('Database connection error:', error);
110
+ return false;
111
+ } finally {
112
+ setIsConnecting(false);
113
+ }
114
+ };
115
+
116
+ const handleFetchColumns = async (): Promise<string[]> => {
117
+ setIsLoadingColumns(true);
118
+ setConnectionError(null);
119
+
120
+ try {
121
+ let columns: string[];
122
+
123
+ if (onFetchColumns) {
124
+ // Use provided handler
125
+ columns = await onFetchColumns(connection);
126
+ } else {
127
+ // Default: Try to call backend API
128
+ try {
129
+ const response = await fetch('/api/database/columns', {
130
+ method: 'POST',
131
+ headers: {
132
+ 'Content-Type': 'application/json',
133
+ },
134
+ body: JSON.stringify(connection),
135
+ });
136
+
137
+ if (!response.ok) {
138
+ const errorData = await response.json().catch(() => ({}));
139
+ throw new Error(errorData.message || `HTTP ${response.status}: ${response.statusText}`);
140
+ }
141
+
142
+ const data = await response.json();
143
+ columns = data.columns || data.columnNames || [];
144
+ } catch (fetchError: any) {
145
+ // If API endpoint doesn't exist, show helpful error
146
+ if (fetchError.message?.includes('Failed to fetch') || fetchError.message?.includes('404')) {
147
+ throw new Error(
148
+ 'Backend API endpoint not found. Please implement POST /api/database/columns endpoint or provide onFetchColumns handler.'
149
+ );
150
+ }
151
+ throw fetchError;
152
+ }
153
+ }
154
+
155
+ if (columns && columns.length > 0) {
156
+ setAvailableColumns(columns);
157
+ setIsLoadingColumns(false);
158
+ return columns;
159
+ } else {
160
+ setConnectionError('No columns found in the database. Please check your database connection and table selection.');
161
+ setIsLoadingColumns(false);
162
+ return [];
163
+ }
164
+ } catch (error: any) {
165
+ const errorMessage = error.message || 'Failed to fetch columns. Please try again.';
166
+ setConnectionError(errorMessage);
167
+ setIsLoadingColumns(false);
168
+ console.error('Fetch columns error:', error);
169
+ return [];
170
+ }
171
+ };
172
+
173
+ const handleConnectAndNext = async () => {
174
+ // Validate connection fields
175
+ if (dbType === 'mongodb') {
176
+ if (!connection.connectionString && (!connection.host || !connection.database)) {
177
+ setConnectionError('Please provide connection string or host and database');
178
+ setConnectionSuccess(false);
179
+ return;
180
+ }
181
+ } else {
182
+ if (!connection.host || !connection.database || !connection.username || !connection.password) {
183
+ setConnectionError('Please fill in all required fields');
184
+ setConnectionSuccess(false);
185
+ return;
186
+ }
187
+ }
188
+
189
+ // Clear previous errors
190
+ setConnectionError(null);
191
+ setConnectionSuccess(false);
192
+
193
+ // Test connection first
194
+ const connectionSuccess = await handleTestConnection();
195
+
196
+ if (!connectionSuccess) {
197
+ // Connection failed, don't proceed
198
+ return;
199
+ }
200
+
201
+ // If connection successful, fetch columns
202
+ const fetchedColumns = await handleFetchColumns();
203
+
204
+ if (fetchedColumns && fetchedColumns.length > 0) {
205
+ // Successfully fetched columns, move to next step
206
+ setCurrentStep('columns');
207
+ setConnectionError(null);
208
+ } else {
209
+ // Column fetching failed, show error but keep connection success
210
+ // Error is already set in handleFetchColumns
211
+ }
212
+ };
213
+
214
+ const handleColumnToggle = (column: string, category: keyof ColumnSelection) => {
215
+ setColumnSelection((prev) => {
216
+ const currentColumns = prev[category];
217
+ const isSelected = currentColumns.includes(column);
218
+
219
+ return {
220
+ ...prev,
221
+ [category]: isSelected
222
+ ? currentColumns.filter((c) => c !== column)
223
+ : [...currentColumns, column],
224
+ };
225
+ });
226
+ };
227
+
228
+ const handleSave = () => {
229
+ const config: DatabaseConfig = {
230
+ connection,
231
+ columnSelection,
232
+ };
233
+
234
+ if (onSave) {
235
+ onSave(config);
236
+ }
237
+
238
+ // Close modal and reset
239
+ setIsModalOpen(false);
240
+ setCurrentStep('connection');
241
+ setConnectionError(null);
242
+ };
243
+
244
+ const handleClose = () => {
245
+ setIsModalOpen(false);
246
+ setCurrentStep('connection');
247
+ setConnectionError(null);
248
+ setConnectionSuccess(false);
249
+ setAvailableColumns([]);
250
+ setColumnSelection({
251
+ embeddingColumns: [],
252
+ llmColumns: [],
253
+ chromaColumns: [],
254
+ });
255
+ };
256
+
257
+ return (
258
+ <>
259
+ {/* Sidebar Button/Item - This can be integrated into admin sidebar */}
260
+ <button
261
+ onClick={() => setIsModalOpen(true)}
262
+ className="w-full flex items-center gap-3 px-4 py-2.5 text-left hover:bg-gray-100 rounded-lg transition-colors"
263
+ >
264
+ <svg
265
+ xmlns="http://www.w3.org/2000/svg"
266
+ className="h-5 w-5 text-gray-600"
267
+ fill="none"
268
+ viewBox="0 0 24 24"
269
+ stroke="currentColor"
270
+ >
271
+ <path
272
+ strokeLinecap="round"
273
+ strokeLinejoin="round"
274
+ strokeWidth={2}
275
+ d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"
276
+ />
277
+ </svg>
278
+ <span className="text-sm font-medium text-gray-700">Database Setup</span>
279
+ </button>
280
+
281
+ {/* Modal */}
282
+ {isModalOpen && (
283
+ <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50">
284
+ <div className="bg-white rounded-lg shadow-xl w-full max-w-2xl max-h-[90vh] overflow-hidden flex flex-col">
285
+ {/* Modal Header */}
286
+ <div className="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
287
+ <h2 className="text-xl font-semibold text-gray-900">
288
+ {currentStep === 'connection' ? 'Database Connection' : 'Select Columns'}
289
+ </h2>
290
+ <button
291
+ onClick={handleClose}
292
+ className="text-gray-400 hover:text-gray-600 transition-colors"
293
+ >
294
+ <svg
295
+ xmlns="http://www.w3.org/2000/svg"
296
+ className="h-6 w-6"
297
+ fill="none"
298
+ viewBox="0 0 24 24"
299
+ stroke="currentColor"
300
+ >
301
+ <path
302
+ strokeLinecap="round"
303
+ strokeLinejoin="round"
304
+ strokeWidth={2}
305
+ d="M6 18L18 6M6 6l12 12"
306
+ />
307
+ </svg>
308
+ </button>
309
+ </div>
310
+
311
+ {/* Modal Content */}
312
+ <div className="flex-1 overflow-y-auto px-6 py-4">
313
+ {currentStep === 'connection' ? (
314
+ <div className="space-y-6">
315
+ {/* Database Type Selection */}
316
+ <div>
317
+ <label className="block text-sm font-medium text-gray-700 mb-3">
318
+ Database Type
319
+ </label>
320
+ <div className="grid grid-cols-2 gap-4">
321
+ <button
322
+ onClick={() => handleDbTypeChange('mongodb')}
323
+ className={clsx(
324
+ 'p-4 border-2 rounded-lg transition-all text-left',
325
+ dbType === 'mongodb'
326
+ ? 'border-blue-500 bg-blue-50'
327
+ : 'border-gray-200 hover:border-gray-300'
328
+ )}
329
+ >
330
+ <div className="flex items-center gap-3">
331
+ <div className={clsx(
332
+ 'w-5 h-5 rounded-full border-2 flex items-center justify-center',
333
+ dbType === 'mongodb' ? 'border-blue-500' : 'border-gray-300'
334
+ )}>
335
+ {dbType === 'mongodb' && (
336
+ <div className="w-3 h-3 rounded-full bg-blue-500" />
337
+ )}
338
+ </div>
339
+ <div>
340
+ <div className="font-semibold text-gray-900">MongoDB</div>
341
+ <div className="text-xs text-gray-500">NoSQL Database</div>
342
+ </div>
343
+ </div>
344
+ </button>
345
+ <button
346
+ onClick={() => handleDbTypeChange('postgres')}
347
+ className={clsx(
348
+ 'p-4 border-2 rounded-lg transition-all text-left',
349
+ dbType === 'postgres'
350
+ ? 'border-blue-500 bg-blue-50'
351
+ : 'border-gray-200 hover:border-gray-300'
352
+ )}
353
+ >
354
+ <div className="flex items-center gap-3">
355
+ <div className={clsx(
356
+ 'w-5 h-5 rounded-full border-2 flex items-center justify-center',
357
+ dbType === 'postgres' ? 'border-blue-500' : 'border-gray-300'
358
+ )}>
359
+ {dbType === 'postgres' && (
360
+ <div className="w-3 h-3 rounded-full bg-blue-500" />
361
+ )}
362
+ </div>
363
+ <div>
364
+ <div className="font-semibold text-gray-900">PostgreSQL</div>
365
+ <div className="text-xs text-gray-500">SQL Database</div>
366
+ </div>
367
+ </div>
368
+ </button>
369
+ </div>
370
+ </div>
371
+
372
+ {/* Connection Fields */}
373
+ {dbType === 'mongodb' ? (
374
+ <div className="space-y-4">
375
+ <div>
376
+ <label className="block text-sm font-medium text-gray-700 mb-2">
377
+ Connection String (Recommended)
378
+ </label>
379
+ <input
380
+ type="text"
381
+ value={connection.connectionString || ''}
382
+ onChange={(e) => handleConnectionChange('connectionString', e.target.value)}
383
+ placeholder="mongodb://username:password@host:port/database"
384
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
385
+ />
386
+ <p className="mt-1 text-xs text-gray-500">
387
+ Or fill individual fields below
388
+ </p>
389
+ </div>
390
+ <div className="border-t border-gray-200 pt-4">
391
+ <div className="grid grid-cols-2 gap-4">
392
+ <div>
393
+ <label className="block text-sm font-medium text-gray-700 mb-2">
394
+ Host *
395
+ </label>
396
+ <input
397
+ type="text"
398
+ value={connection.host}
399
+ onChange={(e) => handleConnectionChange('host', e.target.value)}
400
+ placeholder="localhost"
401
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
402
+ />
403
+ </div>
404
+ <div>
405
+ <label className="block text-sm font-medium text-gray-700 mb-2">
406
+ Port *
407
+ </label>
408
+ <input
409
+ type="number"
410
+ value={connection.port}
411
+ onChange={(e) => handleConnectionChange('port', parseInt(e.target.value) || 27017)}
412
+ placeholder="27017"
413
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
414
+ />
415
+ </div>
416
+ </div>
417
+ <div className="mt-4">
418
+ <label className="block text-sm font-medium text-gray-700 mb-2">
419
+ Database Name *
420
+ </label>
421
+ <input
422
+ type="text"
423
+ value={connection.database}
424
+ onChange={(e) => handleConnectionChange('database', e.target.value)}
425
+ placeholder="my_database"
426
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
427
+ />
428
+ </div>
429
+ <div className="mt-4">
430
+ <label className="flex items-center gap-2">
431
+ <input
432
+ type="checkbox"
433
+ checked={connection.ssl || false}
434
+ onChange={(e) => handleConnectionChange('ssl', e.target.checked)}
435
+ className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
436
+ />
437
+ <span className="text-sm text-gray-700">Enable SSL</span>
438
+ </label>
439
+ </div>
440
+ </div>
441
+ </div>
442
+ ) : (
443
+ <div className="space-y-4">
444
+ <div className="grid grid-cols-2 gap-4">
445
+ <div>
446
+ <label className="block text-sm font-medium text-gray-700 mb-2">
447
+ Host *
448
+ </label>
449
+ <input
450
+ type="text"
451
+ value={connection.host}
452
+ onChange={(e) => handleConnectionChange('host', e.target.value)}
453
+ placeholder="localhost"
454
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
455
+ />
456
+ </div>
457
+ <div>
458
+ <label className="block text-sm font-medium text-gray-700 mb-2">
459
+ Port *
460
+ </label>
461
+ <input
462
+ type="number"
463
+ value={connection.port}
464
+ onChange={(e) => handleConnectionChange('port', parseInt(e.target.value) || 5432)}
465
+ placeholder="5432"
466
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
467
+ />
468
+ </div>
469
+ </div>
470
+ <div>
471
+ <label className="block text-sm font-medium text-gray-700 mb-2">
472
+ Database Name *
473
+ </label>
474
+ <input
475
+ type="text"
476
+ value={connection.database}
477
+ onChange={(e) => handleConnectionChange('database', e.target.value)}
478
+ placeholder="my_database"
479
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
480
+ />
481
+ </div>
482
+ <div className="grid grid-cols-2 gap-4">
483
+ <div>
484
+ <label className="block text-sm font-medium text-gray-700 mb-2">
485
+ Username *
486
+ </label>
487
+ <input
488
+ type="text"
489
+ value={connection.username || ''}
490
+ onChange={(e) => handleConnectionChange('username', e.target.value)}
491
+ placeholder="postgres"
492
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
493
+ />
494
+ </div>
495
+ <div>
496
+ <label className="block text-sm font-medium text-gray-700 mb-2">
497
+ Password *
498
+ </label>
499
+ <input
500
+ type="password"
501
+ value={connection.password || ''}
502
+ onChange={(e) => handleConnectionChange('password', e.target.value)}
503
+ placeholder="••••••••"
504
+ className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
505
+ />
506
+ </div>
507
+ </div>
508
+ <div>
509
+ <label className="flex items-center gap-2">
510
+ <input
511
+ type="checkbox"
512
+ checked={connection.ssl || false}
513
+ onChange={(e) => handleConnectionChange('ssl', e.target.checked)}
514
+ className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
515
+ />
516
+ <span className="text-sm text-gray-700">Enable SSL</span>
517
+ </label>
518
+ </div>
519
+ </div>
520
+ )}
521
+
522
+ {connectionError && (
523
+ <div className="bg-red-50 border border-red-200 rounded-lg p-4">
524
+ <p className="text-sm text-red-800">{connectionError}</p>
525
+ </div>
526
+ )}
527
+ {connectionSuccess && !connectionError && !isConnecting && (
528
+ <div className="bg-green-50 border border-green-200 rounded-lg p-4">
529
+ <p className="text-sm text-green-800">✓ Connection successful!</p>
530
+ </div>
531
+ )}
532
+ </div>
533
+ ) : (
534
+ <div className="space-y-6">
535
+ {isLoadingColumns ? (
536
+ <div className="flex flex-col items-center justify-center py-8">
537
+ <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mb-3"></div>
538
+ <p className="text-sm text-gray-600">Loading columns...</p>
539
+ </div>
540
+ ) : availableColumns.length === 0 ? (
541
+ <div className="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
542
+ <p className="text-sm text-yellow-800">No columns available. Please go back and check your connection.</p>
543
+ </div>
544
+ ) : (
545
+ <>
546
+ <div>
547
+ <p className="text-sm text-gray-600 mb-2">
548
+ Select which columns to use for <strong>Embeddings</strong>, <strong>LLM processing</strong>, and <strong>ChromaDB storage</strong>.
549
+ </p>
550
+ <p className="text-xs text-gray-500 mb-4">
551
+ Found {availableColumns.length} column{availableColumns.length !== 1 ? 's' : ''} in your database.
552
+ </p>
553
+ </div>
554
+
555
+ {/* Embedding Columns */}
556
+ <div>
557
+ <label className="block text-sm font-medium text-gray-700 mb-2">
558
+ Works with Embeddings
559
+ </label>
560
+ <p className="text-xs text-gray-500 mb-3">Select columns that will be used for embedding generation</p>
561
+ <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
562
+ {availableColumns.length === 0 ? (
563
+ <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
564
+ ) : (
565
+ availableColumns.map((column) => (
566
+ <label
567
+ key={`embedding-${column}`}
568
+ className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
569
+ >
570
+ <input
571
+ type="checkbox"
572
+ checked={columnSelection.embeddingColumns.includes(column)}
573
+ onChange={() => handleColumnToggle(column, 'embeddingColumns')}
574
+ className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
575
+ />
576
+ <span className="text-sm text-gray-700">{column}</span>
577
+ </label>
578
+ ))
579
+ )}
580
+ </div>
581
+ {columnSelection.embeddingColumns.length > 0 && (
582
+ <p className="text-xs text-green-600 mt-1">
583
+ {columnSelection.embeddingColumns.length} column{columnSelection.embeddingColumns.length !== 1 ? 's' : ''} selected
584
+ </p>
585
+ )}
586
+ </div>
587
+
588
+ {/* LLM Columns */}
589
+ <div>
590
+ <label className="block text-sm font-medium text-gray-700 mb-2">
591
+ Works with LLM
592
+ </label>
593
+ <p className="text-xs text-gray-500 mb-3">Select columns that will be processed by the LLM</p>
594
+ <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
595
+ {availableColumns.length === 0 ? (
596
+ <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
597
+ ) : (
598
+ availableColumns.map((column) => (
599
+ <label
600
+ key={`llm-${column}`}
601
+ className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
602
+ >
603
+ <input
604
+ type="checkbox"
605
+ checked={columnSelection.llmColumns.includes(column)}
606
+ onChange={() => handleColumnToggle(column, 'llmColumns')}
607
+ className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
608
+ />
609
+ <span className="text-sm text-gray-700">{column}</span>
610
+ </label>
611
+ ))
612
+ )}
613
+ </div>
614
+ {columnSelection.llmColumns.length > 0 && (
615
+ <p className="text-xs text-green-600 mt-1">
616
+ {columnSelection.llmColumns.length} column{columnSelection.llmColumns.length !== 1 ? 's' : ''} selected
617
+ </p>
618
+ )}
619
+ </div>
620
+
621
+ {/* ChromaDB Columns */}
622
+ <div>
623
+ <label className="block text-sm font-medium text-gray-700 mb-2">
624
+ Works with ChromaDB
625
+ </label>
626
+ <p className="text-xs text-gray-500 mb-3">Select columns that will be stored in ChromaDB</p>
627
+ <div className="grid grid-cols-2 gap-2 max-h-40 overflow-y-auto border border-gray-200 rounded-lg p-3 bg-gray-50">
628
+ {availableColumns.length === 0 ? (
629
+ <p className="text-sm text-gray-500 col-span-2 text-center py-2">No columns available</p>
630
+ ) : (
631
+ availableColumns.map((column) => (
632
+ <label
633
+ key={`chroma-${column}`}
634
+ className="flex items-center gap-2 cursor-pointer hover:bg-white p-2 rounded transition-colors"
635
+ >
636
+ <input
637
+ type="checkbox"
638
+ checked={columnSelection.chromaColumns.includes(column)}
639
+ onChange={() => handleColumnToggle(column, 'chromaColumns')}
640
+ className="rounded border-gray-300 text-blue-600 focus:ring-blue-500"
641
+ />
642
+ <span className="text-sm text-gray-700">{column}</span>
643
+ </label>
644
+ ))
645
+ )}
646
+ </div>
647
+ {columnSelection.chromaColumns.length > 0 && (
648
+ <p className="text-xs text-green-600 mt-1">
649
+ {columnSelection.chromaColumns.length} column{columnSelection.chromaColumns.length !== 1 ? 's' : ''} selected
650
+ </p>
651
+ )}
652
+ </div>
653
+ </>
654
+ )}
655
+ </div>
656
+ )}
657
+ </div>
658
+
659
+ {/* Modal Footer */}
660
+ <div className="px-6 py-4 border-t border-gray-200 flex items-center justify-between">
661
+ <button
662
+ onClick={currentStep === 'columns' ? () => setCurrentStep('connection') : handleClose}
663
+ className="px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 rounded-lg transition-colors"
664
+ >
665
+ {currentStep === 'columns' ? 'Back' : 'Cancel'}
666
+ </button>
667
+ <div className="flex gap-3">
668
+ {currentStep === 'connection' ? (
669
+ <button
670
+ onClick={handleConnectAndNext}
671
+ disabled={isConnecting || isLoadingColumns}
672
+ className="px-6 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors flex items-center gap-2"
673
+ >
674
+ {isConnecting && (
675
+ <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
676
+ )}
677
+ {isLoadingColumns ? 'Loading Columns...' : isConnecting ? 'Connecting...' : 'Connect & Next'}
678
+ </button>
679
+ ) : (
680
+ <button
681
+ onClick={handleSave}
682
+ disabled={
683
+ columnSelection.embeddingColumns.length === 0 &&
684
+ columnSelection.llmColumns.length === 0 &&
685
+ columnSelection.chromaColumns.length === 0
686
+ }
687
+ className="px-6 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
688
+ >
689
+ Save Configuration
690
+ </button>
691
+ )}
692
+ </div>
693
+ </div>
694
+ </div>
695
+ </div>
696
+ )}
697
+ </>
698
+ );
699
+ };
700
+
701
+ export default AdminSetup;