File size: 17,963 Bytes
8b41737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { useState } from "react";
import type { DatasetInfo, Preset } from "../types";

// Consistent group colors for visual distinction
const GROUP_COLORS = [
  { bg: "bg-blue-500", border: "border-blue-500", text: "text-blue-400", label: "text-blue-300" },
  { bg: "bg-emerald-500", border: "border-emerald-500", text: "text-emerald-400", label: "text-emerald-300" },
  { bg: "bg-amber-500", border: "border-amber-500", text: "text-amber-400", label: "text-amber-300" },
  { bg: "bg-purple-500", border: "border-purple-500", text: "text-purple-400", label: "text-purple-300" },
  { bg: "bg-rose-500", border: "border-rose-500", text: "text-rose-400", label: "text-rose-300" },
  { bg: "bg-cyan-500", border: "border-cyan-500", text: "text-cyan-400", label: "text-cyan-300" },
];

interface SidebarProps {
  datasets: DatasetInfo[];
  presets: Preset[];
  loading: Record<string, boolean>;
  groups: Record<string, DatasetInfo[]>;
  groupIds: string[];
  currentGroupId: string | null;
  onAddDataset: (repo: string, column?: string, split?: string, promptColumn?: string) => void;
  onRemoveDataset: (id: string) => void;
  onToggleDataset: (id: string) => void;
  onSetCurrentGroup: (groupId: string) => void;
  onLoadPreset: (preset: Preset) => void;
  onSavePreset: (name: string, repo: string, column: string, split?: string) => void;
  onDeletePreset: (id: string, datasetId?: string) => void;
  onUpdatePreset: (presetId: string, datasetId: string, updates: { name?: string }) => void;
}

