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

PullRequestTitleEditor.tsx

131 lines4.3 KB

1import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2import type { EnvironmentId, PullRequestDetail, PullRequestRef } from "@t3tools/contracts";
3import { useState } from "react";
4import {
5 capitalizeTerm,
6 sourceControlClients,
7} from "@t3tools/client-runtime/source-control-clients";
8​
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 { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
15import { PullRequestDetailTitleRow } from "./PullRequestDetailLayout";
16import { PullRequestEditButton } from "./PullRequestEditButton";
17import { readableFailure } from "./pullRequestDetail.logic";
18import { canEditPullRequestChangeRequest } from "@t3tools/client-runtime/pull-request-editing";
19​
20/** The pull request title, which the reader can rename in place when the host allows it. */
21export function PullRequestTitleEditor({
22 environmentId,
23 reference,
24 pullRequestKey,
25 detail,
26 canWriteSourceControl,
27 onSaved,
28}: {
29 environmentId: EnvironmentId;
30 reference: PullRequestRef;
31 pullRequestKey: string;
32 detail: PullRequestDetail;
33 canWriteSourceControl: boolean;
34 onSaved: () => void;
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 [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 const result = await update({ environmentId, input: { ...reference, title } });
53 setTitleSaving(false);
54 if (result._tag === "Failure") {
55 toastManager.add({
56 type: "error",
57 title: "The title could not be saved",
58 description: readableFailure(
59 squashAtomCommandFailure(result),
60 "The host refused the new title.",
61 ),
62 });
63 return;
64 }
65 setTitleScope(null);
66 onSaved();
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="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={`${capitalizeTerm(sourceControlClients.get(detail.provider).changeRequest.singular)} title`}
99 onChange={(event) => setTitleScope({ pullRequestKey, text: event.target.value })}
100 onKeyDown={(event) => {
101 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
102 if (event.key === "Enter") {
103 event.preventDefault();
104 void saveTitle(titleDraft);
105 } else if (event.key === "Escape") {
106 event.preventDefault();
107 setTitleScope(null);
108 }
109 }}
110 />
111 <div className="flex justify-end gap-2">
112 <Button
113 size="xs"
114 variant="ghost"
115 disabled={titleSaving}
116 onClick={() => setTitleScope(null)}
117 >
118 Cancel
119 </Button>
120 <Button
121 size="xs"
122 variant="outline"
123 disabled={!canWriteSourceControl || titleSaving || titleDraft.trim().length === 0}
124 onClick={() => void saveTitle(titleDraft)}
125 >
126 {titleSaving ? "Saving..." : "Save"}
127 </Button>
128 </div>
129 </div>
130 );