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

PullRequestTitleEditor.tsx

127 lines4.1 KB

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