The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | splitPullRequestBody, |
| 3 | type PullRequestBodySegment, |
| 4 | } from "@t3tools/client-runtime/pull-request-markdown"; |
| 5 | import { markdownTaskMarkerOffset } from "@t3tools/shared/markdownTaskList"; |
| 6 | import type { ReactNode } from "react"; |
| 7 | import { View } from "react-native"; |
| 8 | import { useProject } from "../../state/entities"; |
| 9 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 10 | import { |
| 11 | PullRequestMedia, |
| 12 | PullRequestMediaContext, |
| 13 | usePullRequestMediaUrl, |
| 14 | } from "./PullRequestMedia"; |
| 15 | import { videoMimeType } from "@t3tools/shared/video"; |
| 16 | import { createContext, useContext, useMemo, useState } from "react"; |
| 17 | import { |
| 18 | changeRequestReferenceAutolinker, |
| 19 | resolveChangeRequestMediaUrl, |
| 20 | } from "@t3tools/client-runtime/pull-request-markdown"; |
| 21 | import { |
| 22 | sourceControlClients, |
| 23 | type ReferenceAutolink, |
| 24 | } from "@t3tools/client-runtime/source-control-clients"; |
| 25 | import { nativePullRequestMarkdown } from "./pullRequestAutolinks"; |
| 26 | |
| 27 | import { useOpenPullRequest } from "../../native/useOpenPullRequest"; |
| 28 | import { SelectableMarkdownText } from "../../native/SelectableMarkdownText"; |
| 29 | import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview"; |
| 30 | import { markdownImageSources } from "../../lib/markdownImageSources"; |
| 31 | import { FilePreviewGallery, useFilePreviewGallery } from "../../components/ui/FilePreviewGallery"; |
| 32 | import { pullRequestImageSource } from "./pullRequestMediaSource"; |
| 33 | import { |
| 34 | FilePreviewModal, |
| 35 | type FilePreviewSource, |
| 36 | type FilePreviewItem, |
| 37 | } from "../../components/ui/FilePreviewModal"; |
| 38 | import { ThreadMarkdownImageView } from "../threads/ThreadMarkdownImage"; |
| 39 | import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText"; |
| 40 | import type { SelectableMarkdownTextProps } from "../../native/SelectableMarkdownText.types"; |
| 41 | |
| 42 | const AutolinksContext = createContext<((text: string) => ReadonlyArray<ReferenceAutolink>) | null>( |
| 43 | null, |
| 44 | ); |
| 45 | const LineBreaksContext = createContext(false); |
| 46 | const GithubEmojiContext = createContext(false); |
| 47 | const ChangeRequestUrlContext = createContext<string | null>(null); |
| 48 | |
| 49 | export function PullRequestMarkdownRepository(props: { |
| 50 | /** The change request the markdown belongs to, whose host decides what its text links. */ |
| 51 | readonly changeRequest: { readonly provider: string; readonly url: string } | null | undefined; |
| 52 | readonly params: PullRequestParams; |
| 53 | readonly children: ReactNode; |
| 54 | }) { |
| 55 | const provider = props.changeRequest?.provider; |
| 56 | const url = props.changeRequest?.url; |
| 57 | const autolinks = |
| 58 | provider === undefined || url === undefined |
| 59 | ? null |
| 60 | : changeRequestReferenceAutolinker({ provider, url }); |
| 61 | const lineBreaks = sourceControlClients.find(provider ?? "")?.hardLineBreaks ?? false; |
| 62 | const target = pullRequestTarget(props.params); |
| 63 | const project = useProject({ |
| 64 | environmentId: target.environmentId, |
| 65 | projectId: target.input.projectId, |
| 66 | }); |
| 67 | const cwd = project?.workspaceRoot; |
| 68 | const media = cwd |
| 69 | ? { environmentId: target.environmentId, cwd, repository: target.input.repository } |
| 70 | : null; |
| 71 | return ( |
| 72 | <AutolinksContext value={autolinks}> |
| 73 | <GithubEmojiContext value={provider === "github"}> |
| 74 | <LineBreaksContext value={lineBreaks}> |
| 75 | <ChangeRequestUrlContext value={url ?? null}> |
| 76 | <PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext> |
| 77 | </ChangeRequestUrlContext> |
| 78 | </LineBreaksContext> |
| 79 | </GithubEmojiContext> |
| 80 | </AutolinksContext> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | function MarkdownImage(props: { |
| 85 | readonly href: string; |
| 86 | readonly alt: string | null; |
| 87 | readonly onPreview: (source: FilePreviewSource) => void; |
| 88 | }) { |
| 89 | const media = usePullRequestMediaUrl(props.href); |
| 90 | const context = useContext(PullRequestMediaContext); |
| 91 | const withGallery = useFilePreviewGallery(); |
| 92 | return ( |
| 93 | <ThreadMarkdownImageView |
| 94 | uri={media.uri} |
| 95 | sourceKey={props.href} |
| 96 | cacheKey={media.cacheKey} |
| 97 | unavailable={media.unavailable || !/^https?:\/\//i.test(props.href)} |
| 98 | alt={props.alt} |
| 99 | onPressPreview={(source) => |
| 100 | props.onPreview( |
| 101 | withGallery({ |
| 102 | ...pullRequestImageSource(props.href, context), |
| 103 | name: source.name, |
| 104 | sourceIdentifier: source.sourceIdentifier, |
| 105 | }), |
| 106 | ) |
| 107 | } |
| 108 | /> |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | export function PullRequestMarkdown(props: { |
| 113 | readonly markdown: string; |
| 114 | readonly imageGroupMarkdown?: string; |
| 115 | readonly lineBreaks?: boolean; |
| 116 | /** |
| 117 | * `markerOffset` is into `markdown`. Returning false, or a tap that can't be traced back to |
| 118 | * the source, undoes the tick the renderer already drew. |
| 119 | */ |
| 120 | readonly onTaskListChange?: (input: { |
| 121 | readonly markerOffset: number; |
| 122 | readonly checked: boolean; |
| 123 | }) => boolean; |
| 124 | }) { |
| 125 | const mediaContext = useContext(PullRequestMediaContext); |
| 126 | const changeRequestUrl = useContext(ChangeRequestUrlContext); |
| 127 | const segments = useMemo(() => splitPullRequestBody(props.markdown), [props.markdown]); |
| 128 | const [undoneTicks, setUndoneTicks] = useState(0); |
| 129 | const gallery: FilePreviewItem[] = markdownImageSources( |
| 130 | props.imageGroupMarkdown ?? props.markdown, |
| 131 | ) |
| 132 | .map((href) => (changeRequestUrl ? resolveChangeRequestMediaUrl(href, changeRequestUrl) : href)) |
| 133 | .filter( |
| 134 | (uri) => |
| 135 | /^https?:\/\//i.test(uri) && |
| 136 | videoMimeType({ name: uri.split(/[?#]/)[0] ?? uri, mimeType: "" }) === null, |
| 137 | ) |
| 138 | .map((uri) => pullRequestImageSource(uri, mediaContext)); |
| 139 | const segmentProps = { |
| 140 | gallery, |
| 141 | lineBreaks: props.lineBreaks, |
| 142 | undoneTicks, |
| 143 | onTaskListChange: props.onTaskListChange, |
| 144 | onTaskListUndone: () => setUndoneTicks((count) => count + 1), |
| 145 | }; |
| 146 | return ( |
| 147 | <View style={{ gap: 12 }}> |
| 148 | {segments.map((segment) => ( |
| 149 | <PullRequestBodySegmentView key={segment.id} segment={segment} {...segmentProps} /> |
| 150 | ))} |
| 151 | </View> |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | function PullRequestBodySegmentView(props: { |
| 156 | readonly segment: PullRequestBodySegment; |
| 157 | readonly gallery: FilePreviewItem[]; |
| 158 | readonly lineBreaks: boolean | undefined; |
| 159 | readonly undoneTicks: number; |
| 160 | readonly onTaskListChange: |
| 161 | | ((input: { readonly markerOffset: number; readonly checked: boolean }) => boolean) |
| 162 | | undefined; |
| 163 | readonly onTaskListUndone: () => void; |
| 164 | }) { |
| 165 | const { segment, onTaskListChange } = props; |
| 166 | const autolinks = useContext(AutolinksContext); |
| 167 | const githubEmoji = useContext(GithubEmojiContext); |
| 168 | if (segment.kind === "markdown") { |
| 169 | const prose = nativePullRequestMarkdown(segment.text, autolinks, githubEmoji); |
| 170 | return ( |
| 171 | <PullRequestMarkdownContent |
| 172 | key={props.undoneTicks} |
| 173 | markdown={prose.markdown} |
| 174 | gallery={props.gallery} |
| 175 | lineBreaks={props.lineBreaks} |
| 176 | onTaskListItemPress={ |
| 177 | onTaskListChange |
| 178 | ? (event) => { |
| 179 | const offset = markdownTaskMarkerOffset( |
| 180 | prose.markdown, |
| 181 | event.index, |
| 182 | event.text, |
| 183 | event.checked, |
| 184 | ); |
| 185 | const kept = |
| 186 | offset !== null && |
| 187 | onTaskListChange({ |
| 188 | markerOffset: segment.sourceOffset + prose.sourceOffset(offset), |
| 189 | checked: event.checked, |
| 190 | }); |
| 191 | if (!kept) props.onTaskListUndone(); |
| 192 | } |
| 193 | : undefined |
| 194 | } |
| 195 | /> |
| 196 | ); |
| 197 | } |
| 198 | if (segment.kind === "attachment") { |
| 199 | return <PullRequestMedia url={segment.url} kind={segment.media} />; |
| 200 | } |
| 201 | return ( |
| 202 | <View style={{ gap: 12 }}> |
| 203 | {segment.rows.map((cells, rowIndex) => ( |
| 204 | // oxlint-disable-next-line react/no-array-index-key -- rows have no identity besides position |
| 205 | <View key={rowIndex} style={{ flexDirection: "row", gap: 12 }}> |
| 206 | {cells.map((cell, cellIndex) => ( |
| 207 | // oxlint-disable-next-line react/no-array-index-key -- cells have no identity besides position |
| 208 | <View key={cellIndex} style={{ flex: 1, minWidth: 0, gap: 12 }}> |
| 209 | {cell.segments.map((cellSegment) => ( |
| 210 | <PullRequestBodySegmentView key={cellSegment.id} {...props} segment={cellSegment} /> |
| 211 | ))} |
| 212 | </View> |
| 213 | ))} |
| 214 | </View> |
| 215 | ))} |
| 216 | </View> |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | function PullRequestMarkdownContent(props: { |
| 221 | readonly markdown: string; |
| 222 | readonly gallery: FilePreviewItem[]; |
| 223 | readonly lineBreaks: boolean | undefined; |
| 224 | readonly onTaskListItemPress?: SelectableMarkdownTextProps["onTaskListItemPress"]; |
| 225 | }) { |
| 226 | const hostLineBreaks = useContext(LineBreaksContext); |
| 227 | const mediaContext = useContext(PullRequestMediaContext); |
| 228 | const changeRequestUrl = useContext(ChangeRequestUrlContext); |
| 229 | const resolveMediaUrl = (href: string) => |
| 230 | changeRequestUrl ? resolveChangeRequestMediaUrl(href, changeRequestUrl) : href; |
| 231 | const openPullRequest = useOpenPullRequest(); |
| 232 | const openLink = (href: string) => { |
| 233 | void openPullRequest(href); |
| 234 | }; |
| 235 | const [preview, setPreview] = useState<FilePreviewSource | null>(null); |
| 236 | const renderImage: MarkdownImageRenderer = (image) => ( |
| 237 | <MarkdownImage href={resolveMediaUrl(image.href)} alt={image.alt} onPreview={setPreview} /> |
| 238 | ); |
| 239 | const styles = useMarkdownPreviewStyles(); |
| 240 | return ( |
| 241 | <FilePreviewGallery value={props.gallery}> |
| 242 | <SelectableMarkdownText |
| 243 | markdown={props.markdown} |
| 244 | preserveSoftBreaks={props.lineBreaks ?? hostLineBreaks} |
| 245 | textStyle={styles.nativeTextStyle} |
| 246 | onLinkPress={openLink} |
| 247 | renderImage={renderImage} |
| 248 | onImagePress={(href) => |
| 249 | setPreview(pullRequestImageSource(resolveMediaUrl(href), mediaContext)) |
| 250 | } |
| 251 | onTaskListItemPress={props.onTaskListItemPress} |
| 252 | /> |
| 253 | <FilePreviewModal source={preview} onRequestClose={() => setPreview(null)} /> |
| 254 | </FilePreviewGallery> |
| 255 | ); |
| 256 | } |