The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { EnvironmentId, PullRequestContextMetadata } from "@t3tools/contracts"; |
| 2 | import { useGT } from "gt-react"; |
| 3 | import { CircleDashedIcon, FilmIcon, ImageIcon } from "lucide-react"; |
| 4 | import { |
| 5 | useEffect, |
| 6 | useState, |
| 7 | type ComponentProps, |
| 8 | type CSSProperties, |
| 9 | type MouseEvent, |
| 10 | type ReactNode, |
| 11 | } from "react"; |
| 12 | import { useAssetUrlState } from "~/assets/assetUrls"; |
| 13 | import { documentChipKindForFileName } from "~/pierre-icons"; |
| 14 | |
| 15 | import { PULL_REQUEST_STATE_PRESENTATION } from "~/components/pullRequest/pullRequestIcons"; |
| 16 | import type { PullRequestContextDisplayState } from "~/lib/composerContextRecords"; |
| 17 | import { PierreEntryIcon } from "./chat/PierreEntryIcon"; |
| 18 | import { middleTruncateAttachmentName } from "./composerInlineChip"; |
| 19 | import { PullRequestContextDetails } from "./PullRequestContextDetails"; |
| 20 | import { ContextChip, ContextChipLabel, type ContextChipKind } from "./ContextChip"; |
| 21 | import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "./ui/popover"; |
| 22 | import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; |
| 23 | import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview"; |
| 24 | import { usePullRequestPreviewTarget } from "~/lib/openPullRequestLink"; |
| 25 | |
| 26 | export const PULL_REQUEST_CHIP_KINDS = { |
| 27 | open: "pr-open", |
| 28 | draft: "pr-draft", |
| 29 | merged: "pr-merged", |
| 30 | closed: "pr-closed", |
| 31 | unknown: "pull-request", |
| 32 | } as const satisfies Record<PullRequestContextDisplayState | "unknown", ContextChipKind>; |
| 33 | |
| 34 | export function ContextChipShell({ |
| 35 | icon, |
| 36 | label, |
| 37 | tooltip, |
| 38 | ...props |
| 39 | }: Omit<ComponentProps<typeof ContextChip>, "className" | "render"> & { |
| 40 | icon: ReactNode; |
| 41 | label: string; |
| 42 | tooltip?: ReactNode; |
| 43 | }) { |
| 44 | const chip = ( |
| 45 | <ContextChip |
| 46 | data-context-unresolved={props.state === "unresolved" ? "true" : undefined} |
| 47 | tabIndex={tooltip ? 0 : undefined} |
| 48 | {...props} |
| 49 | > |
| 50 | {icon} |
| 51 | <ContextChipLabel>{label}</ContextChipLabel> |
| 52 | </ContextChip> |
| 53 | ); |
| 54 | if (!tooltip) return chip; |
| 55 | return ( |
| 56 | <Tooltip> |
| 57 | <TooltipTrigger render={chip} /> |
| 58 | <TooltipPopup side="top" className="whitespace-pre-wrap"> |
| 59 | {tooltip} |
| 60 | </TooltipPopup> |
| 61 | </Tooltip> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | export function ContextChipPopover(props: { |
| 66 | kind: ContextChipKind; |
| 67 | icon: ReactNode; |
| 68 | label: string; |
| 69 | copyMarkdown?: string | undefined; |
| 70 | accessibleLabel: string; |
| 71 | /** Overrides the kind's accent color. */ |
| 72 | accent?: string | undefined; |
| 73 | children: ReactNode; |
| 74 | }) { |
| 75 | const gt = useGT(); |
| 76 | return ( |
| 77 | <Popover> |
| 78 | <PopoverTrigger |
| 79 | render={ |
| 80 | <ContextChip |
| 81 | kind={props.kind} |
| 82 | style={ |
| 83 | props.accent |
| 84 | ? ({ "--context-chip-accent": props.accent } as CSSProperties) |
| 85 | : undefined |
| 86 | } |
| 87 | render={<button type="button" />} |
| 88 | aria-label={gt("{label}. Show details", { label: props.accessibleLabel })} |
| 89 | data-markdown-copy={props.copyMarkdown} |
| 90 | /> |
| 91 | } |
| 92 | > |
| 93 | {props.icon} |
| 94 | <ContextChipLabel>{props.label}</ContextChipLabel> |
| 95 | </PopoverTrigger> |
| 96 | <PopoverPopup side="top" width="lg" padding="compact"> |
| 97 | <PopoverTitle className="sr-only">{props.accessibleLabel}</PopoverTitle> |
| 98 | {props.children} |
| 99 | </PopoverPopup> |
| 100 | </Popover> |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | export function PullRequestChip(props: { |
| 105 | metadata: PullRequestContextMetadata; |
| 106 | environmentId: EnvironmentId | null; |
| 107 | label: string; |
| 108 | kindLabel: string; |
| 109 | kind: ContextChipKind; |
| 110 | copyMarkdown?: string; |
| 111 | onOpen: (event: MouseEvent<HTMLElement>, url: string) => void; |
| 112 | }) { |
| 113 | const gt = useGT(); |
| 114 | const previewTarget = usePullRequestPreviewTarget(props.environmentId, props.metadata.url); |
| 115 | const displayState = |
| 116 | props.metadata.state === "open" && props.metadata.isDraft ? "draft" : props.metadata.state; |
| 117 | const StateIcon = PULL_REQUEST_STATE_PRESENTATION[displayState].Icon; |
| 118 | const button = ( |
| 119 | <ContextChip |
| 120 | kind={props.kind} |
| 121 | render={<button type="button" />} |
| 122 | aria-label={gt("Open {kind} {label}: {title}", { |
| 123 | kind: props.kindLabel, |
| 124 | label: props.label, |
| 125 | title: props.metadata.title, |
| 126 | })} |
| 127 | data-markdown-copy={props.copyMarkdown} |
| 128 | onClick={(event) => props.onOpen(event, props.metadata.url)} |
| 129 | onAuxClick={(event) => props.onOpen(event, props.metadata.url)} |
| 130 | > |
| 131 | <StateIcon /> |
| 132 | <ContextChipLabel>{props.label}</ContextChipLabel> |
| 133 | </ContextChip> |
| 134 | ); |
| 135 | if (previewTarget !== null) { |
| 136 | return ( |
| 137 | <PullRequestLinkPreview |
| 138 | link={button} |
| 139 | originalUrl={props.metadata.url} |
| 140 | target={previewTarget} |
| 141 | confirmBeforeOpen={false} |
| 142 | fallback={<PullRequestContextDetails metadata={props.metadata} />} |
| 143 | /> |
| 144 | ); |
| 145 | } |
| 146 | return ( |
| 147 | <Tooltip> |
| 148 | <TooltipTrigger render={button} /> |
| 149 | <TooltipPopup side="top"> |
| 150 | <PullRequestContextDetails metadata={props.metadata} /> |
| 151 | </TooltipPopup> |
| 152 | </Tooltip> |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | export function ImageChipButton({ |
| 157 | name, |
| 158 | previewUrl, |
| 159 | size, |
| 160 | suffix, |
| 161 | disabled, |
| 162 | onPreviewError, |
| 163 | ...props |
| 164 | }: Omit<ComponentProps<typeof ContextChip>, "kind" | "render"> & { |
| 165 | name: string; |
| 166 | previewUrl: string | undefined; |
| 167 | size: string; |
| 168 | suffix?: string | null | undefined; |
| 169 | disabled?: boolean | undefined; |
| 170 | onPreviewError?: (() => void) | undefined; |
| 171 | }) { |
| 172 | const gt = useGT(); |
| 173 | return ( |
| 174 | <ContextChip |
| 175 | kind="image" |
| 176 | render={<button type="button" disabled={disabled} />} |
| 177 | aria-label={gt("Image attachment, {name}, {size}", { name, size })} |
| 178 | {...props} |
| 179 | > |
| 180 | {previewUrl ? ( |
| 181 | <img |
| 182 | key={previewUrl} |
| 183 | src={previewUrl} |
| 184 | alt="" |
| 185 | className="size-[1em] shrink-0 self-center rounded-sm object-cover" |
| 186 | onError={onPreviewError} |
| 187 | /> |
| 188 | ) : ( |
| 189 | <ImageIcon /> |
| 190 | )} |
| 191 | <ContextChipLabel className="flex items-baseline gap-[0.33em]"> |
| 192 | <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap"> |
| 193 | {middleTruncateAttachmentName(name)} |
| 194 | </span> |
| 195 | <span className="shrink-0 text-3xs text-current">{size}</span> |
| 196 | {suffix ? <span className="text-3xs text-current">{suffix}</span> : null} |
| 197 | </ContextChipLabel> |
| 198 | </ContextChip> |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | export function FileChip(props: { |
| 203 | name: string; |
| 204 | size: string; |
| 205 | isVideo: boolean; |
| 206 | theme: "light" | "dark"; |
| 207 | accessibleLabel: string; |
| 208 | tooltip: string; |
| 209 | suffix?: string | null; |
| 210 | copyMarkdown?: string; |
| 211 | disabled?: boolean; |
| 212 | error?: boolean; |
| 213 | unresolved?: boolean; |
| 214 | onOpen?: (() => void) | undefined; |
| 215 | svgPreview?: |
| 216 | | { |
| 217 | file?: Blob | null | undefined; |
| 218 | environmentId?: EnvironmentId | undefined; |
| 219 | attachmentId?: string | undefined; |
| 220 | } |
| 221 | | undefined; |
| 222 | }) { |
| 223 | if (props.svgPreview && !props.unresolved) return <SvgFileChip {...props} />; |
| 224 | const content = <FileChipContent {...props} />; |
| 225 | const state = props.unresolved |
| 226 | ? ("unresolved" as const) |
| 227 | : props.error |
| 228 | ? ("invalid" as const) |
| 229 | : null; |
| 230 | const attributes = { |
| 231 | kind: props.isVideo ? "video" : (documentChipKindForFileName(props.name) ?? "file"), |
| 232 | ...(state ? { state } : {}), |
| 233 | "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "), |
| 234 | "data-markdown-copy": props.copyMarkdown, |
| 235 | "data-context-unresolved": props.unresolved ? "true" : undefined, |
| 236 | } as const; |
| 237 | return ( |
| 238 | <Tooltip> |
| 239 | <TooltipTrigger |
| 240 | render={ |
| 241 | props.onOpen ? ( |
| 242 | <ContextChip |
| 243 | render={<button type="button" disabled={props.disabled} />} |
| 244 | onClick={props.onOpen} |
| 245 | {...attributes} |
| 246 | > |
| 247 | {content} |
| 248 | </ContextChip> |
| 249 | ) : ( |
| 250 | <ContextChip tabIndex={0} {...attributes}> |
| 251 | {content} |
| 252 | </ContextChip> |
| 253 | ) |
| 254 | } |
| 255 | /> |
| 256 | <TooltipPopup side="top" className="whitespace-pre-wrap"> |
| 257 | {props.tooltip} |
| 258 | </TooltipPopup> |
| 259 | </Tooltip> |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | function SvgFileChip(props: ComponentProps<typeof FileChip>) { |
| 264 | const preview = props.svgPreview; |
| 265 | const [local, setLocal] = useState<{ file: Blob; url: string }>(); |
| 266 | const [failedPreviewUrl, setFailedPreviewUrl] = useState<string>(); |
| 267 | const asset = useAssetUrlState( |
| 268 | preview?.file ? null : (preview?.environmentId ?? null), |
| 269 | preview?.attachmentId |
| 270 | ? { |
| 271 | _tag: "attachment", |
| 272 | attachmentId: preview.attachmentId, |
| 273 | fileName: props.name, |
| 274 | mimeType: "image/svg+xml", |
| 275 | disposition: "inline", |
| 276 | } |
| 277 | : null, |
| 278 | ); |
| 279 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates an object URL for the local SVG file and revokes it on cleanup |
| 280 | useEffect(() => { |
| 281 | const file = preview?.file; |
| 282 | if (!file) return; |
| 283 | const url = URL.createObjectURL(file); |
| 284 | // oxlint-disable-next-line react/set-state-in-effect -- Publish a Blob URL with its cleanup registered. |
| 285 | setLocal({ file, url }); |
| 286 | return () => URL.revokeObjectURL(url); |
| 287 | }, [preview?.file]); |
| 288 | const url = preview?.file |
| 289 | ? local?.file === preview.file |
| 290 | ? local.url |
| 291 | : undefined |
| 292 | : asset._tag === "Success" |
| 293 | ? asset.url |
| 294 | : undefined; |
| 295 | if (url !== undefined && failedPreviewUrl === url) { |
| 296 | return <FileChip {...props} svgPreview={undefined} />; |
| 297 | } |
| 298 | return ( |
| 299 | <Tooltip> |
| 300 | <TooltipTrigger |
| 301 | render={ |
| 302 | <ImageChipButton |
| 303 | name={props.name} |
| 304 | size={props.size} |
| 305 | previewUrl={url} |
| 306 | suffix={props.suffix} |
| 307 | disabled={props.disabled} |
| 308 | onClick={props.onOpen} |
| 309 | onPreviewError={() => setFailedPreviewUrl(url)} |
| 310 | data-markdown-copy={props.copyMarkdown} |
| 311 | aria-label={props.accessibleLabel} |
| 312 | {...(props.error ? { state: "invalid" as const } : {})} |
| 313 | /> |
| 314 | } |
| 315 | /> |
| 316 | <TooltipPopup side="top" className="whitespace-pre-wrap"> |
| 317 | {props.tooltip} |
| 318 | </TooltipPopup> |
| 319 | </Tooltip> |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | function FileChipContent(props: { |
| 324 | name: string; |
| 325 | size: string; |
| 326 | isVideo: boolean; |
| 327 | theme: "light" | "dark"; |
| 328 | suffix?: string | null; |
| 329 | unresolved?: boolean; |
| 330 | }) { |
| 331 | return ( |
| 332 | <> |
| 333 | {props.unresolved ? ( |
| 334 | <CircleDashedIcon /> |
| 335 | ) : props.isVideo ? ( |
| 336 | <FilmIcon /> |
| 337 | ) : ( |
| 338 | <PierreEntryIcon pathValue={props.name} kind="file" theme={props.theme} /> |
| 339 | )} |
| 340 | <ContextChipLabel className="flex items-baseline gap-[0.33em]"> |
| 341 | <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap"> |
| 342 | {middleTruncateAttachmentName(props.name)} |
| 343 | </span> |
| 344 | <span className="shrink-0 text-3xs text-current">{props.size}</span> |
| 345 | {props.suffix ? <span className="text-3xs text-current">{props.suffix}</span> : null} |
| 346 | </ContextChipLabel> |
| 347 | </> |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | export function UnresolvedChip(props: { label: string; tooltip: string; copyMarkdown?: string }) { |
| 352 | const gt = useGT(); |
| 353 | return ( |
| 354 | <ContextChipShell |
| 355 | icon={<CircleDashedIcon />} |
| 356 | label={props.label} |
| 357 | state="unresolved" |
| 358 | aria-label={gt("Unavailable context, {label}", { label: props.label })} |
| 359 | data-markdown-copy={props.copyMarkdown} |
| 360 | tooltip={props.tooltip} |
| 361 | /> |
| 362 | ); |
| 363 | } |