export default function Sidebar({
  datasets, presets, loading,
  groups, groupIds, currentGroupId,
  onAddDataset, onRemoveDataset, onToggleDataset, onSetCurrentGroup,
  onLoadPreset, onSavePreset, onDeletePreset, onUpdatePreset,
}: SidebarProps) {
  const [showAddModal, setShowAddModal] = useState(false);
  const [repoInput, setRepoInput] = useState("");
  const [columnInput, setColumnInput] = useState("model_responses");
  const [splitInput, setSplitInput] = useState("train");
  const [promptColumnInput, setPromptColumnInput] = useState("formatted_prompt");
  const [presetSearch, setPresetSearch] = useState("");
  // Track which dataset is currently being saved as a preset (by dataset id)
  const [savingPresetForId, setSavingPresetForId] = useState<string | null>(null);
  const [presetName, setPresetName] = useState("");
  // Track which dataset is selected for preset editing
  const [editingDatasetId, setEditingDatasetId] = useState<string | null>(null);
  const [editPresetName, setEditPresetName] = useState("");

  const handleAdd = () => {
    if (!repoInput.trim()) return;
    onAddDataset(
      repoInput.trim(),
      columnInput.trim() || undefined,
      splitInput.trim() || undefined,
      promptColumnInput.trim() || undefined,
    );
    setRepoInput("");
    setShowAddModal(false);
  };

  const handleSavePresetForRepo = (ds: DatasetInfo) => {
    if (!presetName.trim()) return;
    onSavePreset(presetName.trim(), ds.repo, ds.column, ds.split);
    setPresetName("");
    setSavingPresetForId(null);
  };

  const getGroupColor = (groupId: string) => {
    const idx = groupIds.indexOf(groupId);
    return GROUP_COLORS[idx % GROUP_COLORS.length];
  };

  return (
    <div className="w-72 min-w-72 bg-gray-900 border-r border-gray-700 flex flex-col h-full">
      {/* Presets section */}
      <div className="p-3 border-b border-gray-700">
        <div className="flex items-center justify-between mb-2">
          <h3 className="text-xs font-semibold text-gray-400 uppercase tracking-wider">Presets</h3>
        </div>
        {presets.length === 0 ? (
          <p className="text-xs text-gray-500 italic">No presets saved</p>
        ) : (
          <>
            {presets.length > 6 && (
              <input
                type="text"
                value={presetSearch}
                onChange={(e) => setPresetSearch(e.target.value)}
                placeholder="Search presets..."
                className="w-full px-2 py-1 mb-2 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              />
            )}
            <div className="flex flex-wrap gap-1 max-h-32 overflow-y-auto">
              {presets
                .filter((p) => !presetSearch || p.name.toLowerCase().includes(presetSearch.toLowerCase()) || p.repo.toLowerCase().includes(presetSearch.toLowerCase()))
                .map((p) => (
                <div key={p.id} className="group relative">
                  <button
                    onClick={() => onLoadPreset(p)}
                    className="px-2 py-1 text-xs bg-gray-800 hover:bg-gray-700 rounded border border-gray-600 text-gray-300 transition-colors"
                    title={`${p.repo} (${p.column}, ${p.split ?? "train"})`}
                  >
                    {p.name}
                  </button>
                  <div className="hidden group-hover:flex absolute top-full left-0 mt-1 z-10 gap-1">
                    <button
                      onClick={() => onDeletePreset(p.id)}
                      className="px-1.5 py-0.5 text-[10px] bg-red-900 hover:bg-red-800 rounded text-red-300"
                    >
                      Delete
                    </button>
                  </div>
                </div>
              ))}
            </div>
          </>
        )}
      </div>

      {/* Datasets section — grouped by question fingerprint */}
      <div className="flex-1 overflow-y-auto p-3">
        <h3 className="text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">Loaded Repos</h3>
        {datasets.length === 0 ? (
          <p className="text-xs text-gray-500 italic">No repos loaded. Add one below.</p>
        ) : (
          <div className="space-y-3">
            {groupIds.map((gid) => {
              const color = getGroupColor(gid);
              const groupDatasets = groups[gid];
              const isCurrentGroup = gid === currentGroupId;

              return (
                <div key={gid}>
                  {/* Group header — clickable to switch group */}
                  <button
                    onClick={() => onSetCurrentGroup(gid)}
                    className={`w-full flex items-center gap-1.5 mb-1 px-1 py-0.5 rounded transition-colors ${
                      isCurrentGroup ? "bg-gray-800" : "hover:bg-gray-800/50"
                    }`}
                  >
                    <span className={`inline-block w-2 h-2 rounded-full ${color.bg} shrink-0`} />
                    <span className={`text-[10px] font-semibold uppercase tracking-wider ${
                      isCurrentGroup ? color.label : "text-gray-500"
                    }`}>
                      Group {groupIds.indexOf(gid) + 1}
                      <span className="normal-case font-normal ml-1 text-gray-600">
                        ({groupDatasets.length} repo{groupDatasets.length !== 1 ? "s" : ""})
                      </span>
                    </span>
                    {isCurrentGroup && (
                      <span className="text-[9px] text-gray-600 ml-auto">viewing</span>
                    )}
                  </button>

                  {/* Repos in this group */}
                  <div className={`space-y-1 border-l-2 ml-1 pl-2 ${
                    isCurrentGroup ? color.border : "border-gray-700"
                  }`}>
                    {groupDatasets.map((ds) => (
                      <div key={ds.id}>
                        <div
                          onClick={() => {
                            if (ds.presetId) {
                              setEditingDatasetId(editingDatasetId === ds.id ? null : ds.id);
                              setEditPresetName(ds.presetName || "");
                              setShowAddModal(false);
                            }
                          }}
                          className={`flex items-center gap-2 px-2 py-1.5 rounded text-sm transition-colors ${
                            ds.active ? "bg-gray-800" : "bg-gray-900 opacity-60"
                          } ${editingDatasetId === ds.id ? "ring-1 ring-blue-500" : ""} ${ds.presetId ? "cursor-pointer" : ""}`}
                        >
                          <input
                            type="checkbox"
                            checked={ds.active}
                            onChange={() => onToggleDataset(ds.id)}
                            onClick={(e) => e.stopPropagation()}
                            className="rounded border-gray-600 bg-gray-800 text-blue-500 focus:ring-blue-500 focus:ring-offset-0"
                          />
                          <div className="flex-1 min-w-0">
                            <div className="text-gray-200 truncate text-xs font-medium" title={ds.presetName ? `${ds.presetName}\n${ds.repo}` : ds.repo}>
                              {ds.presetName || ds.name}
                            </div>
                            <div className="text-[10px] text-gray-500">
                              {ds.column} | {ds.n_rows} rows | {ds.n_samples} samples
                            </div>
                          </div>
                          {/* Save as preset */}
                          <button
                            onClick={(e) => {
                              e.stopPropagation();
                              setSavingPresetForId(savingPresetForId === ds.id ? null : ds.id);
                              setPresetName("");
                            }}
                            className={`transition-colors shrink-0 ${
                              savingPresetForId === ds.id
                                ? "text-blue-400"
                                : "text-gray-600 hover:text-blue-400"
                            }`}
                            title="Save as preset"
                          >
                            <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 5a2 2 0 012-2h10a2 2 0 012 2v16l-7-3.5L5 21V5z" />
                            </svg>
                          </button>
                          {/* Remove */}
                          <button
                            onClick={(e) => { e.stopPropagation(); onRemoveDataset(ds.id); }}
                            className="text-gray-600 hover:text-red-400 transition-colors shrink-0"
                            title="Remove"
                          >
                            <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                            </svg>
                          </button>
                        </div>
                        {/* Inline preset name input */}
                        {savingPresetForId === ds.id && (
                          <div className="flex gap-1 mt-1 ml-6">
                            <input
                              type="text"
                              value={presetName}
                              onChange={(e) => setPresetName(e.target.value)}
                              onKeyDown={(e) => {
                                if (e.key === "Enter") handleSavePresetForRepo(ds);
                                if (e.key === "Escape") setSavingPresetForId(null);
                              }}
                              placeholder="Preset name..."
                              className="flex-1 px-2 py-1 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
                              autoFocus
                            />
                            <button
                              onClick={() => handleSavePresetForRepo(ds)}
                              className="px-2 py-1 text-xs bg-blue-600 hover:bg-blue-500 rounded text-white"
                            >
                              Save
                            </button>
                          </div>
                        )}
                      </div>
                    ))}
                  </div>
                </div>
              );
            })}
          </div>
        )}
      </div>

      {/* Preset edit panel */}
      {editingDatasetId && (() => {
        const editDs = datasets.find(d => d.id === editingDatasetId);
        if (!editDs?.presetId) return null;
        return (
          <div className="p-3 border-t border-gray-700 space-y-2">
            <div className="text-[10px] text-gray-500 uppercase font-semibold tracking-wider">Edit Preset</div>
            <input
              type="text"
              value={editPresetName}
              onChange={(e) => setEditPresetName(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter" && editPresetName.trim()) {
                  onUpdatePreset(editDs.presetId!, editDs.id, { name: editPresetName.trim() });
                  setEditingDatasetId(null);
                }
                if (e.key === "Escape") setEditingDatasetId(null);
              }}
              placeholder="Preset name..."
              className="w-full px-2 py-1 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              autoFocus
            />
            <div className="flex gap-2">
              <button
                onClick={() => {
                  if (editPresetName.trim()) {
                    onUpdatePreset(editDs.presetId!, editDs.id, { name: editPresetName.trim() });
                    setEditingDatasetId(null);
                  }
                }}
                disabled={!editPresetName.trim()}
                className="flex-1 px-2 py-1 text-xs bg-blue-600 hover:bg-blue-500 disabled:bg-gray-700 disabled:text-gray-500 rounded text-white transition-colors"
              >
                Save
              </button>
              <button
                onClick={() => {
                  onDeletePreset(editDs.presetId!, editDs.id);
                  setEditingDatasetId(null);
                }}
                className="px-2 py-1 text-xs bg-red-900 hover:bg-red-800 rounded text-red-300 transition-colors"
              >
                Delete
              </button>
              <button
                onClick={() => setEditingDatasetId(null)}
                className="px-2 py-1 text-xs bg-gray-700 hover:bg-gray-600 rounded text-gray-300 transition-colors"
              >
                Cancel
              </button>
            </div>
          </div>
        );
      })()}

      {/* Add repo section */}
      <div className="p-3 border-t border-gray-700">
        {!showAddModal ? (
          <button
            onClick={() => {
              setEditingDatasetId(null);
              setShowAddModal(true);
              setRepoInput("");
              setColumnInput("model_responses");
              setSplitInput("train");
              setPromptColumnInput("formatted_prompt");
            }}
            className="w-full px-3 py-2 text-sm bg-blue-600 hover:bg-blue-500 rounded text-white font-medium transition-colors"
          >
            + Add Repo
          </button>
        ) : (
          <div className="space-y-2">
            <input
              type="text"
              value={repoInput}
              onChange={(e) => setRepoInput(e.target.value)}
              onKeyDown={(e) => e.key === "Enter" && handleAdd()}
              placeholder="org/dataset-name"
              className="w-full px-2 py-1.5 text-sm bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              autoFocus
            />
            <div className="flex gap-2">
              <input
                type="text"
                value={columnInput}
                onChange={(e) => setColumnInput(e.target.value)}
                placeholder="Column"
                className="flex-1 px-2 py-1 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              />
              <input
                type="text"
                value={splitInput}
                onChange={(e) => setSplitInput(e.target.value)}
                placeholder="Split"
                className="w-16 px-2 py-1 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              />
            </div>
            <div className="flex gap-2">
              <input
                type="text"
                value={promptColumnInput}
                onChange={(e) => setPromptColumnInput(e.target.value)}
                placeholder="Prompt col"
                className="flex-1 px-2 py-1 text-xs bg-gray-800 border border-gray-600 rounded text-gray-200 placeholder-gray-500 focus:border-blue-500 focus:outline-none"
              />
            </div>
            <div className="flex gap-2">
              <button
                onClick={handleAdd}
                disabled={!repoInput.trim() || loading[repoInput.trim()]}
                className="flex-1 px-2 py-1.5 text-sm bg-blue-600 hover:bg-blue-500 disabled:bg-gray-700 disabled:text-gray-500 rounded text-white transition-colors"
              >
                {loading[repoInput.trim()] ? "Loading..." : "Load"}
              </button>
              <button
                onClick={() => setShowAddModal(false)}
                className="px-3 py-1.5 text-sm bg-gray-700 hover:bg-gray-600 rounded text-gray-300 transition-colors"
              >
                Cancel
              </button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
}