The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { EnvironmentId, PullRequestRef, PullRequestReviewGuideStop } from "@t3tools/contracts"; |
| 2 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 3 | import { ScrollTextIcon, TriangleAlertIcon } from "lucide-react"; |
| 4 | import { useEffect, useEffectEvent } from "react"; |
| 5 | |
| 6 | import type { ReviewCommentContext } from "~/reviewCommentContext"; |
| 7 | |
| 8 | import { Alert, AlertAction, AlertDescription } from "../ui/alert"; |
| 9 | import { Button } from "../ui/button"; |
| 10 | import { Spinner } from "../ui/spinner"; |
| 11 | import type { ExcerptCommenting } from "./guide/GuideExcerpt"; |
| 12 | import { GuideProseContext } from "./guide/GuideProse"; |
| 13 | import { GuideTour } from "./guide/GuideTour"; |
| 14 | import { useReviewGuideWriter } from "./guide/useReviewGuideWriter"; |
| 15 | import { |
| 16 | nextPendingReviewCommentId, |
| 17 | pullRequestReviewKey, |
| 18 | usePendingReviewComments, |
| 19 | usePullRequestReviewStore, |
| 20 | } from "./pullRequestReviewStore"; |
| 21 | import type { PullRequestCompletionScope } from "./PullRequestMarkdownTextarea"; |
| 22 | |
| 23 | export function PullRequestGuideTab({ |
| 24 | environmentId, |
| 25 | reference, |
| 26 | title, |
| 27 | workspaceRoot, |
| 28 | active, |
| 29 | canComment, |
| 30 | rangeComments, |
| 31 | completionScope, |
| 32 | onAskAboutLines, |
| 33 | onAskAboutStop, |
| 34 | onFinishReview, |
| 35 | onOpenFile, |
| 36 | onTourShown, |
| 37 | }: { |
| 38 | environmentId: EnvironmentId; |
| 39 | reference: PullRequestRef; |
| 40 | title: string | null; |
| 41 | workspaceRoot: string; |
| 42 | active: boolean; |
| 43 | canComment: boolean; |
| 44 | rangeComments: boolean; |
| 45 | completionScope: PullRequestCompletionScope | null; |
| 46 | onAskAboutLines: ((comment: ReviewCommentContext, request: string) => void) | null; |
| 47 | onAskAboutStop: |
| 48 | | ((stop: PullRequestReviewGuideStop, index: number, question: string) => void) |
| 49 | | null; |
| 50 | onFinishReview: (() => void) | null; |
| 51 | onOpenFile: (path: string) => void; |
| 52 | onTourShown?: (() => void) | undefined; |
| 53 | }) { |
| 54 | const gt = useGT(); |
| 55 | const resolve = useMessages(); |
| 56 | const { query, data, running, canWrite, write } = useReviewGuideWriter({ |
| 57 | environmentId, |
| 58 | reference, |
| 59 | title, |
| 60 | }); |
| 61 | const reviewKey = pullRequestReviewKey(reference); |
| 62 | const pending = usePendingReviewComments(reference); |
| 63 | const addComment = usePullRequestReviewStore((store) => store.addComment); |
| 64 | const removeComment = usePullRequestReviewStore((store) => store.removeComment); |
| 65 | const commenting: ExcerptCommenting | null = |
| 66 | !canComment && onAskAboutLines === null |
| 67 | ? null |
| 68 | : { |
| 69 | canComment, |
| 70 | rangeComments, |
| 71 | completionScope, |
| 72 | pending, |
| 73 | onComment: ({ path, oldPath, position, startPosition, body }) => |
| 74 | addComment(reviewKey, { |
| 75 | id: nextPendingReviewCommentId(), |
| 76 | path, |
| 77 | ...(oldPath === null ? {} : { oldPath }), |
| 78 | ...(startPosition === undefined ? {} : { startPosition }), |
| 79 | position, |
| 80 | body, |
| 81 | }), |
| 82 | onRemove: (commentId) => removeComment(reviewKey, commentId), |
| 83 | onAskAgent: onAskAboutLines, |
| 84 | }; |
| 85 | const guide = data?.guide ?? null; |
| 86 | const tourShown = active && guide !== null; |
| 87 | const reportTourShown = useEffectEvent(() => onTourShown?.()); |
| 88 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the parent once the loaded guide tour is on screen, which data arrival can cause |
| 89 | useEffect(() => { |
| 90 | if (tourShown) reportTourShown(); |
| 91 | }, [tourShown]); |
| 92 | const runState = data?.run ?? null; |
| 93 | const currentHeadSha = data?.currentHeadSha ?? null; |
| 94 | const stale = guide !== null && currentHeadSha !== null && currentHeadSha !== guide.headSha; |
| 95 | const failure = |
| 96 | runState?._tag === "Failed" && runState.headSha === currentHeadSha ? runState.detail : null; |
| 97 | const writeButton = (label: string, size: "sm" | "xs") => |
| 98 | canWrite ? ( |
| 99 | <Button size={size} variant="outline" disabled={running} onClick={() => void write()}> |
| 100 | {running ? <Spinner /> : <ScrollTextIcon aria-hidden className="size-3.5" />} |
| 101 | {running ? gt("Reading the change...") : label} |
| 102 | </Button> |
| 103 | ) : null; |
| 104 | |
| 105 | if (guide === null) { |
| 106 | if (query.isPending && data === null) { |
| 107 | return ( |
| 108 | <div className="flex min-h-48 items-center justify-center"> |
| 109 | <Spinner /> |
| 110 | </div> |
| 111 | ); |
| 112 | } |
| 113 | return ( |
| 114 | <div className="scrollbar-gutter-both flex h-full flex-col items-center justify-center-safe gap-2 overflow-y-auto px-4 py-10 text-center"> |
| 115 | <p className="text-sm font-medium text-foreground"> |
| 116 | {running |
| 117 | ? gt("Writing a review guide") |
| 118 | : failure === null |
| 119 | ? gt("No review guide yet") |
| 120 | : gt("Could not write a review guide")} |
| 121 | </p> |
| 122 | <p className="max-w-md text-xs text-muted-foreground"> |
| 123 | {running |
| 124 | ? gt( |
| 125 | "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready.", |
| 126 | ) |
| 127 | : failure === null |
| 128 | ? (query.error ?? |
| 129 | gt( |
| 130 | "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.", |
| 131 | )) |
| 132 | : resolve(FAILURE_HINT)} |
| 133 | </p> |
| 134 | {writeButton(failure === null ? gt("Write guide") : gt("Try again"), "sm")} |
| 135 | {failure === null || running ? null : <FailureDetails detail={failure} />} |
| 136 | </div> |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | const banner = |
| 141 | stale || failure !== null || running ? ( |
| 142 | <Alert variant={running ? "info" : "warning"}> |
| 143 | {running ? <Spinner /> : <TriangleAlertIcon />} |
| 144 | <AlertDescription> |
| 145 | {running ? ( |
| 146 | gt("Rewriting this guide. You will be told when the new one is ready.") |
| 147 | ) : failure === null ? ( |
| 148 | gt("Written before the latest push.") |
| 149 | ) : ( |
| 150 | <> |
| 151 | {gt( |
| 152 | "Could not rewrite this guide. Check the review guide model in Settings → Source control.", |
| 153 | )} |
| 154 | <FailureDetails detail={failure} /> |
| 155 | </> |
| 156 | )} |
| 157 | </AlertDescription> |
| 158 | {!running && canWrite ? ( |
| 159 | <AlertAction>{writeButton(gt("Rewrite"), "xs")}</AlertAction> |
| 160 | ) : null} |
| 161 | </Alert> |
| 162 | ) : null; |
| 163 | |
| 164 | return ( |
| 165 | <GuideProseContext value={{ environmentId, cwd: workspaceRoot }}> |
| 166 | <GuideTour |
| 167 | environmentId={environmentId} |
| 168 | reference={reference} |
| 169 | guide={guide} |
| 170 | banner={banner} |
| 171 | active={active} |
| 172 | commenting={commenting} |
| 173 | onAsk={ |
| 174 | onAskAboutStop === null |
| 175 | ? null |
| 176 | : (index, question) => { |
| 177 | const stop = guide.stops[index]; |
| 178 | if (stop !== undefined) onAskAboutStop(stop, index, question); |
| 179 | } |
| 180 | } |
| 181 | onFinish={onFinishReview} |
| 182 | onOpenFile={onOpenFile} |
| 183 | /> |
| 184 | </GuideProseContext> |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | const FAILURE_HINT = msg("Check the review guide model in Settings → Source control & issues."); |
| 189 | |
| 190 | function FailureDetails({ detail }: { detail: string }) { |
| 191 | return ( |
| 192 | <details className="w-full max-w-lg text-xs text-muted-foreground"> |
| 193 | <summary className="cursor-pointer rounded-sm -outline-offset-2 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"> |
| 194 | <T>Technical details</T> |
| 195 | </summary> |
| 196 | <pre className="scrollbar-gutter-both mt-2 max-h-40 overflow-auto whitespace-pre-wrap break-words rounded-lg border border-border bg-muted/40 p-3 text-left font-mono text-xs"> |
| 197 | {detail} |
| 198 | </pre> |
| 199 | </details> |
| 200 | ); |
| 201 | } |