feat(web): React 19 + Vite + Tailwind v4 + shadcn foundation

- design tokens ported verbatim from the product design, data-theme light/dark
- app shell: topbar, sidebar/rail/tabbar responsive nav, lang + theme menus
- ui primitives (button/card/badge/dot/chip/tabs/dialog/sheet/toast/kv/seg/progress)
- api client, reconnecting ws client, zustand stores, route guard to /connect
- full zh/en dictionaries (key parity test), 32 vitest specs, dist committed
This commit is contained in:
yuez committed 2026-10-21 11:33:36 +08:00
1 parent 4e98ef8658
commit 5b55e622ea
63 files changed
+8466

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
import { create } from "zustand";
import type { InfoResponse } from "@/lib/api";
import { getDeviceToken, setDeviceToken } from "@/lib/api";
import type { WsConnectionState } from "@/lib/ws";
export const CONNECTION_KEY = "pi2-connection";
export const ENDPOINT_KEY = "pi2-endpoint";
export const DEVICE_KEY = "pi2-device";
export const CONNECTED_AT_KEY = "pi2-connected-at";
function read(key: string): string | null {
try {
return localStorage.getItem(key);
} catch {
return null;
}
}
function write(key: string, value: string): void {
try {
localStorage.setItem(key, value);
} catch {
/* private mode */
}
}
function drop(key: string): void {
try {
localStorage.removeItem(key);
} catch {
/* private mode */
}
}
export type PairingState = "paired" | "unpaired";
export interface ConnectionStore {
/** Mirror of the prototype's `pi2-connection` marker. */
paired: boolean;
endpoint: string;
deviceName: string;
connectedAt: number;
token: string | null;
server: InfoResponse | null;
wsState: WsConnectionState;
isPaired: () => boolean;
refreshFromStorage: () => void;
connect: (options?: { endpoint?: string; deviceName?: string; token?: string | null }) => void;
disconnect: () => void;
setServer: (server: InfoResponse | null) => void;
setWsState: (state: WsConnectionState) => void;
setToken: (token: string | null) => void;
}
function initialState() {
return {
paired: read(CONNECTION_KEY) === "paired",
endpoint: read(ENDPOINT_KEY) ?? "",
deviceName: read(DEVICE_KEY) ?? "",
connectedAt: Number(read(CONNECTED_AT_KEY)) || 0,
token: getDeviceToken(),
server: null as InfoResponse | null,
wsState: "idle" as WsConnectionState,
};
}
export const useConnectionStore = create<ConnectionStore>((set, get) => ({
...initialState(),
isPaired: () => get().paired,
refreshFromStorage: () => set(initialState()),
connect: ({ endpoint, deviceName, token } = {}) => {
const now = Date.now();
if (endpoint) write(ENDPOINT_KEY, endpoint);
if (deviceName) write(DEVICE_KEY, deviceName);
if (token !== undefined) setDeviceToken(token);
write(CONNECTED_AT_KEY, String(now));
write(CONNECTION_KEY, "paired");
set({
paired: true,
endpoint: endpoint ?? get().endpoint,
deviceName: deviceName ?? get().deviceName,
connectedAt: now,
token: getDeviceToken(),
});
},
disconnect: () => {
drop(ENDPOINT_KEY);
drop(DEVICE_KEY);
drop(CONNECTED_AT_KEY);
setDeviceToken(null);
write(CONNECTION_KEY, "unpaired");
set({
paired: false,
endpoint: "",
deviceName: "",
connectedAt: 0,
token: null,
server: null,
wsState: "idle",
});
},
setServer: (server) => set({ server }),
setWsState: (wsState) => set({ wsState }),
setToken: (token) => {
setDeviceToken(token);
set({ token: getDeviceToken() });
},
}));
+71
View File
@@ -0,0 +1,71 @@
import { create } from "zustand";
import type { SessionState } from "@/lib/protocol";
export interface SessionsStore {
/** Sessions grouped by workspace — the chat tree is per workspace. */
byWorkspace: Record<string, SessionState[]>;
selectedSessionId: string | null;
status: "idle" | "loading" | "error";
error: string | null;
setSessions: (workspaceId: string, sessions: SessionState[]) => void;
upsertSession: (session: SessionState) => void;
removeSession: (sessionId: string) => void;
selectSession: (sessionId: string | null) => void;
listFor: (workspaceId: string) => SessionState[];
getSession: (sessionId: string) => SessionState | undefined;
setStatus: (status: "idle" | "loading" | "error", error?: string | null) => void;
reset: () => void;
}
const initial = {
byWorkspace: {} as Record<string, SessionState[]>,
selectedSessionId: null as string | null,
status: "idle" as const,
error: null as string | null,
};
export const useSessionsStore = create<SessionsStore>((set, get) => ({
...initial,
setSessions: (workspaceId, sessions) =>
set((state) => ({
byWorkspace: { ...state.byWorkspace, [workspaceId]: sessions },
status: "idle",
error: null,
})),
upsertSession: (session) =>
set((state) => {
const list = state.byWorkspace[session.workspaceId] ?? [];
const index = list.findIndex((s) => s.sessionId === session.sessionId);
const next = index === -1 ? [...list, session] : list.map((s, i) => (i === index ? session : s));
return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: next } };
}),
removeSession: (sessionId) =>
set((state) => {
const byWorkspace: Record<string, SessionState[]> = {};
for (const [workspaceId, list] of Object.entries(state.byWorkspace)) {
byWorkspace[workspaceId] = list.filter((s) => s.sessionId !== sessionId);
}
return { byWorkspace, selectedSessionId: state.selectedSessionId === sessionId ? null : state.selectedSessionId };
}),
selectSession: (selectedSessionId) => set({ selectedSessionId }),
listFor: (workspaceId) => get().byWorkspace[workspaceId] ?? [],
getSession: (sessionId) => {
for (const list of Object.values(get().byWorkspace)) {
const found = list.find((s) => s.sessionId === sessionId);
if (found) return found;
}
return undefined;
},
setStatus: (status, error = null) => set({ status, error }),
reset: () => set(initial),
}));
+53
View File
@@ -0,0 +1,53 @@
import { create } from "zustand";
import type { WorkspaceInfo } from "@/lib/protocol";
export type LoadStatus = "idle" | "loading" | "error" | "ready";
export interface WorkspacesStore {
items: WorkspaceInfo[];
status: LoadStatus;
error: string | null;
selectedId: string | null;
setWorkspaces: (items: WorkspaceInfo[]) => void;
upsert: (item: WorkspaceInfo) => void;
remove: (workspaceId: string) => void;
select: (workspaceId: string | null) => void;
setStatus: (status: LoadStatus, error?: string | null) => void;
reset: () => void;
}
const initial = {
items: [] as WorkspaceInfo[],
status: "idle" as LoadStatus,
error: null as string | null,
selectedId: null as string | null,
};
export const useWorkspacesStore = create<WorkspacesStore>((set) => ({
...initial,
setWorkspaces: (items) => set({ items, status: "ready", error: null }),
upsert: (item) =>
set((state) => {
const index = state.items.findIndex((w) => w.workspaceId === item.workspaceId);
if (index === -1) return { items: [...state.items, item] };
const items = state.items.slice();
items[index] = item;
return { items };
}),
remove: (workspaceId) =>
set((state) => ({
items: state.items.filter((w) => w.workspaceId !== workspaceId),
selectedId: state.selectedId === workspaceId ? null : state.selectedId,
})),
select: (selectedId) => set({ selectedId }),
setStatus: (status, error = null) => set({ status, error }),
reset: () => set(initial),
}));