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

pullRequestFileJump.ts

26 lines1.2 KB

1// The changed-files sheet hands its pick to the diff screen beneath it. Picking the file the
2// screen already shows still jumps back to it, which a route param that did not change could not
3// say. A screen that is not mounted yet opens at its `path` param instead.
4type Listener = (path: string) => void;
5const listeners = new Map<string, Set<Listener>>();
6​
7// The sheet pops to the nearest file diff (`StackActions.popTo`), so only that screen jumps; an
8// earlier diff of the same pull request further down the stack keeps its file and selection.
9export function requestPullRequestFileJump(
10 routes: ReadonlyArray<{ readonly key: string; readonly name: string }>,
11 path: string,
12): void {
13 const target = routes.findLast((route) => route.name === "PullRequestFile");
14 if (!target) return;
15 for (const listener of listeners.get(target.key) ?? []) listener(path);
17​
18export function subscribePullRequestFileJump(routeKey: string, listener: Listener): () => void {
19 let scoped = listeners.get(routeKey);
20 if (!scoped) listeners.set(routeKey, (scoped = new Set()));
21 scoped.add(listener);
22 return () => {
23 scoped.delete(listener);
24 if (scoped.size === 0) listeners.delete(routeKey);
25 };