Open History to see the last commit
1import type { EnvironmentId, PullRequestRef, PullRequestReviewGuide } from "@t3tools/contracts";
2import { useEffect, useEffectEvent, useMemo, 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 = useMemo(() => (stop === null ? [] : stopFilePaths(stop)), [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 = useMemo(
64 () =>
65 createPullRequestDiffFileContentsLoader(getDiffFileContents, {
66 environmentId,
67 reference,
68 commit: null,
69 cacheKey: `pull-request-guide:${key}`,
70 }),
71 [environmentId, getDiffFileContents, key, reference],
72 );
73
74 const scrollRef = useRef<HTMLDivElement>(null);
75 const goTo = (next: number) => {
76 update((current) => ({
77 position: next,
78 seen: next > position ? new Set(current.seen).add(position) : current.seen,
79 }));
80 setRailOpen(false);
81 scrollRef.current?.scrollTo({ top: 0 });
82 };
83 const step = (delta: 1 | -1) => goTo(stepTourPosition(position, delta, stopCount));
84
85 const stepFromKey = useEffectEvent((event: KeyboardEvent) => {
86 if (event.defaultPrevented || event.isComposing || isCommandPaletteOpen()) return;
87 if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
88 if (event.key !== "j" && event.key !== "k") return;
89 if (isEditableFocused(event.target)) return;
90 event.preventDefault();
91 step(event.key === "j" ? 1 : -1);
92 });
93 useEffect(() => {
94 if (!active) return;
95 const onKeyDown = (event: KeyboardEvent) => stepFromKey(event);
96 document.addEventListener("keydown", onKeyDown);
97 return () => document.removeEventListener("keydown", onKeyDown);
98 }, [active]);
99
100 return (
101 <div ref={scrollRef} className="@container/guide h-full overflow-y-auto scrollbar-gutter-both">
102 <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">
103 <aside className="sticky top-4 self-start @max-[56rem]/guide:hidden">
104 <GuideRail stops={guide.stops} position={position} seen={tour.seen} onSelect={goTo} />
105 </aside>
106 <main className="flex min-w-0 flex-col gap-4">
107 {banner}
108 {diff.error !== null ? (
109 <p className="text-xs text-muted-foreground">
110 The diff could not be loaded, so excerpts are missing: {diff.error}
111 </p>
112 ) : null}
113 {stop === null ? (
114 <p className="text-sm text-muted-foreground">This change has no files to tour.</p>
115 ) : (
116 <GuideStop
117 key={position}
118 stop={stop}
119 index={position}
120 stopCount={stopCount}
121 railOpen={railOpen}
122 onRailOpenChange={setRailOpen}
123 stopList={
124 <GuideRail
125 stops={guide.stops}
126 position={position}
127 seen={tour.seen}
128 onSelect={goTo}
129 />
130 }
131 nextTitle={guide.stops[position + 1]?.title ?? null}
132 files={diff.files}
133 loadDiffFiles={loadDiffFiles}
134 diffLoading={diff.loading}
135 commenting={commenting}
136 onStep={step}
137 onAsk={onAsk === null ? null : (question) => onAsk(position, question)}
138 onFinish={
139 onFinish === null
140 ? null
141 : () => {
142 update((current) => ({ seen: new Set(current.seen).add(position) }));
143 onFinish();
144 }
145 }
146 onOpenFile={onOpenFile}
147 />
148 )}
149 </main>
150 </div>
151 </div>
152 );
153}