Skip to content

Commit cfaf032

Browse files
fix(web): PR bodies keep before/after video tables intact
1 parent 36d4d71 commit cfaf032

6 files changed

Lines changed: 280 additions & 71 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx‎

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { splitPullRequestBody } from "@t3tools/client-runtime/pull-request-markdown";
1+
import {
2+
splitPullRequestBody,
3+
type PullRequestBodySegment,
4+
} from "@t3tools/client-runtime/pull-request-markdown";
25
import type { ReactNode } from "react";
36
import { View } from "react-native";
47
import { useProject } from "../../state/entities";
@@ -96,19 +99,55 @@ export function PullRequestMarkdown(props: {
9699
readonly imageGroupMarkdown?: string;
97100
}) {
98101
const segments = useMemo(() => splitPullRequestBody(props.markdown), [props.markdown]);
102+
const imageGroupMarkdown = props.imageGroupMarkdown ?? props.markdown;
103+
return (
104+
<View style={{ gap: 12 }}>
105+
{segments.map((segment) => (
106+
<PullRequestBodySegmentView
107+
key={segment.id}
108+
segment={segment}
109+
imageGroupMarkdown={imageGroupMarkdown}
110+
/>
111+
))}
112+
</View>
113+
);
114+
}
115+
116+
function PullRequestBodySegmentView(props: {
117+
readonly segment: PullRequestBodySegment;
118+
readonly imageGroupMarkdown: string;
119+
}) {
120+
const { segment } = props;
121+
if (segment.kind === "markdown") {
122+
return (
123+
<PullRequestMarkdownContent
124+
markdown={segment.text}
125+
imageGroupMarkdown={props.imageGroupMarkdown}
126+
/>
127+
);
128+
}
129+
if (segment.kind === "attachment") {
130+
return <PullRequestMedia url={segment.url} kind={segment.media} />;
131+
}
99132
return (
100133
<View style={{ gap: 12 }}>
101-
{segments.map((segment) =>
102-
segment.kind === "markdown" ? (
103-
<PullRequestMarkdownContent
104-
key={segment.id}
105-
markdown={segment.text}
106-
imageGroupMarkdown={props.imageGroupMarkdown ?? props.markdown}
107-
/>
108-
) : (
109-
<PullRequestMedia key={segment.id} url={segment.url} kind={segment.media} />
110-
),
111-
)}
134+
{segment.rows.map((cells, rowIndex) => (
135+
// oxlint-disable-next-line react/no-array-index-key -- rows have no identity besides position
136+
<View key={rowIndex} style={{ flexDirection: "row", gap: 12 }}>
137+
{cells.map((cell, cellIndex) => (
138+
// oxlint-disable-next-line react/no-array-index-key -- cells have no identity besides position
139+
<View key={cellIndex} style={{ flex: 1, minWidth: 0, gap: 12 }}>
140+
{cell.segments.map((cellSegment) => (
141+
<PullRequestBodySegmentView
142+
key={cellSegment.id}
143+
segment={cellSegment}
144+
imageGroupMarkdown={props.imageGroupMarkdown}
145+
/>
146+
))}
147+
</View>
148+
))}
149+
</View>
150+
))}
112151
</View>
113152
);
114153
}

‎apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@ describe("PR attachments", () => {
2626
},
2727
]);
2828
});
29+
30+
it("collects videos from a before/after table", () => {
31+
const media = splitPullRequestBody(
32+
"<table>\n<tr><th>Before</th><th>After</th></tr>\n<tr><td>\n\nhttps://drop.example.com/before.mp4\n\n</td><td>\n\nhttps://drop.example.com/after.mp4\n\n</td></tr>\n</table>",
33+
);
34+
expect(
35+
pullRequestDescriptionContent({ type: "root" } as MarkdownNode, media).attachments,
36+
).toEqual([
37+
{ uri: "https://drop.example.com/before.mp4", caption: "Video", kind: "video" },
38+
{ uri: "https://drop.example.com/after.mp4", caption: "Video", kind: "video" },
39+
]);
40+
});
2941
});

‎apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export function pullRequestDescriptionContent(
2828
if (node.type === "break") text.push(" ");
2929
};
3030
visit(document);
31-
for (const segment of media) {
31+
const collect = (segment: PullRequestBodySegment) => {
32+
if (segment.kind === "table") {
33+
segment.rows.flat().forEach((cell) => cell.segments.forEach(collect));
34+
return;
35+
}
3236
if (segment.kind === "attachment" && !attachments.has(segment.url)) {
3337
const name = new URL(segment.url).pathname.split("/").pop();
3438
attachments.set(segment.url, {
@@ -37,6 +41,7 @@ export function pullRequestDescriptionContent(
3741
kind: segment.media,
3842
});
3943
}
40-
}
44+
};
45+
media.forEach(collect);
4146
return { preview: text.join("").trim(), attachments: [...attachments.values()] };
4247
}

