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