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 { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts"; |
| 3 | import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react"; |
| 4 | import { T, useGT } from "gt-react"; |
| 5 | import { useRef, useState } from "react"; |
| 6 | |
| 7 | import { Button } from "../ui/button"; |
| 8 | import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover"; |
| 9 | import { Toggle, ToggleGroup } from "../ui/toggle-group"; |
| 10 | import { PullRequestCommentForm } from "./PullRequestCommentForm"; |
| 11 | import { PullRequestReviewForm } from "./PullRequestReviewForm"; |
| 12 | import { |
| 13 | pullRequestReviewKey, |
| 14 | usePendingReviewComments, |
| 15 | usePullRequestReviewStore, |
| 16 | } from "./pullRequestReviewStore"; |
| 17 | import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea"; |
| 18 | |
| 19 | import { changeRequestTermKind } from "./changeRequestTerm"; |
| 20 | |
| 21 | export function PullRequestComposer({ |
| 22 | environmentId, |
| 23 | reference, |
| 24 | detail, |
| 25 | actionPending, |
| 26 | onCommentAction, |
| 27 | onCommented, |
| 28 | onReviewSubmitted, |
| 29 | openReviewRequest, |
| 30 | }: { |
| 31 | environmentId: EnvironmentId; |
| 32 | reference: PullRequestRef; |
| 33 | detail: PullRequestDetailView; |
| 34 | actionPending: boolean; |
| 35 | onCommentAction: ( |
| 36 | body: string, |
| 37 | action: "close" | "reopen", |
| 38 | ) => Promise<{ readonly commentPosted: boolean }>; |
| 39 | onCommented: () => void; |
| 40 | onReviewSubmitted: () => void; |
| 41 | openReviewRequest?: number | undefined; |
| 42 | }) { |
| 43 | const gt = useGT(); |
| 44 | const term = changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest); |
| 45 | const [open, setOpen] = useState(false); |
| 46 | const [reviewPending, setReviewPending] = useState(false); |
| 47 | const [requestedMode, setRequestedMode] = useState<"comment" | "review">("comment"); |
| 48 | const commentRef = useRef<HTMLTextAreaElement>(null); |
| 49 | const reviewRef = useRef<HTMLTextAreaElement>(null); |
| 50 | const pendingComments = usePendingReviewComments(reference); |
| 51 | const clearComments = usePullRequestReviewStore((store) => store.clear); |
| 52 | // Unsent summaries outlive the popover; select a boolean to avoid per-keystroke renders. |
| 53 | const reviewKey = pullRequestReviewKey(reference); |
| 54 | const summaryStarted = usePullRequestReviewStore( |
| 55 | (store) => (store.summaries[reviewKey] ?? "").trim().length > 0, |
| 56 | ); |
| 57 | const reviewStarted = pendingComments.length > 0 || summaryStarted; |
| 58 | const [answeredOpenRequest, setAnsweredOpenRequest] = useState(openReviewRequest); |
| 59 | if (openReviewRequest !== answeredOpenRequest) { |
| 60 | setAnsweredOpenRequest(openReviewRequest); |
| 61 | setRequestedMode("review"); |
| 62 | setOpen(true); |
| 63 | } |
| 64 | |
| 65 | const canComment = detail.capabilities.comment && detail.viewerPermissions.comment; |
| 66 | const verdicts = detail.capabilities.review.verdicts.filter((verdict) => |
| 67 | detail.viewerPermissions.verdicts.includes(verdict), |
| 68 | ); |
| 69 | if (!canComment && verdicts.length === 0) return null; |
| 70 | |
| 71 | const mode = canComment ? (verdicts.length === 0 ? "comment" : requestedMode) : "review"; |
| 72 | |
| 73 | return ( |
| 74 | <Popover |
| 75 | open={open} |
| 76 | onOpenChange={(next) => { |
| 77 | if (next) setRequestedMode(reviewStarted ? "review" : "comment"); |
| 78 | setOpen(next); |
| 79 | }} |
| 80 | > |
| 81 | <PopoverTrigger |
| 82 | render={<Button size="icon" variant="glass" />} |
| 83 | aria-label={ |
| 84 | pendingComments.length > 0 |
| 85 | ? gt( |
| 86 | "{count, plural, one {Review {term, select, mr {merge request} cr {change request} other {pull request}}, {count} comment pending} other {Review {term, select, mr {merge request} cr {change request} other {pull request}}, {count} comments pending}}", |
| 87 | { count: pendingComments.length, term }, |
| 88 | ) |
| 89 | : reviewStarted || !canComment |
| 90 | ? gt( |
| 91 | "Review {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 92 | { term }, |
| 93 | ) |
| 94 | : gt( |
| 95 | "Comment on {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 96 | { term }, |
| 97 | ) |
| 98 | } |
| 99 | > |
| 100 | <MessageSquareIcon className="size-4" /> |
| 101 | {pendingComments.length > 0 ? ( |
| 102 | <span |
| 103 | aria-hidden |
| 104 | className="absolute -top-1 -right-1 flex h-3.5 min-w-3.5 items-center justify-center rounded-full bg-info px-1 text-3xs font-semibold tabular-nums text-white" |
| 105 | > |
| 106 | {pendingComments.length} |
| 107 | </span> |
| 108 | ) : null} |
| 109 | </PopoverTrigger> |
| 110 | <PopoverPopup |
| 111 | keepMounted |
| 112 | side="top" |
| 113 | align="end" |
| 114 | sideOffset={8} |
| 115 | width="lg" |
| 116 | initialFocus={mode === "review" ? reviewRef : commentRef} |
| 117 | aria-label={gt( |
| 118 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} composer", |
| 119 | { term }, |
| 120 | )} |
| 121 | > |
| 122 | <div className="mb-3 flex items-center justify-between gap-2"> |
| 123 | {canComment && verdicts.length > 0 ? ( |
| 124 | <ToggleGroup |
| 125 | aria-label={gt("Composer mode")} |
| 126 | variant="segmented" |
| 127 | value={[mode]} |
| 128 | onValueChange={(next) => { |
| 129 | const value = next[0]; |
| 130 | if (value === "comment" || value === "review") setRequestedMode(value); |
| 131 | }} |
| 132 | > |
| 133 | <Toggle value="comment"> |
| 134 | <T context="noun, composer mode">Comment</T> |
| 135 | </Toggle> |
| 136 | <Toggle value="review"> |
| 137 | {pendingComments.length > 0 |
| 138 | ? gt("Review ({count})", { count: pendingComments.length }) |
| 139 | : gt("Review", { $context: "noun, composer mode" })} |
| 140 | </Toggle> |
| 141 | </ToggleGroup> |
| 142 | ) : ( |
| 143 | <PopoverTitle> |
| 144 | {mode === "review" |
| 145 | ? gt( |
| 146 | "Review {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 147 | { term }, |
| 148 | ) |
| 149 | : gt( |
| 150 | "Comment on {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 151 | { term }, |
| 152 | )} |
| 153 | </PopoverTitle> |
| 154 | )} |
| 155 | <div className="flex items-center gap-1"> |
| 156 | {mode === "review" && pendingComments.length > 0 ? ( |
| 157 | <Button |
| 158 | size="icon-xs" |
| 159 | variant="ghost" |
| 160 | aria-label={gt("Discard pending line comments")} |
| 161 | title={gt("Discard pending line comments")} |
| 162 | disabled={reviewPending} |
| 163 | onClick={() => clearComments(reviewKey)} |
| 164 | > |
| 165 | <Trash2Icon className="size-3.5" /> |
| 166 | </Button> |
| 167 | ) : null} |
| 168 | <PopoverClose |
| 169 | render={<Button size="icon-xs" variant="ghost" />} |
| 170 | aria-label={gt("Close composer")} |
| 171 | > |
| 172 | <XIcon className="size-3.5" /> |
| 173 | </PopoverClose> |
| 174 | </div> |
| 175 | </div> |
| 176 | {/* Keep forms mounted to preserve drafts and submit guards across dismissal. */} |
| 177 | {verdicts.length > 0 ? ( |
| 178 | <div hidden={mode !== "review"}> |
| 179 | <PullRequestReviewForm |
| 180 | active={open && mode === "review"} |
| 181 | environmentId={environmentId} |
| 182 | reference={reference} |
| 183 | completionScope={pullRequestCompletionScope(detail, environmentId, reference)} |
| 184 | term={term} |
| 185 | verdicts={verdicts} |
| 186 | requestChangesSummaryRequired={sourceControlClients |
| 187 | .get(detail.provider) |
| 188 | .reviewSummaryRequired("request-changes")} |
| 189 | textareaRef={reviewRef} |
| 190 | pending={reviewPending} |
| 191 | onPendingChange={setReviewPending} |
| 192 | onSubmitted={() => { |
| 193 | setOpen(false); |
| 194 | onReviewSubmitted(); |
| 195 | }} |
| 196 | /> |
| 197 | </div> |
| 198 | ) : null} |
| 199 | {canComment ? ( |
| 200 | <div hidden={mode !== "comment"}> |
| 201 | <PullRequestCommentForm |
| 202 | active={open && mode === "comment"} |
| 203 | environmentId={environmentId} |
| 204 | reference={reference} |
| 205 | detail={detail} |
| 206 | actionPending={actionPending} |
| 207 | textareaRef={commentRef} |
| 208 | onCommentAction={onCommentAction} |
| 209 | onCommented={onCommented} |
| 210 | onClose={() => setOpen(false)} |
| 211 | /> |
| 212 | </div> |
| 213 | ) : null} |
| 214 | </PopoverPopup> |
| 215 | </Popover> |
| 216 | ); |
| 217 | } |