|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 3 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 4 | +import { AsyncResult } from "effect/reactivity"; |
| 5 | +import { useEffect, useRef, useState } from "react"; |
| 6 | +import { View } from "react-native"; |
| 7 | + |
| 8 | +import { AppText as Text } from "../../components/ui/AppText"; |
| 9 | +import * as Dialog from "../../components/ui/Dialog"; |
| 10 | +import { |
| 11 | + describeOutdatedServer, |
| 12 | + serverEnvironment, |
| 13 | + updateOutdatedServer, |
| 14 | +} from "../../state/server"; |
| 15 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 16 | +import { SettingsActionRow } from "./components/SettingsActionRow"; |
| 17 | +import { SettingsSection } from "./components/SettingsSection"; |
| 18 | +import { findEnvironmentUpdate } from "./environment-maintenance"; |
| 19 | + |
| 20 | +export function OutdatedServerUpdateSection({ |
| 21 | + environmentId, |
| 22 | + label, |
| 23 | +}: { |
| 24 | + readonly environmentId: EnvironmentId; |
| 25 | + readonly label: string; |
| 26 | +}) { |
| 27 | + const describe = useAtomCommand(describeOutdatedServer, { reportFailure: false }); |
| 28 | + const update = useAtomCommand(updateOutdatedServer, { reportFailure: false }); |
| 29 | + const state = useAtomValue(serverEnvironment.updateStateAtom(environmentId)); |
| 30 | + const [release, setRelease] = useState<{ |
| 31 | + fromVersion: string; |
| 32 | + targetVersion: string | null; |
| 33 | + } | null>(null); |
| 34 | + const [checking, setChecking] = useState(false); |
| 35 | + const [error, setError] = useState<string | null>(null); |
| 36 | + const controller = useRef<AbortController | null>(null); |
| 37 | + useEffect(() => () => controller.current?.abort(), []); |
| 38 | + const busy = checking || state.status === "running"; |
| 39 | + |
| 40 | + async function check() { |
| 41 | + if (busy || controller.current !== null) return; |
| 42 | + const request = new AbortController(); |
| 43 | + controller.current = request; |
| 44 | + setChecking(true); |
| 45 | + setError(null); |
| 46 | + const timeout = setTimeout(() => request.abort(), 20_000); |
| 47 | + const result = await settlePromise(async () => { |
| 48 | + const description = await describe(environmentId); |
| 49 | + if (AsyncResult.isFailure(description)) throw squashAtomCommandFailure(description); |
| 50 | + const fromVersion = description.value.serverVersion; |
| 51 | + const targetVersion = await findEnvironmentUpdate(fromVersion, request.signal); |
| 52 | + if (!request.signal.aborted) setRelease({ fromVersion, targetVersion }); |
| 53 | + }); |
| 54 | + clearTimeout(timeout); |
| 55 | + controller.current = null; |
| 56 | + setChecking(false); |
| 57 | + if (AsyncResult.isFailure(result)) { |
| 58 | + const failure = squashAtomCommandFailure(result); |
| 59 | + setError(failure instanceof Error ? failure.message : "Could not check for updates."); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function requestUpdate() { |
| 64 | + const targetVersion = release?.targetVersion; |
| 65 | + if (busy || !release || !targetVersion) return; |
| 66 | + Dialog.alert( |
| 67 | + `Update ${label}?`, |
| 68 | + `Install T3 Code ${targetVersion}. T3 Code restarts on that machine and reconnects. Running threads may be interrupted.`, |
| 69 | + [ |
| 70 | + { text: "Cancel", style: "cancel" }, |
| 71 | + { |
| 72 | + text: "Update", |
| 73 | + onPress: () => { |
| 74 | + setError(null); |
| 75 | + void update({ |
| 76 | + environmentId, |
| 77 | + input: { targetVersion }, |
| 78 | + fromVersion: release.fromVersion, |
| 79 | + }); |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <SettingsSection title="T3 Code"> |
| 88 | + <View className="gap-1 p-4"> |
| 89 | + <Text className="text-base text-foreground"> |
| 90 | + {release ? `Version ${release.fromVersion}` : "Update required"} |
| 91 | + </Text> |
| 92 | + <Text className="text-sm text-foreground-muted"> |
| 93 | + {state.status === "running" |
| 94 | + ? state.stage === "resuming" |
| 95 | + ? "Restarting and reconnecting…" |
| 96 | + : state.stage === "installing" |
| 97 | + ? "Installing update…" |
| 98 | + : "Downloading update…" |
| 99 | + : release |
| 100 | + ? release.targetVersion |
| 101 | + ? `Version ${release.targetVersion} is available.` |
| 102 | + : "No newer version is available yet." |
| 103 | + : "This app needs a newer version of T3 Code on this machine."} |
| 104 | + </Text> |
| 105 | + {error || state.status === "failed" ? ( |
| 106 | + <Text selectable accessibilityRole="alert" className="text-sm text-danger-foreground"> |
| 107 | + {error ?? (state.status === "failed" ? state.message : null)} |
| 108 | + </Text> |
| 109 | + ) : null} |
| 110 | + </View> |
| 111 | + <SettingsActionRow |
| 112 | + icon="arrow.clockwise" |
| 113 | + label="Check for updates" |
| 114 | + disabled={busy} |
| 115 | + loading={checking} |
| 116 | + onPress={() => void check()} |
| 117 | + /> |
| 118 | + {release?.targetVersion ? ( |
| 119 | + <SettingsActionRow |
| 120 | + icon="arrow.up.circle" |
| 121 | + label={state.status === "failed" ? "Retry update" : `Update to ${release.targetVersion}`} |
| 122 | + disabled={busy} |
| 123 | + loading={state.status === "running"} |
| 124 | + onPress={requestUpdate} |
| 125 | + /> |
| 126 | + ) : null} |
| 127 | + </SettingsSection> |
| 128 | + ); |
| 129 | +} |
0 commit comments