The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 2 | import type { EnvironmentId, PullRequestDetail, PullRequestRef } from "@t3tools/contracts"; |
| 3 | import { msg, T, useGT } from "gt-react"; |
| 4 | import { useState } from "react"; |
| 5 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 6 | |
| 7 | import { m } from "~/i18n/messages"; |
| 8 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 9 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 10 | import { Button } from "../ui/button"; |
| 11 | import { Input } from "../ui/input"; |
| 12 | import { toastManager } from "../ui/toast"; |
| 13 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 14 | import { PullRequestDetailTitleRow } from "./PullRequestDetailLayout"; |
| 15 | import { PullRequestEditButton } from "./PullRequestEditButton"; |
| 16 | import { readableFailure } from "./pullRequestDetail.logic"; |
| 17 | import { canEditPullRequestChangeRequest } from "@t3tools/client-runtime/pull-request-editing"; |
| 18 | |
| 19 | import { changeRequestTermKind } from "./changeRequestTerm"; |
| 20 | |
| 21 | /** The pull request title, which the reader can rename in place when the host allows it. */ |
| 22 | export function PullRequestTitleEditor({ |
| 23 | environmentId, |
| 24 | reference, |
| 25 | pullRequestKey, |
| 26 | detail, |
| 27 | canWriteSourceControl, |
| 28 | }: { |
| 29 | environmentId: EnvironmentId; |
| 30 | reference: PullRequestRef; |
| 31 | pullRequestKey: string; |
| 32 | detail: PullRequestDetail; |
| 33 | canWriteSourceControl: boolean; |
| 34 | }) { |
| 35 | const update = useAtomCommand(pullRequestEnvironment.update, { reportFailure: false }); |
| 36 | const [titleScope, setTitleScope] = useState<{ |
| 37 | readonly pullRequestKey: string; |
| 38 | readonly text: string; |
| 39 | } | null>(null); |
| 40 | const titleDraft = titleScope?.pullRequestKey === pullRequestKey ? titleScope.text : null; |
| 41 | const gt = useGT(); |
| 42 | const [titleSaving, setTitleSaving] = useState(false); |
| 43 | |
| 44 | const saveTitle = async (next: string) => { |
| 45 | const title = next.trim(); |
| 46 | if (!canWriteSourceControl || titleSaving) return; |
| 47 | if (title.length === 0 || title === detail.title) { |
| 48 | setTitleScope(null); |
| 49 | return; |
| 50 | } |
| 51 | setTitleSaving(true); |
| 52 | // The new title shows while the host takes it, so the editor closes at once. |
| 53 | setTitleScope(null); |
| 54 | const result = await update({ environmentId, input: { ...reference, title } }); |
| 55 | setTitleSaving(false); |
| 56 | if (result._tag === "Failure") { |
| 57 | setTitleScope({ pullRequestKey, text: title }); |
| 58 | toastManager.add({ |
| 59 | type: "error", |
| 60 | title: m(msg("The title could not be saved")), |
| 61 | description: readableFailure( |
| 62 | squashAtomCommandFailure(result), |
| 63 | m(msg("The host refused the new title.")), |
| 64 | ), |
| 65 | }); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | if (titleDraft === null) { |
| 70 | return ( |
| 71 | <PullRequestDetailTitleRow className="group gap-1"> |
| 72 | <Tooltip> |
| 73 | <TooltipTrigger |
| 74 | render={ |
| 75 | <h1 className="min-w-0 flex-1 truncate text-base font-semibold leading-snug"> |
| 76 | {detail.title} |
| 77 | </h1> |
| 78 | } |
| 79 | /> |
| 80 | <TooltipPopup side="top">{detail.title}</TooltipPopup> |
| 81 | </Tooltip> |
| 82 | {canEditPullRequestChangeRequest(detail) ? ( |
| 83 | <PullRequestEditButton |
| 84 | aria-label={gt("Edit title")} |
| 85 | onClick={() => setTitleScope({ pullRequestKey, text: detail.title })} |
| 86 | /> |
| 87 | ) : null} |
| 88 | </PullRequestDetailTitleRow> |
| 89 | ); |
| 90 | } |
| 91 | return ( |
| 92 | <div className="space-y-2"> |
| 93 | <Input |
| 94 | autoFocus |
| 95 | size="sm" |
| 96 | disabled={titleSaving} |
| 97 | value={titleDraft} |
| 98 | aria-label={gt( |
| 99 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} title", |
| 100 | { |
| 101 | term: changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest), |
| 102 | }, |
| 103 | )} |
| 104 | onChange={(event) => setTitleScope({ pullRequestKey, text: event.target.value })} |
| 105 | onKeyDown={(event) => { |
| 106 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 107 | if (event.key === "Enter") { |
| 108 | event.preventDefault(); |
| 109 | void saveTitle(titleDraft); |
| 110 | } else if (event.key === "Escape") { |
| 111 | event.preventDefault(); |
| 112 | setTitleScope(null); |
| 113 | } |
| 114 | }} |
| 115 | /> |
| 116 | <div className="flex justify-end gap-2"> |
| 117 | <Button |
| 118 | size="xs" |
| 119 | variant="ghost" |
| 120 | disabled={titleSaving} |
| 121 | onClick={() => setTitleScope(null)} |
| 122 | > |
| 123 | <T>Cancel</T> |
| 124 | </Button> |
| 125 | <Button |
| 126 | size="xs" |
| 127 | variant="outline" |
| 128 | disabled={!canWriteSourceControl || titleSaving || titleDraft.trim().length === 0} |
| 129 | onClick={() => void saveTitle(titleDraft)} |
| 130 | > |
| 131 | {titleSaving ? gt("Saving...") : gt("Save")} |
| 132 | </Button> |
| 133 | </div> |
| 134 | </div> |
| 135 | ); |
| 136 | } |