Skip to content

Commit dcf2134

Browse files
author
Dylan Huang
committed
Refactor GlobalState and Dashboard components for improved data handling and UI
- Changed dataset and expandedRows in GlobalState from arrays and sets to objects for better performance and reactivity. - Updated setDataset method to create a new dataset object, preserving expansion state more efficiently. - Refactored Dashboard to utilize totalCount for dataset management and replaced the Row component with EvaluationTable for cleaner structure. - Introduced EvaluationRow and EvaluationTable components to enhance modularity and maintainability of the evaluation display.
1 parent 907bbf9 commit dcf2134

File tree

5 files changed

+326
-239
lines changed

5 files changed

+326
-239
lines changed

vite-app/src/GlobalState.tsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,47 @@ import type { EvaluationRow } from "./types/eval-protocol";
33

44
export class GlobalState {
55
isConnected: boolean = false;
6-
dataset: EvaluationRow[] = [];
7-
expandedRows: Set<string> = new Set();
6+
dataset: Record<string, EvaluationRow> = {};
7+
expandedRows: Record<string, boolean> = {};
88

99
constructor() {
1010
makeAutoObservable(this);
1111
}
1212

1313
setDataset(dataset: EvaluationRow[]) {
14-
// Preserve expansion state for existing rows
15-
const newExpandedRows = new Set<string>();
14+
// Create new dataset object to avoid multiple re-renders
1615
dataset.forEach((row) => {
17-
if (this.expandedRows.has(row.input_metadata.row_id)) {
18-
newExpandedRows.add(row.input_metadata.row_id);
19-
}
16+
this.dataset[row.input_metadata.row_id] = row;
2017
});
21-
this.expandedRows = newExpandedRows;
22-
this.dataset = dataset;
2318
}
2419

2520
toggleRowExpansion(rowId: string) {
26-
if (this.expandedRows.has(rowId)) {
27-
this.expandedRows.delete(rowId);
21+
if (this.expandedRows[rowId]) {
22+
this.expandedRows[rowId] = false;
2823
} else {
29-
this.expandedRows.add(rowId);
24+
this.expandedRows[rowId] = true;
3025
}
3126
}
3227

3328
isRowExpanded(rowId: string): boolean {
34-
return this.expandedRows.has(rowId);
29+
return this.expandedRows[rowId];
3530
}
3631

37-
// Method to expand/collapse all rows
3832
setAllRowsExpanded(expanded: boolean) {
39-
if (expanded) {
40-
this.dataset.forEach((row) => {
41-
this.expandedRows.add(row.input_metadata.row_id);
42-
});
43-
} else {
44-
this.expandedRows.clear();
45-
}
33+
Object.keys(this.dataset).forEach((rowId) => {
34+
this.expandedRows[rowId] = expanded;
35+
});
36+
}
37+
38+
// Computed values following MobX best practices
39+
get sortedDataset() {
40+
return Object.values(this.dataset).sort(
41+
(a, b) =>
42+
new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
43+
);
44+
}
45+
46+
get totalCount() {
47+
return Object.keys(this.dataset).length;
4648
}
4749
}

vite-app/src/components/Dashboard.tsx

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { observer } from "mobx-react";
22
import { state } from "../App";
33
import Button from "./Button";
4-
import { Row } from "./Row";
4+
import { EvaluationTable } from "./EvaluationTable";
55

66
interface DashboardProps {
77
onRefresh: () => void;
@@ -49,8 +49,6 @@ const Dashboard = observer(({ onRefresh }: DashboardProps) => {
4949
const expandAll = () => state.setAllRowsExpanded(true);
5050
const collapseAll = () => state.setAllRowsExpanded(false);
5151

52-
const expandedCount = state.expandedRows.size;
53-
5452
return (
5553
<div className="text-sm">
5654
{/* Summary Stats */}
@@ -59,7 +57,7 @@ const Dashboard = observer(({ onRefresh }: DashboardProps) => {
5957
<h2 className="text-sm font-semibold text-gray-900">
6058
Dataset Summary
6159
</h2>
62-
{state.dataset.length > 0 && (
60+
{state.totalCount > 0 && (
6361
<div className="flex gap-2">
6462
<Button onClick={expandAll} size="sm" variant="secondary">
6563
Expand All
@@ -73,69 +71,16 @@ const Dashboard = observer(({ onRefresh }: DashboardProps) => {
7371
<div className="text-xs space-y-1">
7472
<div>
7573
<span className="font-semibold text-gray-700">Total Rows:</span>{" "}
76-
{state.dataset.length}
74+
{state.totalCount}
7775
</div>
78-
{state.dataset.length > 0 && (
79-
<div>
80-
<span className="font-semibold text-gray-700">Expanded:</span>{" "}
81-
{expandedCount} of {state.dataset.length}
82-
</div>
83-
)}
8476
</div>
8577
</div>
8678

8779
{/* Show empty state or main table */}
88-
{state.dataset.length === 0 ? (
80+
{state.totalCount === 0 ? (
8981
<EmptyState onRefresh={onRefresh} />
9082
) : (
91-
<div className="bg-white border border-gray-200 overflow-x-auto">
92-
<table className="w-full min-w-max">
93-
{/* Table Header */}
94-
<thead className="bg-gray-50 border-b border-gray-200">
95-
<tr>
96-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700 w-8">
97-
{/* Expand/Collapse column */}
98-
</th>
99-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
100-
Name
101-
</th>
102-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
103-
Status
104-
</th>
105-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
106-
Row ID
107-
</th>
108-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
109-
Model
110-
</th>
111-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
112-
Score
113-
</th>
114-
<th className="px-3 py-2 text-left text-xs font-semibold text-gray-700">
115-
Created
116-
</th>
117-
</tr>
118-
</thead>
119-
120-
{/* Table Body */}
121-
<tbody className="divide-y divide-gray-200">
122-
{state.dataset
123-
.slice()
124-
.sort(
125-
(a, b) =>
126-
new Date(b.created_at).getTime() -
127-
new Date(a.created_at).getTime()
128-
)
129-
.map((row, index) => (
130-
<Row
131-
key={row.input_metadata.row_id}
132-
row={row}
133-
index={index}
134-
/>
135-
))}
136-
</tbody>
137-
</table>
138-
</div>
83+
<EvaluationTable />
13984
)}
14085
</div>
14186
);

0 commit comments

Comments
 (0)