‎apps/web/src/components/pullRequest/PullRequestMarkdown.tsx‎

Lines changed: 115 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import { PULL_REQUESTS_PANEL_REF } from "~/rightPanelStore";
1919

2020
import ChatMarkdown from "../ChatMarkdown";
2121
import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
22-
import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestMarkdown.logic";
22+
import {
23+
remarkPullRequestAutolinks,
24+
splitPullRequestBody,
25+
type PullRequestBodySegment,
26+
} from "./pullRequestMarkdown.logic";
2327

2428
export const PullRequestMarkdownContext = createContext<{
2529
autolinks: ((text: string) => ReadonlyArray<ReferenceAutolink>) | null;
@@ -54,6 +58,106 @@ function PullRequestHostVideo({
5458
);
5559
}
5660

61+
function PullRequestBodySegmentView({
62+
segment,
63+
cwd,
64+
environmentId,
65+
threadRef,
66+
extraRemarkPlugins,
67+
}: {
68+
segment: PullRequestBodySegment;
69+
cwd: string;
70+
environmentId: EnvironmentId;
71+
threadRef: ScopedThreadRef | undefined;
72+
extraRemarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
73+
}) {
74+
if (segment.kind === "table") {
75+
return (
76+
<div className="space-y-3">
77+
{segment.rows.map((cells, rowIndex) => (
78+
<div
79+
// oxlint-disable-next-line react/no-array-index-key -- rows have no identity besides position
80+
key={rowIndex}
81+
className="grid gap-3"
82+
style={{ gridTemplateColumns: `repeat(${cells.length}, minmax(0, 1fr))` }}
83+
>
84+
{cells.map((cell, cellIndex) => (
85+
<div
86+
// oxlint-disable-next-line react/no-array-index-key -- cells have no identity besides position
87+
key={cellIndex}
88+
className={cn("min-w-0 space-y-3", cell.header && "font-medium")}
89+
>
90+
{cell.segments.map((cellSegment) => (
91+
<PullRequestBodySegmentView
92+
key={cellSegment.id}
93+
segment={cellSegment}
94+
cwd={cwd}
95+
environmentId={environmentId}
96+
threadRef={threadRef}
97+
extraRemarkPlugins={extraRemarkPlugins}
98+
/>
99+
))}
100+
</div>
101+
))}
102+
</div>
103+
))}
104+
</div>
105+
);
106+
}
107+
if (segment.kind === "markdown" || segment.media === "image") {
108+
return (
109+
<ChatMarkdown
110+
text={segment.kind === "markdown" ? segment.text : `![](<${segment.url}>)`}
111+
cwd={cwd}
112+
threadRef={threadRef}
113+
pullRequestPanelRef={threadRef ?? PULL_REQUESTS_PANEL_REF}
114+
environmentId={environmentId}
115+
extraRemarkPlugins={extraRemarkPlugins}
116+
sourceControlMedia
117+
/>
118+
);
119+
}
120+
const hostMedia = sourceControlClients.mediaUrl(segment.url);
121+
if (segment.media === "video" && hostMedia !== null) {
122+
return (
123+
<PullRequestHostVideo
124+
key={`${segment.id}:${segment.url}`}
125+
environmentId={environmentId}
126+
cwd={cwd}
127+
url={segment.url}
128+
fetchUrl={hostMedia.fetchUrl}
129+
/>
130+
);
131+
}
132+
if (segment.media === "video") {
133+
return (
134+
<MediaVideoPlayer
135+
key={`${segment.id}:${segment.url}`}
136+
src={segment.url}
137+
originalUrl={segment.url}
138+
label="Pull request video"
139+
className="w-full"
140+
videoClassName="rounded-lg border border-border/60"
141+
/>
142+
);
143+
}
144+
return (
145+
// Desktop converts blocked _blank navigation to openExternal.
146+
<a
147+
href={segment.url}
148+
rel="noreferrer noopener"
149+
target="_blank"
150+
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"
151+
>
152+
<PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
153+
<span className="min-w-0 flex-1 truncate">
154+
Open attachment on {hostMedia?.definition.label ?? new URL(segment.url).hostname}
155+
</span>
156+
<ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
157+
</a>
158+
);
159+
}
160+
57161
export function PullRequestMarkdown({
58162
text,
59163
cwd,
@@ -82,62 +186,16 @@ export function PullRequestMarkdown({
82186
)}
83187
data-image-gallery
84188
>
85-
{segments.map((segment) => {
86-
if (segment.kind === "markdown" || segment.media === "image") {
87-
return (
88-
<ChatMarkdown
89-
key={segment.id}
90-
text={segment.kind === "markdown" ? segment.text : `![](<${segment.url}>)`}
91-
cwd={cwd}
92-
threadRef={resolvedThreadRef}
93-
pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
94-
environmentId={environmentId}
95-
extraRemarkPlugins={extraRemarkPlugins}
96-
sourceControlMedia
97-
/>
98-
);
99-
}
100-
const hostMedia = sourceControlClients.mediaUrl(segment.url);
101-
if (segment.media === "video" && hostMedia !== null) {
102-
return (
103-
<PullRequestHostVideo
104-
key={`${segment.id}:${segment.url}`}
105-
environmentId={environmentId}
106-
cwd={cwd}
107-
url={segment.url}
108-
fetchUrl={hostMedia.fetchUrl}
109-
/>
110-
);
111-
}
112-
if (segment.media === "video") {
113-
return (
114-
<MediaVideoPlayer
115-
key={`${segment.id}:${segment.url}`}
116-
src={segment.url}
117-
originalUrl={segment.url}
118-
label="Pull request video"
119-
className="w-full"
120-
videoClassName="rounded-lg border border-border/60"
121-
/>
122-
);
123-
}
124-
return (
125-
// Desktop converts blocked _blank navigation to openExternal.
126-
<a
127-
key={segment.id}
128-
href={segment.url}
129-
rel="noreferrer noopener"
130-
target="_blank"
131-
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"
132-
>
133-
<PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
134-
<span className="min-w-0 flex-1 truncate">
135-
Open attachment on {hostMedia?.definition.label ?? new URL(segment.url).hostname}
136-
</span>
137-
<ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
138-
</a>
139-
);
140-
})}
189+
{segments.map((segment) => (
190+
<PullRequestBodySegmentView
191+
key={segment.id}
192+
segment={segment}
193+
cwd={cwd}
194+
environmentId={environmentId}
195+
threadRef={resolvedThreadRef}
196+
extraRemarkPlugins={extraRemarkPlugins}
197+
/>
198+
))}
141199
</div>
142200
);
143201
}

‎packages/client-runtime/src/pullRequestMarkdown.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,50 @@ describe("media on custom hosts", () => {
122122
{ id: "markdown:0", kind: "markdown", text: body },
123123
]);
124124
});
125+
it("keeps a before/after table of videos together, splitting each cell", () => {
126+
const before =
127+
"https://github.com/user-attachments/assets/c69c7c1c-ef2f-47dd-97e8-fe42e40712eb";
128+
const after = "https://github.com/user-attachments/assets/1867b6ec-4f88-4248-b2df-6b0f9808105b";
129+
expect(
130+
splitPullRequestBody(
131+
`Intro\n\n<table>\n<tr><th>Before</th><th>After</th></tr>\n<tr>\n<td>\n\n${before}\n\n</td>\n<td>\n\n${after}\n\n</td>\n</tr>\n</table>\n\nOutro`,
132+
),
133+
).toEqual([
134+
{ id: "markdown:0", kind: "markdown", text: "Intro" },
135+
{
136+
id: "table:1",
137+
kind: "table",
138+
rows: [
139+
[
140+
{ header: true, segments: [{ id: "markdown:0", kind: "markdown", text: "Before" }] },
141+
{ header: true, segments: [{ id: "markdown:0", kind: "markdown", text: "After" }] },
142+
],
143+
[
144+
{
145+
header: false,
146+
segments: [{ id: "attachment:0", kind: "attachment", url: before, media: "video" }],
147+
},
148+
{
149+
header: false,
150+
segments: [{ id: "attachment:0", kind: "attachment", url: after, media: "video" }],
151+
},
152+
],
153+
],
154+
},
155+
{ id: "markdown:2", kind: "markdown", text: "Outro" },
156+
]);
157+
});
158+
159+
it("leaves a table without media, or an unclosed one, as markdown", () => {
160+
const plain = "<table>\n<tr><td>a</td><td>b</td></tr>\n</table>";
161+
expect(splitPullRequestBody(plain)).toEqual([
162+
{ id: "markdown:0", kind: "markdown", text: plain },
163+
]);
164+
expect(splitPullRequestBody("<table>\n<tr><td>\n\nhttps://example.com/a.mp4")).toEqual([
165+
{ id: "markdown:0", kind: "markdown", text: "<table>\n<tr><td>" },
166+
{ id: "attachment:1", kind: "attachment", url: "https://example.com/a.mp4", media: "video" },
167+
]);
168+
});
125169
});
126170

127171
describe("change request reference autolinks", () => {

0 commit comments

Comments
 (0)