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

PullRequestTitleEditor.tsx

129 lines4.4 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}: {
28 environmentId: EnvironmentId;
29 reference: PullRequestRef;
30 pullRequestKey: string;
31 detail: PullRequestDetail;
32 canWriteSourceControl: boolean;
33}) {
34 const update = useAtomCommand(pullRequestEnvironment.update, { reportFailure: false });
35 const [titleScope, setTitleScope] = useState<{
36 readonly pullRequestKey: string;
37 readonly text: string;
38 } | null>(null);
39 const titleDraft = titleScope?.pullRequestKey === pullRequestKey ? titleScope.text : null;
40 const [titleSaving, setTitleSaving] = useState(false);
41​
42 const saveTitle = async (next: string) => {
43 const title = next.trim();
44 if (!canWriteSourceControl || titleSaving) return;
45 if (title.length === 0 || title === detail.title) {
46 setTitleScope(null);
47 return;
48 }
49 setTitleSaving(true);
50 // The new title shows while the host takes it, so the editor closes at once.
51 setTitleScope(null);
52 const result = await update({ environmentId, input: { ...reference, title } });
53 setTitleSaving(false);
54 if (result._tag === "Failure") {
55 setTitleScope({ pullRequestKey, text: title });
56 toastManager.add({
57 type: "error",
58 title: "The title could not be saved",
59 description: readableFailure(
60 squashAtomCommandFailure(result),
61 "The host refused the new title.",
62 ),
63 });
64 }
65 };
66​
67 if (titleDraft === null) {
68 return (
69 <PullRequestDetailTitleRow className="group gap-1">
70 <Tooltip>
71 <TooltipTrigger
72 render={
73 <h1 className="min-w-0 flex-1 truncate text-base font-semibold leading-snug">
74 {detail.title}
75 </h1>
76 }
77 />
78 <TooltipPopup side="top">{detail.title}</TooltipPopup>
79 </Tooltip>
80 {canEditPullRequestChangeRequest(detail) ? (
81 <PullRequestEditButton
82 aria-label="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={`${capitalizeTerm(sourceControlClients.get(detail.provider).changeRequest.singular)} title`}
97 onChange={(event) => setTitleScope({ pullRequestKey, text: event.target.value })}
98 onKeyDown={(event) => {
99 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
100 if (event.key === "Enter") {
101 event.preventDefault();
102 void saveTitle(titleDraft);
103 } else if (event.key === "Escape") {
104 event.preventDefault();
105 setTitleScope(null);
106 }
107 }}
108 />
109 <div className="flex justify-end gap-2">
110 <Button
111 size="xs"
112 variant="ghost"
113 disabled={titleSaving}
114 onClick={() => setTitleScope(null)}
115 >
116 Cancel
117 </Button>
118 <Button
119 size="xs"
120 variant="outline"
121 disabled={!canWriteSourceControl || titleSaving || titleDraft.trim().length === 0}
122 onClick={() => void saveTitle(titleDraft)}
123 >
124 {titleSaving ? "Saving..." : "Save"}
125 </Button>
126 </div>
127 </div>
128 );