80 lines · 4.0 KBRaw
| 1 | import { Banner, Button, cn } from "@refs/design-system"; |
| 2 | import { CheckCircleIcon, GitMergeIcon, InfoIcon, WarningCircleIcon } from "@refs/design-system/icons"; |
| 3 | import type { Mergeability } from "@refs/backend/contract"; |
| 4 | import { useState } from "react"; |
| 5 | import { useFetcher } from "react-router"; |
| 6 | |
| 7 | function Status({ icon, tone, title, children }: { icon: React.ReactNode; tone: string; title: string; children?: React.ReactNode }) { |
| 8 | return ( |
| 9 | <div className="flex min-w-0 items-start gap-3"> |
| 10 | <span className={cn("mt-0.5 shrink-0 [&_svg]:size-5", tone)}>{icon}</span> |
| 11 | <div className="flex min-w-0 flex-col"> |
| 12 | <span className="text-base font-semibold text-ink">{title}</span> |
| 13 | {children && <span className="text-compact text-muted [overflow-wrap:anywhere]">{children}</span>} |
| 14 | </div> |
| 15 | </div> |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * The merge state of a pull request: clean, conflicts, unavailable or merged (`mergeability` is null). For people who |
| 21 | * may merge, a clean state shows the one primary action. Posts `merge` to the current route. |
| 22 | */ |
| 23 | export function MergeBox({ mergeability, base, head, canMerge }: { mergeability: Mergeability | null; base: string; head: string; canMerge: boolean }) { |
| 24 | const fetcher = useFetcher<{ intent: string; error: string | null }>(); |
| 25 | const [confirming, setConfirming] = useState(false); |
| 26 | const merging = fetcher.state !== "idle"; |
| 27 | const panel = "flex min-w-0 flex-col gap-3 rounded-panel bg-panel p-4 shadow-card sm:flex-row sm:items-center sm:justify-between"; |
| 28 | if (!mergeability) |
| 29 | return ( |
| 30 | <div className={cn(panel, "bg-success-tint")}> |
| 31 | <Status icon={<GitMergeIcon weight="fill" />} tone="text-success" title="Merged"> |
| 32 | <code className="font-mono text-xs">{head}</code> is part of <code className="font-mono text-xs">{base}</code>. |
| 33 | </Status> |
| 34 | </div> |
| 35 | ); |
| 36 | return ( |
| 37 | <div className="flex min-w-0 flex-col gap-3"> |
| 38 | <div className={panel}> |
| 39 | {mergeability.state === "clean" ? ( |
| 40 | <> |
| 41 | <Status icon={<CheckCircleIcon weight="fill" />} tone="text-success" title={`No conflicts with ${base}`}> |
| 42 | Merging adds a merge commit to {base}. |
| 43 | </Status> |
| 44 | {canMerge ? ( |
| 45 | <fetcher.Form method="post" className="flex shrink-0 gap-2"> |
| 46 | <input type="hidden" name="intent" value="merge" /> |
| 47 | {confirming ? ( |
| 48 | <> |
| 49 | <Button type="button" variant="secondary" onClick={() => setConfirming(false)} disabled={merging}> |
| 50 | Cancel |
| 51 | </Button> |
| 52 | <Button type="submit" variant="primary" icon={<GitMergeIcon />} loading={merging}> |
| 53 | Confirm merge |
| 54 | </Button> |
| 55 | </> |
| 56 | ) : ( |
| 57 | <Button type="button" variant="primary" icon={<GitMergeIcon />} onClick={() => setConfirming(true)}> |
| 58 | Merge pull request |
| 59 | </Button> |
| 60 | )} |
| 61 | </fetcher.Form> |
| 62 | ) : ( |
| 63 | <span className="shrink-0 text-compact text-muted">Only people with write access can merge.</span> |
| 64 | )} |
| 65 | </> |
| 66 | ) : mergeability.state === "conflicts" ? ( |
| 67 | <Status icon={<WarningCircleIcon weight="fill" />} tone="text-caution" title={`${mergeability.paths.length === 1 ? "A file conflicts" : `${mergeability.paths.length} files conflict`} with ${base}`}> |
| 68 | Merge {base} into {head} locally and push to resolve. Both sides changed <span className="font-mono text-xs">{mergeability.paths.slice(0, 8).join(", ")}</span> |
| 69 | {mergeability.paths.length > 8 ? ` and ${mergeability.paths.length - 8} more` : ""}. |
| 70 | </Status> |
| 71 | ) : ( |
| 72 | <Status icon={<InfoIcon />} tone="text-muted" title="Merge state unavailable"> |
| 73 | {mergeability.reason} |
| 74 | </Status> |
| 75 | )} |
| 76 | </div> |
| 77 | {fetcher.data?.intent === "merge" && fetcher.data.error && <Banner variant="error" description={fetcher.data.error} />} |
| 78 | </div> |
| 79 | ); |
| 80 | } |