Skip to content

Commit d541411

Browse files
committed
feat(ui): add i18n and index picker UX
Signed-off-by: Martin Vogel <[email protected]>
1 parent d9d7065 commit d541411

21 files changed

Lines changed: 717 additions & 97 deletions

‎graph-ui/src/App.tsx‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { GraphTab } from "./components/GraphTab";
33
import { StatsTab } from "./components/StatsTab";
44
import { ControlTab } from "./components/ControlTab";
55
import type { TabId } from "./lib/types";
6-
7-
const TABS: { id: TabId; label: string }[] = [
8-
{ id: "graph", label: "Graph" },
9-
{ id: "stats", label: "Projects" },
10-
{ id: "control", label: "Control" },
11-
];
6+
import { useUiMessages } from "./lib/i18n";
127

138
export function App() {
9+
const t = useUiMessages();
1410
const [activeTab, setActiveTab] = useState<TabId>("stats");
1511
const [selectedProject, setSelectedProject] = useState<string | null>(null);
12+
const tabs: { id: TabId; label: string }[] = [
13+
{ id: "graph", label: t.tabs.graph },
14+
{ id: "stats", label: t.tabs.projects },
15+
{ id: "control", label: t.tabs.control },
16+
];
1617

1718
return (
1819
<div className="h-screen flex flex-col bg-background text-foreground">
@@ -28,25 +29,27 @@ export function App() {
2829

2930
{/* Tabs inline in header */}
3031
<nav className="flex items-center gap-0.5">
31-
{TABS.map((t) => (
32+
{tabs.map((tab) => (
3233
<button
33-
key={t.id}
34-
onClick={() => setActiveTab(t.id)}
34+
key={tab.id}
35+
onClick={() => setActiveTab(tab.id)}
3536
className={`px-3 py-1 rounded-md text-[12px] font-medium transition-all ${
36-
activeTab === t.id
37+
activeTab === tab.id
3738
? "bg-primary/15 text-primary"
3839
: "text-muted-foreground hover:text-foreground hover:bg-white/[0.04]"
3940
}`}
4041
>
41-
{t.label}
42+
{tab.label}
4243
</button>
4344
))}
4445
</nav>
4546
</div>
4647

4748
{selectedProject && (
4849
<div className="flex items-center gap-2 px-3 py-1 rounded-lg bg-white/[0.04] border border-border/30">
49-
<span className="text-[10px] text-foreground/30 uppercase tracking-wider">Graph</span>
50+
<span className="text-[10px] text-foreground/30 uppercase tracking-wider">
51+
{t.graph.selectedLabel}
52+
</span>
5053
<span className="text-[11px] text-primary font-mono truncate max-w-[300px]">
5154
{selectedProject}
5255
</span>

‎graph-ui/src/components/ControlTab.tsx‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect, useCallback } from "react";
22
import { ScrollArea } from "@/components/ui/scroll-area";
33
import type { ProcessInfo } from "../lib/types";
4+
import { useUiMessages } from "../lib/i18n";
45

56
/* ── Gauge component ────────────────────────────────────── */
67

@@ -30,6 +31,7 @@ function ProcessCard({ proc, selected, onSelect, onKill }: {
3031
proc: ProcessInfo; selected: boolean;
3132
onSelect: () => void; onKill: () => void;
3233
}) {
34+
const t = useUiMessages();
3335
return (
3436
<button
3537
onClick={onSelect}
@@ -46,15 +48,15 @@ function ProcessCard({ proc, selected, onSelect, onKill }: {
4648
PID {proc.pid}
4749
</span>
4850
{proc.is_self && (
49-
<span className="text-[9px] px-1.5 py-0.5 rounded bg-primary/15 text-primary font-medium">THIS</span>
51+
<span className="text-[9px] px-1.5 py-0.5 rounded bg-primary/15 text-primary font-medium">{t.control.thisProcess}</span>
5052
)}
5153
</div>
5254
{!proc.is_self && (
5355
<button
5456
onClick={(e) => { e.stopPropagation(); onKill(); }}
5557
className="px-2 py-1 rounded-lg text-[10px] text-foreground/20 hover:text-destructive hover:bg-destructive/10 transition-all"
5658
>
57-
Kill
59+
{t.control.kill}
5860
</button>
5961
)}
6062
</div>
@@ -69,7 +71,7 @@ function ProcessCard({ proc, selected, onSelect, onKill }: {
6971
<p className="text-[13px] font-semibold tabular-nums text-foreground/70">{proc.rss_mb.toFixed(0)} MB</p>
7072
</div>
7173
<div>
72-
<p className="text-[9px] text-foreground/20 uppercase">Uptime</p>
74+
<p className="text-[9px] text-foreground/20 uppercase">{t.control.uptime}</p>
7375
<p className="text-[13px] font-semibold tabular-nums text-foreground/70">{proc.elapsed}</p>
7476
</div>
7577
</div>
@@ -82,6 +84,7 @@ function ProcessCard({ proc, selected, onSelect, onKill }: {
8284
/* ── Log viewer ─────────────────────────────────────────── */
8385

8486
function LogViewer() {
87+
const t = useUiMessages();
8588
const [lines, setLines] = useState<string[]>([]);
8689

8790
useEffect(() => {
@@ -100,13 +103,13 @@ function LogViewer() {
100103
return (
101104
<div className="rounded-xl border border-border/30 bg-black/30 overflow-hidden">
102105
<div className="px-4 py-2 border-b border-border/20">
103-
<span className="text-[11px] font-medium text-foreground/40">Process Logs</span>
106+
<span className="text-[11px] font-medium text-foreground/40">{t.control.processLogs}</span>
104107
<span className="text-[10px] text-foreground/15 ml-2">{lines.length} lines</span>
105108
</div>
106109
<ScrollArea className="h-[400px]">
107110
<div className="p-3 font-mono text-[10px] leading-relaxed">
108111
{lines.length === 0 ? (
109-
<p className="text-foreground/15 text-center py-8">No logs yet</p>
112+
<p className="text-foreground/15 text-center py-8">{t.control.noLogs}</p>
110113
) : (
111114
lines.map((line, i) => {
112115
const isErr = line.includes("level=error");
@@ -132,6 +135,7 @@ function LogViewer() {
132135
/* ── Main Control Tab ───────────────────────────────────── */
133136

134137
export function ControlTab() {
138+
const t = useUiMessages();
135139
const [processes, setProcesses] = useState<ProcessInfo[]>([]);
136140
const [selfMetrics, setSelfMetrics] = useState({ rss_mb: 0, user_cpu: 0, sys_cpu: 0 });
137141
const [selectedPid, setSelectedPid] = useState<number | null>(null);
@@ -156,7 +160,7 @@ export function ControlTab() {
156160
}, [fetchProcesses]);
157161

158162
const killProcess = useCallback(async (pid: number) => {
159-
if (!confirm(`Kill process ${pid}?`)) return;
163+
if (!confirm(t.control.killConfirm(pid))) return;
160164
try {
161165
await fetch("/api/process-kill", {
162166
method: "POST",
@@ -165,7 +169,7 @@ export function ControlTab() {
165169
});
166170
setTimeout(fetchProcesses, 1000);
167171
} catch { /* ignore */ }
168-
}, [fetchProcesses]);
172+
}, [fetchProcesses, t.control]);
169173

170174
/* Aggregates */
171175
const totalCpu = processes.reduce((s, p) => s + p.cpu, 0);
@@ -174,32 +178,32 @@ export function ControlTab() {
174178
return (
175179
<ScrollArea className="h-full">
176180
<div className="p-8 max-w-4xl mx-auto">
177-
<h2 className="text-[15px] font-semibold text-foreground/80 mb-6">Control Panel</h2>
181+
<h2 className="text-[15px] font-semibold text-foreground/80 mb-6">{t.control.panel}</h2>
178182

179183
{/* Aggregate gauges */}
180184
<div className="flex gap-4 mb-8">
181-
<Gauge label="Total CPU" value={totalCpu} max={100 * processes.length || 100} unit="%" color="text-foreground/80" />
182-
<Gauge label="Total RAM" value={totalRam} max={4096} unit="MB" color="text-foreground/80" />
183-
<Gauge label="Processes" value={processes.length} max={10} unit="" color="text-primary" />
184-
<Gauge label="Self RAM" value={selfMetrics.rss_mb} max={2048} unit="MB" color="text-primary" />
185+
<Gauge label={t.control.totalCpu} value={totalCpu} max={100 * processes.length || 100} unit="%" color="text-foreground/80" />
186+
<Gauge label={t.control.totalRam} value={totalRam} max={4096} unit="MB" color="text-foreground/80" />
187+
<Gauge label={t.control.processes} value={processes.length} max={10} unit="" color="text-primary" />
188+
<Gauge label={t.control.selfRam} value={selfMetrics.rss_mb} max={2048} unit="MB" color="text-primary" />
185189
</div>
186190

187191
{/* Process grid */}
188192
<div className="mb-8">
189193
<div className="flex items-center justify-between mb-4">
190194
<h3 className="text-[13px] font-medium text-foreground/50">
191-
Active Processes
195+
{t.control.activeProcesses}
192196
</h3>
193197
<button
194198
onClick={fetchProcesses}
195199
className="text-[11px] text-primary/60 hover:text-primary transition-colors"
196200
>
197-
Refresh
201+
{t.common.refresh}
198202
</button>
199203
</div>
200204

201205
{processes.length === 0 ? (
202-
<p className="text-foreground/20 text-[12px] text-center py-8">No processes found</p>
206+
<p className="text-foreground/20 text-[12px] text-center py-8">{t.control.noProcesses}</p>
203207
) : (
204208
<div className="grid grid-cols-2 gap-3">
205209
{processes.map((p) => (

‎graph-ui/src/components/Sidebar.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMemo, useState } from "react";
22
import { ScrollArea } from "@/components/ui/scroll-area";
33
import type { GraphNode } from "../lib/types";
4+
import { useUiMessages } from "../lib/i18n";
45

56
interface SidebarProps {
67
nodes: GraphNode[];
@@ -105,6 +106,7 @@ function TreeItem({ dir, depth, onSelect, selectedPath }: {
105106
}
106107

107108
export function Sidebar({ nodes, onSelectPath, selectedPath }: SidebarProps) {
109+
const t = useUiMessages();
108110
const [search, setSearch] = useState("");
109111
const tree = useMemo(() => flattenSingleChild(buildFileTree(nodes)), [nodes]);
110112

@@ -122,7 +124,7 @@ export function Sidebar({ nodes, onSelectPath, selectedPath }: SidebarProps) {
122124
<div className="relative">
123125
<input
124126
type="text"
125-
placeholder="Search..."
127+
placeholder={t.graph.search}
126128
value={search}
127129
onChange={(e) => setSearch(e.target.value)}
128130
className="w-full bg-white/[0.04] border border-white/[0.06] rounded-lg px-3 py-1.5 text-[12px] text-foreground placeholder-foreground/25 outline-none focus:border-primary/40 focus:bg-white/[0.06] transition-all"
@@ -134,7 +136,9 @@ export function Sidebar({ nodes, onSelectPath, selectedPath }: SidebarProps) {
134136
<div className="py-1">
135137
{filtered ? (
136138
filtered.length === 0 ? (
137-
<p className="text-foreground/20 text-[12px] px-4 py-6 text-center">No matches</p>
139+
<p className="text-foreground/20 text-[12px] px-4 py-6 text-center">
140+
{t.common.noMatches}
141+
</p>
138142
) : (
139143
filtered.map((n) => (
140144
<button
@@ -160,7 +164,7 @@ export function Sidebar({ nodes, onSelectPath, selectedPath }: SidebarProps) {
160164
onClick={() => onSelectPath("", new Set())}
161165
className="w-full px-3 py-1.5 rounded-lg bg-white/[0.04] hover:bg-white/[0.07] text-[11px] text-foreground/40 font-medium transition-all"
162166
>
163-
Clear selection
167+
{t.graph.clearSelection}
164168
</button>
165169
</div>
166170
)}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* @vitest-environment jsdom */
2+
import "@testing-library/jest-dom/vitest";
3+
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import { StatsTab } from "./StatsTab";
6+
7+
function mockProjectsFetch(extra?: (url: string, init?: RequestInit) => Response | undefined) {
8+
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
9+
const url = String(input);
10+
const overridden = extra?.(url, init);
11+
if (overridden) return overridden;
12+
if (url === "/rpc") {
13+
return new Response(JSON.stringify({
14+
result: { content: [{ text: JSON.stringify({ projects: [] }) }] },
15+
}), { status: 200, headers: { "Content-Type": "application/json" } });
16+
}
17+
if (url.startsWith("/api/ui-config")) {
18+
return new Response(JSON.stringify({ lang: "en" }), {
19+
status: 200,
20+
headers: { "Content-Type": "application/json" },
21+
});
22+
}
23+
if (url.startsWith("/api/browse")) {
24+
return new Response(JSON.stringify({
25+
path: "/home/dev",
26+
parent: "/home",
27+
dirs: ["alpha", "beta"],
28+
roots: ["/", "D:/"],
29+
}), { status: 200, headers: { "Content-Type": "application/json" } });
30+
}
31+
if (url === "/api/index") {
32+
return new Response(JSON.stringify({ status: "indexing", slot: 0 }), {
33+
status: 202,
34+
headers: { "Content-Type": "application/json" },
35+
});
36+
}
37+
return new Response("{}", { status: 200 });
38+
});
39+
vi.stubGlobal("fetch", fetchMock);
40+
return fetchMock;
41+
}
42+
43+
describe("StatsTab index modal", () => {
44+
afterEach(() => {
45+
vi.unstubAllGlobals();
46+
});
47+
48+
it("submits a custom path and project name", async () => {
49+
let submitted: unknown = null;
50+
mockProjectsFetch((url, init) => {
51+
if (url === "/api/index") {
52+
submitted = JSON.parse(String(init?.body));
53+
return new Response(JSON.stringify({ status: "indexing", slot: 0 }), {
54+
status: 202,
55+
headers: { "Content-Type": "application/json" },
56+
});
57+
}
58+
return undefined;
59+
});
60+
61+
render(<StatsTab onSelectProject={() => {}} />);
62+
fireEvent.click(await screen.findByRole("button", { name: "Index your first repository" }));
63+
64+
fireEvent.change(await screen.findByLabelText("Repository path"), {
65+
target: { value: "D:\\work\\信租风控通后端" },
66+
});
67+
fireEvent.change(screen.getByLabelText("Project name"), {
68+
target: { value: "信租风控通后端" },
69+
});
70+
fireEvent.click(screen.getByRole("button", { name: "Index This Folder" }));
71+
72+
await waitFor(() => {
73+
expect(submitted).toEqual({
74+
root_path: "D:\\work\\信租风控通后端",
75+
project_name: "信租风控通后端",
76+
});
77+
});
78+
});
79+
80+
it("filters picker rows and exposes quick row indexing", async () => {
81+
mockProjectsFetch();
82+
83+
render(<StatsTab onSelectProject={() => {}} />);
84+
fireEvent.click(await screen.findByRole("button", { name: "Index your first repository" }));
85+
86+
fireEvent.change(await screen.findByPlaceholderText("Filter folders"), {
87+
target: { value: "bet" },
88+
});
89+
90+
expect(screen.queryByText("alpha")).not.toBeInTheDocument();
91+
expect(screen.getByText("beta")).toBeInTheDocument();
92+
expect(screen.getByRole("button", { name: "Index beta" })).toBeInTheDocument();
93+
expect(screen.getByRole("button", { name: "Browse D:/" })).toBeInTheDocument();
94+
});
95+
});

0 commit comments

Comments
 (0)