|
| 1 | +import { useNavigation } from "@react-navigation/native"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { Alert, Modal, Pressable, ScrollView, View } from "react-native"; |
| 4 | +import { SafeAreaView } from "react-native-safe-area-context"; |
| 5 | +import type { PullRequestReviewThread } from "@t3tools/contracts"; |
| 6 | +import { AppText as Text } from "../../components/AppText"; |
| 7 | +import { ControlPill } from "../../components/ControlPill"; |
| 8 | +import { composerPullRequests } from "../../state/pull-requests"; |
| 9 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 10 | +import { useEnvironmentQuery } from "../../state/query"; |
| 11 | +import { PullRequestCommentControls } from "./PullRequestCommentControls"; |
| 12 | +import { PullRequestMarkdown } from "./PullRequestMarkdown"; |
| 13 | +import { PullRequestAvatar } from "./PullRequestPresentation"; |
| 14 | +import { |
| 15 | + PullRequestPendingBadge, |
| 16 | + PullRequestPendingCommentActions, |
| 17 | +} from "./PullRequestPendingComment"; |
| 18 | +import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft"; |
| 19 | +import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 20 | +import { usePullRequestPermissions } from "./usePullRequestPermissions"; |
| 21 | +import { relativeTime } from "../../lib/time"; |
| 22 | + |
| 23 | +function CommentThread({ |
| 24 | + params, |
| 25 | + thread, |
| 26 | + onOpen, |
| 27 | +}: { |
| 28 | + params: PullRequestParams; |
| 29 | + thread: PullRequestReviewThread; |
| 30 | + onOpen: () => void; |
| 31 | +}) { |
| 32 | + const target = pullRequestTarget(params); |
| 33 | + const detail = useEnvironmentQuery(composerPullRequests.detail(target)); |
| 34 | + const activity = useEnvironmentQuery(composerPullRequests.activity(target)); |
| 35 | + const permissions = usePullRequestPermissions(params, detail.data); |
| 36 | + const resolve = useAtomCommand(composerPullRequests.setThreadResolution, { |
| 37 | + reportFailure: false, |
| 38 | + }); |
| 39 | + const [busy, setBusy] = useState(false); |
| 40 | + const first = thread.comments[0]; |
| 41 | + if (!first) return null; |
| 42 | + const toggle = async () => { |
| 43 | + if (busy) return; |
| 44 | + setBusy(true); |
| 45 | + const result = await resolve({ |
| 46 | + ...target, |
| 47 | + input: { ...target.input, threadId: thread.id, resolved: !thread.isResolved }, |
| 48 | + }); |
| 49 | + setBusy(false); |
| 50 | + if (result._tag === "Failure") Alert.alert("Could not update the thread", "Try again."); |
| 51 | + else activity.refresh(); |
| 52 | + }; |
| 53 | + const canResolve = permissions.canResolve && thread.canResolve !== false; |
| 54 | + return ( |
| 55 | + <View className="overflow-hidden rounded-2xl border border-border-subtle bg-grouped-card"> |
| 56 | + <Pressable |
| 57 | + onPress={onOpen} |
| 58 | + accessibilityRole="button" |
| 59 | + accessibilityLabel={`Open discussion in ${thread.path}`} |
| 60 | + className="border-b border-border-subtle px-3 py-2" |
| 61 | + > |
| 62 | + <Text className="font-mono text-2xs text-foreground-muted" numberOfLines={2}> |
| 63 | + {thread.path} |
| 64 | + {thread.line ? `:${thread.line}` : ""} |
| 65 | + </Text> |
| 66 | + </Pressable> |
| 67 | + <PullRequestCommentControls |
| 68 | + params={params} |
| 69 | + comment={{ ...first, url: first.url ?? null }} |
| 70 | + disabled={busy} |
| 71 | + onResolve={canResolve && !thread.isResolved ? () => void toggle() : undefined} |
| 72 | + onUnresolve={canResolve && thread.isResolved ? () => void toggle() : undefined} |
| 73 | + render={({ actions, reactions }) => ( |
| 74 | + <View className="gap-2 p-3"> |
| 75 | + <View className="flex-row items-center gap-2"> |
| 76 | + <PullRequestAvatar actor={first.author} size={24} /> |
| 77 | + <Text className="min-w-0 flex-1 text-xs font-t3-medium" numberOfLines={1}> |
| 78 | + {first.author?.login ?? "Unknown author"} |
| 79 | + </Text> |
| 80 | + <Text className="text-2xs text-foreground-muted"> |
| 81 | + {relativeTime(first.createdAt)} |
| 82 | + </Text> |
| 83 | + {actions} |
| 84 | + </View> |
| 85 | + {thread.isResolved ? ( |
| 86 | + <Text className="text-xs text-foreground-muted"> |
| 87 | + Resolved · {thread.commentCount ?? thread.comments.length} comments |
| 88 | + </Text> |
| 89 | + ) : ( |
| 90 | + <PullRequestMarkdown markdown={first.body} /> |
| 91 | + )} |
| 92 | + {reactions} |
| 93 | + <Pressable accessibilityRole="button" onPress={onOpen} className="py-2"> |
| 94 | + <Text className="text-xs text-foreground-muted"> |
| 95 | + {thread.isResolved |
| 96 | + ? "Expand discussion" |
| 97 | + : `${Math.max(0, (thread.commentCount ?? thread.comments.length) - 1)} replies · Open discussion`} |
| 98 | + </Text> |
| 99 | + </Pressable> |
| 100 | + </View> |
| 101 | + )} |
| 102 | + /> |
| 103 | + </View> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +/** One PR-wide checklist remains accessible while switching review tabs. */ |
| 108 | +export function PullRequestCommentsSheet({ |
| 109 | + params, |
| 110 | + visible, |
| 111 | + onClose, |
| 112 | +}: { |
| 113 | + params: PullRequestParams; |
| 114 | + visible: boolean; |
| 115 | + onClose: () => void; |
| 116 | +}) { |
| 117 | + const navigation = useNavigation(); |
| 118 | + const draft = usePullRequestReviewDraft(params); |
| 119 | + const [showResolved, setShowResolved] = useState(false); |
| 120 | + const threads = draft.threads.filter((thread) => showResolved || !thread.isResolved); |
| 121 | + return ( |
| 122 | + <Modal |
| 123 | + visible={visible} |
| 124 | + animationType="slide" |
| 125 | + presentationStyle="pageSheet" |
| 126 | + onRequestClose={onClose} |
| 127 | + > |
| 128 | + <SafeAreaView className="flex-1 bg-screen"> |
| 129 | + <View className="flex-row items-center gap-3 border-b border-border-subtle px-4 py-3"> |
| 130 | + <Text className="flex-1 text-lg font-t3-semibold">Comments</Text> |
| 131 | + <ControlPill icon="xmark" accessibilityLabel="Close comments" onPress={onClose} /> |
| 132 | + </View> |
| 133 | + <ScrollView contentContainerStyle={{ padding: 16, gap: 12 }}> |
| 134 | + <Pressable |
| 135 | + onPress={() => setShowResolved(!showResolved)} |
| 136 | + accessibilityRole="checkbox" |
| 137 | + accessibilityState={{ checked: showResolved }} |
| 138 | + className="py-2" |
| 139 | + > |
| 140 | + <Text className="text-sm">{showResolved ? "Hide resolved" : "Show resolved"}</Text> |
| 141 | + </Pressable> |
| 142 | + {draft.comments.map((comment) => ( |
| 143 | + <View |
| 144 | + key={comment.id} |
| 145 | + className="gap-2 rounded-2xl border border-border-subtle bg-grouped-card p-3" |
| 146 | + > |
| 147 | + <View className="flex-row items-center gap-2"> |
| 148 | + <PullRequestPendingBadge /> |
| 149 | + <View className="flex-1" /> |
| 150 | + <PullRequestPendingCommentActions params={params} comment={comment} /> |
| 151 | + </View> |
| 152 | + <Text className="font-mono text-2xs text-foreground-muted" numberOfLines={2}> |
| 153 | + {comment.path} |
| 154 | + </Text> |
| 155 | + <PullRequestMarkdown markdown={comment.body} /> |
| 156 | + <Pressable |
| 157 | + accessibilityRole="button" |
| 158 | + onPress={() => { |
| 159 | + onClose(); |
| 160 | + navigation.navigate("PullRequestInlineComment", { |
| 161 | + ...params, |
| 162 | + draftId: comment.id, |
| 163 | + path: comment.path, |
| 164 | + position: comment.position, |
| 165 | + headSha: comment.headSha, |
| 166 | + code: comment.code, |
| 167 | + ...(comment.oldPath ? { oldPath: comment.oldPath } : {}), |
| 168 | + }); |
| 169 | + }} |
| 170 | + > |
| 171 | + <Text className="py-2 text-xs text-foreground-muted">Open pending comment</Text> |
| 172 | + </Pressable> |
| 173 | + </View> |
| 174 | + ))} |
| 175 | + {threads.map((thread) => ( |
| 176 | + <CommentThread |
| 177 | + key={thread.id} |
| 178 | + params={params} |
| 179 | + thread={thread} |
| 180 | + onOpen={() => { |
| 181 | + onClose(); |
| 182 | + navigation.navigate("PullRequestDiscussion", { |
| 183 | + ...params, |
| 184 | + discussionId: thread.id, |
| 185 | + }); |
| 186 | + }} |
| 187 | + /> |
| 188 | + ))} |
| 189 | + {!threads.length && !draft.comments.length ? ( |
| 190 | + <Text className="text-sm text-foreground-muted">No unresolved comments.</Text> |
| 191 | + ) : null} |
| 192 | + </ScrollView> |
| 193 | + </SafeAreaView> |
| 194 | + </Modal> |
| 195 | + ); |
| 196 | +} |
0 commit comments