1import type { PullRequestDiffParams } from "./usePullRequestDiff";
2import { pullRequestReviewKey } from "./pullRequestReviewDrafts";
3
4// The changed-files sheet hands its pick to the diff screen beneath it. Picking the file the
5// screen already shows still jumps back to it, which a route param that did not change could not
6// say. A screen that is not mounted yet opens at its `path` param instead.
7type Listener = (path: string) => void;
8const listeners = new Map<string, Set<Listener>>();
9
10export function pullRequestFileJumpScope(params: PullRequestDiffParams): string {
11 return JSON.stringify([pullRequestReviewKey(params), params.commit ?? null]);
12}
13
14export function requestPullRequestFileJump(scope: string, path: string): void {
15 for (const listener of listeners.get(scope) ?? []) listener(path);
16}
17
18export function subscribePullRequestFileJump(scope: string, listener: Listener): () => void {
19 let scoped = listeners.get(scope);
20 if (!scoped) listeners.set(scope, (scoped = new Set()));
21 scoped.add(listener);
22 return () => {
23 scoped.delete(listener);
24 if (scoped.size === 0) listeners.delete(scope);
25 };
26}