Problem: After migrating to pure Zustand, useDaemonSession returned only SessionState from the store, but components need both state AND imperative APIs (like sendAgentMessage, createAgent, etc.). Solution: Create DaemonSession type that combines: - SessionState (from Zustand store) - Imperative APIs (from SessionContext) - Wrapped store actions (getDraftInput, setFocusedAgentId, etc.) Changes: - Added DaemonSession type to use-daemon-session.ts - useDaemonSession now merges sessionState + context + store actions - Wraps store actions to bind serverId automatically - Updated prop types in components to use DaemonSession - Fixed use-session-directory.ts to use SessionState This restores component compatibility while keeping pure Zustand architecture. Still TODO: Fix remaining components that use SessionContext directly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { useCallback, useMemo } from "react";
|
|
import { useStoreWithEqualityFn } from "zustand/traditional";
|
|
import { useSessionStore, type SessionState } from "@/stores/session-store";
|
|
|
|
export function useSessionDirectory(): Map<string, SessionState> {
|
|
const sessions = useSessionStore((state) => state.sessions);
|
|
|
|
return useMemo(() => {
|
|
return new Map<string, SessionState>(Object.entries(sessions));
|
|
}, [sessions]);
|
|
}
|
|
|
|
type SessionSelector<T> = (session: SessionState | null) => T;
|
|
type EqualityFn<T> = ((left: T, right: T) => boolean) | undefined;
|
|
|
|
export function useSessionForServer(serverId: string | null): SessionState | null;
|
|
export function useSessionForServer<T>(
|
|
serverId: string | null,
|
|
selector: SessionSelector<T>,
|
|
equalityFn?: EqualityFn<T>
|
|
): T;
|
|
export function useSessionForServer<T>(
|
|
serverId: string | null,
|
|
selector?: SessionSelector<T>,
|
|
equalityFn?: EqualityFn<T>
|
|
): SessionState | null | T {
|
|
const baseSelector = useCallback(
|
|
(state: ReturnType<typeof useSessionStore.getState>) =>
|
|
(serverId ? state.sessions[serverId] ?? null : null),
|
|
[serverId]
|
|
);
|
|
|
|
if (selector) {
|
|
const derivedSelector = useCallback(
|
|
(state: ReturnType<typeof useSessionStore.getState>) => selector(baseSelector(state)),
|
|
[baseSelector, selector]
|
|
);
|
|
return useStoreWithEqualityFn(useSessionStore, derivedSelector, equalityFn);
|
|
}
|
|
|
|
return useSessionStore(baseSelector);
|
|
}
|