The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { resolveEnvironmentMachineKind } from "@t3tools/contracts"; |
| 3 | import { |
| 4 | pullRequestRoutingConnectionKey, |
| 5 | pullRequestRoutingPermissionFor, |
| 6 | type PullRequestRoutingPermission, |
| 7 | } from "@t3tools/client-runtime/connection"; |
| 8 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 9 | import { useState } from "react"; |
| 10 | |
| 11 | import { environmentCatalog } from "~/connection/catalog"; |
| 12 | import type { MessageResolver } from "~/i18n/messages"; |
| 13 | import type { EnvironmentPresentation } from "~/state/environments"; |
| 14 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 15 | import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; |
| 16 | import { toastManager } from "../ui/toast"; |
| 17 | import { EnvironmentRow, EnvironmentTransportLabel } from "./EnvironmentRow"; |
| 18 | import { FoldedSettingsSection } from "./FoldedSettingsSection"; |
| 19 | import { useSearchableSetting } from "./settingsSearch"; |
| 20 | |
| 21 | const options: ReadonlyArray<{ value: PullRequestRoutingPermission; label: string }> = [ |
| 22 | { value: "off", label: msg("Off", { $context: "pull request routing permission" }) }, |
| 23 | { value: "read", label: msg("Read PRs") }, |
| 24 | { value: "read-write", label: msg("Read and act") }, |
| 25 | ]; |
| 26 | |
| 27 | const summaryLabels = { |
| 28 | "read-write": msg("{machines} read and act"), |
| 29 | read: msg("{machines} read PRs"), |
| 30 | } as const; |
| 31 | |
| 32 | function summarizePullRequestRouting( |
| 33 | entries: ReadonlyArray<{ |
| 34 | readonly label: string; |
| 35 | readonly permission: PullRequestRoutingPermission; |
| 36 | }>, |
| 37 | resolve: MessageResolver, |
| 38 | ): string | null { |
| 39 | const groups = (["read-write", "read"] as const).flatMap((permission) => { |
| 40 | const labels = entries.filter((entry) => entry.permission === permission); |
| 41 | return labels.length === 0 |
| 42 | ? [] |
| 43 | : [ |
| 44 | resolve(summaryLabels[permission], { |
| 45 | machines: labels.map((entry) => entry.label).join(", "), |
| 46 | }), |
| 47 | ]; |
| 48 | }); |
| 49 | return groups.length === 0 ? null : groups.join(" · "); |
| 50 | } |
| 51 | |
| 52 | export function PullRequestRoutingSettings({ |
| 53 | environments, |
| 54 | }: { |
| 55 | readonly environments: ReadonlyArray<EnvironmentPresentation>; |
| 56 | }) { |
| 57 | const searchableSetting = useSearchableSetting(); |
| 58 | const permissions = useAtomValue(environmentCatalog.pullRequestRoutingPermissionsValueAtom); |
| 59 | const catalog = useAtomValue(environmentCatalog.catalogValueAtom); |
| 60 | const update = useAtomCommand(environmentCatalog.setPullRequestRoutingPermission); |
| 61 | const [saving, setSaving] = useState(false); |
| 62 | const gt = useGT(); |
| 63 | const m = useMessages(); |
| 64 | const items = options.map((option) => ({ ...option, label: m(option.label) })); |
| 65 | |
| 66 | if (environments.length < 2) return null; |
| 67 | |
| 68 | const { id, title } = searchableSetting("pull-request-routing"); |
| 69 | return ( |
| 70 | <FoldedSettingsSection |
| 71 | id={id} |
| 72 | title={title} |
| 73 | summary={ |
| 74 | summarizePullRequestRouting( |
| 75 | environments.map((environment) => ({ |
| 76 | label: environment.label, |
| 77 | permission: pullRequestRoutingPermissionFor(environment.entry, permissions), |
| 78 | })), |
| 79 | m, |
| 80 | ) ?? gt("Off", { $context: "pull request routing permission" }) |
| 81 | } |
| 82 | > |
| 83 | <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4"> |
| 84 | <T> |
| 85 | Machines you trust here can read PR data through each other's source control access. |
| 86 | Enable both machines. Read and act may use broader permissions than the machine that owns |
| 87 | them. This applies only to this device. |
| 88 | </T> |
| 89 | </p> |
| 90 | {environments.map((environment) => ( |
| 91 | <EnvironmentRow |
| 92 | key={environment.environmentId} |
| 93 | kind={resolveEnvironmentMachineKind(environment.serverConfig)} |
| 94 | label={environment.label} |
| 95 | subtitle={<EnvironmentTransportLabel environment={environment} />} |
| 96 | > |
| 97 | <Select |
| 98 | items={items} |
| 99 | value={pullRequestRoutingPermissionFor(environment.entry, permissions)} |
| 100 | disabled={ |
| 101 | !catalog.isReady || |
| 102 | saving || |
| 103 | pullRequestRoutingConnectionKey(environment.entry) === null |
| 104 | } |
| 105 | onValueChange={(permission) => { |
| 106 | if (permission === null) return; |
| 107 | setSaving(true); |
| 108 | void update({ environmentId: environment.environmentId, permission }).then( |
| 109 | (result) => { |
| 110 | setSaving(false); |
| 111 | if (result._tag === "Failure") |
| 112 | toastManager.add({ |
| 113 | type: "error", |
| 114 | title: gt("Could not save pull request routing permission"), |
| 115 | }); |
| 116 | }, |
| 117 | ); |
| 118 | }} |
| 119 | > |
| 120 | <SelectTrigger |
| 121 | size="xs" |
| 122 | className="w-32" |
| 123 | aria-label={gt("{environment} pull request routing", { |
| 124 | environment: environment.label, |
| 125 | })} |
| 126 | > |
| 127 | <SelectValue /> |
| 128 | </SelectTrigger> |
| 129 | <SelectPopup align="end" alignItemWithTrigger={false}> |
| 130 | {items.map(({ value, label }) => ( |
| 131 | <SelectItem key={value} value={value}> |
| 132 | {label} |
| 133 | </SelectItem> |
| 134 | ))} |
| 135 | </SelectPopup> |
| 136 | </Select> |
| 137 | </EnvironmentRow> |
| 138 | ))} |
| 139 | </FoldedSettingsSection> |
| 140 | ); |
| 141 | } |