Open History to see the last commit
1import { useRef } from "react";
2
3import { filterSidebarV2VisibleThreads } from "../Sidebar.logic";
4
5// AppSidebarLayout owns this so drawer and Settings remounts preserve the chosen project scope.
6// Only route changes may widen that scope; scroll position is lost on remount.
7// Leaving Settings commits the router's location before its matches, briefly hiding thread params.
8// Ignore null keys so the same thread does not look new during that gap.
9export interface SidebarRouteReveal {
10 observedKey: string | null;
11 pendingKey: string | null;
12}
13
14export function pendingSidebarRouteReveal(
15 reveal: SidebarRouteReveal,
16 routeKey: string | null,
17): string | null {
18 if (routeKey === null) return null;
19 if (reveal.observedKey !== routeKey) {
20 reveal.observedKey = routeKey;
21 reveal.pendingKey = routeKey;
22 }
23 return reveal.pendingKey;
24}
25
26export function completeSidebarRouteReveal(reveal: SidebarRouteReveal): void {
27 reveal.pendingKey = null;
28}
29
30// Each route change and each mount scroll to the open thread once. Render records
31// the route, so a thread left before its row mounted still counts as a change.
32// The row can mount after the route, once its shells arrive, so its ref scrolls.
33export function useActiveRowReveal(reveal: SidebarRouteReveal, routeKey: string | null) {
34 const mountRevealOwed = useRef(true);
35 pendingSidebarRouteReveal(reveal, routeKey);
36 return (row: HTMLElement | null, rowKey: string) => {
37 if (row === null) return;
38 if (reveal.pendingKey !== rowKey && !mountRevealOwed.current) return;
39 completeSidebarRouteReveal(reveal);
40 mountRevealOwed.current = false;
41 row.scrollIntoView({ block: "nearest" });
42 };
43}
44
45type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
46
47// Render records the route before deciding, so the first render of a new route
48// already widens the scope. Only a route reveal widens: the once-per-mount
49// scroll never changes a scope the user chose.
50export function resolveSidebarRouteReveal<T extends RevealableThread>(input: {
51 readonly reveal: SidebarRouteReveal;
52 readonly routeKey: string | null;
53 readonly isSearching: boolean;
54 readonly scopedProjectKeys: ReadonlySet<string> | null;
55 readonly delegatedUnderParent: ReadonlySet<string>;
56 readonly findThread: (key: string) => T | undefined;
57}): { readonly pendingKey: string | null; readonly widensScope: boolean } {
58 const { reveal, routeKey, isSearching, scopedProjectKeys, delegatedUnderParent, findThread } =
59 input;
60 const pendingKey = pendingSidebarRouteReveal(reveal, routeKey);
61 // Search results replace the thread list; the reveal waits for it to return.
62 const thread = pendingKey === null || isSearching ? undefined : findThread(pendingKey);
63 const widensScope =
64 thread !== undefined &&
65 scopedProjectKeys !== null &&
66 filterSidebarV2VisibleThreads([thread], scopedProjectKeys, delegatedUnderParent).length === 0 &&
67 filterSidebarV2VisibleThreads([thread], null, delegatedUnderParent).length === 1;
68 return { pendingKey, widensScope };
69}