@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 97 lines 4.23 KB
Raw
1 import { useRef } from "react";
2
3 import { observeResize } from "~/lib/observeResize";
4
5 import { filterSidebarV2VisibleThreads } from "../Sidebar.logic";
6
7 // AppSidebarLayout owns this so drawer and Settings remounts preserve the chosen project scope.
8 // Only route changes may widen that scope; scroll position is lost on remount.
9 // Leaving Settings commits the router's location before its matches, briefly hiding thread params.
10 // Ignore null keys so the same thread does not look new during that gap.
11 export interface SidebarRouteReveal {
12 observedKey: string | null;
13 pendingKey: string | null;
14 // A draft's first send names its thread without navigating; the user is not looking for it.
15 pendingInPlace?: boolean;
16 }
17
18 export function pendingSidebarRouteReveal(
19 reveal: SidebarRouteReveal,
20 routeKey: string | null,
21 inPlace = false,
22 ): string | null {
23 if (routeKey === null) return null;
24 if (reveal.observedKey !== routeKey) {
25 reveal.observedKey = routeKey;
26 reveal.pendingKey = routeKey;
27 reveal.pendingInPlace = inPlace;
28 }
29 return reveal.pendingKey;
30 }
31
32 export function completeSidebarRouteReveal(reveal: SidebarRouteReveal): void {
33 reveal.pendingKey = null;
34 }
35
36 // Each route change and each mount scroll to the open thread once. Render records
37 // the route, so a thread left before its row mounted still counts as a change.
38 // The row can mount after the route, once its shells arrive, so its ref scrolls.
39 export function useActiveRowReveal(
40 reveal: SidebarRouteReveal,
41 routeKey: string | null,
42 inPlace = false,
43 ) {
44 const mountRevealOwed = useRef(true);
45 pendingSidebarRouteReveal(reveal, routeKey, inPlace);
46 // Returns a ref cleanup while it waits on a hidden row, so an unmount releases the wait.
47 const revealRow = (row: HTMLElement | null, rowKey: string): (() => void) | undefined => {
48 if (row === null) return undefined;
49 if (reveal.pendingKey !== rowKey && !mountRevealOwed.current) return undefined;
50 // The closed narrow sheet keeps rows mounted but hidden, and opening it does not
51 // re-render them, so wait for the row to get a box before spending the reveal.
52 if (row.getClientRects().length === 0) {
53 const stopWaiting = observeResize(row, () => {
54 if (row.getClientRects().length === 0) return;
55 stopWaiting();
56 revealRow(row, rowKey);
57 });
58 return stopWaiting;
59 }
60 const revealInPlace = reveal.pendingKey === rowKey && reveal.pendingInPlace === true;
61 completeSidebarRouteReveal(reveal);
62 mountRevealOwed.current = false;
63 // A row under the pointer was just clicked, so it is in view; scrolling it would force a
64 // layout mid-commit. Keyboard activation can come from a focused row scrolled out of view.
65 if (revealInPlace || row.matches(":hover")) return undefined;
66 row.scrollIntoView({ block: "nearest" });
67 return undefined;
68 };
69 return revealRow;
70 }
71
72 type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
73
74 // Render records the route before deciding, so the first render of a new route
75 // already widens the scope. Only a route reveal widens: the once-per-mount
76 // scroll never changes a scope the user chose.
77 export function resolveSidebarRouteReveal<T extends RevealableThread>(input: {
78 readonly reveal: SidebarRouteReveal;
79 readonly routeKey: string | null;
80 readonly routeKeyInPlace: boolean;
81 readonly isSearching: boolean;
82 readonly scopedProjectKeys: ReadonlySet<string> | null;
83 readonly delegatedUnderParent: ReadonlySet<string>;
84 readonly findThread: (key: string) => T | undefined;
85 }): { readonly pendingKey: string | null; readonly widensScope: boolean } {
86 const { reveal, routeKey, isSearching, scopedProjectKeys, delegatedUnderParent, findThread } =
87 input;
88 const pendingKey = pendingSidebarRouteReveal(reveal, routeKey, input.routeKeyInPlace);
89 // Search results replace the thread list; the reveal waits for it to return.
90 const thread = pendingKey === null || isSearching ? undefined : findThread(pendingKey);
91 const widensScope =
92 thread !== undefined &&
93 scopedProjectKeys !== null &&
94 filterSidebarV2VisibleThreads([thread], scopedProjectKeys, delegatedUnderParent).length === 0 &&
95 filterSidebarV2VisibleThreads([thread], null, delegatedUnderParent).length === 1;
96 return { pendingKey, widensScope };
97 }