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

PullRequestRoutingSettings.tsx

119 lines4.9 KB

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