Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useSidebarRowThreadKey.ts

29 lines1.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
3import { resolveListedAncestorThreadRef } from "@t3tools/client-runtime/state/threads";
4import { Atom } from "effect/reactivity";
5​
6import { scopedThreadKey } from "../../lib/scopedEntities";
7import { environmentThreadShells } from "../../state/threads";
8​
9const NULL_ROW_KEY_ATOM = Atom.make<string | null>(null);
10​
11// Deriving the resolved key inside an atom family avoids subscribing to the full shell list when
12// no row is selected (phone portrait) and avoids re-rendering on shell frames that do not change
13// the open thread's listed ancestor row.
14const sidebarRowThreadKeyAtom = Atom.family((openThreadKey: string) =>
15 Atom.make((get): string | null => {
16 const openThreadRef = parseScopedThreadKey(openThreadKey);
17 if (openThreadRef === null) return openThreadKey;
18 const threads = get(environmentThreadShells.quietThreadShellsAtom);
19 const rowRef = resolveListedAncestorThreadRef(openThreadRef, threads);
20 return scopedThreadKey(rowRef.environmentId, rowRef.threadId);
21 }),
22);
23​
24/** An open subagent, side chat or delegated task has no row; its listed ancestor's row is selected. */
25export function useSidebarRowThreadKey(openThreadKey: string | null): string | null {
26 return useAtomValue(
27 openThreadKey === null ? NULL_ROW_KEY_ATOM : sidebarRowThreadKeyAtom(openThreadKey),
28 );