The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT, useMessages } from "gt-react-native"; |
| 2 | import { useAtom, useAtomValue } from "@effect/atom-react"; |
| 3 | import { serverUpdateFailureText } from "@t3tools/client-runtime/state/server"; |
| 4 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 5 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 6 | import { AsyncResult } from "effect/reactivity"; |
| 7 | import { useState } from "react"; |
| 8 | import { View } from "react-native"; |
| 9 | |
| 10 | import { AppText as Text } from "../../components/ui/AppText"; |
| 11 | import * as Dialog from "../../components/ui/Dialog"; |
| 12 | import { |
| 13 | describeOutdatedServer, |
| 14 | serverEnvironment, |
| 15 | updateOutdatedServer, |
| 16 | } from "../../state/server"; |
| 17 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 18 | import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow"; |
| 19 | import { SettingsSection } from "./components/SettingsSection"; |
| 20 | import { |
| 21 | environmentReleaseCheckAtom, |
| 22 | releaseCheckError, |
| 23 | releaseCheckValue, |
| 24 | } from "./environmentUpdateCheck"; |
| 25 | |
| 26 | export function OutdatedServerUpdateSection({ |
| 27 | environmentId, |
| 28 | label, |
| 29 | }: { |
| 30 | readonly environmentId: EnvironmentId; |
| 31 | readonly label: string; |
| 32 | }) { |
| 33 | const gt = useGT(); |
| 34 | const messages = useMessages(); |
| 35 | const describe = useAtomCommand(describeOutdatedServer, { reportFailure: false }); |
| 36 | const update = useAtomCommand(updateOutdatedServer, { reportFailure: false }); |
| 37 | const state = useAtomValue(serverEnvironment.updateStateAtom(environmentId)); |
| 38 | const [checkResult, startCheck] = useAtom(environmentReleaseCheckAtom(environmentId)); |
| 39 | const release = releaseCheckValue(checkResult); |
| 40 | const checking = checkResult.waiting; |
| 41 | // Starting an update replaces a failed check's message with the update's own state. |
| 42 | const [checkErrorDismissed, setCheckErrorDismissed] = useState(false); |
| 43 | const error = checkErrorDismissed ? null : releaseCheckError(checkResult); |
| 44 | const busy = checking || state.status === "running"; |
| 45 | |
| 46 | function check() { |
| 47 | if (busy) return; |
| 48 | setCheckErrorDismissed(false); |
| 49 | startCheck(async () => { |
| 50 | const description = await describe(environmentId); |
| 51 | if (AsyncResult.isFailure(description)) throw squashAtomCommandFailure(description); |
| 52 | return description.value.serverVersion; |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | function requestUpdate() { |
| 57 | const targetVersion = release?.targetVersion; |
| 58 | if (busy || !release || !targetVersion) return; |
| 59 | Dialog.alert( |
| 60 | gt("Update {environment}?", { environment: label }), |
| 61 | gt( |
| 62 | "Install {product} {version}. {product} restarts on that machine and reconnects. Running threads may be interrupted.", |
| 63 | { product: "T3 Code", version: targetVersion }, |
| 64 | ), |
| 65 | [ |
| 66 | { text: gt("Cancel"), style: "cancel" }, |
| 67 | { |
| 68 | text: gt("Update", { $context: "verb, install an update" }), |
| 69 | onPress: () => { |
| 70 | setCheckErrorDismissed(true); |
| 71 | void update({ |
| 72 | environmentId, |
| 73 | input: { targetVersion }, |
| 74 | fromVersion: release.fromVersion, |
| 75 | }); |
| 76 | }, |
| 77 | }, |
| 78 | ], |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return ( |
| 83 | <SettingsSection title="T3 Code"> |
| 84 | <View className="gap-1 p-4"> |
| 85 | <Text className="text-base text-foreground"> |
| 86 | {release |
| 87 | ? gt("Version {version}", { version: release.fromVersion }) |
| 88 | : gt("Update required")} |
| 89 | </Text> |
| 90 | <Text className="text-sm text-foreground-muted"> |
| 91 | {state.status === "running" |
| 92 | ? state.stage === "resuming" |
| 93 | ? gt("Restarting and reconnecting…") |
| 94 | : state.stage === "installing" |
| 95 | ? gt("Installing update…") |
| 96 | : gt("Downloading update…") |
| 97 | : release |
| 98 | ? release.targetVersion |
| 99 | ? gt("Version {version} is available.", { version: release.targetVersion }) |
| 100 | : gt("No newer version is available yet.") |
| 101 | : gt("This app needs a newer version of {product} on this machine.", { |
| 102 | product: "T3 Code", |
| 103 | })} |
| 104 | </Text> |
| 105 | {error || state.status === "failed" ? ( |
| 106 | <Text selectable accessibilityRole="alert" className="text-sm text-danger-foreground"> |
| 107 | {error ?? (state.status === "failed" ? serverUpdateFailureText(state, messages) : null)} |
| 108 | </Text> |
| 109 | ) : null} |
| 110 | </View> |
| 111 | <SettingsActionRow |
| 112 | icon="arrow.clockwise" |
| 113 | label={gt("Check for updates")} |
| 114 | disabled={busy} |
| 115 | loading={checking} |
| 116 | onPress={check} |
| 117 | /> |
| 118 | {release?.targetVersion ? ( |
| 119 | <SettingsActionRow |
| 120 | icon="arrow.up.circle" |
| 121 | label={ |
| 122 | state.status === "failed" |
| 123 | ? gt("Retry update") |
| 124 | : gt("Update to {version}", { version: release.targetVersion }) |
| 125 | } |
| 126 | disabled={busy} |
| 127 | loading={state.status === "running"} |
| 128 | onPress={requestUpdate} |
| 129 | /> |
| 130 | ) : null} |
| 131 | </SettingsSection> |
| 132 | ); |
| 133 | } |