Next.js app with a browser-side buttplug/buttplug-wasm control layer (server never touches real-time device commands), SQLite storage via Drizzle, single-secret auth, recordings/replay with device remapping, a usage stats dashboard, Docker deployment, and Gitea CI. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W8WkFF5ppURBAB918593Eb
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { create } from "zustand";
|
|
import type { ConnectedDeviceInfo } from "./types";
|
|
|
|
export const actuatorKey = (deviceIndex: number, featureIndex: number): string =>
|
|
`${deviceIndex}:${featureIndex}`;
|
|
|
|
interface ButtplugStoreState {
|
|
connected: boolean;
|
|
scanning: boolean;
|
|
devices: Record<number, ConnectedDeviceInfo>;
|
|
/** Last-known slider value per `${deviceIndex}:${featureIndex}`, for UI display only. */
|
|
actuatorValues: Record<string, number>;
|
|
error: string | null;
|
|
setConnected: (connected: boolean) => void;
|
|
setScanning: (scanning: boolean) => void;
|
|
upsertDevice: (device: ConnectedDeviceInfo) => void;
|
|
removeDevice: (index: number) => void;
|
|
setActuatorValue: (deviceIndex: number, featureIndex: number, value: number) => void;
|
|
setError: (message: string | null) => void;
|
|
reset: () => void;
|
|
}
|
|
|
|
export const useButtplugStore = create<ButtplugStoreState>((set) => ({
|
|
connected: false,
|
|
scanning: false,
|
|
devices: {},
|
|
actuatorValues: {},
|
|
error: null,
|
|
setConnected: (connected) => set({ connected }),
|
|
setScanning: (scanning) => set({ scanning }),
|
|
upsertDevice: (device) => set((s) => ({ devices: { ...s.devices, [device.index]: device } })),
|
|
removeDevice: (index) =>
|
|
set((s) => {
|
|
const devices = { ...s.devices };
|
|
delete devices[index];
|
|
return { devices };
|
|
}),
|
|
setActuatorValue: (deviceIndex, featureIndex, value) =>
|
|
set((s) => ({
|
|
actuatorValues: { ...s.actuatorValues, [actuatorKey(deviceIndex, featureIndex)]: value },
|
|
})),
|
|
setError: (message) => set({ error: message }),
|
|
reset: () => set({ connected: false, scanning: false, devices: {}, actuatorValues: {} }),
|
|
}));
|