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

routeReveal.ts

102 lines4.4 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;
14 // A draft's first send names its thread without navigating; the user is not looking for it.
15 pendingInPlace?: boolean;
17​
18export 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;
31​
32export function completeSidebarRouteReveal(reveal: SidebarRouteReveal): void {
33 reveal.pendingKey = null;
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.
39export 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 const revealInPlace = reveal.pendingKey === rowKey && reveal.pendingInPlace === true;
51 // A row under the pointer was just clicked, so it is in view. Both cases skip the scroll,
52 // so settle them before measuring: this runs mid-commit, where a measure forces layout.
53 if (revealInPlace || row.matches(":hover")) {
54 completeSidebarRouteReveal(reveal);
55 mountRevealOwed.current = false;
56 return undefined;
57 }
58 // The closed narrow sheet keeps rows mounted but hidden, and opening it does not
59 // re-render them, so wait for the row to get a box before spending the reveal.
60 if (row.getClientRects().length === 0) {
61 const stopWaiting = observeResize(row, () => {
62 if (row.getClientRects().length === 0) return;
63 stopWaiting();
64 revealRow(row, rowKey);
65 });
66 return stopWaiting;
67 }
68 completeSidebarRouteReveal(reveal);
69 mountRevealOwed.current = false;
70 // Keyboard activation can come from a focused row scrolled out of view.
71 row.scrollIntoView({ block: "nearest" });
72 return undefined;
73 };
74 return revealRow;
76​
77type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
78​
79// Render records the route before deciding, so the first render of a new route
80// already widens the scope. Only a route reveal widens: the once-per-mount
81// scroll never changes a scope the user chose.
82export function resolveSidebarRouteReveal<T extends RevealableThread>(input: {
83 readonly reveal: SidebarRouteReveal;
84 readonly routeKey: string | null;
85 readonly routeKeyInPlace: boolean;
86 readonly isSearching: boolean;
87 readonly scopedProjectKeys: ReadonlySet<string> | null;
88 readonly delegatedUnderParent: ReadonlySet<string>;
89 readonly findThread: (key: string) => T | undefined;
90}): { readonly pendingKey: string | null; readonly widensScope: boolean } {
91 const { reveal, routeKey, isSearching, scopedProjectKeys, delegatedUnderParent, findThread } =
92 input;
93 const pendingKey = pendingSidebarRouteReveal(reveal, routeKey, input.routeKeyInPlace);
94 // Search results replace the thread list; the reveal waits for it to return.
95 const thread = pendingKey === null || isSearching ? undefined : findThread(pendingKey);
96 const widensScope =
97 thread !== undefined &&
98 scopedProjectKeys !== null &&
99 filterSidebarV2VisibleThreads([thread], scopedProjectKeys, delegatedUnderParent).length === 0 &&
100 filterSidebarV2VisibleThreads([thread], null, delegatedUnderParent).length === 1;
101 return { pendingKey, widensScope };