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