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

PullRequestMarkdown.tsx

135 lines4.8 KB

1import { ExternalLinkIcon, PaperclipIcon } from "lucide-react";
2import { markdownImageSourceFragment } from "@t3tools/client-runtime/markdown-images";
3import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
4import type { AssetResource, EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
5import { createContext, useContext } from "react";
6import type { Options as ReactMarkdownOptions } from "react-markdown";
7​
8import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
9import { cn } from "~/lib/utils";
10import { PULL_REQUESTS_PANEL_REF } from "~/rightPanelStore";
11​
12import ChatMarkdown from "../ChatMarkdown";
13import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
14import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestMarkdown.logic";
15​
16export const PullRequestMarkdownContext = createContext<{
17 repositoryUrl: string | null;
18 threadRef: ScopedThreadRef | null;
19} | null>(null);
20​
21function PullRequestGitHubVideo({
22 environmentId,
23 cwd,
24 url,
25 fetchUrl,
26}: {
27 environmentId: EnvironmentId;
28 cwd: string;
29 url: string;
30 fetchUrl: string;
31}) {
32 const resource: AssetResource = { _tag: "github-media", cwd, url: fetchUrl };
33 const assetUrl = useAssetUrlState(environmentId, resource);
34 const refreshAssetUrl = useAssetUrlRefresh(environmentId, resource);
35 const src =
36 assetUrl._tag === "Success" ? assetUrl.url : assetUrl._tag === "Failure" ? fetchUrl : null;
37 return (
38 <MediaVideoPlayer
39 src={src === null ? null : src + markdownImageSourceFragment(url)}
40 originalUrl={url}
41 label="Pull request video"
42 className="w-full"
43 videoClassName="rounded-lg border border-border/60"
44 onRetry={refreshAssetUrl}
45 />
46 );
48​
49export function PullRequestMarkdown({
50 text,
51 cwd,
52 environmentId,
53 threadRef,
54 className,
55}: {
56 text: string;
57 cwd: string;
58 environmentId: EnvironmentId;
59 threadRef?: ScopedThreadRef | null;
60 className?: string;
61}) {
62 const segments = splitPullRequestBody(text);
63 const context = useContext(PullRequestMarkdownContext);
64 const repositoryUrl = context?.repositoryUrl;
65 const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined;
66 const extraRemarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]> = repositoryUrl
67 ? [[remarkPullRequestAutolinks, { repositoryUrl }]]
68 : [];
69 return (
70 <div
71 className={cn(
72 "space-y-3 [&_[data-markdown-details]]:border-0 [&_[data-markdown-details-summary]]:text-foreground/80 [&_[data-markdown-details-summary]>svg]:text-muted-foreground/60",
73 className,
74 )}
75 data-image-gallery
76 >
77 {segments.map((segment) => {
78 if (segment.kind === "markdown" || segment.media === "image") {
79 return (
80 <ChatMarkdown
81 key={segment.id}
82 text={segment.kind === "markdown" ? segment.text : `![](<${segment.url}>)`}
83 cwd={cwd}
84 threadRef={resolvedThreadRef}
85 pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
86 environmentId={environmentId}
87 extraRemarkPlugins={extraRemarkPlugins}
88 githubMedia
89 />
90 );
91 }
92 const githubMediaUrl = segment.media === "video" ? githubMediaFetchUrl(segment.url) : null;
93 if (githubMediaUrl !== null) {
94 return (
95 <PullRequestGitHubVideo
96 key={`${segment.id}:${segment.url}`}
97 environmentId={environmentId}
98 cwd={cwd}
99 url={segment.url}
100 fetchUrl={githubMediaUrl}
101 />
102 );
103 }
104 if (segment.media === "video") {
105 return (
106 <MediaVideoPlayer
107 key={`${segment.id}:${segment.url}`}
108 src={segment.url}
109 originalUrl={segment.url}
110 label="Pull request video"
111 className="w-full"
112 videoClassName="rounded-lg border border-border/60"
113 />
114 );
115 }
116 return (
117 // A plain anchor rather than the page's openExternal button: the desktop window
118 // turns a blocked _blank into openExternal itself, and in a browser tab — where
119 // there is no shell to call — this is the only one of the two that goes anywhere.
120 <a
121 key={segment.id}
122 href={segment.url}
123 rel="noreferrer noopener"
124 target="_blank"
125 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"
126 >
127 <PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
128 <span className="min-w-0 flex-1 truncate">Open attachment on GitHub</span>
129 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
130 </a>
131 );
132 })}
133 </div>
134 );