feat(app): add zustand store for session state management

This commit is contained in:
Mohamed Boudra
2025-11-27 21:36:03 +01:00
parent bf58e81ce8
commit 08dc2ed937
7 changed files with 142 additions and 121 deletions
+55
View File
@@ -0,0 +1,55 @@
import { create } from "zustand";
import type { SessionContextValue } from "@/contexts/session-context";
// SessionData mirrors SessionContextValue so consumers can subscribe to a single source of truth.
export type SessionData = SessionContextValue;
interface SessionStore {
sessions: Record<string, SessionData>;
setSession: (serverId: string, data: SessionData) => void;
updateSession: (serverId: string, partial: Partial<SessionData>) => void;
clearSession: (serverId: string) => void;
getSession: (serverId: string) => SessionData | undefined;
}
export const useSessionStore = create<SessionStore>((set, get) => ({
sessions: {},
setSession: (serverId, data) => {
set((state) => ({
sessions: {
...state.sessions,
[serverId]: data,
},
}));
},
updateSession: (serverId, partial) => {
set((state) => {
const existing = state.sessions[serverId];
const next: SessionData | undefined = existing
? { ...existing, ...partial, serverId }
: (partial.serverId ? { ...(partial as SessionData), serverId } : undefined);
if (!next) {
return state;
}
return {
sessions: {
...state.sessions,
[serverId]: next,
},
};
});
},
clearSession: (serverId) => {
set((state) => {
if (!(serverId in state.sessions)) {
return state;
}
const nextSessions = { ...state.sessions };
delete nextSessions[serverId];
return { sessions: nextSessions };
});
},
getSession: (serverId) => get().sessions[serverId],
}));