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
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { serverUpdateFailureText } from "@t3tools/client-runtime/state/server"; |
| 3 | import { T, useGT, useMessages, Var } from "gt-react-native"; |
| 4 | import * as Dialog from "../../components/ui/Dialog"; |
| 5 | import { useAtom, useAtomValue } from "@effect/atom-react"; |
| 6 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 7 | import { |
| 8 | AuthProvidersManageScope, |
| 9 | AuthOrchestrationReadScope, |
| 10 | type EnvironmentId, |
| 11 | type ServerProvider, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 14 | import { managedUpdatesDenial } from "@t3tools/client-runtime/contract-labels"; |
| 15 | import { sessionPermissionsUnavailableMessage } from "@t3tools/shared/authScopeOptions"; |
| 16 | import * as Cause from "effect/Cause"; |
| 17 | import * as Exit from "effect/Exit"; |
| 18 | import { AsyncResult } from "effect/reactivity"; |
| 19 | import { useRef, useState } from "react"; |
| 20 | import { View } from "react-native"; |
| 21 | import { tryOpenExternalUrl } from "../../lib/openExternalUrl"; |
| 22 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 23 | import { m } from "../../i18n/messages"; |
| 24 | |
| 25 | import { AppText as Text } from "../../components/ui/AppText"; |
| 26 | import { ProviderIcon } from "../../components/ProviderIcon"; |
| 27 | import { ScreenScrollView } from "../../components/ui/ScreenScrollView"; |
| 28 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 29 | import { environmentPresentations } from "../../state/presentation"; |
| 30 | import { serverEnvironment } from "../../state/server"; |
| 31 | import { environmentSession, useEnvironmentScope, readEnvironmentScope } from "../../state/session"; |
| 32 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 33 | import { useRemoteConnections } from "../../state/use-remote-environment-registry"; |
| 34 | import { ConnectionEnvironmentRow } from "../connection/ConnectionEnvironmentRow"; |
| 35 | import { EnvironmentRoutesSection } from "./EnvironmentRoutesSection"; |
| 36 | import { OutdatedServerUpdateSection } from "./OutdatedServerUpdateSection"; |
| 37 | import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow"; |
| 38 | import { SettingsScreen } from "./components/SettingsScreen"; |
| 39 | import { SettingsSection } from "./components/SettingsSection"; |
| 40 | import { |
| 41 | canMaintainEnvironment, |
| 42 | canUpdateEnvironmentProvider, |
| 43 | supportsEnvironmentUpdate, |
| 44 | } from "./environment-maintenance"; |
| 45 | import { environmentReleaseCheckAtom, releaseCheckValue } from "./environmentUpdateCheck"; |
| 46 | |
| 47 | export function SettingsEnvironmentDetailRouteScreen({ |
| 48 | route, |
| 49 | }: StaticScreenProps<{ |
| 50 | readonly environmentId: EnvironmentId; |
| 51 | }>) { |
| 52 | return ( |
| 53 | <EnvironmentDetail |
| 54 | key={route.params.environmentId} |
| 55 | environmentId={route.params.environmentId} |
| 56 | /> |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | function EnvironmentDetail({ environmentId }: { readonly environmentId: EnvironmentId }) { |
| 61 | const gt = useGT(); |
| 62 | const messages = useMessages(); |
| 63 | const insets = useSafeAreaInsets(); |
| 64 | const navigation = useNavigation(); |
| 65 | const connections = useRemoteConnections(); |
| 66 | const environment = connections.connectedEnvironments.find( |
| 67 | (entry) => entry.environmentId === environmentId, |
| 68 | ); |
| 69 | const config = useAtomValue(serverEnvironment.configValueAtom(environmentId)); |
| 70 | const session = useAtomValue(environmentSession.sessionStateValueAtom(environmentId)); |
| 71 | const sessionResult = useAtomValue(environmentSession.sessionStateAtom(environmentId)); |
| 72 | const updateState = useAtomValue(serverEnvironment.updateStateAtom(environmentId)); |
| 73 | const outdated = |
| 74 | useAtomValue(environmentPresentations.presentationAtom(environmentId))?.entry |
| 75 | .serverUpdateRequired === true; |
| 76 | const updateServer = useAtomCommand(serverEnvironment.updateServer); |
| 77 | const updateProvider = useAtomCommand(serverEnvironment.updateProvider); |
| 78 | const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders); |
| 79 | const [connectionExpanded, setConnectionExpanded] = useState(false); |
| 80 | const [pending, setPending] = useState<string | null>(null); |
| 81 | const pendingRef = useRef(false); |
| 82 | const [error, setError] = useState<string | null>(null); |
| 83 | const [notice, setNotice] = useState<string | null>(null); |
| 84 | const [releaseCheck, checkRelease] = useAtom(environmentReleaseCheckAtom(environmentId), { |
| 85 | mode: "promiseExit", |
| 86 | }); |
| 87 | const release = releaseCheckValue(releaseCheck); |
| 88 | |
| 89 | const connected = environment?.isEnabled === true && environment.connectionState === "connected"; |
| 90 | const allowed = |
| 91 | !AsyncResult.isFailure(sessionResult) && canMaintainEnvironment(session, connected); |
| 92 | const running = updateState.status === "running"; |
| 93 | const providerBusy = |
| 94 | config?.providers.some( |
| 95 | (provider) => |
| 96 | provider.updateState?.status === "running" || provider.updateState?.status === "queued", |
| 97 | ) ?? false; |
| 98 | const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope); |
| 99 | const canRead = useEnvironmentScope(environmentId, AuthOrchestrationReadScope); |
| 100 | const busy = !connected || pending !== null || running || providerBusy; |
| 101 | const disabled = !allowed || busy; |
| 102 | const version = config?.environment.serverVersion; |
| 103 | const checkedRelease = release?.fromVersion === version ? release : null; |
| 104 | const capabilities = config?.environment.capabilities; |
| 105 | const managedPolicy = config?.managedPolicy; |
| 106 | const supportUrl = managedPolicy?.supportUrl; |
| 107 | // The server refuses checks and installs; say so instead of offering them. |
| 108 | const updatesDenial = managedUpdatesDenial(managedPolicy, messages); |
| 109 | |
| 110 | async function run(label: string, action: () => Promise<void>) { |
| 111 | if (pendingRef.current) return; |
| 112 | pendingRef.current = true; |
| 113 | setPending(label); |
| 114 | setError(null); |
| 115 | setNotice(null); |
| 116 | const result = await settlePromise(action); |
| 117 | pendingRef.current = false; |
| 118 | setPending(null); |
| 119 | if (AsyncResult.isFailure(result)) { |
| 120 | const cause = squashAtomCommandFailure(result); |
| 121 | setError( |
| 122 | cause instanceof Error |
| 123 | ? (errorDisplayMessage(cause, m) ?? cause.message) |
| 124 | : gt("The action could not be completed. Try again."), |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | function requestServerUpdate() { |
| 130 | const targetVersion = checkedRelease?.targetVersion; |
| 131 | if (disabled || !targetVersion || !capabilities || !supportsEnvironmentUpdate(capabilities)) |
| 132 | return; |
| 133 | Dialog.alert( |
| 134 | environment |
| 135 | ? gt("Update {environment}?", { environment: environment.environmentLabel }) |
| 136 | : gt("Update environment?"), |
| 137 | capabilities.serverSelfUpdate === "desktop-managed" |
| 138 | ? gt( |
| 139 | "Install {product} {version}. The desktop app will close and relaunch. Running threads may be interrupted.", |
| 140 | { product: "T3 Code", version: targetVersion }, |
| 141 | ) |
| 142 | : gt( |
| 143 | "Install {product} {version}. The server will restart and reconnect. Running threads may be interrupted.", |
| 144 | { product: "T3 Code", version: targetVersion }, |
| 145 | ), |
| 146 | [ |
| 147 | { text: gt("Cancel"), style: "cancel" }, |
| 148 | { |
| 149 | text: gt("Update", { $context: "verb, install an update" }), |
| 150 | onPress: () => |
| 151 | void run("server", async () => { |
| 152 | if ( |
| 153 | AsyncResult.isFailure( |
| 154 | appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)), |
| 155 | ) || |
| 156 | !canMaintainEnvironment( |
| 157 | appAtomRegistry.get(environmentSession.sessionStateValueAtom(environmentId)), |
| 158 | connected, |
| 159 | ) |
| 160 | ) |
| 161 | return; |
| 162 | const result = await updateServer({ |
| 163 | environmentId, |
| 164 | input: { targetVersion }, |
| 165 | }); |
| 166 | if (AsyncResult.isFailure(result)) throw squashAtomCommandFailure(result); |
| 167 | setNotice(gt("Updated to {version}.", { version: result.value.targetVersion })); |
| 168 | }), |
| 169 | }, |
| 170 | ], |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | function requestProviderUpdate(provider: ServerProvider) { |
| 175 | if ( |
| 176 | busy || |
| 177 | !readEnvironmentScope(environmentId, AuthProvidersManageScope) || |
| 178 | !canUpdateEnvironmentProvider(provider) |
| 179 | ) |
| 180 | return; |
| 181 | void run(provider.instanceId, async () => { |
| 182 | const result = await updateProvider({ |
| 183 | environmentId, |
| 184 | input: { |
| 185 | provider: provider.driver, |
| 186 | instanceId: provider.instanceId, |
| 187 | }, |
| 188 | }); |
| 189 | if (AsyncResult.isFailure(result)) throw squashAtomCommandFailure(result); |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | return ( |
| 194 | <SettingsScreen title={environment?.environmentLabel ?? gt("Environment")}> |
| 195 | <ScreenScrollView |
| 196 | contentInsetAdjustmentBehavior="automatic" |
| 197 | className="flex-1" |
| 198 | contentContainerClassName="gap-6 px-5 pt-4" |
| 199 | contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 200 | > |
| 201 | {!environment ? ( |
| 202 | <Text className="text-base text-foreground-muted"> |
| 203 | <T>This environment is no longer saved on this device.</T> |
| 204 | </Text> |
| 205 | ) : ( |
| 206 | <> |
| 207 | <SettingsSection title={gt("Connection")}> |
| 208 | <ConnectionEnvironmentRow |
| 209 | environment={environment} |
| 210 | expanded={connectionExpanded} |
| 211 | onToggle={() => setConnectionExpanded((value) => !value)} |
| 212 | onReconnect={connections.onReconnectEnvironment} |
| 213 | onRemove={connections.onRemoveEnvironmentPress} |
| 214 | onSetEnabled={connections.onSetEnvironmentEnabled} |
| 215 | onUpdate={connections.onUpdateEnvironment} |
| 216 | /> |
| 217 | </SettingsSection> |
| 218 | <EnvironmentRoutesSection |
| 219 | environmentId={environmentId} |
| 220 | connected={connected} |
| 221 | onAddRoute={() => |
| 222 | navigation.navigate("SettingsEnvironmentNew", { routeFor: environmentId }) |
| 223 | } |
| 224 | /> |
| 225 | {outdated ? ( |
| 226 | <OutdatedServerUpdateSection |
| 227 | environmentId={environmentId} |
| 228 | label={environment.environmentLabel} |
| 229 | /> |
| 230 | ) : !connected ? ( |
| 231 | <Text className="px-2 text-sm text-foreground-muted"> |
| 232 | <T>Connect this environment to manage it.</T> |
| 233 | </Text> |
| 234 | ) : !allowed ? ( |
| 235 | <Text className="px-2 text-sm text-foreground-muted"> |
| 236 | {AsyncResult.isFailure(sessionResult) || session === null || !session.authenticated |
| 237 | ? sessionPermissionsUnavailableMessage( |
| 238 | { |
| 239 | connected, |
| 240 | failed: AsyncResult.isFailure(sessionResult), |
| 241 | rejected: |
| 242 | !AsyncResult.isFailure(sessionResult) && session?.authenticated === false, |
| 243 | }, |
| 244 | messages, |
| 245 | ) |
| 246 | : gt("This connection does not have permission to manage the environment.")} |
| 247 | </Text> |
| 248 | ) : null} |
| 249 | {error ? ( |
| 250 | <Text selectable className="px-2 text-sm text-danger-foreground"> |
| 251 | {error} |
| 252 | </Text> |
| 253 | ) : null} |
| 254 | {notice ? <Text className="px-2 text-sm text-foreground-muted">{notice}</Text> : null} |
| 255 | {config && !outdated ? ( |
| 256 | <> |
| 257 | <SettingsSection title="T3 Code"> |
| 258 | <View className="gap-1 p-4"> |
| 259 | <Text className="text-base text-foreground"> |
| 260 | <T> |
| 261 | Version <Var>{version}</Var> |
| 262 | </T> |
| 263 | </Text> |
| 264 | {managedPolicy ? ( |
| 265 | <Text className="text-sm text-foreground-muted"> |
| 266 | <T> |
| 267 | Managed by <Var>{managedPolicy.organization}</Var> |
| 268 | </T> |
| 269 | </Text> |
| 270 | ) : null} |
| 271 | {updatesDenial ? ( |
| 272 | <Text className="text-sm text-foreground-muted">{updatesDenial}</Text> |
| 273 | ) : null} |
| 274 | {running ? ( |
| 275 | <Text className="text-sm text-foreground-muted"> |
| 276 | {updateState.stage === "resuming" |
| 277 | ? gt("Restarting and reconnecting…") |
| 278 | : gt("Downloading update…")} |
| 279 | </Text> |
| 280 | ) : updateState.status === "failed" ? ( |
| 281 | <Text selectable className="text-sm text-danger-foreground"> |
| 282 | {serverUpdateFailureText(updateState, messages)} |
| 283 | </Text> |
| 284 | ) : null} |
| 285 | {checkedRelease && !updatesDenial ? ( |
| 286 | <Text className="text-sm text-foreground-muted"> |
| 287 | {checkedRelease.targetVersion |
| 288 | ? gt("Version {version} is available.", { |
| 289 | version: checkedRelease.targetVersion, |
| 290 | }) |
| 291 | : gt("You are up to date.")} |
| 292 | </Text> |
| 293 | ) : null} |
| 294 | {checkedRelease?.targetVersion && |
| 295 | !updatesDenial && |
| 296 | !supportsEnvironmentUpdate(config.environment.capabilities) ? ( |
| 297 | <Text className="text-sm text-foreground-muted"> |
| 298 | {capabilities?.serverSelfUpdate === "desktop-managed" |
| 299 | ? gt("Update the desktop app on this machine.") |
| 300 | : gt("Update and restart {product} on this machine.", { |
| 301 | product: "T3 Code", |
| 302 | })} |
| 303 | </Text> |
| 304 | ) : null} |
| 305 | </View> |
| 306 | {supportUrl ? ( |
| 307 | <SettingsActionRow |
| 308 | icon="questionmark.bubble" |
| 309 | label={gt("Get help from {organization}", { |
| 310 | organization: managedPolicy.organization, |
| 311 | })} |
| 312 | onPress={() => void tryOpenExternalUrl(supportUrl)} |
| 313 | /> |
| 314 | ) : null} |
| 315 | {updatesDenial ? null : ( |
| 316 | <SettingsActionRow |
| 317 | icon="arrow.clockwise" |
| 318 | label={gt("Check for updates")} |
| 319 | disabled={disabled} |
| 320 | loading={pending === "check"} |
| 321 | onPress={() => { |
| 322 | if (disabled) return; |
| 323 | const fromVersion = config.environment.serverVersion; |
| 324 | void run("check", async () => { |
| 325 | const exit = await checkRelease(async () => fromVersion); |
| 326 | // `run` owns the error message, so a failed lookup rethrows to it. |
| 327 | if (Exit.isFailure(exit)) throw Cause.squash(exit.cause); |
| 328 | }); |
| 329 | }} |
| 330 | /> |
| 331 | )} |
| 332 | {checkedRelease?.targetVersion && |
| 333 | !updatesDenial && |
| 334 | supportsEnvironmentUpdate(config.environment.capabilities) ? ( |
| 335 | <SettingsActionRow |
| 336 | icon="arrow.up.circle" |
| 337 | label={gt("Update to {version}", { version: checkedRelease.targetVersion })} |
| 338 | disabled={disabled} |
| 339 | loading={pending === "server" || running} |
| 340 | onPress={requestServerUpdate} |
| 341 | /> |
| 342 | ) : null} |
| 343 | </SettingsSection> |
| 344 | <SettingsSection title={gt("Providers")}> |
| 345 | <SettingsActionRow |
| 346 | icon="arrow.clockwise" |
| 347 | label={gt("Refresh providers")} |
| 348 | disabled={busy || !canRead} |
| 349 | loading={pending === "refresh"} |
| 350 | onPress={() => { |
| 351 | if (busy || !canRead) return; |
| 352 | void run("refresh", async () => { |
| 353 | if (!readEnvironmentScope(environmentId, AuthOrchestrationReadScope)) |
| 354 | return; |
| 355 | const result = await refreshProviders({ |
| 356 | environmentId, |
| 357 | input: { reloadSecrets: true }, |
| 358 | }); |
| 359 | if (AsyncResult.isFailure(result)) throw squashAtomCommandFailure(result); |
| 360 | setNotice(gt("Provider status refreshed.")); |
| 361 | }); |
| 362 | }} |
| 363 | /> |
| 364 | {config.providers |
| 365 | .filter((provider) => provider.enabled) |
| 366 | .map((provider) => ( |
| 367 | <View key={provider.instanceId}> |
| 368 | <View className="gap-1 p-4"> |
| 369 | <View className="flex-row items-center gap-2"> |
| 370 | <ProviderIcon |
| 371 | provider={provider.driver} |
| 372 | iconUrl={provider.iconUrl} |
| 373 | size={18} |
| 374 | /> |
| 375 | <Text className="min-w-0 flex-1 text-base font-t3-medium text-foreground"> |
| 376 | {provider.displayName ?? provider.driver} |
| 377 | </Text> |
| 378 | </View> |
| 379 | <Text className="text-sm text-foreground-muted"> |
| 380 | {provider.versionAdvisory?.latestVersion |
| 381 | ? gt("{version} · Latest {latest}", { |
| 382 | version: provider.installed |
| 383 | ? (provider.version ?? gt("Version unknown")) |
| 384 | : gt("Not installed"), |
| 385 | latest: provider.versionAdvisory.latestVersion, |
| 386 | }) |
| 387 | : provider.installed |
| 388 | ? (provider.version ?? gt("Version unknown")) |
| 389 | : gt("Not installed")} |
| 390 | </Text> |
| 391 | {provider.updateState && provider.updateState.status !== "idle" ? ( |
| 392 | <Text |
| 393 | selectable |
| 394 | className={ |
| 395 | provider.updateState.status === "failed" |
| 396 | ? "text-sm text-danger-foreground" |
| 397 | : "text-sm text-foreground-muted" |
| 398 | } |
| 399 | > |
| 400 | {provider.updateState.message ?? |
| 401 | { |
| 402 | queued: gt("Update queued"), |
| 403 | running: gt("Update running"), |
| 404 | succeeded: gt("Update succeeded"), |
| 405 | failed: gt("Update failed"), |
| 406 | unchanged: gt("Update unchanged"), |
| 407 | }[provider.updateState.status]} |
| 408 | </Text> |
| 409 | ) : null} |
| 410 | {provider.compatibilityAdvisory?.message ? ( |
| 411 | <Text selectable className="text-sm text-foreground-muted"> |
| 412 | {provider.compatibilityAdvisory.message} |
| 413 | </Text> |
| 414 | ) : null} |
| 415 | {provider.unavailableReason || provider.message ? ( |
| 416 | <Text selectable className="text-sm text-foreground-muted"> |
| 417 | {provider.unavailableReason ?? provider.message} |
| 418 | </Text> |
| 419 | ) : null} |
| 420 | {provider.versionAdvisory?.status === "behind_latest" && |
| 421 | !provider.versionAdvisory.canUpdate ? ( |
| 422 | <Text className="text-sm text-foreground-muted"> |
| 423 | <T>Update this provider on the environment's machine.</T> |
| 424 | </Text> |
| 425 | ) : null} |
| 426 | </View> |
| 427 | {canUpdateEnvironmentProvider(provider) ? ( |
| 428 | <SettingsActionRow |
| 429 | icon="arrow.up.circle" |
| 430 | label={gt("Update {provider}", { |
| 431 | provider: provider.displayName ?? provider.driver, |
| 432 | })} |
| 433 | disabled={busy || !canManageProviders} |
| 434 | loading={pending === provider.instanceId} |
| 435 | onPress={() => requestProviderUpdate(provider)} |
| 436 | /> |
| 437 | ) : null} |
| 438 | </View> |
| 439 | ))} |
| 440 | </SettingsSection> |
| 441 | </> |
| 442 | ) : null} |
| 443 | </> |
| 444 | )} |
| 445 | </ScreenScrollView> |
| 446 | </SettingsScreen> |
| 447 | ); |
| 448 | } |