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

routeReveal.ts

87 lines3.9 KB

1import { useRef } from "react";
2​
3import { observeResize } from "~/lib/observeResize";
4​
5import { 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.
11export interface SidebarRouteReveal {
12 observedKey: string | null;
13 pendingKey: string | null;
15​
16export function pendingSidebarRouteReveal(
17 reveal: SidebarRouteReveal,
18 routeKey: string | null,
19): string | null {
20 if (routeKey === null) return null;
21 if (reveal.observedKey !== routeKey) {
22 reveal.observedKey = routeKey;
23 reveal.pendingKey = routeKey;
24 }
25 return reveal.pendingKey;
27​
28export function completeSidebarRouteReveal(reveal: SidebarRouteReveal): void {
29 reveal.pendingKey = null;
31​
32// Each route change and each mount scroll to the open thread once. Render records
33// the route, so a thread left before its row mounted still counts as a change.
34// The row can mount after the route, once its shells arrive, so its ref scrolls.
35export function useActiveRowReveal(reveal: SidebarRouteReveal, routeKey: string | null) {
36 const mountRevealOwed = useRef(true);
37 pendingSidebarRouteReveal(reveal, routeKey);
38 // Returns a ref cleanup while it waits on a hidden row, so an unmount releases the wait.
39 const revealRow = (row: HTMLElement | null, rowKey: string): (() => void) | undefined => {
40 if (row === null) return undefined;
41 if (reveal.pendingKey !== rowKey && !mountRevealOwed.current) return undefined;
42 // The closed narrow sheet keeps rows mounted but hidden, and opening it does not
43 // re-render them, so wait for the row to get a box before spending the reveal.
44 if (row.getClientRects().length === 0) {
45 const stopWaiting = observeResize(row, () => {
46 if (row.getClientRects().length === 0) return;
47 stopWaiting();
48 revealRow(row, rowKey);
49 });
50 return stopWaiting;
51 }
52 completeSidebarRouteReveal(reveal);
53 mountRevealOwed.current = false;
54 // A row under the pointer was just clicked, so it is in view; scrolling it would force a
55 // layout mid-commit. Keyboard activation can come from a focused row scrolled out of view.
56 if (row.matches(":hover")) return undefined;
57 row.scrollIntoView({ block: "nearest" });
58 return undefined;
59 };
60 return revealRow;
62​
63type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
64​
65// Render records the route before deciding, so the first render of a new route
66// already widens the scope. Only a route reveal widens: the once-per-mount
67// scroll never changes a scope the user chose.
68export function resolveSidebarRouteReveal<T extends RevealableThread>(input: {
69 readonly reveal: SidebarRouteReveal;
70 readonly routeKey: string | null;
71 readonly isSearching: boolean;
72 readonly scopedProjectKeys: ReadonlySet<string> | null;
73 readonly delegatedUnderParent: ReadonlySet<string>;
74 readonly findThread: (key: string) => T | undefined;
75}): { readonly pendingKey: string | null; readonly widensScope: boolean } {
76 const { reveal, routeKey, isSearching, scopedProjectKeys, delegatedUnderParent, findThread } =
77 input;
78 const pendingKey = pendingSidebarRouteReveal(reveal, routeKey);
79 // Search results replace the thread list; the reveal waits for it to return.
80 const thread = pendingKey === null || isSearching ? undefined : findThread(pendingKey);
81 const widensScope =
82 thread !== undefined &&
83 scopedProjectKeys !== null &&
84 filterSidebarV2VisibleThreads([thread], scopedProjectKeys, delegatedUnderParent).length === 0 &&
85 filterSidebarV2VisibleThreads([thread], null, delegatedUnderParent).length === 1;
86 return { pendingKey, widensScope };