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