Files
kandev/apps/web/hooks/use-environment-session-id.ts
T
José Almeida 9d275d3009 refactor: route user terminals by environment (#758)
* refactor: route user terminals by environment

* refactor: remove terminal compatibility shims

* fix: address terminal PR review feedback

* fix: wait for remote shell readiness

* fix: require agent mode for session terminals
2026-05-01 13:27:25 +01:00

35 lines
1.3 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;
if (envId === cacheRef.current.envId) return cacheRef.current.sessionId;
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;
});
}