@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 161 lines 6.07 KB
Raw
1 import { useGT } from "gt-react-native";
2 import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3 import { useNavigation } from "@react-navigation/native";
4 import { useAtomValue } from "@effect/atom-react";
5 import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
6 import type { EnvironmentId } from "@t3tools/contracts";
7 import { useRef, useState } from "react";
8 import { RefreshControl } from "../../components/ui/RefreshControl";
9 import { useSafeAreaInsets } from "react-native-safe-area-context";
10
11 import { SettingsScreen } from "./components/SettingsScreen";
12 import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows";
13 import { LocalEnvironmentList } from "../connection/LocalEnvironmentList";
14 import { PullRequestRoutingSettings } from "../connection/PullRequestRoutingSettings";
15 import { splitEnvironmentSections } from "../connection/environmentSections";
16 import { useUniwindTheme } from "../../lib/useUniwindTheme";
17 import { useRemoteConnections } from "../../state/use-remote-environment-registry";
18 import { relayEnvironmentDiscovery } from "../../state/relay";
19 import { useAtomCommand } from "../../state/use-atom-command";
20 import {
21 applyShowcaseLocalEnvironmentDisplayUrls,
22 resolveShowcaseEnvironmentUpdateDisplayUrl,
23 SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
24 SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS,
25 } from "../showcase/showcaseEnvironmentRows";
26
27 const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1";
28
29 export function SettingsEnvironmentsRouteScreen() {
30 const gt = useGT();
31 const {
32 connectedEnvironments,
33 onReconnectEnvironment,
34 onRemoveEnvironmentPress,
35 onSetEnvironmentEnabled,
36 onUpdateEnvironment,
37 } = useRemoteConnections();
38 const navigation = useNavigation();
39 const insets = useSafeAreaInsets();
40 const environmentSections = splitEnvironmentSections({
41 connectedEnvironments,
42 cloudEnvironments: null,
43 });
44 const localEnvironments = SHOWCASE_ENABLED
45 ? applyShowcaseLocalEnvironmentDisplayUrls(environmentSections.localEnvironments)
46 : environmentSections.localEnvironments;
47 const connectedCloudEnvironments = SHOWCASE_ENABLED
48 ? SHOWCASE_CONNECTED_CLOUD_ENVIRONMENTS
49 : environmentSections.connectedCloudEnvironments;
50 const headerIconColor = useUniwindTheme()["--color-icon"];
51 const relaySession = useAtomValue(managedRelaySessionAtom);
52 const refreshRelayEnvironments = useAtomCommand(
53 relayEnvironmentDiscovery.refresh,
54 "relay environment refresh",
55 );
56 const [isRefreshingCloud, setIsRefreshingCloud] = useState(false);
57 const cloudRefreshPendingRef = useRef(false);
58 async function refreshCloudEnvironments() {
59 if (!relaySession || cloudRefreshPendingRef.current) return;
60 cloudRefreshPendingRef.current = true;
61 setIsRefreshingCloud(true);
62 await refreshRelayEnvironments();
63 cloudRefreshPendingRef.current = false;
64 setIsRefreshingCloud(false);
65 }
66
67 const openEnvironment = (environmentId: EnvironmentId) => {
68 navigation.navigate("SettingsEnvironmentDetail", { environmentId });
69 };
70 const handleUpdateEnvironment = (
71 environmentId: EnvironmentId,
72 updates: { readonly label: string; readonly displayUrl: string },
73 ) => {
74 if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
75 const actualEnvironment = environmentSections.localEnvironments.find(
76 (environment) => environment.environmentId === environmentId,
77 );
78 const presentedEnvironment = localEnvironments.find(
79 (environment) => environment.environmentId === environmentId,
80 );
81 return onUpdateEnvironment(environmentId, {
82 ...updates,
83 displayUrl:
84 actualEnvironment && presentedEnvironment
85 ? resolveShowcaseEnvironmentUpdateDisplayUrl({
86 actualDisplayUrl: actualEnvironment.displayUrl,
87 presentedDisplayUrl: presentedEnvironment.displayUrl,
88 submittedDisplayUrl: updates.displayUrl,
89 })
90 : updates.displayUrl,
91 });
92 };
93
94 return (
95 <SettingsScreen
96 title={gt("Environments")}
97 refresh={
98 relaySession
99 ? {
100 accessibilityLabel: gt("Refresh cloud environments"),
101 disabled: isRefreshingCloud,
102 onPress: () => void refreshCloudEnvironments(),
103 }
104 : undefined
105 }
106 actions={[
107 {
108 accessibilityLabel: gt("Add environment"),
109 icon: "plus",
110 tintColor: headerIconColor,
111 onPress: () => navigation.navigate("SettingsEnvironmentNew"),
112 },
113 ]}
114 >
115 <ScrollView
116 alwaysBounceVertical
117 contentInsetAdjustmentBehavior="automatic"
118 showsVerticalScrollIndicator={false}
119 className="flex-1"
120 contentContainerClassName="px-5 pt-4"
121 contentContainerStyle={{
122 paddingBottom: Math.max(insets.bottom, 18) + 18,
123 }}
124 refreshControl={
125 relaySession ? (
126 <RefreshControl
127 refreshing={isRefreshingCloud}
128 onRefresh={() => void refreshCloudEnvironments()}
129 />
130 ) : undefined
131 }
132 >
133 <LocalEnvironmentList
134 environments={localEnvironments}
135 expandedId={null}
136 onToggle={openEnvironment}
137 opensDetails
138 onReconnect={onReconnectEnvironment}
139 onRemove={onRemoveEnvironmentPress}
140 onSetEnabled={onSetEnvironmentEnabled}
141 onUpdate={handleUpdateEnvironment}
142 />
143
144 {/* Keep connected relays visible and removable without cloud config or sign-in; this component gates discovery. */}
145 <CloudEnvironmentRows
146 connectedCloudEnvironments={connectedCloudEnvironments}
147 onOpenEnvironment={openEnvironment}
148 onSetEnvironmentEnabled={onSetEnvironmentEnabled}
149 onRemoveEnvironment={onRemoveEnvironmentPress}
150 {...(SHOWCASE_ENABLED
151 ? {
152 showcaseAvailableEnvironments: SHOWCASE_AVAILABLE_CLOUD_ENVIRONMENTS,
153 showcaseSignedIn: true,
154 }
155 : {})}
156 />
157 <PullRequestRoutingSettings />
158 </ScrollView>
159 </SettingsScreen>
160 );
161 }