Files
kandev/apps/web/hooks/use-environment-session-id.ts
T
2026-08-30 13:28:47 +00:00

39 lines
1.5 KiB
TypeScript

import { useCallback, useRef } from "react";
import { useAppStore } from "@/components/state-provider";
type StoreSlice = {
tasks: { activeSessionId: string | null };
environmentIdBySessionId: Record<string, string>;
};
/**
* Return a sessionId that only changes when the underlying environment changes.
*
* Some workspace readiness APIs still need a session lookup handle. This hook
* keeps that handle stable across same-environment tab switches so downstream
* components don't needlessly reconnect or re-fetch.
*/
export function useEnvironmentSessionId(): string | null {
const cacheRef = useRef({ envId: null as string | null, sessionId: null as string | null });
const selector = useCallback((state: StoreSlice) => {
const sid = state.tasks.activeSessionId;
const envId = sid ? (state.environmentIdBySessionId[sid] ?? sid) : null;
const cachedSessionId = cacheRef.current.sessionId;
const cachedSessionIsUsable =
cachedSessionId === sid ||
(cachedSessionId !== null && state.environmentIdBySessionId[cachedSessionId] === envId);
if (envId === cacheRef.current.envId && cachedSessionIsUsable) return cachedSessionId;
cacheRef.current = { envId, sessionId: sid };
return sid;
}, []);
return useAppStore(selector);
}
/** Return the active task environment ID without falling back to session ID. */
export function useEnvironmentId(): string | null {
return useAppStore((state: StoreSlice) => {
const sid = state.tasks.activeSessionId;
return sid ? (state.environmentIdBySessionId[sid] ?? null) : null;
});
}