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

PullRequestMarkdown.tsx

78 lines3.0 KB

1import { ExternalLinkIcon, PaperclipIcon, PlayIcon } 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";
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 threadRef,
25 className,
26}: {
27 text: string;
28 cwd: string;
29 environmentId: EnvironmentId;
30 /** Thread the body is shown beside, so its links can open in that thread's in-app browser. */
31 threadRef?: ScopedThreadRef | null;
32 className?: string;
33}) {
34 const segments = splitPullRequestBody(text);
35 const repositoryUrl = useContext(PullRequestMarkdownContext);
36 const extraRemarkPlugins = useMemo<NonNullable<ReactMarkdownOptions["remarkPlugins"]>>(
37 () => (repositoryUrl ? [[remarkPullRequestAutolinks, { repositoryUrl }]] : []),
38 [repositoryUrl],
39 );
40 return (
41 <div className={cn("space-y-3", className)}>
42 {segments.map((segment) => {
43 if (segment.kind === "markdown") {
44 return (
45 <ChatMarkdown
46 key={segment.id}
47 text={segment.text}
48 cwd={cwd}
49 threadRef={threadRef ?? undefined}
50 environmentId={environmentId}
51 extraRemarkPlugins={extraRemarkPlugins}
52 />
53 );
54 }
55 const isVideo = segment.media === "video";
56 const Icon = isVideo ? PlayIcon : PaperclipIcon;
57 return (
58 // A plain anchor rather than the page's openExternal button: the desktop window
59 // turns a blocked _blank into openExternal itself, and in a browser tab — where
60 // there is no shell to call — this is the only one of the two that goes anywhere.
61 <a
62 key={segment.id}
63 href={segment.url}
64 rel="noreferrer noopener"
65 target="_blank"
66 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"
67 >
68 <Icon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
69 <span className="min-w-0 flex-1 truncate">
70 {isVideo ? "Play video on GitHub" : "Open attachment on GitHub"}
71 </span>
72 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
73 </a>
74 );
75 })}
76 </div>
77 );