Open History to see the last commit
1import type { EnvironmentId, PullRequestRef, PullRequestReviewGuide } from "@t3tools/contracts";
2import { T, Var } from "gt-react";
3import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react";
4
5import { createPullRequestDiffFileContentsLoader } from "~/lib/diffFileContents";
6import { isEditableFocused } from "~/lib/editableFocus";
7import { pullRequestEnvironment } from "~/state/pullRequests";
8import { useAtomCommand } from "~/state/use-atom-command";
9
10import { isCommandPaletteOpen } from "~/commandPaletteBus";
11
12import { GuideRail } from "./GuideRail";
13import { GuideStop } from "./GuideStop";
14import type { ExcerptCommenting } from "./GuideExcerpt";
15import {
16 guideProgressKey,
17 guideRunKey,
18 useGuideProgress,
19 useGuideRunStore,
20 type GuideProgress,
21} from "./guideRunStore";
22import { stepTourPosition, stopFilePaths } from "@t3tools/client-runtime/review-guide-tour";
23import { useGuideDiffFiles } from "./useGuideDiffFiles";
24
25export function GuideTour({
26 environmentId,
27 reference,
28 guide,
29 banner,
30 active,
31 commenting,
32 onAsk,
33 onFinish,
34 onOpenFile,
35}: {
36 environmentId: EnvironmentId;
37 reference: PullRequestRef;
38 guide: PullRequestReviewGuide;
39 banner: ReactNode;
40 active: boolean;
41 commenting: ExcerptCommenting | null;
42 onAsk: ((stop: number, question: string) => void) | null;
43 onFinish: (() => void) | null;
44 onOpenFile: (path: string) => void;
45}) {
46 const key = guideProgressKey(guideRunKey(environmentId, reference), guide.headSha);
47 const tour = useGuideProgress(key);
48 const update = (change: (current: GuideProgress) => Partial<GuideProgress>) =>
49 useGuideRunStore.getState().setProgress(key, (current) => ({ ...current, ...change(current) }));
50 const [railOpen, setRailOpen] = useState(false);
51
52 const stopCount = guide.stops.length;
53 const position = Math.max(Math.min(tour.position, stopCount - 1), 0);
54 const stop = guide.stops[position] ?? null;
55
56 const wantedPaths = stop === null ? [] : stopFilePaths(stop);
57 const diff = useGuideDiffFiles({
58 environmentId,
59 reference,
60 headSha: guide.headSha,
61 wantedPaths,
62 });
63 const getDiffFileContents = useAtomCommand(pullRequestEnvironment.diffFileContents);
64 const loadDiffFiles = createPullRequestDiffFileContentsLoader(getDiffFileContents, {
65 environmentId,
66 reference,
67 commit: null,
68 cacheKey: `pull-request-guide:${key}`,
69 });
70
71 const scrollRef = useRef<HTMLDivElement>(null);
72 const railRef = useRef<HTMLElement>(null);
73 const goTo = (next: number) => {
74 const focused = document.activeElement;
75 if (scrollRef.current?.contains(focused) && !railRef.current?.contains(focused)) {
76 // Keeps focus off the unmounting stop; it is not navigation, so no workspace ring.
77 scrollRef.current.focus({ preventScroll: true, focusVisible: false });
78 }
79 update((current) => ({
80 position: next,
81 seen: next > position ? new Set(current.seen).add(position) : current.seen,
82 }));
83 setRailOpen(false);
84 scrollRef.current?.scrollTo({ top: 0 });
85 };
86 const step = (delta: 1 | -1) => goTo(stepTourPosition(position, delta, stopCount));
87
88 const stepFromKey = useEffectEvent((event: KeyboardEvent) => {
89 if (event.defaultPrevented || event.isComposing || isCommandPaletteOpen()) return;
90 if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
91 const key = event.key.toLowerCase();
92 if (key !== "j" && key !== "k") return;
93 if (isEditableFocused(event.target)) return;
94 event.preventDefault();
95 step(key === "j" ? 1 : -1);
96 });
97 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- document keydown listener for j/k tour stepping while the guide is active
98 useEffect(() => {
99 if (!active) return;
100 const onKeyDown = (event: KeyboardEvent) => stepFromKey(event);
101 document.addEventListener("keydown", onKeyDown);
102 return () => document.removeEventListener("keydown", onKeyDown);
103 }, [active]);
104
105 return (
106 <div
107 ref={scrollRef}
108 tabIndex={-1}
109 data-workspace-focus-default
110 aria-keyshortcuts="J K"
111 className="@container/guide h-full overflow-y-auto scrollbar-gutter-both outline-none"
112 >
113 <div className="mx-auto grid max-w-[100rem] gap-6 px-4 py-4 @min-[56rem]/guide:grid-cols-[16rem_minmax(0,1fr)] @min-[90rem]/guide:gap-8 @min-[90rem]/guide:px-6">
114 <aside ref={railRef} className="sticky top-4 self-start @max-[56rem]/guide:hidden">
115 <GuideRail stops={guide.stops} position={position} seen={tour.seen} onSelect={goTo} />
116 </aside>
117 <main className="flex min-w-0 flex-col gap-4">
118 {banner}
119 {diff.error !== null ? (
120 <p className="text-xs text-muted-foreground">
121 <T>
122 The diff could not be loaded, so excerpts are missing: <Var>{diff.error}</Var>
123 </T>
124 </p>
125 ) : null}
126 {stop === null ? (
127 <p className="text-sm text-muted-foreground">
128 <T>This change has no files to tour.</T>
129 </p>
130 ) : (
131 <GuideStop
132 key={position}
133 stop={stop}
134 index={position}
135 stopCount={stopCount}
136 railOpen={railOpen}
137 onRailOpenChange={setRailOpen}
138 stopList={
139 <GuideRail
140 stops={guide.stops}
141 position={position}
142 seen={tour.seen}
143 onSelect={goTo}
144 />
145 }
146 nextTitle={guide.stops[position + 1]?.title ?? null}
147 files={diff.files}
148 loadDiffFiles={loadDiffFiles}
149 diffLoading={diff.loading}
150 commenting={commenting}
151 onStep={step}
152 onAsk={onAsk === null ? null : (question) => onAsk(position, question)}
153 onFinish={
154 onFinish === null
155 ? null
156 : () => {
157 update((current) => ({ seen: new Set(current.seen).add(position) }));
158 onFinish();
159 }
160 }
161 onOpenFile={onOpenFile}
162 />
163 )}
164 </main>
165 </div>
166 </div>
167 );
168}