Open History to see the last commit
1import { ExternalLinkIcon, PaperclipIcon, PlayIcon } from "lucide-react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { createContext, useContext, useMemo } from "react";
4import type { Options as ReactMarkdownOptions } from "react-markdown";
5
6import { cn } from "~/lib/utils";
7
8import ChatMarkdown from "../ChatMarkdown";
9import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestMarkdown.logic";
10
11export const PullRequestMarkdownContext = createContext<string | null>(null);
12
13/**
14 * A pull request body, rendered with the app's markdown renderer plus a card for each upload
15 * embedded in it, which that renderer drops on the floor.
16 *
17 * These upload URLs do not identify the media format. The card links to GitHub, where the
18 * original upload can be opened or downloaded even when its codec cannot play in the client.
19 */
20export function PullRequestMarkdown({
21 text,
22 cwd,
23 environmentId,
24 className,
25}: {
26 text: string;
27 cwd: string;
28 environmentId: EnvironmentId;
29 className?: string;
30}) {
31 const segments = splitPullRequestBody(text);
32 const repositoryUrl = useContext(PullRequestMarkdownContext);
33 const extraRemarkPlugins = useMemo<NonNullable<ReactMarkdownOptions["remarkPlugins"]>>(
34 () => (repositoryUrl ? [[remarkPullRequestAutolinks, { repositoryUrl }]] : []),
35 [repositoryUrl],
36 );
37 return (
38 <div className={cn("space-y-3", className)}>
39 {segments.map((segment) => {
40 if (segment.kind === "markdown") {
41 return (
42 <ChatMarkdown
43 key={segment.id}
44 text={segment.text}
45 cwd={cwd}
46 environmentId={environmentId}
47 extraRemarkPlugins={extraRemarkPlugins}
48 />
49 );
50 }
51 const isVideo = segment.media === "video";
52 const Icon = isVideo ? PlayIcon : PaperclipIcon;
53 return (
54 // A plain anchor rather than the page's openExternal button: the desktop window
55 // turns a blocked _blank into openExternal itself, and in a browser tab — where
56 // there is no shell to call — this is the only one of the two that goes anywhere.
57 <a
58 key={segment.id}
59 href={segment.url}
60 rel="noreferrer noopener"
61 target="_blank"
62 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"
63 >
64 <Icon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
65 <span className="min-w-0 flex-1 truncate">
66 {isVideo ? "Play video on GitHub" : "Open attachment on GitHub"}
67 </span>
68 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
69 </a>
70 );
71 })}
72 </div>
73 );
74}