The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import { T, useGT } from "gt-react"; |
| 4 | import { ArrowLeftIcon, ExternalLinkIcon } from "lucide-react"; |
| 5 | import type { MouseEvent as ReactMouseEvent } from "react"; |
| 6 | |
| 7 | import { useOpenLink } from "~/browser/useOpenLink"; |
| 8 | import { cn } from "~/lib/utils"; |
| 9 | import { Button } from "../ui/button"; |
| 10 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 11 | import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu"; |
| 12 | |
| 13 | import { changeRequestTermKind } from "./changeRequestTerm"; |
| 14 | |
| 15 | interface CrumbsPullRequest { |
| 16 | readonly url: string; |
| 17 | readonly provider: string; |
| 18 | readonly number: number; |
| 19 | readonly repository: string; |
| 20 | readonly title: string; |
| 21 | } |
| 22 | |
| 23 | const openNumberContextMenu = (event: ReactMouseEvent, detail: CrumbsPullRequest): void => { |
| 24 | event.preventDefault(); |
| 25 | event.stopPropagation(); |
| 26 | void showPullRequestLinkContextMenu({ |
| 27 | url: detail.url, |
| 28 | openLabel: openOnHostLabel(sourceControlClients.get(detail.provider)), |
| 29 | position: { x: event.clientX, y: event.clientY }, |
| 30 | }); |
| 31 | }; |
| 32 | |
| 33 | const ROW_CLASS_NAME = |
| 34 | "col-start-1 row-start-1 flex min-w-0 items-center gap-1 text-sm text-muted-foreground transition-[opacity,transform] ease-out motion-reduce:transform-none motion-reduce:transition-none sm:text-xs"; |
| 35 | |
| 36 | function BackButton({ onBack, focusable }: { onBack: () => void; focusable: boolean }) { |
| 37 | const gt = useGT(); |
| 38 | return ( |
| 39 | <Tooltip> |
| 40 | <TooltipTrigger |
| 41 | render={ |
| 42 | <Button |
| 43 | size="icon-micro" |
| 44 | variant="ghost-muted" |
| 45 | {...(focusable ? {} : { tabIndex: -1 })} |
| 46 | onClick={onBack} |
| 47 | className="-ml-1.5" |
| 48 | aria-label={gt("Back to this thread's pull requests")} |
| 49 | > |
| 50 | <ArrowLeftIcon aria-hidden className="size-3.5" /> |
| 51 | </Button> |
| 52 | } |
| 53 | /> |
| 54 | <TooltipPopup side="top"> |
| 55 | <T>Back to pull requests</T> |
| 56 | </TooltipPopup> |
| 57 | </Tooltip> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | function NumberLink({ |
| 62 | detail, |
| 63 | threadRef, |
| 64 | toneClassName, |
| 65 | openOnHostText, |
| 66 | focusable, |
| 67 | }: { |
| 68 | detail: CrumbsPullRequest; |
| 69 | threadRef: ScopedThreadRef | null; |
| 70 | toneClassName: string; |
| 71 | openOnHostText: string; |
| 72 | focusable: boolean; |
| 73 | }) { |
| 74 | const gt = useGT(); |
| 75 | const openLink = useOpenLink(threadRef); |
| 76 | return ( |
| 77 | <Tooltip> |
| 78 | <TooltipTrigger |
| 79 | render={ |
| 80 | <button |
| 81 | type="button" |
| 82 | {...(focusable ? {} : { tabIndex: -1 })} |
| 83 | onClick={(event) => void openLink(detail.url, { event }).catch(console.error)} |
| 84 | onAuxClick={(event) => { |
| 85 | if (event.button === 1) void openLink(detail.url, { event }).catch(console.error); |
| 86 | }} |
| 87 | onContextMenu={(event) => openNumberContextMenu(event, detail)} |
| 88 | className={cn( |
| 89 | "inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 hover:underline", |
| 90 | toneClassName, |
| 91 | )} |
| 92 | aria-label={gt( |
| 93 | "Open {term, select, mr {merge request} cr {change request} other {pull request}} #{number} on host", |
| 94 | { |
| 95 | term: changeRequestTermKind( |
| 96 | sourceControlClients.get(detail.provider).changeRequest, |
| 97 | ), |
| 98 | number: detail.number, |
| 99 | }, |
| 100 | )} |
| 101 | > |
| 102 | #{detail.number} |
| 103 | <ExternalLinkIcon aria-hidden className="size-2.5" /> |
| 104 | </button> |
| 105 | } |
| 106 | /> |
| 107 | <TooltipPopup side="top">{openOnHostText}</TooltipPopup> |
| 108 | </Tooltip> |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * The repository and number above the title, swapped for the number and title once the reader has |
| 114 | * scrolled the title away. |
| 115 | */ |
| 116 | export function PullRequestDetailCrumbs({ |
| 117 | detail, |
| 118 | threadRef, |
| 119 | toneClassName, |
| 120 | condensed, |
| 121 | repositoryUrl, |
| 122 | openOnHostText, |
| 123 | onBack, |
| 124 | }: { |
| 125 | detail: CrumbsPullRequest | null; |
| 126 | threadRef: ScopedThreadRef | null; |
| 127 | toneClassName: string | null; |
| 128 | condensed: boolean; |
| 129 | repositoryUrl: string | null; |
| 130 | openOnHostText: string; |
| 131 | onBack: (() => void) | undefined; |
| 132 | }) { |
| 133 | const gt = useGT(); |
| 134 | const openLink = useOpenLink(threadRef); |
| 135 | const ready = detail !== null && toneClassName !== null; |
| 136 | return ( |
| 137 | <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden"> |
| 138 | <div |
| 139 | aria-hidden={condensed} |
| 140 | inert={condensed} |
| 141 | data-workspace-scroll-swap |
| 142 | className={cn( |
| 143 | ROW_CLASS_NAME, |
| 144 | condensed |
| 145 | ? "pointer-events-none -translate-y-1 opacity-0 duration-100" |
| 146 | : "translate-y-0 opacity-100 delay-50 duration-150", |
| 147 | )} |
| 148 | > |
| 149 | {ready ? ( |
| 150 | <> |
| 151 | {onBack ? <BackButton onBack={onBack} focusable /> : null} |
| 152 | <Tooltip> |
| 153 | <TooltipTrigger |
| 154 | render={ |
| 155 | repositoryUrl ? ( |
| 156 | <button |
| 157 | type="button" |
| 158 | onClick={(event) => |
| 159 | void openLink(repositoryUrl, { event }).catch(console.error) |
| 160 | } |
| 161 | onAuxClick={(event) => { |
| 162 | if (event.button === 1) |
| 163 | void openLink(repositoryUrl, { event }).catch(console.error); |
| 164 | }} |
| 165 | className="min-w-0 cursor-pointer truncate text-left font-medium text-muted-foreground underline-offset-2 hover:text-foreground hover:underline" |
| 166 | > |
| 167 | {detail.repository} |
| 168 | </button> |
| 169 | ) : ( |
| 170 | <span className="min-w-0 truncate font-medium text-muted-foreground"> |
| 171 | {detail.repository} |
| 172 | </span> |
| 173 | ) |
| 174 | } |
| 175 | /> |
| 176 | <TooltipPopup side="top"> |
| 177 | {repositoryUrl |
| 178 | ? gt("Open {repository} repository", { repository: detail.repository }) |
| 179 | : detail.repository} |
| 180 | </TooltipPopup> |
| 181 | </Tooltip> |
| 182 | <NumberLink |
| 183 | detail={detail} |
| 184 | threadRef={threadRef} |
| 185 | toneClassName={toneClassName} |
| 186 | openOnHostText={openOnHostText} |
| 187 | focusable |
| 188 | /> |
| 189 | </> |
| 190 | ) : null} |
| 191 | </div> |
| 192 | <div |
| 193 | aria-hidden={!condensed} |
| 194 | inert={!condensed} |
| 195 | data-workspace-scroll-swap |
| 196 | className={cn( |
| 197 | ROW_CLASS_NAME, |
| 198 | condensed |
| 199 | ? "translate-y-0 opacity-100 delay-50 duration-150" |
| 200 | : "pointer-events-none translate-y-1 opacity-0 duration-100", |
| 201 | )} |
| 202 | > |
| 203 | {ready ? ( |
| 204 | <> |
| 205 | {onBack ? <BackButton onBack={onBack} focusable={condensed} /> : null} |
| 206 | <NumberLink |
| 207 | detail={detail} |
| 208 | threadRef={threadRef} |
| 209 | toneClassName={toneClassName} |
| 210 | openOnHostText={openOnHostText} |
| 211 | focusable={condensed} |
| 212 | /> |
| 213 | <Tooltip> |
| 214 | <TooltipTrigger |
| 215 | render={ |
| 216 | <span className="min-w-0 truncate font-medium text-foreground"> |
| 217 | {detail.title} |
| 218 | </span> |
| 219 | } |
| 220 | /> |
| 221 | <TooltipPopup side="top">{detail.title}</TooltipPopup> |
| 222 | </Tooltip> |
| 223 | </> |
| 224 | ) : null} |
| 225 | </div> |
| 226 | </div> |
| 227 | ); |
| 228 | } |