|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + connectionCatalogDisplayUrl, |
| 4 | + gitHubRoutingConnectionKey, |
| 5 | + gitHubRoutingPermissionFor, |
| 6 | + type GitHubRoutingPermission, |
| 7 | +} from "@t3tools/client-runtime/connection"; |
| 8 | +import { useState } from "react"; |
| 9 | +import { Alert, Pressable, View } from "react-native"; |
| 10 | + |
| 11 | +import { AppText as Text } from "../../components/AppText"; |
| 12 | +import { SymbolView } from "../../components/AppSymbol"; |
| 13 | +import { environmentCatalog } from "../../connection/catalog"; |
| 14 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 15 | +import { SettingsSection } from "../settings/components/SettingsSection"; |
| 16 | + |
| 17 | +const options: ReadonlyArray<{ |
| 18 | + value: GitHubRoutingPermission; |
| 19 | + label: string; |
| 20 | + description: string; |
| 21 | +}> = [ |
| 22 | + { value: "off", label: "Off", description: "Keep GitHub requests on this environment." }, |
| 23 | + { |
| 24 | + value: "read", |
| 25 | + label: "Read PRs", |
| 26 | + description: "Share PR data with other enabled environments.", |
| 27 | + }, |
| 28 | + { |
| 29 | + value: "read-write", |
| 30 | + label: "Read and act", |
| 31 | + description: "Actions may use broader GitHub permissions than the original environment.", |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +export function GitHubRoutingSettings() { |
| 36 | + const catalog = useAtomValue(environmentCatalog.catalogValueAtom); |
| 37 | + const permissions = useAtomValue(environmentCatalog.githubRoutingPermissionsValueAtom); |
| 38 | + const update = useAtomCommand(environmentCatalog.setGitHubRoutingPermission); |
| 39 | + const [expanded, setExpanded] = useState<string | null>(null); |
| 40 | + const [saving, setSaving] = useState(false); |
| 41 | + if (catalog.entries.size === 0) return null; |
| 42 | + |
| 43 | + return ( |
| 44 | + <View className="mt-5 gap-3"> |
| 45 | + <SettingsSection title="GitHub routing"> |
| 46 | + {[...catalog.entries.values()].map((entry, index) => { |
| 47 | + const environmentId = entry.target.environmentId; |
| 48 | + const selected = gitHubRoutingPermissionFor(entry, permissions); |
| 49 | + const disabled = !catalog.isReady || saving || gitHubRoutingConnectionKey(entry) === null; |
| 50 | + return ( |
| 51 | + <View |
| 52 | + key={environmentId} |
| 53 | + className={index === 0 ? undefined : "border-t border-border-subtle"} |
| 54 | + > |
| 55 | + <Pressable |
| 56 | + accessibilityRole="button" |
| 57 | + accessibilityLabel={`${entry.target.label} GitHub routing`} |
| 58 | + accessibilityState={{ expanded: expanded === environmentId }} |
| 59 | + className="flex-row items-center gap-3 p-4" |
| 60 | + onPress={() => setExpanded(expanded === environmentId ? null : environmentId)} |
| 61 | + > |
| 62 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 63 | + <Text className="text-base font-t3-bold text-foreground"> |
| 64 | + {entry.target.label} |
| 65 | + </Text> |
| 66 | + <Text className="text-xs text-foreground-muted" numberOfLines={1}> |
| 67 | + {connectionCatalogDisplayUrl(entry) ?? "T3 Connect"} |
| 68 | + </Text> |
| 69 | + </View> |
| 70 | + <Text className="text-sm text-foreground-muted"> |
| 71 | + {options.find((option) => option.value === selected)?.label} |
| 72 | + </Text> |
| 73 | + <SymbolView |
| 74 | + name={expanded === environmentId ? "chevron.up" : "chevron.down"} |
| 75 | + size={12} |
| 76 | + tintColorClassName="accent-icon-muted" |
| 77 | + /> |
| 78 | + </Pressable> |
| 79 | + {expanded === environmentId |
| 80 | + ? options.map((option) => ( |
| 81 | + <Pressable |
| 82 | + key={option.value} |
| 83 | + accessibilityRole="radio" |
| 84 | + accessibilityState={{ checked: selected === option.value, disabled }} |
| 85 | + disabled={disabled} |
| 86 | + className="flex-row items-center gap-4 border-t border-border-subtle p-4 disabled:opacity-50" |
| 87 | + onPress={() => { |
| 88 | + setSaving(true); |
| 89 | + void update({ environmentId, permission: option.value }).then((result) => { |
| 90 | + setSaving(false); |
| 91 | + if (result._tag === "Failure") |
| 92 | + Alert.alert( |
| 93 | + "Could not save GitHub routing permission", |
| 94 | + "Try again before leaving this screen.", |
| 95 | + ); |
| 96 | + }); |
| 97 | + }} |
| 98 | + > |
| 99 | + <View className="min-w-0 flex-1 gap-1"> |
| 100 | + <Text className="text-base text-foreground">{option.label}</Text> |
| 101 | + <Text className="text-sm leading-normal text-foreground-muted"> |
| 102 | + {option.description} |
| 103 | + </Text> |
| 104 | + </View> |
| 105 | + {selected === option.value ? ( |
| 106 | + <SymbolView |
| 107 | + name="checkmark" |
| 108 | + size={18} |
| 109 | + tintColorClassName="accent-icon" |
| 110 | + weight="semibold" |
| 111 | + /> |
| 112 | + ) : null} |
| 113 | + </Pressable> |
| 114 | + )) |
| 115 | + : null} |
| 116 | + </View> |
| 117 | + ); |
| 118 | + })} |
| 119 | + </SettingsSection> |
| 120 | + <Text className="px-2 text-sm text-foreground-muted"> |
| 121 | + Choose environments you trust to share PR data and use each other's GitHub access. Enable |
| 122 | + both environments. This applies only to this client. |
| 123 | + </Text> |
| 124 | + </View> |
| 125 | + ); |
| 126 | +} |
0 commit comments