The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 2 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 4 | import { |
| 5 | pullRequestListLines, |
| 6 | resolveThreadPullRequestChains, |
| 7 | threadPullRequestKeyOf, |
| 8 | type PullRequestListLine, |
| 9 | } from "@t3tools/client-runtime/thread-pull-requests"; |
| 10 | import { useGT, useMessages } from "gt-react-native"; |
| 11 | import { memo, useMemo } from "react"; |
| 12 | import { Pressable, useWindowDimensions, View } from "react-native"; |
| 13 | |
| 14 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 15 | import { AppText as Text } from "../../components/ui/AppText"; |
| 16 | import * as Dialog from "../../components/ui/Dialog"; |
| 17 | import { Menu } from "../../components/ui/Menu"; |
| 18 | import { cn } from "../../lib/cn"; |
| 19 | import { RelativeTime } from "../../components/RelativeTime"; |
| 20 | import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types"; |
| 21 | import { useScaledTextRole } from "../settings/appearance/useScaledTextRole"; |
| 22 | import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs"; |
| 23 | import type { LinkedPullRequestActions } from "./useLinkedPullRequestActions"; |
| 24 | import { pullRequestListReviewStatus } from "../pull-requests/pullRequestList"; |
| 25 | import { |
| 26 | PullRequestAvatar, |
| 27 | PullRequestConflictBadge, |
| 28 | PullRequestDiffStats, |
| 29 | } from "../pull-requests/PullRequestPresentation"; |
| 30 | import { |
| 31 | pullRequestChecksStatus, |
| 32 | pullRequestConflict, |
| 33 | pullRequestStatus, |
| 34 | } from "../pull-requests/pullRequestStatus"; |
| 35 | |
| 36 | export function useLinkedPullRequestLines( |
| 37 | thread: EnvironmentThreadShell | null, |
| 38 | runtime: EnvironmentRuntimeState | null, |
| 39 | ) { |
| 40 | const supported = (runtime?.serverConfig ?? null) !== null; |
| 41 | const pullRequests = thread?.pullRequests; |
| 42 | return useMemo(() => { |
| 43 | const chains = resolveThreadPullRequestChains(supported ? (pullRequests ?? []) : []); |
| 44 | const open = chains.filter((chain) => chain.layers.some(isOpenLink)); |
| 45 | const done = chains.filter((chain) => !chain.layers.some(isOpenLink)); |
| 46 | return [...pullRequestListLines(open), ...pullRequestListLines(done)]; |
| 47 | }, [pullRequests, supported]); |
| 48 | } |
| 49 | |
| 50 | function isOpenLink(link: PullRequestListLine["link"]) { |
| 51 | return link.snapshot === null || link.snapshot.state === "open"; |
| 52 | } |
| 53 | |
| 54 | export function linkedPullRequestKey(line: PullRequestListLine) { |
| 55 | return threadPullRequestKeyOf(line.link); |
| 56 | } |
| 57 | |
| 58 | export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: { |
| 59 | readonly line: PullRequestListLine; |
| 60 | readonly separator: boolean; |
| 61 | readonly surfaceClassName: string; |
| 62 | readonly openPullRequest: (url: string) => Promise<boolean>; |
| 63 | readonly actions: LinkedPullRequestActions | null; |
| 64 | }) { |
| 65 | const gt = useGT(); |
| 66 | const messages = useMessages(); |
| 67 | const { link, depth, stack } = props.line; |
| 68 | const snapshot = link.snapshot; |
| 69 | const status = snapshot === null ? null : pullRequestStatus(snapshot, messages); |
| 70 | const conflict = snapshot === null ? null : pullRequestConflict(snapshot, messages); |
| 71 | const open = snapshot === null || snapshot.state === "open"; |
| 72 | const checks = |
| 73 | open && snapshot?.checksState ? pullRequestChecksStatus(snapshot.checksState) : null; |
| 74 | const review = |
| 75 | open && snapshot?.reviewDecision |
| 76 | ? pullRequestListReviewStatus( |
| 77 | { |
| 78 | reviewDecision: snapshot.reviewDecision, |
| 79 | viewerReviewRequested: false, |
| 80 | }, |
| 81 | messages, |
| 82 | ) |
| 83 | : null; |
| 84 | const watching = open && link.watch !== undefined; |
| 85 | const additions = snapshot?.additions ?? 0; |
| 86 | const deletions = snapshot?.deletions ?? 0; |
| 87 | const term = changeRequestTermKind( |
| 88 | (sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined)) |
| 89 | .changeRequest, |
| 90 | ); |
| 91 | const metaLineHeight = useScaledTextRole("label").lineHeight * useWindowDimensions().fontScale; |
| 92 | const row = ( |
| 93 | <Pressable |
| 94 | accessibilityRole="button" |
| 95 | accessibilityLabel={[ |
| 96 | gt( |
| 97 | "{term, select, mr {Merge request {number}, {title}} cr {Change request {number}, {title}} other {Pull request {number}, {title}}}", |
| 98 | { |
| 99 | term, |
| 100 | number: link.number, |
| 101 | title: snapshot?.title ?? gt("waiting for host state"), |
| 102 | }, |
| 103 | ), |
| 104 | status?.label, |
| 105 | conflict === null || snapshot === null |
| 106 | ? undefined |
| 107 | : snapshot.baseBranch |
| 108 | ? gt("conflicts with {branch}", { branch: snapshot.baseBranch }) |
| 109 | : gt("has conflicts"), |
| 110 | link.repository, |
| 111 | stack |
| 112 | ? gt("{size, plural, one {stack of {size}} other {stack of {size}}}", { |
| 113 | size: stack.size, |
| 114 | }) |
| 115 | : undefined, |
| 116 | watching ? gt("watching") : undefined, |
| 117 | checks === null |
| 118 | ? undefined |
| 119 | : snapshot?.checksState === "passing" |
| 120 | ? gt("checks passing") |
| 121 | : snapshot?.checksState === "failing" |
| 122 | ? gt("checks failing") |
| 123 | : gt("checks pending"), |
| 124 | review?.accessibleLabel, |
| 125 | ] |
| 126 | .filter(Boolean) |
| 127 | .join(", ")} |
| 128 | accessibilityHint={props.actions ? gt("Touch and hold for actions.") : undefined} |
| 129 | onPress={() => { |
| 130 | void props.openPullRequest(link.url).then((opened) => { |
| 131 | if (!opened) |
| 132 | Dialog.alert( |
| 133 | gt( |
| 134 | "{term, select, mr {Unable to open MR} cr {Unable to open change request} other {Unable to open PR}}", |
| 135 | { term }, |
| 136 | ), |
| 137 | gt( |
| 138 | "{term, select, mr {The merge request could not be opened.} cr {The change request could not be opened.} other {The pull request could not be opened.}}", |
| 139 | { term }, |
| 140 | ), |
| 141 | ); |
| 142 | }); |
| 143 | }} |
| 144 | style={{ paddingLeft: 16 + Math.min(depth, 3) * 16 }} |
| 145 | className="flex-row gap-3 rounded-xl pr-4 active:bg-row-hover" |
| 146 | > |
| 147 | {depth > 0 ? <View className="-ml-2 w-px self-stretch bg-border" /> : null} |
| 148 | <View className="self-start pt-2.5"> |
| 149 | <SymbolView |
| 150 | name={status?.icon ?? PullRequestGlyph.pullRequest} |
| 151 | size={20} |
| 152 | tintColorClassName={status?.tint ?? "accent-icon-muted"} |
| 153 | /> |
| 154 | {conflict ? <PullRequestConflictBadge surfaceClassName={props.surfaceClassName} /> : null} |
| 155 | </View> |
| 156 | <View |
| 157 | className={cn( |
| 158 | "min-w-0 flex-1 gap-1 py-2.5", |
| 159 | props.separator && "border-b border-border-subtle", |
| 160 | )} |
| 161 | > |
| 162 | <View className="flex-row items-center gap-1.5"> |
| 163 | {snapshot?.author ? <PullRequestAvatar actor={snapshot.author} size={16} /> : null} |
| 164 | <Text className="min-w-0 shrink text-xs text-foreground-muted" numberOfLines={1}> |
| 165 | {link.repository} |
| 166 | </Text> |
| 167 | <Text className="shrink-0 text-xs text-foreground-tertiary">#{link.number}</Text> |
| 168 | <View className="flex-1" /> |
| 169 | {stack ? ( |
| 170 | <View className="flex-row items-center gap-1"> |
| 171 | <SymbolView |
| 172 | name={PullRequestGlyph.stack} |
| 173 | size={12} |
| 174 | tintColorClassName="accent-foreground-muted" |
| 175 | /> |
| 176 | <Text className="text-2xs tabular-nums text-foreground-muted">{stack.size}</Text> |
| 177 | </View> |
| 178 | ) : null} |
| 179 | {watching ? ( |
| 180 | <SymbolView name="eye" size={13} tintColorClassName="accent-foreground-muted" /> |
| 181 | ) : null} |
| 182 | {checks ? ( |
| 183 | <SymbolView name={checks.icon} size={13} tintColorClassName={checks.tint} /> |
| 184 | ) : null} |
| 185 | {review ? ( |
| 186 | <SymbolView name={review.icon} size={13} tintColorClassName={review.tint} /> |
| 187 | ) : null} |
| 188 | {additions + deletions > 0 || snapshot?.updatedAt ? ( |
| 189 | <View |
| 190 | className="-ml-1.5 flex-row flex-wrap items-center gap-x-1.5 overflow-hidden" |
| 191 | style={{ height: metaLineHeight, flexShrink: 100 }} |
| 192 | > |
| 193 | <View className="w-0" /> |
| 194 | {additions + deletions > 0 ? ( |
| 195 | <PullRequestDiffStats additions={additions} deletions={deletions} compact /> |
| 196 | ) : null} |
| 197 | {snapshot?.updatedAt ? ( |
| 198 | <Text className="text-2xs tabular-nums text-foreground-tertiary"> |
| 199 | <RelativeTime timestamp={snapshot.updatedAt} /> |
| 200 | </Text> |
| 201 | ) : null} |
| 202 | </View> |
| 203 | ) : null} |
| 204 | </View> |
| 205 | <Text |
| 206 | className="text-base leading-5 font-t3-medium text-foreground" |
| 207 | numberOfLines={2} |
| 208 | ellipsizeMode="tail" |
| 209 | > |
| 210 | {snapshot?.title ?? gt("Waiting for host state")} |
| 211 | </Text> |
| 212 | </View> |
| 213 | </Pressable> |
| 214 | ); |
| 215 | const actions = props.actions; |
| 216 | if (actions === null) return row; |
| 217 | return ( |
| 218 | <Menu |
| 219 | shouldOpenOnLongPress |
| 220 | title={`#${link.number} · ${link.repository}`} |
| 221 | actions={actions.menuActions(link)} |
| 222 | onPressAction={({ nativeEvent }) => actions.onMenuAction(link, nativeEvent.event)} |
| 223 | > |
| 224 | {row} |
| 225 | </Menu> |
| 226 | ); |
| 227 | }); |