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 { useNavigation } from "@react-navigation/native"; |
| 2 | import { |
| 3 | applyPendingPullRequestReactions, |
| 4 | type PendingPullRequestReaction, |
| 5 | PULL_REQUEST_REACTION_ORDER, |
| 6 | pullRequestReactionEmoji, |
| 7 | pullRequestReactionName, |
| 8 | pullRequestReactionTooltip, |
| 9 | pullRequestReactionsSignature, |
| 10 | } from "@t3tools/client-runtime/pull-request-reactions"; |
| 11 | import type { |
| 12 | PullRequestComment, |
| 13 | PullRequestReaction, |
| 14 | PullRequestReactionContent, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import { AsyncResult } from "effect/reactivity"; |
| 17 | import { T, useGT, useLocale, useMessages } from "gt-react-native"; |
| 18 | import { useState } from "react"; |
| 19 | import { Pressable, View } from "react-native"; |
| 20 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 21 | import { AppText as Text } from "../../components/ui/AppText"; |
| 22 | import * as Dialog from "../../components/ui/Dialog"; |
| 23 | import { Menu } from "../../components/ui/Menu"; |
| 24 | import { cn } from "../../lib/cn"; |
| 25 | import { composerPullRequests } from "../../state/pull-requests"; |
| 26 | import { useEnvironmentQuery } from "../../state/query"; |
| 27 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 28 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 29 | import { usePullRequestPermissions } from "./usePullRequestPermissions"; |
| 30 | |
| 31 | const EMPTY_PENDING: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction> = |
| 32 | new Map(); |
| 33 | const PILL = "min-h-8 flex-row items-center gap-1 rounded-full border px-2.5 active:opacity-70"; |
| 34 | |
| 35 | /** Reaction pills for any subject; `onReact` resolves false when the host refused the change. */ |
| 36 | export function ReactionPills(props: { |
| 37 | readonly reactions: ReadonlyArray<PullRequestReaction>; |
| 38 | readonly canReact: boolean; |
| 39 | readonly onReact: (content: PullRequestReactionContent, reacted: boolean) => Promise<boolean>; |
| 40 | readonly onChanged?: () => void; |
| 41 | }) { |
| 42 | // Keyed by what the host last reported, so a refresh that lands the change drops the overlay. |
| 43 | const gt = useGT(); |
| 44 | const messages = useMessages(); |
| 45 | const locale = useLocale(); |
| 46 | const [pending, setPending] = useState({ signature: "", values: EMPTY_PENDING }); |
| 47 | const signature = pullRequestReactionsSignature(props.reactions); |
| 48 | const values = pending.signature === signature ? pending.values : EMPTY_PENDING; |
| 49 | const shown = applyPendingPullRequestReactions(props.reactions, values); |
| 50 | const toggle = async (content: PullRequestReactionContent, reacted: boolean) => { |
| 51 | const entry: PendingPullRequestReaction = { reacted }; |
| 52 | setPending({ signature, values: new Map([...values, [content, entry]]) }); |
| 53 | if (!(await props.onReact(content, reacted))) { |
| 54 | setPending((current) => { |
| 55 | if (current.values.get(content) !== entry) return current; |
| 56 | const next = new Map(current.values); |
| 57 | next.delete(content); |
| 58 | return { signature: current.signature, values: next }; |
| 59 | }); |
| 60 | Dialog.alert(gt("Couldn't save the reaction"), gt("Try again in a moment.")); |
| 61 | return; |
| 62 | } |
| 63 | props.onChanged?.(); |
| 64 | }; |
| 65 | return ( |
| 66 | <> |
| 67 | {shown.map((reaction) => ( |
| 68 | <Pressable |
| 69 | key={reaction.content} |
| 70 | accessibilityRole="button" |
| 71 | accessibilityLabel={pullRequestReactionTooltip(reaction, messages, locale)} |
| 72 | accessibilityState={{ selected: reaction.viewerHasReacted, disabled: !props.canReact }} |
| 73 | disabled={!props.canReact} |
| 74 | onPress={() => void toggle(reaction.content, !reaction.viewerHasReacted)} |
| 75 | className={cn( |
| 76 | PILL, |
| 77 | reaction.viewerHasReacted |
| 78 | ? "border-primary bg-primary/10" |
| 79 | : "border-border-subtle bg-grouped-card", |
| 80 | )} |
| 81 | > |
| 82 | <Text className="text-sm">{pullRequestReactionEmoji(reaction.content)}</Text> |
| 83 | <Text className="text-xs font-t3-medium text-foreground-muted">{reaction.count}</Text> |
| 84 | </Pressable> |
| 85 | ))} |
| 86 | {props.canReact ? ( |
| 87 | <Menu |
| 88 | accessibilityLabel={gt("Add a reaction")} |
| 89 | actions={PULL_REQUEST_REACTION_ORDER.map((content) => ({ |
| 90 | id: content, |
| 91 | title: `${pullRequestReactionEmoji(content)} ${pullRequestReactionName(content, messages)}`, |
| 92 | state: shown.some( |
| 93 | (reaction) => reaction.content === content && reaction.viewerHasReacted, |
| 94 | ) |
| 95 | ? ("on" as const) |
| 96 | : ("off" as const), |
| 97 | }))} |
| 98 | onPressAction={({ nativeEvent }) => { |
| 99 | const content = PULL_REQUEST_REACTION_ORDER.find((id) => id === nativeEvent.event); |
| 100 | if (!content) return; |
| 101 | const reacted = shown.some( |
| 102 | (reaction) => reaction.content === content && reaction.viewerHasReacted, |
| 103 | ); |
| 104 | void toggle(content, !reacted); |
| 105 | }} |
| 106 | > |
| 107 | <View className={cn(PILL, "border-border-subtle")}> |
| 108 | <SymbolView name="face.smiling" size={15} tintColorClassName="accent-icon-muted" /> |
| 109 | </View> |
| 110 | </Menu> |
| 111 | ) : null} |
| 112 | </> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | function usePullRequestReact(params: PullRequestParams, subjectId?: string) { |
| 117 | const target = pullRequestTarget(params); |
| 118 | const setReaction = useAtomCommand(composerPullRequests.setReaction, { reportFailure: false }); |
| 119 | return async (content: PullRequestReactionContent, reacted: boolean) => |
| 120 | !AsyncResult.isFailure( |
| 121 | await setReaction({ |
| 122 | ...target, |
| 123 | input: { |
| 124 | ...target.input, |
| 125 | ...(subjectId === undefined ? {} : { subjectId }), |
| 126 | content, |
| 127 | reacted, |
| 128 | }, |
| 129 | }), |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** Reactions on the change request itself, which is where its description's reactions live. */ |
| 134 | export function PullRequestReactions(props: { readonly params: PullRequestParams }) { |
| 135 | const target = pullRequestTarget(props.params); |
| 136 | const detail = useEnvironmentQuery(composerPullRequests.detail(target)); |
| 137 | const activity = useEnvironmentQuery(composerPullRequests.activity(target)); |
| 138 | const { canReact } = usePullRequestPermissions(props.params, detail.data); |
| 139 | const reactions = activity.data?.reactions ?? []; |
| 140 | const onReact = usePullRequestReact(props.params); |
| 141 | if (!activity.data || (reactions.length === 0 && !canReact)) return null; |
| 142 | return ( |
| 143 | <View className="flex-row flex-wrap items-center gap-1.5"> |
| 144 | <ReactionPills |
| 145 | reactions={reactions} |
| 146 | canReact={canReact} |
| 147 | onReact={onReact} |
| 148 | onChanged={activity.refresh} |
| 149 | /> |
| 150 | </View> |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | export function PullRequestCommentFooter(props: { |
| 155 | readonly params: PullRequestParams; |
| 156 | readonly comment: Pick< |
| 157 | PullRequestComment, |
| 158 | "id" | "kind" | "body" | "author" | "canEdit" | "reactions" |
| 159 | >; |
| 160 | readonly permissions: Pick< |
| 161 | ReturnType<typeof usePullRequestPermissions>, |
| 162 | "canEditComment" | "canReactComment" |
| 163 | >; |
| 164 | readonly onChanged: () => void; |
| 165 | readonly onEdit?: () => void; |
| 166 | }) { |
| 167 | const navigation = useNavigation(); |
| 168 | const gt = useGT(); |
| 169 | const { comment } = props; |
| 170 | const reactions = comment.reactions ?? []; |
| 171 | const editableKind = comment.kind === "review" ? null : comment.kind; |
| 172 | const canEdit = editableKind !== null && props.permissions.canEditComment(comment); |
| 173 | const canReact = props.permissions.canReactComment(comment); |
| 174 | const onReact = usePullRequestReact(props.params, comment.id); |
| 175 | if (reactions.length === 0 && !canReact && !canEdit) return null; |
| 176 | return ( |
| 177 | <View className="flex-row flex-wrap items-center gap-1.5"> |
| 178 | <ReactionPills |
| 179 | reactions={reactions} |
| 180 | canReact={canReact} |
| 181 | onReact={onReact} |
| 182 | onChanged={props.onChanged} |
| 183 | /> |
| 184 | {canEdit && editableKind !== null ? ( |
| 185 | <Pressable |
| 186 | accessibilityRole="button" |
| 187 | accessibilityLabel={gt("Edit comment")} |
| 188 | onPress={() => { |
| 189 | props.onEdit?.(); |
| 190 | navigation.navigate("PullRequestEdit", { |
| 191 | ...props.params, |
| 192 | field: "comment", |
| 193 | comment: { |
| 194 | id: comment.id, |
| 195 | kind: editableKind, |
| 196 | body: comment.body, |
| 197 | author: comment.author, |
| 198 | ...(comment.canEdit === undefined ? {} : { canEdit: comment.canEdit }), |
| 199 | }, |
| 200 | }); |
| 201 | }} |
| 202 | className={cn(PILL, "ml-auto border-transparent")} |
| 203 | > |
| 204 | <SymbolView name="pencil" size={13} tintColorClassName="accent-icon-muted" /> |
| 205 | <Text className="text-xs font-t3-medium text-foreground-muted"> |
| 206 | <T context="verb, edit a comment">Edit</T> |
| 207 | </Text> |
| 208 | </Pressable> |
| 209 | ) : null} |
| 210 | </View> |
| 211 | ); |
| 212 | } |