The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, useGT, Var } from "gt-react"; |
| 2 | import { ExternalLinkIcon, PaperclipIcon } from "lucide-react"; |
| 3 | import { markdownImageSourceFragment } from "@t3tools/client-runtime/markdown-images"; |
| 4 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 5 | import { |
| 6 | SOURCE_CONTROL_MEDIA_ASSET, |
| 7 | type AssetResource, |
| 8 | type EnvironmentId, |
| 9 | type ScopedThreadRef, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import { createContext, useContext } from "react"; |
| 12 | import type { PluggableList } from "unified"; |
| 13 | |
| 14 | import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls"; |
| 15 | import { cn } from "~/lib/utils"; |
| 16 | import { PULL_REQUESTS_PANEL_REF } from "~/rightPanelStore"; |
| 17 | |
| 18 | import ChatMarkdown from "../ChatMarkdown"; |
| 19 | import { MediaVideoPlayer } from "../media/MediaVideoPlayer"; |
| 20 | import { splitPullRequestBody, type PullRequestBodySegment } from "./pullRequestMarkdown.logic"; |
| 21 | |
| 22 | export const PullRequestMarkdownContext = createContext<{ |
| 23 | /** Stable per repository, so comments share one renderer and its parse cache. */ |
| 24 | remarkPlugins: PluggableList; |
| 25 | lineBreaks: boolean; |
| 26 | threadRef: ScopedThreadRef | null; |
| 27 | /** The change request's `owner/name`, which can differ from the checkout the body renders in. */ |
| 28 | repository: string | null; |
| 29 | } | null>(null); |
| 30 | |
| 31 | function PullRequestHostVideo({ |
| 32 | environmentId, |
| 33 | cwd, |
| 34 | url, |
| 35 | fetchUrl, |
| 36 | repository, |
| 37 | }: { |
| 38 | environmentId: EnvironmentId; |
| 39 | cwd: string; |
| 40 | url: string; |
| 41 | fetchUrl: string; |
| 42 | repository: string | undefined; |
| 43 | }) { |
| 44 | const gt = useGT(); |
| 45 | const resource: AssetResource = { |
| 46 | _tag: SOURCE_CONTROL_MEDIA_ASSET, |
| 47 | cwd, |
| 48 | url: fetchUrl, |
| 49 | ...(repository ? { repository } : {}), |
| 50 | }; |
| 51 | const assetUrl = useAssetUrlState(environmentId, resource); |
| 52 | const refreshAssetUrl = useAssetUrlRefresh(environmentId, resource); |
| 53 | const src = |
| 54 | assetUrl._tag === "Success" ? assetUrl.url : assetUrl._tag === "Failure" ? fetchUrl : null; |
| 55 | return ( |
| 56 | <MediaVideoPlayer |
| 57 | src={src === null ? null : src + markdownImageSourceFragment(url)} |
| 58 | originalUrl={url} |
| 59 | label={gt("Pull request video")} |
| 60 | className="w-full" |
| 61 | videoClassName="rounded-lg border border-border/60" |
| 62 | onRetry={refreshAssetUrl} |
| 63 | /> |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | function PullRequestBodySegmentView({ |
| 68 | segment, |
| 69 | cwd, |
| 70 | environmentId, |
| 71 | threadRef, |
| 72 | lineBreaks, |
| 73 | onTaskListChange, |
| 74 | }: { |
| 75 | segment: PullRequestBodySegment; |
| 76 | cwd: string; |
| 77 | environmentId: EnvironmentId; |
| 78 | threadRef: ScopedThreadRef | undefined; |
| 79 | lineBreaks: boolean; |
| 80 | onTaskListChange: ((input: { markerOffset: number; checked: boolean }) => void) | undefined; |
| 81 | }) { |
| 82 | const gt = useGT(); |
| 83 | const context = useContext(PullRequestMarkdownContext); |
| 84 | const repository = context?.repository ?? undefined; |
| 85 | if (segment.kind === "table") { |
| 86 | return ( |
| 87 | <div className="space-y-3"> |
| 88 | {segment.rows.map((cells, rowIndex) => ( |
| 89 | <div |
| 90 | // oxlint-disable-next-line react/no-array-index-key -- rows have no identity besides position |
| 91 | key={rowIndex} |
| 92 | className="grid gap-3" |
| 93 | style={{ gridTemplateColumns: `repeat(${cells.length}, minmax(0, 1fr))` }} |
| 94 | > |
| 95 | {cells.map((cell, cellIndex) => ( |
| 96 | <div |
| 97 | // oxlint-disable-next-line react/no-array-index-key -- cells have no identity besides position |
| 98 | key={cellIndex} |
| 99 | className={cn("min-w-0 space-y-3", cell.header && "font-medium")} |
| 100 | > |
| 101 | {cell.segments.map((cellSegment) => ( |
| 102 | <PullRequestBodySegmentView |
| 103 | key={cellSegment.id} |
| 104 | segment={cellSegment} |
| 105 | cwd={cwd} |
| 106 | environmentId={environmentId} |
| 107 | threadRef={threadRef} |
| 108 | lineBreaks={lineBreaks} |
| 109 | onTaskListChange={onTaskListChange} |
| 110 | /> |
| 111 | ))} |
| 112 | </div> |
| 113 | ))} |
| 114 | </div> |
| 115 | ))} |
| 116 | </div> |
| 117 | ); |
| 118 | } |
| 119 | if (segment.kind === "markdown" || segment.media === "image") { |
| 120 | return ( |
| 121 | <ChatMarkdown |
| 122 | text={segment.kind === "markdown" ? segment.text : ``} |
| 123 | cwd={cwd} |
| 124 | threadRef={threadRef} |
| 125 | pullRequestPanelRef={threadRef ?? PULL_REQUESTS_PANEL_REF} |
| 126 | environmentId={environmentId} |
| 127 | {...(context ? { extraRemarkPlugins: context.remarkPlugins } : {})} |
| 128 | lineBreaks={lineBreaks} |
| 129 | sourceControlMedia |
| 130 | sourceControlMediaRepository={repository} |
| 131 | onTaskListChange={ |
| 132 | onTaskListChange && segment.kind === "markdown" |
| 133 | ? ({ markerOffset, checked }) => |
| 134 | onTaskListChange({ |
| 135 | markerOffset: segment.sourceOffset + markerOffset, |
| 136 | checked, |
| 137 | }) |
| 138 | : undefined |
| 139 | } |
| 140 | /> |
| 141 | ); |
| 142 | } |
| 143 | const hostMedia = sourceControlClients.mediaUrl(segment.url); |
| 144 | if (segment.media === "video" && hostMedia !== null) { |
| 145 | return ( |
| 146 | <PullRequestHostVideo |
| 147 | key={`${segment.id}:${segment.url}`} |
| 148 | environmentId={environmentId} |
| 149 | cwd={cwd} |
| 150 | url={segment.url} |
| 151 | fetchUrl={hostMedia.fetchUrl} |
| 152 | repository={repository} |
| 153 | /> |
| 154 | ); |
| 155 | } |
| 156 | if (segment.media === "video") { |
| 157 | return ( |
| 158 | <MediaVideoPlayer |
| 159 | key={`${segment.id}:${segment.url}`} |
| 160 | src={segment.url} |
| 161 | originalUrl={segment.url} |
| 162 | label={gt("Pull request video")} |
| 163 | className="w-full" |
| 164 | videoClassName="rounded-lg border border-border/60" |
| 165 | /> |
| 166 | ); |
| 167 | } |
| 168 | return ( |
| 169 | // Desktop converts blocked _blank navigation to openExternal. |
| 170 | <a |
| 171 | href={segment.url} |
| 172 | rel="noreferrer noopener" |
| 173 | target="_blank" |
| 174 | className="flex items-center gap-2 rounded-lg border border-border/60 bg-muted/30 px-3 py-2 text-sm hover:bg-muted/60" |
| 175 | > |
| 176 | <PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" /> |
| 177 | <span className="min-w-0 flex-1 truncate"> |
| 178 | <T> |
| 179 | Open attachment on{" "} |
| 180 | <Var>{hostMedia?.definition.label ?? new URL(segment.url).hostname}</Var> |
| 181 | </T> |
| 182 | </span> |
| 183 | <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" /> |
| 184 | </a> |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | export function PullRequestMarkdown({ |
| 189 | text, |
| 190 | cwd, |
| 191 | environmentId, |
| 192 | threadRef, |
| 193 | lineBreaks, |
| 194 | className, |
| 195 | onTaskListChange, |
| 196 | }: { |
| 197 | text: string; |
| 198 | cwd: string; |
| 199 | environmentId: EnvironmentId; |
| 200 | threadRef?: ScopedThreadRef | null; |
| 201 | lineBreaks?: boolean; |
| 202 | className?: string; |
| 203 | /** `markerOffset` is into `text`. Checkboxes stay read-only without it. */ |
| 204 | onTaskListChange?: ((input: { markerOffset: number; checked: boolean }) => void) | undefined; |
| 205 | }) { |
| 206 | const segments = splitPullRequestBody(text); |
| 207 | const context = useContext(PullRequestMarkdownContext); |
| 208 | const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined; |
| 209 | return ( |
| 210 | <div |
| 211 | className={cn( |
| 212 | "space-y-3 [&_[data-markdown-details]]:border-0 [&_[data-markdown-details-summary]]:text-foreground/80 [&_[data-markdown-details-summary]>svg]:text-muted-foreground/60", |
| 213 | className, |
| 214 | )} |
| 215 | data-image-gallery |
| 216 | > |
| 217 | {segments.map((segment) => ( |
| 218 | <PullRequestBodySegmentView |
| 219 | key={segment.id} |
| 220 | segment={segment} |
| 221 | cwd={cwd} |
| 222 | environmentId={environmentId} |
| 223 | threadRef={resolvedThreadRef} |
| 224 | lineBreaks={lineBreaks ?? context?.lineBreaks ?? false} |
| 225 | onTaskListChange={onTaskListChange} |
| 226 | /> |
| 227 | ))} |
| 228 | </div> |
| 229 | ); |
| 230 | } |