The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { NativeScrollView } from "../../native/NativeScrollViewMarker"; |
| 3 | import { LegendList } from "@legendapp/list/react-native"; |
| 4 | import { useAtomValue } from "@effect/atom-react"; |
| 5 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 6 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 7 | import { |
| 8 | emptySelection, |
| 9 | isSelected, |
| 10 | changeBatches, |
| 11 | selectionChanges, |
| 12 | toggleSelection, |
| 13 | } from "@t3tools/client-runtime/pull-request-selection"; |
| 14 | import type { PullRequestLabelCandidate } from "@t3tools/contracts"; |
| 15 | import { AsyncResult } from "effect/reactivity"; |
| 16 | import { T, useGT } from "gt-react-native"; |
| 17 | import { useEffect, useState } from "react"; |
| 18 | import { Platform, View } from "react-native"; |
| 19 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 20 | import { AppText as Text } from "../../components/ui/AppText"; |
| 21 | import { Button } from "../../components/ui/Button"; |
| 22 | import * as Dialog from "../../components/ui/Dialog"; |
| 23 | import { EmptyState } from "../../components/ui/EmptyState"; |
| 24 | import { SelectionRow } from "../../components/ui/SelectionRow"; |
| 25 | import { composerPullRequests } from "../../state/pull-requests"; |
| 26 | import { useEnvironmentQuery } from "../../state/query"; |
| 27 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 28 | import { PullRequestLabelDot } from "./PullRequestLabelDot"; |
| 29 | import { pullRequestLabelColor } from "./pullRequestList"; |
| 30 | import { PullRequestSearchSheet } from "./PullRequestSearchSheet"; |
| 31 | import { usePullRequestConnected } from "./usePullRequestConnected"; |
| 32 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 33 | import { m } from "../../i18n/messages"; |
| 34 | |
| 35 | export function PullRequestLabelsScreen({ route }: StaticScreenProps<PullRequestParams>) { |
| 36 | const navigation = useNavigation(); |
| 37 | const gt = useGT(); |
| 38 | const target = pullRequestTarget(route.params); |
| 39 | const pr = useEnvironmentQuery(composerPullRequests.detail(target)).data; |
| 40 | const canWrite = useAtomValue( |
| 41 | composerPullRequests.setLabels.permissionAtom(target.environmentId), |
| 42 | ); |
| 43 | const connected = usePullRequestConnected(route.params.environmentId); |
| 44 | const allowed = |
| 45 | connected && |
| 46 | canWrite && |
| 47 | pr?.capabilities.labels === true && |
| 48 | pr.viewerPermissions.labels !== false; |
| 49 | // Name the first thing in the way, so the notice says what would actually fix it. |
| 50 | const readOnlyReason = |
| 51 | !pr || allowed |
| 52 | ? null |
| 53 | : !connected |
| 54 | ? gt("Reconnect to change labels.") |
| 55 | : !canWrite |
| 56 | ? gt("This device isn't allowed to change source control on this environment.") |
| 57 | : pr.capabilities.labels !== true |
| 58 | ? gt("This provider doesn't support changing labels.") |
| 59 | : gt("Changing labels needs triage access on this repository."); |
| 60 | const result = useEnvironmentQuery(composerPullRequests.labelCandidates(target)); |
| 61 | const setLabels = useAtomCommand(composerPullRequests.setLabels, { reportFailure: false }); |
| 62 | const [query, setQuery] = useState(""); |
| 63 | const [draft, setDraft] = useState(emptySelection<PullRequestLabelCandidate>); |
| 64 | const needle = query.trim().toLowerCase(); |
| 65 | // Host order, not applied-first: re-sorting on toggle would move the row out from under a tap. |
| 66 | const candidates = (result.data?.candidates ?? []).filter((candidate) => |
| 67 | `${candidate.name} ${candidate.description ?? ""}`.toLowerCase().includes(needle), |
| 68 | ); |
| 69 | const latest = result.data?.candidates; |
| 70 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- picks are sent when the sheet leaves the navigation stack, however it is dismissed |
| 71 | useEffect( |
| 72 | () => |
| 73 | navigation.addListener("beforeRemove", () => { |
| 74 | const { add, remove } = selectionChanges( |
| 75 | draft, |
| 76 | (name, label) => |
| 77 | latest?.find((entry) => entry.name === name)?.isApplied ?? label.isApplied, |
| 78 | ); |
| 79 | const sendBatch = async ( |
| 80 | labels: ReadonlyArray<PullRequestLabelCandidate>, |
| 81 | applied: boolean, |
| 82 | ) => { |
| 83 | const names = labels.map((label) => label.name); |
| 84 | const response = await setLabels({ |
| 85 | ...target, |
| 86 | input: { ...target.input, labels: names, applied }, |
| 87 | }); |
| 88 | if (!AsyncResult.isFailure(response)) return; |
| 89 | const cause = squashAtomCommandFailure(response); |
| 90 | Dialog.alert( |
| 91 | applied |
| 92 | ? gt("Couldn't add {labels}", { labels: names.join(", ") }) |
| 93 | : gt("Couldn't remove {labels}", { labels: names.join(", ") }), |
| 94 | cause instanceof Error |
| 95 | ? (errorDisplayMessage(cause, m) ?? cause.message) |
| 96 | : gt("The provider refused the change."), |
| 97 | ); |
| 98 | }; |
| 99 | const send = async (labels: ReadonlyArray<PullRequestLabelCandidate>, applied: boolean) => { |
| 100 | for (const batch of changeBatches(labels)) await sendBatch(batch, applied); |
| 101 | }; |
| 102 | // Adding first keeps the Labels row from showing empty between the two writes. |
| 103 | void send(add, true).then(() => send(remove, false)); |
| 104 | }), |
| 105 | [navigation, draft, latest, setLabels, target, gt], |
| 106 | ); |
| 107 | return ( |
| 108 | <PullRequestSearchSheet |
| 109 | title={gt("Labels", { $context: "pull request labels sheet title" })} |
| 110 | placeholder={gt("Search labels")} |
| 111 | closeAccessibilityLabel={gt("Close labels")} |
| 112 | clearAccessibilityLabel={gt("Clear label search")} |
| 113 | query={query} |
| 114 | onQueryChange={setQuery} |
| 115 | > |
| 116 | {({ paddingBottom }) => ( |
| 117 | <LegendList |
| 118 | renderScrollComponent={NativeScrollView} |
| 119 | data={candidates} |
| 120 | keyExtractor={(candidate) => candidate.name} |
| 121 | extraData={{ draft, allowed, count: candidates.length }} |
| 122 | estimatedItemSize={72} |
| 123 | recycleItems |
| 124 | className="flex-1" |
| 125 | contentInsetAdjustmentBehavior="automatic" |
| 126 | automaticallyAdjustKeyboardInsets={Platform.OS === "ios"} |
| 127 | keyboardDismissMode={Platform.OS === "ios" ? "interactive" : "on-drag"} |
| 128 | keyboardShouldPersistTaps="handled" |
| 129 | showsVerticalScrollIndicator={false} |
| 130 | contentContainerStyle={{ paddingHorizontal: 16, paddingTop: 16, paddingBottom }} |
| 131 | ListHeaderComponent={ |
| 132 | readOnlyReason ? ( |
| 133 | <View className="pb-3"> |
| 134 | <Text className="text-xs text-foreground-muted">{readOnlyReason}</Text> |
| 135 | </View> |
| 136 | ) : undefined |
| 137 | } |
| 138 | renderItem={({ item, index }) => ( |
| 139 | <SelectionRow |
| 140 | title={item.name} |
| 141 | {...(item.description ? { subtitle: item.description } : {})} |
| 142 | role="checkbox" |
| 143 | // Sent when the sheet closes; a failure puts the host's answer back. |
| 144 | selected={isSelected(draft, item.name, item.isApplied)} |
| 145 | disabled={!allowed} |
| 146 | isFirst={index === 0} |
| 147 | isLast={index === candidates.length - 1} |
| 148 | leading={<PullRequestLabelDot color={pullRequestLabelColor(item.color)} />} |
| 149 | onPress={() => |
| 150 | setDraft((current) => toggleSelection(current, item.name, item, item.isApplied)) |
| 151 | } |
| 152 | /> |
| 153 | )} |
| 154 | ListEmptyComponent={ |
| 155 | result.isPending && !result.data ? ( |
| 156 | <AppActivityIndicator className="my-8" /> |
| 157 | ) : ( |
| 158 | <View> |
| 159 | <EmptyState |
| 160 | title={ |
| 161 | result.error |
| 162 | ? gt("Couldn't load labels") |
| 163 | : needle |
| 164 | ? gt("No matching labels") |
| 165 | : gt("This repository has no labels") |
| 166 | } |
| 167 | detail={ |
| 168 | result.error ?? |
| 169 | (needle |
| 170 | ? gt("Try another name.") |
| 171 | : gt("Create labels on the provider to use them here.")) |
| 172 | } |
| 173 | variant="plain" |
| 174 | /> |
| 175 | {result.error ? <Button label={gt("Retry")} onPress={result.refresh} /> : null} |
| 176 | </View> |
| 177 | ) |
| 178 | } |
| 179 | ListFooterComponent={ |
| 180 | result.data?.truncated ? ( |
| 181 | <Text className="py-4 text-xs text-foreground-muted"> |
| 182 | <T> |
| 183 | This repository has more labels than are listed here. Apply the rest on the |
| 184 | provider. |
| 185 | </T> |
| 186 | </Text> |
| 187 | ) : undefined |
| 188 | } |
| 189 | /> |
| 190 | )} |
| 191 | </PullRequestSearchSheet> |
| 192 | ); |
| 193 | } |