| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | ChevronDown, |
| 5 | GitCommitHorizontal, |
| 6 | GitMerge, |
| 7 | ListOrdered, |
| 8 | type LucideIcon, |
| 9 | } from "lucide-react"; |
| 10 | import { useRouter } |
| 11 | import { useAction } |
| 12 | import { useState } |
| 13 | import { toast } |
| 14 | import { mergePullRequest } |
| 15 | import { |
| 16 | type MergeMethod, |
| 17 | mergeMethods, |
| 18 | } from "@/components/pull-requests/common"; |
| 19 | import { Button } |
| 20 | import { |
| 21 | DropdownMenu, |
| 22 | DropdownMenuContent, |
| 23 | DropdownMenuRadioGroup, |
| 24 | DropdownMenuRadioItem, |
| 25 | DropdownMenuTrigger, |
| 26 | } from "@/components/ui/dropdown-menu"; |
| 27 | import { Input } |
| 28 | import { Spinner } |
| 29 | import { Textarea } |
| 30 | |
| 31 | const methods = { |
| 32 | merge: { |
| 33 | icon: GitMerge, |
| 34 | label: "Merge pull request", |
| 35 | option: "Create a merge commit", |
| 36 | description: |
| 37 | "Every commit lands on the base branch through a merge commit.", |
| 38 | }, |
| 39 | squash: { |
| 40 | icon: GitCommitHorizontal, |
| 41 | label: "Squash and merge", |
| 42 | option: "Squash and merge", |
| 43 | description: "Every commit is combined into one on the base branch.", |
| 44 | }, |
| 45 | rebase: { |
| 46 | icon: ListOrdered, |
| 47 | label: "Rebase and merge", |
| 48 | option: "Rebase and merge", |
| 49 | description: "Every commit is replayed onto the base branch, one by one.", |
| 50 | }, |
| 51 | } satisfies Record< |
| 52 | MergeMethod, |
| 53 | { icon: |
| 54 | >; |
| 55 | |
| 56 | /** The merge button with its method picked from the attached menu. A squash asks for its commit message before it merges. */ |
| 57 | export function MergeButton({ |
| 58 | username, |
| 59 | repo, |
| 60 | number, |
| 61 | disabled, |
| 62 | squash, |
| 63 | }: { |
| 64 | username: string |
| 65 | repo: string |
| 66 | number: number |
| 67 | disabled: boolean |
| 68 | /** The message a squash commits unless it is rewritten. */ |
| 69 | squash: { |
| 70 | }) { |
| 71 | const router = |
| 72 | const [method, |
| 73 | const [editing, |
| 74 | const [title, |
| 75 | const [message, |
| 76 | |
| 77 | // Starts from the message as it stands now: the head may have moved since the page loaded. |
| 78 | const startSquash = |
| 79 | setTitle(squash?.title |
| 80 | setMessage(squash?.message |
| 81 | setEditing(true); |
| 82 | }; |
| 83 | |
| 84 | const merge = |
| 85 | onSuccess: () => |
| 86 | toast.success("Pull request merged. |
| 87 | router.refresh(); |
| 88 | }, |
| 89 | onError: ({ error |
| 90 | toast.error(error |
| 91 | }); |
| 92 | |
| 93 | if (editing) { |
| 94 | return ( |
| 95 | <form |
| 96 | className="flex w-full flex-col gap-2" |
| 97 | onSubmit={(event) |
| 98 | event.preventDefault(); |
| 99 | merge.execute({ |
| 100 | username, |
| 101 | repo, |
| 102 | number, |
| 103 | method: "squash", |
| 104 | title: title.trim |
| 105 | message, |
| 106 | }); |
| 107 | }} |
| 108 | > |
| 109 | <Input |
| 110 | aria-label="Commit title" |
| 111 | value={title} |
| 112 | maxLength={200} |
| 113 | onChange={(event) |
| 114 | /> |
| 115 | <Textarea |
| 116 | aria-label="Commit message" |
| 117 | value={message} |
| 118 | rows={6} |
| 119 | className="font-mono text-xs" |
| 120 | onChange={(event) |
| 121 | /> |
| 122 | <div className="flex gap-2 |
| 123 | <Button |
| 124 | type="submit" |
| 125 | size="sm" |
| 126 | disabled={disabled || |
| 127 | > |
| 128 | {merge.isExecuting && |
| 129 | Confirm squash and merge |
| 130 | </Button> |
| 131 | <Button |
| 132 | type="button" |
| 133 | size="sm" |
| 134 | variant="ghost" |
| 135 | disabled={merge.isExecuting |
| 136 | onClick={() => |
| 137 | > |
| 138 | Cancel |
| 139 | </Button> |
| 140 | </div> |
| 141 | </form> |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | const Icon = |
| 146 | return ( |
| 147 | <div className="flex |
| 148 | <Button |
| 149 | size="sm" |
| 150 | className="rounded-r-none" |
| 151 | disabled={disabled || |
| 152 | onClick={() => |
| 153 | method === "squash" |
| 154 | ? startSquash() |
| 155 | : merge.execute( |
| 156 | } |
| 157 | > |
| 158 | {merge.isExecuting ? |
| 159 | {methods[method]. |
| 160 | </Button> |
| 161 | <DropdownMenu> |
| 162 | <DropdownMenuTrigger asChild> |
| 163 | <Button |
| 164 | size="icon-sm" |
| 165 | className="rounded-l-none border-l-primary-foreground/20" |
| 166 | disabled={merge.isExecuting |
| 167 | aria-label="Choose a merge method" |
| 168 | > |
| 169 | <ChevronDown /> |
| 170 | </Button> |
| 171 | </DropdownMenuTrigger> |
| 172 | <DropdownMenuContent align=" |
| 173 | <DropdownMenuRadioGroup |
| 174 | value={method} |
| 175 | onValueChange={(next) |
| 176 | > |
| 177 | {mergeMethods.map(( |
| 178 | const { |
| 179 | icon: OptionIcon, |
| 180 | option: label, |
| 181 | description, |
| 182 | } = methods[option |
| 183 | return ( |
| 184 | <DropdownMenuRadioItem |
| 185 | key={option} |
| 186 | value={option} |
| 187 | className="items-start py-1.5" |
| 188 | > |
| 189 | <OptionIcon className=" |
| 190 | <span className="flex flex-col gap-0.5 |
| 191 | <span>{label}</ |
| 192 | <span className="text-xs text-muted-foreground |
| 193 | {description} |
| 194 | </span> |
| 195 | </span> |
| 196 | </DropdownMenuRadioItem> |
| 197 | ); |
| 198 | })} |
| 199 | </DropdownMenuRadioGroup> |
| 200 | </DropdownMenuContent> |
| 201 | </DropdownMenu> |
| 202 | </div> |
| 203 | ); |
| 204 | } |