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