Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubRoutingSettings.tsx

110 lines4.5 KB

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