File size: 12,445 Bytes
37a6ee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
import React, { useRef, useState } from "react";
import { uploadCSV, deleteDataset, deleteSchemaDataset } from "../api";

export default function Sidebar({
  datasets,
  schemaDatasets = [],
  selectedDataset,
  onSelect,
  onUploadSuccess,
  onUploadSchemaClick,
  onDelete,
  history,
  onRunQuestion,
  addToast
}) {
  const fileInputRef = useRef(null);
  const [isUploading, setIsUploading] = useState(false);
  
  const handleUploadClick = () => {
    if (fileInputRef.current) {
      fileInputRef.current.value = ""; // Reset file input
      fileInputRef.current.click();
    }
  };

  const handleFileChange = async (e) => {
    const file = e.target.files[0];
    if (!file) return;

    if (!file.name.endsWith(".csv")) {
      addToast("Only .csv files are allowed.", "error");
      return;
    }

    setIsUploading(true);
    try {
      console.log("[Sidebar] Uploading CSV file:", file.name);
      const result = await uploadCSV(file);
      setIsUploading(false);
      if (onUploadSuccess) {
        onUploadSuccess(result);
      }
    } catch (err) {
      setIsUploading(false);
      addToast("Upload failed: " + err.message, "error");
    }
  };

  const handleDeleteClick = async (e, dbName, mode = "csv") => {
    e.stopPropagation(); // Avoid selecting the dataset card when deleting
    if (window.confirm(`Are you sure you want to delete the dataset '${dbName}'?`)) {
      try {
        console.log(`[Sidebar] Deleting ${mode} dataset:`, dbName);
        if (mode === "schema") {
          await deleteSchemaDataset(dbName);
        } else {
          await deleteDataset(dbName);
        }
        if (onDelete) {
          onDelete(dbName, mode);
        }
      } catch (err) {
        addToast("Delete failed: " + err.message, "error");
      }
    }
  };



  // Separate sample vs user datasets
  const sampleDatasets = datasets.filter((ds) => ds.is_sample);
  const userDatasets = datasets.filter((ds) => !ds.is_sample);

  return (
    <aside className="sidebar">
      <div className="sidebar-header">
        <h1 className="sidebar-logo">
          πŸ” NL Analyst<span>.</span>
        </h1>
      </div>
      
      <div className="sidebar-content">
        {/* Hidden File Input */}
        <input
          type="file"
          ref={fileInputRef}
          onChange={handleFileChange}
          accept=".csv"
          style={{ display: "none" }}
        />

        {/* β‘  "πŸ”— Relational Databases" section (purple #8b5cf6) β€” AT THE TOP */}
        <div>
          <div className="divider-label" style={{ color: "#8b5cf6", display: "flex", alignItems: "center", gap: "6px" }}>
            <span>πŸ”—</span> Relational Databases
          </div>
          
          <button
            className="btn-primary"
            onClick={onUploadSchemaClick}
            style={{ 
              width: "100%", 
              justifyContent: "center", 
              backgroundColor: "#8b5cf6", 
              marginBottom: "12px",
              fontSize: "0.8rem",
              padding: "8px 12px"
            }}
            onMouseEnter={(e) => e.target.style.backgroundColor = "#7c3aed"}
            onMouseLeave={(e) => e.target.style.backgroundColor = "#8b5cf6"}
          >
            + Upload Schema DB
          </button>
          
          {schemaDatasets && schemaDatasets.length > 0 ? (
            <div className="dataset-list">
              {schemaDatasets.map((ds) => {
                const isSelected = selectedDataset && selectedDataset.db_name === ds.db_name && selectedDataset.mode === "schema";
                return (
                  <button
                    key={ds.db_name}
                    className={`dataset-item ${isSelected ? "selected" : ""}`}
                    onClick={() => onSelect({ ...ds, mode: "schema" })}
                    style={isSelected ? { borderLeftColor: "#8b5cf6", backgroundColor: "rgba(139, 92, 246, 0.1)" } : {}}
                  >
                    <div className="dataset-info-block">
                      <div className="dataset-title-row">
                        <span className="dataset-name">{ds.display_name}</span>
                        <span 
                          className="badge" 
                          style={{ 
                            backgroundColor: "rgba(139, 92, 246, 0.15)", 
                            color: "#a78bfa", 
                            border: "1px solid rgba(139, 92, 246, 0.3)" 
                          }}
                        >
                          RELATIONAL
                        </span>
                      </div>
                      <span className="dataset-meta">
                        {ds.total_tables} tables β€’ {ds.relationships ? ds.relationships.length : 0} relationships
                      </span>
                    </div>
                    {!ds.is_sample && (
                      <button
                        className="btn-delete-dataset"
                        onClick={(e) => handleDeleteClick(e, ds.db_name, "schema")}
                        title="Delete relational dataset"
                      >
                        <svg
                          xmlns="http://www.w3.org/2000/svg"
                          width="14"
                          height="14"
                          viewBox="0 0 24 24"
                          fill="none"
                          stroke="currentColor"
                          strokeWidth="2"
                          strokeLinecap="round"
                          strokeLinejoin="round"
                        >
                          <polyline points="3 6 5 6 21 6"></polyline>
                          <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                          <line x1="10" y1="11" x2="10" y2="17"></line>
                          <line x1="14" y1="11" x2="14" y2="17"></line>
                        </svg>
                      </button>
                    )}
                  </button>
                );
              })}
            </div>
          ) : (
            <div style={{ fontSize: "0.75rem", color: "var(--text-muted)", textAlign: "center", padding: "10px", border: "1px dashed var(--border-color)", borderRadius: "6px", marginBottom: "16px" }}>
              Upload a .sql schema file to query relational data
            </div>
          )}
        </div>

        {/* β‘‘ Divider line */}
        <hr style={{ border: "none", borderTop: "1px solid var(--border-color)", margin: "16px 0" }} />

        {/* β‘’ "πŸ“Š CSV Datasets" section (blue #3b82f6) β€” IN THE MIDDLE */}
        <div>
          <div className="divider-label" style={{ color: "#3b82f6", display: "flex", alignItems: "center", gap: "6px" }}>
            <span>πŸ“Š</span> CSV Datasets
          </div>
          
          <button
            className="btn-primary"
            onClick={handleUploadClick}
            disabled={isUploading}
            style={{ 
              width: "100%", 
              justifyContent: "center",
              backgroundColor: "#3b82f6",
              marginBottom: "12px"
            }}
            onMouseEnter={(e) => e.target.style.backgroundColor = "#2563eb"}
            onMouseLeave={(e) => e.target.style.backgroundColor = "#3b82f6"}
          >
            {isUploading ? "Uploading..." : "+ Upload CSV"}
          </button>

          <div className="dataset-list">
            {sampleDatasets.map((ds) => {
              const isSelected = selectedDataset && selectedDataset.db_name === ds.db_name && selectedDataset.mode !== "schema";
              return (
                <button
                  key={ds.db_name}
                  className={`dataset-item ${isSelected ? "selected" : ""}`}
                  onClick={() => onSelect(ds)}
                >
                  <div className="dataset-info-block">
                    <div className="dataset-title-row">
                      <span className="dataset-name">{ds.display_name}</span>
                      <span className="badge badge-sample">Sample</span>
                    </div>
                    <span className="dataset-meta">
                      {ds.row_count} rows β€’ {ds.columns.length} columns
                    </span>
                  </div>
                </button>
              );
            })}

            {userDatasets.map((ds) => {
              const isSelected = selectedDataset && selectedDataset.db_name === ds.db_name && selectedDataset.mode !== "schema";
              return (
                <button
                  key={ds.db_name}
                  className={`dataset-item ${isSelected ? "selected" : ""}`}
                  onClick={() => onSelect(ds)}
                >
                  <div className="dataset-info-block">
                    <div className="dataset-title-row">
                      <span className="dataset-name">{ds.display_name}</span>
                      <span className="badge badge-user">User</span>
                    </div>
                    <span className="dataset-meta">
                      {ds.row_count} rows β€’ {ds.columns.length} columns
                    </span>
                  </div>
                  <button
                    className="btn-delete-dataset"
                    onClick={(e) => handleDeleteClick(e, ds.db_name)}
                    title="Delete dataset"
                  >
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      width="14"
                      height="14"
                      viewBox="0 0 24 24"
                      fill="none"
                      stroke="currentColor"
                      strokeWidth="2"
                      strokeLinecap="round"
                      strokeLinejoin="round"
                    >
                      <polyline points="3 6 5 6 21 6"></polyline>
                      <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                      <line x1="10" y1="11" x2="10" y2="17"></line>
                      <line x1="14" y1="11" x2="14" y2="17"></line>
                    </svg>
                  </button>
                </button>
              );
            })}
          </div>
        </div>


        {/* Recent Queries Section */}
        {selectedDataset && history && history.length > 0 && (
          <div style={{ marginTop: "16px", borderTop: "1px solid var(--border-color)", paddingTop: "16px" }}>
            <div className="divider-label">Recent Queries</div>
            <div style={{ display: "flex", flexDirection: "column", gap: "6px" }}>
              {history.slice(0, 5).map((item, idx) => (
                <button
                  key={idx}
                  className="dataset-item"
                  onClick={() => onRunQuestion && onRunQuestion(item.question)}
                  style={{
                    padding: "8px 12px",
                    border: "1px solid var(--border-color)",
                    backgroundColor: "var(--surface-elevated)",
                    borderRadius: "6px"
                  }}
                  title={item.question}
                >
                  <div style={{ display: "flex", alignItems: "center", gap: "8px", overflow: "hidden", width: "100%" }}>
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      width="12"
                      height="12"
                      viewBox="0 0 24 24"
                      fill="none"
                      stroke="var(--text-muted)"
                      strokeWidth="2.5"
                      style={{ flexShrink: 0 }}
                    >
                      <circle cx="12" cy="12" r="10"></circle>
                      <polyline points="12 6 12 12 16 14"></polyline>
                    </svg>
                    <span
                      style={{
                        fontSize: "0.8rem",
                        whiteSpace: "nowrap",
                        overflow: "hidden",
                        textOverflow: "ellipsis",
                        color: "var(--text-secondary)"
                      }}
                    >
                      {item.question}
                    </span>
                  </div>
                </button>
              ))}
            </div>
          </div>
        )}
      </div>
    </aside>
  );
}