Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestMarkdown.tsx

94 lines3.5 KB

1import { ExternalLinkIcon, PaperclipIcon } from "lucide-react";
2import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
3import { createContext, useContext, useMemo } from "react";
4import type { Options as ReactMarkdownOptions } from "react-markdown";
5​
6import { cn } from "~/lib/utils";
7import { PULL_REQUESTS_PANEL_REF } from "~/rightPanelStore";
8​
9import ChatMarkdown from "../ChatMarkdown";
10import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
11import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestMarkdown.logic";
12​
13export const PullRequestMarkdownContext = createContext<{
14 repositoryUrl: string | null;
15 threadRef: ScopedThreadRef | null;
16} | null>(null);
17​
18/** Renders PR uploads inline, with retry and an original link when video playback fails. */
19export function PullRequestMarkdown({
20 text,
21 cwd,
22 environmentId,
23 threadRef,
24 className,
25}: {
26 text: string;
27 cwd: string;
28 environmentId: EnvironmentId;
29 /** Thread the body is shown beside, so its links can open in that thread's in-app browser. */
30 threadRef?: ScopedThreadRef | null;
31 className?: string;
32}) {
33 const segments = splitPullRequestBody(text);
34 const context = useContext(PullRequestMarkdownContext);
35 const repositoryUrl = context?.repositoryUrl;
36 const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined;
37 const extraRemarkPlugins = useMemo<NonNullable<ReactMarkdownOptions["remarkPlugins"]>>(
38 () => (repositoryUrl ? [[remarkPullRequestAutolinks, { repositoryUrl }]] : []),
39 [repositoryUrl],
40 );
41 return (
42 <div
43 className={cn(
44 "space-y-3 [&_[data-markdown-details]]:border-0 [&_[data-markdown-details-summary]]:text-foreground/80 [&_[data-markdown-details-summary]>svg]:text-muted-foreground/60",
45 className,
46 )}
47 data-image-gallery
48 >
49 {segments.map((segment) => {
50 if (segment.kind === "markdown") {
51 return (
52 <ChatMarkdown
53 key={segment.id}
54 text={segment.text}
55 cwd={cwd}
56 threadRef={resolvedThreadRef}
57 pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
58 environmentId={environmentId}
59 extraRemarkPlugins={extraRemarkPlugins}
60 />
61 );
62 }
63 if (segment.media === "video") {
64 return (
65 <MediaVideoPlayer
66 key={`${segment.id}:${segment.url}`}
67 src={segment.url}
68 originalUrl={segment.url}
69 label="Pull request video"
70 className="w-full"
71 videoClassName="rounded-lg border border-border/60"
72 />
73 );
74 }
75 return (
76 // A plain anchor rather than the page's openExternal button: the desktop window
77 // turns a blocked _blank into openExternal itself, and in a browser tab — where
78 // there is no shell to call — this is the only one of the two that goes anywhere.
79 <a
80 key={segment.id}
81 href={segment.url}
82 rel="noreferrer noopener"
83 target="_blank"
84 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"
85 >
86 <PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
87 <span className="min-w-0 flex-1 truncate">Open attachment on GitHub</span>
88 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
89 </a>
90 );
91 })}
92 </div>
93 );