The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId, PullRequestRef, PullRequestReviewGuide } from "@t3tools/contracts"; |
| 2 | import { T, Var } from "gt-react"; |
| 3 | import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react"; |
| 4 | |
| 5 | import { createPullRequestDiffFileContentsLoader } from "~/lib/diffFileContents"; |
| 6 | import { isEditableFocused } from "~/lib/editableFocus"; |
| 7 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 8 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 9 | |
| 10 | import { isCommandPaletteOpen } from "~/commandPaletteBus"; |
| 11 | |
| 12 | import { GuideRail } from "./GuideRail"; |
| 13 | import { GuideStop } from "./GuideStop"; |
| 14 | import type { ExcerptCommenting } from "./GuideExcerpt"; |
| 15 | import { |
| 16 | guideProgressKey, |
| 17 | guideRunKey, |
| 18 | useGuideProgress, |
| 19 | useGuideRunStore, |
| 20 | type GuideProgress, |
| 21 | } from "./guideRunStore"; |
| 22 | import { stepTourPosition, stopFilePaths } from "@t3tools/client-runtime/review-guide-tour"; |
| 23 | import { useGuideDiffFiles } from "./useGuideDiffFiles"; |
| 24 | |
| 25 | export 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 | scrollRef.current.focus({ preventScroll: true }); |
| 77 | } |
| 78 | update((current) => ({ |
| 79 | position: next, |
| 80 | seen: next > position ? new Set(current.seen).add(position) : current.seen, |
| 81 | })); |
| 82 | setRailOpen(false); |
| 83 | scrollRef.current?.scrollTo({ top: 0 }); |
| 84 | }; |
| 85 | const step = (delta: 1 | -1) => goTo(stepTourPosition(position, delta, stopCount)); |
| 86 | |
| 87 | const stepFromKey = useEffectEvent((event: KeyboardEvent) => { |
| 88 | if (event.defaultPrevented || event.isComposing || isCommandPaletteOpen()) return; |
| 89 | if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return; |
| 90 | const key = event.key.toLowerCase(); |
| 91 | if (key !== "j" && key !== "k") return; |
| 92 | if (isEditableFocused(event.target)) return; |
| 93 | event.preventDefault(); |
| 94 | step(key === "j" ? 1 : -1); |
| 95 | }); |
| 96 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- document keydown listener for j/k tour stepping while the guide is active |
| 97 | useEffect(() => { |
| 98 | if (!active) return; |
| 99 | const onKeyDown = (event: KeyboardEvent) => stepFromKey(event); |
| 100 | document.addEventListener("keydown", onKeyDown); |
| 101 | return () => document.removeEventListener("keydown", onKeyDown); |
| 102 | }, [active]); |
| 103 | |
| 104 | return ( |
| 105 | <div |
| 106 | ref={scrollRef} |
| 107 | tabIndex={-1} |
| 108 | aria-keyshortcuts="J K" |
| 109 | className="@container/guide h-full overflow-y-auto scrollbar-gutter-both outline-none" |
| 110 | > |
| 111 | <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"> |
| 112 | <aside ref={railRef} className="sticky top-4 self-start @max-[56rem]/guide:hidden"> |
| 113 | <GuideRail stops={guide.stops} position={position} seen={tour.seen} onSelect={goTo} /> |
| 114 | </aside> |
| 115 | <main className="flex min-w-0 flex-col gap-4"> |
| 116 | {banner} |
| 117 | {diff.error !== null ? ( |
| 118 | <p className="text-xs text-muted-foreground"> |
| 119 | <T> |
| 120 | The diff could not be loaded, so excerpts are missing: <Var>{diff.error}</Var> |
| 121 | </T> |
| 122 | </p> |
| 123 | ) : null} |
| 124 | {stop === null ? ( |
| 125 | <p className="text-sm text-muted-foreground"> |
| 126 | <T>This change has no files to tour.</T> |
| 127 | </p> |
| 128 | ) : ( |
| 129 | <GuideStop |
| 130 | key={position} |
| 131 | stop={stop} |
| 132 | index={position} |
| 133 | stopCount={stopCount} |
| 134 | railOpen={railOpen} |
| 135 | onRailOpenChange={setRailOpen} |
| 136 | stopList={ |
| 137 | <GuideRail |
| 138 | stops={guide.stops} |
| 139 | position={position} |
| 140 | seen={tour.seen} |
| 141 | onSelect={goTo} |
| 142 | /> |
| 143 | } |
| 144 | nextTitle={guide.stops[position + 1]?.title ?? null} |
| 145 | files={diff.files} |
| 146 | loadDiffFiles={loadDiffFiles} |
| 147 | diffLoading={diff.loading} |
| 148 | commenting={commenting} |
| 149 | onStep={step} |
| 150 | onAsk={onAsk === null ? null : (question) => onAsk(position, question)} |
| 151 | onFinish={ |
| 152 | onFinish === null |
| 153 | ? null |
| 154 | : () => { |
| 155 | update((current) => ({ seen: new Set(current.seen).add(position) })); |
| 156 | onFinish(); |
| 157 | } |
| 158 | } |
| 159 | onOpenFile={onOpenFile} |
| 160 | /> |
| 161 | )} |
| 162 | </main> |
| 163 | </div> |
| 164 | </div> |
| 165 | ); |
| 166 | } |