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

GuideTour.tsx

149 lines5.4 KB

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