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 | parsePullRequestAnchor, |
| 3 | pullRequestAnchorMatchesUrl, |
| 4 | } from "@t3tools/shared/pullRequestAnchor"; |
| 5 | import { PullRequestEntryRow } from "./PullRequestEntryRow"; |
| 6 | import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg"; |
| 7 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 8 | import IconGitCommit from "@tabler/icons-react-native/IconGitCommit"; |
| 9 | import { |
| 10 | pullRequestConversationThreads, |
| 11 | type PullRequestActivity, |
| 12 | type PullRequestComment, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import { StackActions, type NavigationProp } from "@react-navigation/native"; |
| 15 | import { msg, T, useGT, useMessages, Var } from "gt-react-native"; |
| 16 | import { useMemo, useState, type ComponentProps, type ReactNode } from "react"; |
| 17 | import { Pressable, StyleSheet, View } from "react-native"; |
| 18 | import { parseMarkdownDocument } from "../../lib/markdownDocument"; |
| 19 | import { AppText as Text } from "../../components/ui/AppText"; |
| 20 | import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; |
| 21 | import { byTimestamp, relativeTime } from "../../lib/time"; |
| 22 | import { PullRequestAvatar } from "./PullRequestPresentation"; |
| 23 | import { PullRequestMarkdown } from "./PullRequestMarkdown"; |
| 24 | import { PullRequestCommentFooter } from "./PullRequestReactions"; |
| 25 | import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent"; |
| 26 | import { discussionCommentCountLabel } from "./pullRequestDiffComments"; |
| 27 | import { normalizedReviewState } from "./pullRequestStatus"; |
| 28 | import type { PullRequestParams } from "./usePullRequestDiff"; |
| 29 | import type { MessageResolver } from "../../i18n/messages"; |
| 30 | |
| 31 | function TimelineEvent(props: { |
| 32 | icon: AppSymbolName | "commit"; |
| 33 | tone?: "success" | "danger"; |
| 34 | children: ReactNode; |
| 35 | }) { |
| 36 | const { themeVariables: colors } = useAppearancePreferences(); |
| 37 | return ( |
| 38 | <PullRequestEntryRow |
| 39 | leading={ |
| 40 | <> |
| 41 | <View className="absolute top-0 bottom-0 w-px bg-border-subtle" /> |
| 42 | <View className="size-8 items-center justify-center rounded-full border border-border-subtle bg-grouped-card"> |
| 43 | {props.icon === "commit" ? ( |
| 44 | <View style={{ transform: [{ rotate: "90deg" }] }}> |
| 45 | <IconGitCommit |
| 46 | size={17} |
| 47 | color={colors["--color-foreground-muted"] ?? "#838383"} |
| 48 | strokeWidth={1.8} |
| 49 | /> |
| 50 | </View> |
| 51 | ) : ( |
| 52 | <SymbolView |
| 53 | name={props.icon} |
| 54 | size={15} |
| 55 | tintColorClassName={ |
| 56 | props.tone === "success" |
| 57 | ? "accent-adaptive-emerald-600-400" |
| 58 | : props.tone === "danger" |
| 59 | ? "accent-danger-foreground" |
| 60 | : "accent-icon-muted" |
| 61 | } |
| 62 | /> |
| 63 | )} |
| 64 | </View> |
| 65 | </> |
| 66 | } |
| 67 | > |
| 68 | <View className="gap-2 pt-1 pb-6">{props.children}</View> |
| 69 | </PullRequestEntryRow> |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | type CommentActions = { |
| 74 | readonly permissions: ComponentProps<typeof PullRequestCommentFooter>["permissions"]; |
| 75 | readonly onChanged: () => void; |
| 76 | }; |
| 77 | |
| 78 | function TimelineComment({ |
| 79 | comment, |
| 80 | params, |
| 81 | actions, |
| 82 | }: { |
| 83 | comment: PullRequestComment; |
| 84 | params: PullRequestParams; |
| 85 | actions: CommentActions; |
| 86 | }) { |
| 87 | const target = parsePullRequestAnchor(params.navigationUrl); |
| 88 | const linked = |
| 89 | target?.kind === "timeline" && pullRequestAnchorMatchesUrl(target.anchor, comment.url); |
| 90 | const [expanded, setExpanded] = useState(linked); |
| 91 | const gt = useGT(); |
| 92 | const messages = useMessages(); |
| 93 | const { themeVariables: colors } = useAppearancePreferences(); |
| 94 | const long = comment.body.length > 450 || comment.body.split("\n").length > 8; |
| 95 | const content = useMemo( |
| 96 | () => pullRequestDescriptionContent(parseMarkdownDocument(comment.body), [], messages), |
| 97 | [comment.body, messages], |
| 98 | ); |
| 99 | const preview = content.preview; |
| 100 | const state = normalizedReviewState(comment.reviewState); |
| 101 | const approved = state === "APPROVED"; |
| 102 | const changes = state === "CHANGES_REQUESTED"; |
| 103 | return ( |
| 104 | <TimelineEvent |
| 105 | icon={approved ? "checkmark" : changes ? "doc.text" : "text.bubble"} |
| 106 | tone={approved ? "success" : changes ? "danger" : undefined} |
| 107 | > |
| 108 | <View accessible accessibilityRole="header" className="flex-row items-center gap-2"> |
| 109 | <PullRequestAvatar actor={comment.author} size={22} /> |
| 110 | <View className="min-w-0 flex-1"> |
| 111 | <Text className="text-sm"> |
| 112 | {approved ? ( |
| 113 | <T> |
| 114 | <Text className="font-t3-medium"> |
| 115 | <Var>{comment.author?.login ?? gt("Unknown author")}</Var> |
| 116 | </Text> |
| 117 | <Text className="text-foreground-muted"> approved</Text> |
| 118 | </T> |
| 119 | ) : changes ? ( |
| 120 | <T> |
| 121 | <Text className="font-t3-medium"> |
| 122 | <Var>{comment.author?.login ?? gt("Unknown author")}</Var> |
| 123 | </Text> |
| 124 | <Text className="text-foreground-muted"> requested changes</Text> |
| 125 | </T> |
| 126 | ) : ( |
| 127 | <T> |
| 128 | <Text className="font-t3-medium"> |
| 129 | <Var>{comment.author?.login ?? gt("Unknown author")}</Var> |
| 130 | </Text> |
| 131 | <Text className="text-foreground-muted"> commented</Text> |
| 132 | </T> |
| 133 | )} |
| 134 | </Text> |
| 135 | </View> |
| 136 | <Text className="text-2xs text-foreground-tertiary"> |
| 137 | {relativeTime(comment.createdAt, undefined, messages)} |
| 138 | </Text> |
| 139 | </View> |
| 140 | {comment.path ? ( |
| 141 | <Text className="font-mono text-2xs text-foreground-muted" numberOfLines={2}> |
| 142 | {comment.path} |
| 143 | </Text> |
| 144 | ) : null} |
| 145 | {preview.trim() || content.attachments.length ? ( |
| 146 | <View |
| 147 | className={ |
| 148 | linked |
| 149 | ? "gap-2 overflow-hidden rounded-2xl border border-primary bg-primary/10 p-3" |
| 150 | : "gap-2 overflow-hidden rounded-2xl bg-grouped-card p-3" |
| 151 | } |
| 152 | > |
| 153 | <View style={long && !expanded ? { maxHeight: 240, overflow: "hidden" } : undefined}> |
| 154 | <PullRequestMarkdown |
| 155 | markdown={long && !expanded ? comment.body.slice(0, 1200) : comment.body} |
| 156 | imageGroupMarkdown={comment.body} |
| 157 | /> |
| 158 | </View> |
| 159 | {long && !expanded ? ( |
| 160 | <View |
| 161 | pointerEvents="none" |
| 162 | style={[ |
| 163 | StyleSheet.absoluteFill, |
| 164 | { top: undefined, bottom: 0, left: 0, right: 0, height: 80, zIndex: 1 }, |
| 165 | ]} |
| 166 | > |
| 167 | <Svg width="100%" height={80}> |
| 168 | <Defs> |
| 169 | <LinearGradient |
| 170 | id="comment-fade" |
| 171 | x1="0" |
| 172 | y1="0" |
| 173 | x2="0" |
| 174 | y2="80" |
| 175 | gradientUnits="userSpaceOnUse" |
| 176 | > |
| 177 | <Stop offset="0" stopColor={colors["--color-grouped-card"]} stopOpacity={0} /> |
| 178 | <Stop |
| 179 | offset="0.5" |
| 180 | stopColor={colors["--color-grouped-card"]} |
| 181 | stopOpacity={0.94} |
| 182 | /> |
| 183 | <Stop |
| 184 | offset="0.72" |
| 185 | stopColor={colors["--color-grouped-card"]} |
| 186 | stopOpacity={1} |
| 187 | /> |
| 188 | <Stop offset="1" stopColor={colors["--color-grouped-card"]} stopOpacity={1} /> |
| 189 | </LinearGradient> |
| 190 | </Defs> |
| 191 | <Rect width="100%" height="100%" fill="url(#comment-fade)" /> |
| 192 | </Svg> |
| 193 | <View |
| 194 | style={{ |
| 195 | position: "absolute", |
| 196 | bottom: 0, |
| 197 | left: 0, |
| 198 | right: 0, |
| 199 | height: 22, |
| 200 | backgroundColor: colors["--color-grouped-card"], |
| 201 | }} |
| 202 | /> |
| 203 | </View> |
| 204 | ) : null} |
| 205 | {long ? ( |
| 206 | <Pressable |
| 207 | accessibilityRole="button" |
| 208 | accessibilityState={{ expanded }} |
| 209 | onPress={() => setExpanded(!expanded)} |
| 210 | hitSlop={8} |
| 211 | style={ |
| 212 | !expanded |
| 213 | ? { position: "absolute", bottom: 2, left: 12, right: 12, height: 44, zIndex: 2 } |
| 214 | : undefined |
| 215 | } |
| 216 | className="flex-row items-center justify-center gap-1 py-2 active:opacity-70" |
| 217 | > |
| 218 | <Text className="text-xs font-t3-medium text-foreground"> |
| 219 | {expanded ? gt("Show less") : gt("Show more")} |
| 220 | </Text> |
| 221 | <SymbolView |
| 222 | name={expanded ? "chevron.up" : "chevron.down"} |
| 223 | size={10} |
| 224 | tintColorClassName="accent-foreground" |
| 225 | /> |
| 226 | </Pressable> |
| 227 | ) : null} |
| 228 | </View> |
| 229 | ) : null} |
| 230 | <PullRequestCommentFooter |
| 231 | params={params} |
| 232 | comment={comment} |
| 233 | permissions={actions.permissions} |
| 234 | onChanged={actions.onChanged} |
| 235 | /> |
| 236 | </TimelineEvent> |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | export function pullRequestTimelineRows( |
| 241 | activity: PullRequestActivity, |
| 242 | params: PullRequestParams, |
| 243 | navigation: Pick<NavigationProp<ReactNavigation.RootParamList>, "navigate" | "dispatch">, |
| 244 | actions: CommentActions, |
| 245 | newestFirst: boolean, |
| 246 | resolve: MessageResolver, |
| 247 | ) { |
| 248 | const linkTarget = parsePullRequestAnchor(params.navigationUrl); |
| 249 | const threads = pullRequestConversationThreads(activity); |
| 250 | const discussionIds = new Set( |
| 251 | threads.flatMap((thread) => thread.comments.map((comment) => comment.id)), |
| 252 | ); |
| 253 | const events = [ |
| 254 | ...activity.comments |
| 255 | .filter((comment) => !discussionIds.has(comment.id)) |
| 256 | .map((comment) => ({ |
| 257 | key: `comment:${comment.id}`, |
| 258 | date: comment.createdAt, |
| 259 | content: ( |
| 260 | <TimelineComment |
| 261 | key={ |
| 262 | linkTarget?.kind === "timeline" && |
| 263 | pullRequestAnchorMatchesUrl(linkTarget.anchor, comment.url) |
| 264 | ? `${comment.id}:${params.navigationId ?? params.navigationUrl}` |
| 265 | : comment.id |
| 266 | } |
| 267 | comment={comment} |
| 268 | params={params} |
| 269 | actions={actions} |
| 270 | /> |
| 271 | ), |
| 272 | })), |
| 273 | ...threads.map((thread) => ({ |
| 274 | key: `discussion:${thread.id}`, |
| 275 | date: thread.comments[0]?.createdAt ?? "", |
| 276 | content: ( |
| 277 | <TimelineEvent icon="text.bubble" tone={thread.isResolved ? "success" : undefined}> |
| 278 | <View accessible accessibilityRole="header" className="flex-row items-center gap-2"> |
| 279 | <PullRequestAvatar actor={thread.comments[0]?.author ?? null} size={22} /> |
| 280 | <Text className="flex-1 text-sm"> |
| 281 | <T> |
| 282 | <Text className="font-t3-medium"> |
| 283 | <Var>{thread.comments[0]?.author?.login ?? resolve(msg("Unknown author"))}</Var> |
| 284 | </Text> |
| 285 | <Text className="text-foreground-muted"> started a discussion</Text> |
| 286 | </T> |
| 287 | </Text> |
| 288 | {thread.comments[0] ? ( |
| 289 | <Text className="text-2xs text-foreground-tertiary"> |
| 290 | {relativeTime(thread.comments[0].createdAt, undefined, resolve)} |
| 291 | </Text> |
| 292 | ) : null} |
| 293 | </View> |
| 294 | <Pressable |
| 295 | accessibilityRole="button" |
| 296 | accessibilityLabel={ |
| 297 | thread.path === null |
| 298 | ? thread.isResolved |
| 299 | ? resolve(msg("Open resolved discussion in the overview")) |
| 300 | : resolve(msg("Open unresolved discussion in the overview")) |
| 301 | : thread.isResolved |
| 302 | ? resolve(msg("Open resolved discussion in {path}", { path: thread.path })) |
| 303 | : resolve(msg("Open unresolved discussion in {path}", { path: thread.path })) |
| 304 | } |
| 305 | onPress={() => |
| 306 | navigation.navigate("PullRequestDiscussion", { ...params, discussionId: thread.id }) |
| 307 | } |
| 308 | className="gap-2 rounded-2xl bg-grouped-card p-3 active:opacity-70" |
| 309 | > |
| 310 | <View className="flex-row items-center gap-2"> |
| 311 | {thread.path === null ? ( |
| 312 | <Text className="flex-1 text-xs"> |
| 313 | <T>Overview</T> |
| 314 | </Text> |
| 315 | ) : ( |
| 316 | <> |
| 317 | <SymbolView name="doc.text" size={14} tintColorClassName="accent-icon-muted" /> |
| 318 | <Text className="flex-1 font-mono text-xs" numberOfLines={2}> |
| 319 | {thread.path} |
| 320 | {thread.line ? `:${thread.line}` : ""} |
| 321 | </Text> |
| 322 | </> |
| 323 | )} |
| 324 | <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-muted" /> |
| 325 | </View> |
| 326 | <Text className="text-xs text-foreground-muted"> |
| 327 | {thread.isResolved |
| 328 | ? resolve(msg("Resolved", { $context: "review thread state" })) |
| 329 | : resolve(msg("Unresolved", { $context: "review thread state" }))} |
| 330 | {thread.isOutdated |
| 331 | ? ` · ${resolve(msg("Outdated", { $context: "review thread state" }))}` |
| 332 | : ""}{" "} |
| 333 | ·{" "} |
| 334 | {discussionCommentCountLabel(thread.commentCount ?? thread.comments.length, resolve)} |
| 335 | </Text> |
| 336 | </Pressable> |
| 337 | </TimelineEvent> |
| 338 | ), |
| 339 | })), |
| 340 | ...activity.commits.map((commit) => ({ |
| 341 | key: `commit:${commit.oid}`, |
| 342 | date: commit.committedDate, |
| 343 | content: ( |
| 344 | <TimelineEvent icon="commit"> |
| 345 | <Pressable |
| 346 | accessibilityRole="button" |
| 347 | accessibilityLabel={resolve( |
| 348 | msg("View commit {sha}, {headline}", { |
| 349 | sha: commit.oid.slice(0, 7), |
| 350 | headline: commit.messageHeadline, |
| 351 | }), |
| 352 | )} |
| 353 | onPress={() => |
| 354 | navigation.dispatch( |
| 355 | StackActions.push("PullRequestFile", { ...params, commit: commit.oid, path: "" }), |
| 356 | ) |
| 357 | } |
| 358 | className="flex-row items-center gap-2 active:opacity-70" |
| 359 | > |
| 360 | <PullRequestAvatar actor={commit.authors?.[0] ?? null} size={22} /> |
| 361 | <Text className="min-w-0 flex-1 text-sm" numberOfLines={1}> |
| 362 | {commit.messageHeadline} |
| 363 | </Text> |
| 364 | <Text className="font-mono text-2xs text-foreground-muted"> |
| 365 | {commit.oid.slice(0, 7)} |
| 366 | </Text> |
| 367 | </Pressable> |
| 368 | </TimelineEvent> |
| 369 | ), |
| 370 | })), |
| 371 | ].sort(byTimestamp(newestFirst)); |
| 372 | if (activity.commentsTruncated || activity.reviewThreadsTruncated) |
| 373 | events.push({ |
| 374 | key: "truncated", |
| 375 | date: "", |
| 376 | content: ( |
| 377 | <Text className="py-3 text-xs text-foreground-muted"> |
| 378 | <T>More activity is available on the provider.</T> |
| 379 | </Text> |
| 380 | ), |
| 381 | }); |
| 382 | return events; |
| 383 | } |