Files
pi2d/web/src/stores/workspaces.ts
T
yuez 5b55e622ea 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
2026-10-21 11:33:36 +08:00

54 lines
1.6 KiB
TypeScript

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),
}));