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 * The card links out instead of playing in place. GitHub serves the file as uploaded — Mac
18 * recordings are `video/quicktime`, which no Chromium decodes — and the desktop window's
19 * `media-src` allows only `'self'`, the app scheme and `blob:`, so a remote source is refused
20 * before a byte is fetched. Opening the host works for every format.
21 */
22export function PullRequestMarkdown({
23 text,
24 cwd,
25 environmentId,
26 className,
27}: {
28 text: string;
29 cwd: string;
30 environmentId: EnvironmentId;
31 className?: string;
32}) {
33 const segments = splitPullRequestBody(text);
34 const repositoryUrl = useContext(PullRequestMarkdownContext);
35 const extraRemarkPlugins = useMemo<NonNullable<ReactMarkdownOptions["remarkPlugins"]>>(
36 () => (repositoryUrl ? [[remarkPullRequestAutolinks, { repositoryUrl }]] : []),
37 [repositoryUrl],
38 );
39 return (
40 <div className={cn("space-y-3", className)}>
41 {segments.map((segment) => {
42 if (segment.kind === "markdown") {
43 return (
44 <ChatMarkdown
45 key={segment.id}
46 text={segment.text}
47 cwd={cwd}
48 environmentId={environmentId}
49 extraRemarkPlugins={extraRemarkPlugins}
50 />
51 );
52 }
53 const isVideo = segment.media === "video";
54 const Icon = isVideo ? PlayIcon : PaperclipIcon;
55 return (
56 // A plain anchor rather than the page's openExternal button: the desktop window
57 // turns a blocked _blank into openExternal itself, and in a browser tab — where
58 // there is no shell to call — this is the only one of the two that goes anywhere.
59 <a
60 key={segment.id}
61 href={segment.url}
62 rel="noreferrer noopener"
63 target="_blank"
64 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"
65 >
66 <Icon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
67 <span className="min-w-0 flex-1 truncate">
68 {isVideo ? "Play video on GitHub" : "Open attachment on GitHub"}
69 </span>
70 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
71 </a>
72 );
73 })}
74 </div>
75 );
76}