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

PullRequestMarkdown.tsx

88 lines3.3 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 className={cn("space-y-3", className)} data-image-gallery>
43 {segments.map((segment) => {
44 if (segment.kind === "markdown") {
45 return (
46 <ChatMarkdown
47 key={segment.id}
48 text={segment.text}
49 cwd={cwd}
50 threadRef={resolvedThreadRef}
51 pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
52 environmentId={environmentId}
53 extraRemarkPlugins={extraRemarkPlugins}
54 />
55 );
56 }
57 if (segment.media === "video") {
58 return (
59 <MediaVideoPlayer
60 key={`${segment.id}:${segment.url}`}
61 src={segment.url}
62 originalUrl={segment.url}
63 label="Pull request video"
64 className="w-full"
65 videoClassName="rounded-lg border border-border/60"
66 />
67 );
68 }
69 return (
70 // A plain anchor rather than the page's openExternal button: the desktop window
71 // turns a blocked _blank into openExternal itself, and in a browser tab — where
72 // there is no shell to call — this is the only one of the two that goes anywhere.
73 <a
74 key={segment.id}
75 href={segment.url}
76 rel="noreferrer noopener"
77 target="_blank"
78 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"
79 >
80 <PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
81 <span className="min-w-0 flex-1 truncate">Open attachment on GitHub</span>
82 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
83 </a>
84 );
85 })}
86 </div>
87 );