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 { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | linkBatchBlockLabel, |
| 4 | linkBatchCandidates, |
| 5 | linkBatchFailureLabel, |
| 6 | type LinkBatchOutcome, |
| 7 | linkBatchSummary, |
| 8 | linkListState, |
| 9 | runLinkBatch, |
| 10 | } from "@t3tools/client-runtime/link-batch"; |
| 11 | import { offersLinkedEnvironments } from "@t3tools/client-runtime/link-request"; |
| 12 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 13 | import { |
| 14 | AuthAccessWriteScope, |
| 15 | type AuthMcpClientAccess, |
| 16 | type EnvironmentId, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import * as Result from "effect/Result"; |
| 19 | import { T, useGT, useMessages } from "gt-react-native"; |
| 20 | import { useMemo, useRef, useState } from "react"; |
| 21 | import { Pressable, ScrollView, View } from "react-native"; |
| 22 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 23 | |
| 24 | import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol"; |
| 25 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 26 | import { AppText as Text } from "../../components/ui/AppText"; |
| 27 | import { Button } from "../../components/ui/Button"; |
| 28 | import { ListChoiceRow } from "../../components/ui/ListChoiceRow"; |
| 29 | import { Sheet } from "../../components/ui/Sheet"; |
| 30 | import { useEnvironmentServerConfig } from "../../state/entities"; |
| 31 | import { useEnvironments } from "../../state/environments"; |
| 32 | import { peerLinkEnvironment, useLinkableMachines } from "../../state/peerLinks"; |
| 33 | import { mintPairingCodeOn } from "../../state/peerPairingCode"; |
| 34 | import { useEnvironmentQuery } from "../../state/query"; |
| 35 | import { useEnvironmentsWithScope } from "../../state/session"; |
| 36 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 37 | import { SettingsSection } from "../settings/components/SettingsSection"; |
| 38 | import { LINK_ACCESS_CHOICES } from "../threads/LinkRequestCard"; |
| 39 | |
| 40 | const NO_MACHINES: ReadonlySet<EnvironmentId> = new Set(); |
| 41 | const NO_OUTCOMES: ReadonlyMap<EnvironmentId, LinkBatchOutcome> = new Map(); |
| 42 | |
| 43 | /** Offers a batch link from `environmentId` once this app has two other machines saved. */ |
| 44 | export function LinkSeveralEnvironmentsAction(props: { |
| 45 | readonly environmentId: EnvironmentId; |
| 46 | readonly environmentLabel: string; |
| 47 | }) { |
| 48 | const gt = useGT(); |
| 49 | const [open, setOpen] = useState(false); |
| 50 | const config = useEnvironmentServerConfig(props.environmentId); |
| 51 | const canLink = useAtomValue(peerLinkEnvironment.link.permissionAtom(props.environmentId)); |
| 52 | const otherMachines = useEnvironments().environments.length - 1; |
| 53 | if (!canLink || !offersLinkedEnvironments(config) || otherMachines < 2) return null; |
| 54 | return ( |
| 55 | <View className="mt-3"> |
| 56 | <Button |
| 57 | label={gt("Link several machines to {environment}…", { |
| 58 | environment: props.environmentLabel, |
| 59 | })} |
| 60 | onPress={() => setOpen(true)} |
| 61 | fullWidth |
| 62 | /> |
| 63 | {open ? ( |
| 64 | <LinkSeveralSheet environmentId={props.environmentId} onClose={() => setOpen(false)} /> |
| 65 | ) : null} |
| 66 | </View> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Each pairing code is minted on its machine and passed straight to the linking |
| 72 | * environment; none is ever held in state, shown, or logged. |
| 73 | */ |
| 74 | function LinkSeveralSheet(props: { |
| 75 | readonly environmentId: EnvironmentId; |
| 76 | readonly onClose: () => void; |
| 77 | }) { |
| 78 | const gt = useGT(); |
| 79 | const m = useMessages(); |
| 80 | const insets = useSafeAreaInsets(); |
| 81 | const machines = useLinkableMachines(props.environmentId, true); |
| 82 | // The same shared list `useLinkableMachines` reads `linked` from. |
| 83 | const links = useEnvironmentQuery( |
| 84 | peerLinkEnvironment.list({ environmentId: props.environmentId, input: {} }), |
| 85 | ); |
| 86 | const linksState = linkListState(links); |
| 87 | const machineIds = machines.map((machine) => machine.environmentId).join("\n"); |
| 88 | const scoped = useMemo( |
| 89 | () => |
| 90 | machineIds |
| 91 | .split("\n") |
| 92 | .filter((id) => id.length > 0) |
| 93 | .map((id) => ({ environmentId: id as EnvironmentId })), |
| 94 | [machineIds], |
| 95 | ); |
| 96 | const writable = useEnvironmentsWithScope(scoped, AuthAccessWriteScope); |
| 97 | const [selected, setSelected] = useState(NO_MACHINES); |
| 98 | const [access, setAccess] = useState<AuthMcpClientAccess>("approval-required"); |
| 99 | const [outcomes, setOutcomes] = useState(NO_OUTCOMES); |
| 100 | const [linking, setLinking] = useState(NO_MACHINES); |
| 101 | // Two taps can both run before a re-render; this guard is synchronous. |
| 102 | const inFlight = useRef(false); |
| 103 | const link = useAtomCommand(peerLinkEnvironment.linkInBatch, { |
| 104 | label: "link several environments", |
| 105 | // The failure cause holds the request, and with it the code; keep it out of the console. |
| 106 | reportFailure: false, |
| 107 | reportDefect: false, |
| 108 | }); |
| 109 | const candidates = linkBatchCandidates(machines, writable, linksState); |
| 110 | const selectable = candidates.flatMap(({ machine, blocked }) => |
| 111 | blocked === null && outcomes.get(machine.environmentId)?.kind !== "linked" ? [machine] : [], |
| 112 | ); |
| 113 | const chosen = selectable.filter((machine) => selected.has(machine.environmentId)); |
| 114 | const busy = linking.size > 0; |
| 115 | const summary = linkBatchSummary(outcomes); |
| 116 | const allChosen = selectable.length > 0 && chosen.length === selectable.length; |
| 117 | |
| 118 | const toggle = (id: EnvironmentId) => |
| 119 | setSelected((current) => { |
| 120 | const next = new Set(current); |
| 121 | if (!next.delete(id)) next.add(id); |
| 122 | return next; |
| 123 | }); |
| 124 | const submit = async () => { |
| 125 | if (inFlight.current || chosen.length === 0) return; |
| 126 | inFlight.current = true; |
| 127 | setLinking(new Set(chosen.map((machine) => machine.environmentId))); |
| 128 | const results = await runLinkBatch({ |
| 129 | machines: chosen, |
| 130 | access, |
| 131 | mint: mintPairingCodeOn, |
| 132 | link: async (input) => { |
| 133 | const result = await link({ environmentId: props.environmentId, input }); |
| 134 | return result._tag === "Success" |
| 135 | ? Result.succeed(result.value) |
| 136 | : Result.fail(squashAtomCommandFailure(result)); |
| 137 | }, |
| 138 | refreshLinks: links.refresh, |
| 139 | onOutcome: (id, outcome) => { |
| 140 | setOutcomes((current) => new Map(current).set(id, outcome)); |
| 141 | setLinking((current) => { |
| 142 | const next = new Set(current); |
| 143 | next.delete(id); |
| 144 | return next; |
| 145 | }); |
| 146 | }, |
| 147 | }); |
| 148 | inFlight.current = false; |
| 149 | setLinking(NO_MACHINES); |
| 150 | // What failed stays picked, so linking again retries only those. |
| 151 | setSelected(new Set(linkBatchSummary(results).failed)); |
| 152 | }; |
| 153 | |
| 154 | return ( |
| 155 | <Sheet |
| 156 | title={gt("Link several environments")} |
| 157 | onClose={props.onClose} |
| 158 | // A batch that lands after a close would still link. |
| 159 | dismissDisabled={busy} |
| 160 | > |
| 161 | <ScrollView |
| 162 | nestedScrollEnabled |
| 163 | className="flex-1 bg-sheet-solid" |
| 164 | contentInsetAdjustmentBehavior="automatic" |
| 165 | contentContainerClassName="gap-5 p-5" |
| 166 | contentContainerStyle={{ paddingBottom: insets.bottom + 20 }} |
| 167 | > |
| 168 | <Text className="font-sans text-sm leading-5 text-foreground-muted"> |
| 169 | <T> |
| 170 | Agents here can then launch, message and wait on threads on each machine you pick. Each |
| 171 | appears under Authorized clients in that machine's Sharing & access, where it can be |
| 172 | revoked. |
| 173 | </T> |
| 174 | </Text> |
| 175 | {candidates.length === 0 ? ( |
| 176 | <Text className="font-sans text-sm leading-5 text-foreground-muted"> |
| 177 | <T>This app knows no other machines. Add them under Environments first.</T> |
| 178 | </Text> |
| 179 | ) : ( |
| 180 | <> |
| 181 | {linksState === "loading" ? ( |
| 182 | <Text |
| 183 | accessibilityLiveRegion="polite" |
| 184 | className="font-sans text-sm leading-5 text-foreground-muted" |
| 185 | > |
| 186 | <T>Checking which machines are already linked…</T> |
| 187 | </Text> |
| 188 | ) : linksState === "failed" ? ( |
| 189 | <View className="gap-2"> |
| 190 | <Text |
| 191 | accessibilityLiveRegion="polite" |
| 192 | className="font-sans text-sm leading-5 text-danger-foreground" |
| 193 | > |
| 194 | {gt( |
| 195 | "Could not check which machines are already linked, so none can be picked: {error}", |
| 196 | { error: links.error ?? "" }, |
| 197 | )} |
| 198 | </Text> |
| 199 | <Button |
| 200 | label={gt("Retry")} |
| 201 | size="compact" |
| 202 | disabled={links.isPending} |
| 203 | onPress={links.refresh} |
| 204 | /> |
| 205 | </View> |
| 206 | ) : null} |
| 207 | <SettingsSection title={gt("Machines")}> |
| 208 | {candidates.map(({ machine, blocked }, index) => { |
| 209 | const id = machine.environmentId; |
| 210 | const outcome = outcomes.get(id); |
| 211 | const available = blocked === null && outcome?.kind !== "linked"; |
| 212 | const checked = available && selected.has(id); |
| 213 | const status = linking.has(id) |
| 214 | ? gt("Linking…") |
| 215 | : outcome?.kind === "linked" |
| 216 | ? gt("Linked", { $context: "status of a machine that was just linked" }) |
| 217 | : outcome?.kind === "failed" |
| 218 | ? `${linkBatchFailureLabel(outcome.failure, m)}${outcome.detail === null ? "" : `: ${outcome.detail}`}` |
| 219 | : blocked === null |
| 220 | ? null |
| 221 | : linkBatchBlockLabel(blocked, m); |
| 222 | return ( |
| 223 | <Pressable |
| 224 | key={id} |
| 225 | accessibilityRole="checkbox" |
| 226 | accessibilityLabel={machine.label} |
| 227 | accessibilityHint={status ?? undefined} |
| 228 | accessibilityState={{ checked, disabled: busy || !available }} |
| 229 | className={ |
| 230 | index > 0 |
| 231 | ? "flex-row items-center gap-3 border-t border-border-subtle p-4 active:opacity-70" |
| 232 | : "flex-row items-center gap-3 p-4 active:opacity-70" |
| 233 | } |
| 234 | disabled={busy || !available} |
| 235 | onPress={() => toggle(id)} |
| 236 | > |
| 237 | <EnvironmentMachineSymbol |
| 238 | kind={machine.machine} |
| 239 | size={18} |
| 240 | tintColorClassName="accent-foreground-muted" |
| 241 | /> |
| 242 | <View className="min-w-0 flex-1 gap-0.5"> |
| 243 | <Text |
| 244 | className={ |
| 245 | available |
| 246 | ? "text-lg text-foreground android:text-base" |
| 247 | : "text-lg text-foreground-muted android:text-base" |
| 248 | } |
| 249 | numberOfLines={1} |
| 250 | > |
| 251 | {machine.label} |
| 252 | </Text> |
| 253 | {status === null ? null : ( |
| 254 | <Text |
| 255 | className={ |
| 256 | outcome?.kind === "failed" |
| 257 | ? "text-sm text-danger-foreground" |
| 258 | : "text-sm text-foreground-muted" |
| 259 | } |
| 260 | > |
| 261 | {status} |
| 262 | </Text> |
| 263 | )} |
| 264 | </View> |
| 265 | {checked ? ( |
| 266 | <SymbolView |
| 267 | name="checkmark" |
| 268 | size={18} |
| 269 | tintColorClassName="accent-icon" |
| 270 | type="monochrome" |
| 271 | weight="semibold" |
| 272 | /> |
| 273 | ) : null} |
| 274 | </Pressable> |
| 275 | ); |
| 276 | })} |
| 277 | </SettingsSection> |
| 278 | <Button |
| 279 | label={allChosen ? gt("Select none") : gt("Select all")} |
| 280 | tone="text" |
| 281 | size="compact" |
| 282 | disabled={busy || selectable.length === 0} |
| 283 | onPress={() => |
| 284 | setSelected( |
| 285 | allChosen |
| 286 | ? NO_MACHINES |
| 287 | : new Set(selectable.map((machine) => machine.environmentId)), |
| 288 | ) |
| 289 | } |
| 290 | /> |
| 291 | </> |
| 292 | )} |
| 293 | <SettingsSection title={gt("What agents here may do there")}> |
| 294 | {LINK_ACCESS_CHOICES.map((choice, index) => ( |
| 295 | <ListChoiceRow |
| 296 | key={choice.access} |
| 297 | label={m(choice.label)} |
| 298 | description={m(choice.description)} |
| 299 | selected={choice.access === access} |
| 300 | separated={index > 0} |
| 301 | disabled={busy} |
| 302 | onPress={() => setAccess(choice.access)} |
| 303 | /> |
| 304 | ))} |
| 305 | </SettingsSection> |
| 306 | {outcomes.size > 0 && !busy ? ( |
| 307 | <Text |
| 308 | accessibilityLiveRegion="polite" |
| 309 | className="font-sans text-sm leading-5 text-foreground-muted" |
| 310 | > |
| 311 | {summary.failed.length === 0 |
| 312 | ? gt("{linked, plural, one {Linked # machine.} other {Linked # machines.}}", { |
| 313 | linked: summary.linked, |
| 314 | }) |
| 315 | : gt( |
| 316 | "Linked {linked} of {total}. The ones that failed stay selected, so you can try them again.", |
| 317 | { linked: summary.linked, total: outcomes.size }, |
| 318 | )} |
| 319 | </Text> |
| 320 | ) : null} |
| 321 | <Button |
| 322 | label={ |
| 323 | busy |
| 324 | ? gt("Linking…") |
| 325 | : gt("{count, plural, one {Link # machine} other {Link # machines}}", { |
| 326 | count: chosen.length, |
| 327 | }) |
| 328 | } |
| 329 | disabled={busy || chosen.length === 0} |
| 330 | onPress={() => void submit()} |
| 331 | tone="primary" |
| 332 | fullWidth |
| 333 | /> |
| 334 | <Button |
| 335 | label={outcomes.size > 0 ? gt("Done") : gt("Cancel")} |
| 336 | tone="secondary" |
| 337 | disabled={busy} |
| 338 | onPress={props.onClose} |
| 339 | fullWidth |
| 340 | /> |
| 341 | </ScrollView> |
| 342 | </Sheet> |
| 343 | ); |
| 344 | } |