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 // A row under the pointer was just clicked, so it is in view; scrolling it would force a
42 // layout mid-commit. Keyboard activation can come from a focused row scrolled out of view.
43 if (row.matches(":hover")) return;
44 row.scrollIntoView({ block: "nearest" });
45 };
46}
47
48type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
49
50// Render records the route before deciding, so the first render of a new route
51// already widens the scope. Only a route reveal widens: the once-per-mount
52// scroll never changes a scope the user chose.
53export function resolveSidebarRouteReveal<T extends RevealableThread>(input: {
54 readonly reveal: SidebarRouteReveal;
55 readonly routeKey: string | null;
56 readonly isSearching: boolean;
57 readonly scopedProjectKeys: ReadonlySet<string> | null;
58 readonly delegatedUnderParent: ReadonlySet<string>;
59 readonly findThread: (key: string) => T | undefined;
60}): { readonly pendingKey: string | null; readonly widensScope: boolean } {
61 const { reveal, routeKey, isSearching, scopedProjectKeys, delegatedUnderParent, findThread } =
62 input;
63 const pendingKey = pendingSidebarRouteReveal(reveal, routeKey);
64 // Search results replace the thread list; the reveal waits for it to return.
65 const thread = pendingKey === null || isSearching ? undefined : findThread(pendingKey);
66 const widensScope =
67 thread !== undefined &&
68 scopedProjectKeys !== null &&
69 filterSidebarV2VisibleThreads([thread], scopedProjectKeys, delegatedUnderParent).length === 0 &&
70 filterSidebarV2VisibleThreads([thread], null, delegatedUnderParent).length === 1;
71 return { pendingKey, widensScope };
72}