Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestTitleEditor.tsx

135 lines4.6 KB

1import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2import type { EnvironmentId, PullRequestDetail, PullRequestRef } from "@t3tools/contracts";
3import { msg, T, useGT } from "gt-react";
4import { useState } from "react";
5import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
6​
7import { m } from "~/i18n/messages";
8import { pullRequestEnvironment } from "~/state/pullRequests";
9import { useAtomCommand } from "~/state/use-atom-command";
10import { Button } from "../ui/button";
11import { Input } from "../ui/input";
12import { toastManager } from "../ui/toast";
13import { SharedTooltipTrigger } from "../ui/shared-tooltip";
14import { PullRequestDetailTitleRow } from "./PullRequestDetailLayout";
15import { PullRequestEditButton } from "./PullRequestEditButton";
16import { readableFailure } from "./pullRequestDetail.logic";
17import { canEditPullRequestChangeRequest } from "@t3tools/client-runtime/pull-request-editing";
18​
19import { changeRequestTermKind } from "./changeRequestTerm";
20​
21/** The pull request title, which the reader can rename in place when the host allows it. */
22export 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 <SharedTooltipTrigger
73 render={
74 <h1 className="min-w-0 flex-1 truncate text-base font-semibold leading-snug">
75 {detail.title}
76 </h1>
77 }
78 label={detail.title}
79 />
80 {canEditPullRequestChangeRequest(detail) ? (
81 <PullRequestEditButton
82 aria-label={gt("Edit title")}
83 onClick={() => setTitleScope({ pullRequestKey, text: detail.title })}
84 />
85 ) : null}
86 </PullRequestDetailTitleRow>
87 );
88 }
89 return (
90 <div className="space-y-2">
91 <Input
92 autoFocus
93 size="sm"
94 disabled={titleSaving}
95 value={titleDraft}
96 aria-label={gt(
97 "{term, select, mr {Merge request} cr {Change request} other {Pull request}} title",
98 {
99 term: changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest),
100 },
101 )}
102 onChange={(event) => setTitleScope({ pullRequestKey, text: event.target.value })}
103 onKeyDown={(event) => {
104 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
105 if (event.key === "Enter") {
106 event.preventDefault();
107 void saveTitle(titleDraft);
108 } else if (event.key === "Escape") {
109 event.preventDefault();
110 event.stopPropagation();
111 setTitleScope(null);
112 }
113 }}
114 />
115 <div className="flex justify-end gap-2">
116 <Button
117 size="xs"
118 variant="ghost"
119 disabled={titleSaving}
120 onClick={() => setTitleScope(null)}
121 >
122 <T>Cancel</T>
123 </Button>
124 <Button
125 size="xs"
126 variant="outline"
127 disabled={!canWriteSourceControl || titleSaving || titleDraft.trim().length === 0}
128 onClick={() => void saveTitle(titleDraft)}
129 >
130 {titleSaving ? gt("Saving...") : gt("Save")}
131 </Button>
132 </div>
133 </div>
134 );