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:
1 parent
4e98ef8658
commit
5b55e622ea
63 files changed
+8466
No files matched your search
@@ -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() });
|
||||
},
|
||||
}));
|
||||
Reference in new issue
Block a user