Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3import { useMemo } from "react";
4
5/**
6 * Takes the thread object but keys on its ids: the shell changes identity on every update
7 * during a run, and effects keyed on this ref must not re-run for that.
8 */
9export function useActiveThreadRef(
10 thread: { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null | undefined,
11): ScopedThreadRef | null {
12 const environmentId = thread?.environmentId ?? null;
13 const threadId = thread?.id ?? null;
14 return useMemo(
15 () =>
16 environmentId !== null && threadId !== null ? scopeThreadRef(environmentId, threadId) : null,
17 [environmentId, threadId],
18 );
19}