feat(app): add zustand store for session state management
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useMemo } from "react";
|
||||
import type { Agent } from "@/contexts/session-context";
|
||||
import { useMemo } from "react";
|
||||
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
|
||||
import { useSessionStore } from "@/stores/session-store";
|
||||
|
||||
export interface AggregatedAgent extends Agent {
|
||||
serverId: string;
|
||||
@@ -13,17 +14,27 @@ export interface AggregatedAgentsResult {
|
||||
}
|
||||
|
||||
export function useAggregatedAgents(): AggregatedAgentsResult {
|
||||
const { connectionStates, sessionSnapshots } = useDaemonConnections();
|
||||
const { connectionStates } = useDaemonConnections();
|
||||
const sessions = useSessionStore((state) => state.sessions);
|
||||
const sessionAgents = useMemo(() => {
|
||||
const agentsByServer: Record<string, Map<string, Agent>> = {};
|
||||
for (const [serverId, session] of Object.entries(sessions)) {
|
||||
if (session?.agents) {
|
||||
agentsByServer[serverId] = session.agents;
|
||||
}
|
||||
}
|
||||
return agentsByServer;
|
||||
}, [sessions]);
|
||||
|
||||
return useMemo(() => {
|
||||
const allAgents: AggregatedAgent[] = [];
|
||||
|
||||
for (const [serverId, snapshot] of sessionSnapshots) {
|
||||
if (snapshot.agents.size === 0) {
|
||||
for (const [serverId, agents] of Object.entries(sessionAgents)) {
|
||||
if (!agents || agents.size === 0) {
|
||||
continue;
|
||||
}
|
||||
const serverLabel = connectionStates.get(serverId)?.daemon.label ?? serverId;
|
||||
for (const agent of snapshot.agents.values()) {
|
||||
for (const agent of agents.values()) {
|
||||
allAgents.push({
|
||||
...agent,
|
||||
serverId,
|
||||
@@ -58,7 +69,6 @@ export function useAggregatedAgents(): AggregatedAgentsResult {
|
||||
return false;
|
||||
});
|
||||
|
||||
console.log("[useAggregatedAgents] aggregated agent count:", allAgents.length, "isLoading:", isLoading);
|
||||
return { agents: allAgents, isLoading };
|
||||
}, [sessionSnapshots, connectionStates]);
|
||||
}, [sessionAgents, connectionStates]);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
import { useMemo } from "react";
|
||||
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
|
||||
import type { SessionContextValue } from "@/contexts/session-context";
|
||||
import { useSessionStore, type SessionData } from "@/stores/session-store";
|
||||
|
||||
export function useSessionDirectory(): Map<string, SessionContextValue> {
|
||||
const { sessionSnapshots } = useDaemonConnections();
|
||||
export function useSessionDirectory(): Map<string, SessionData> {
|
||||
const sessions = useSessionStore((state) => state.sessions);
|
||||
|
||||
return useMemo(() => {
|
||||
const entries = new Map<string, SessionContextValue>();
|
||||
sessionSnapshots.forEach((snapshot, serverId) => {
|
||||
entries.set(serverId, snapshot);
|
||||
});
|
||||
return entries;
|
||||
}, [sessionSnapshots]);
|
||||
return new Map<string, SessionData>(Object.entries(sessions));
|
||||
}, [sessions]);
|
||||
}
|
||||
|
||||
export function useSessionForServer(serverId: string | null): SessionContextValue | null {
|
||||
const { sessionSnapshots } = useDaemonConnections();
|
||||
return serverId ? sessionSnapshots.get(serverId) ?? null : null;
|
||||
export function useSessionForServer(serverId: string | null): SessionData | null {
|
||||
return useSessionStore((state) => (serverId ? state.sessions[serverId] ?? null : null));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user