Open History to see the last commit
1import { useGT, useMessages } from "gt-react-native";
2import { useAtom, useAtomValue } from "@effect/atom-react";
3import { serverUpdateFailureText } from "@t3tools/client-runtime/state/server";
4import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5import type { EnvironmentId } from "@t3tools/contracts";
6import { AsyncResult } from "effect/reactivity";
7import { useState } from "react";
8import { View } from "react-native";
9
10import { AppText as Text } from "../../components/ui/AppText";
11import * as Dialog from "../../components/ui/Dialog";
12import {
13 describeOutdatedServer,
14 serverEnvironment,
15 updateOutdatedServer,
16} from "../../state/server";
17import { useAtomCommand } from "../../state/use-atom-command";
18import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
19import { SettingsSection } from "./components/SettingsSection";
20import {
21 environmentReleaseCheckAtom,
22 releaseCheckError,
23 releaseCheckValue,
24} from "./environmentUpdateCheck";
25
26export 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: label }}?`,
61 gt`Install ${{ product: "T3 Code" }} ${{ version: targetVersion }}. ${{ product: "T3 Code" }} restarts on that machine and reconnects. Running threads may be interrupted.`,
62 [
63 { text: gt`Cancel`, style: "cancel" },
64 {
65 text: gt({ context: "verb, install an update" })`Update`,
66 onPress: () => {
67 setCheckErrorDismissed(true);
68 void update({
69 environmentId,
70 input: { targetVersion },
71 fromVersion: release.fromVersion,
72 });
73 },
74 },
75 ],
76 );
77 }
78
79 return (
80 <SettingsSection title="T3 Code">
81 <View className="gap-1 p-4">
82 <Text className="text-base text-foreground">
83 {release ? gt`Version ${{ version: release.fromVersion }}` : gt`Update required`}
84 </Text>
85 <Text className="text-sm text-foreground-muted">
86 {state.status === "running"
87 ? state.stage === "resuming"
88 ? gt`Restarting and reconnecting…`
89 : state.stage === "installing"
90 ? gt`Installing update…`
91 : gt`Downloading update…`
92 : release
93 ? release.targetVersion
94 ? gt`Version ${{ version: release.targetVersion }} is available.`
95 : gt`No newer version is available yet.`
96 : gt`This app needs a newer version of ${{ product: "T3 Code" }} on this machine.`}
97 </Text>
98 {error || state.status === "failed" ? (
99 <Text selectable accessibilityRole="alert" className="text-sm text-danger-foreground">
100 {error ?? (state.status === "failed" ? serverUpdateFailureText(state, messages) : null)}
101 </Text>
102 ) : null}
103 </View>
104 <SettingsActionRow
105 icon="arrow.clockwise"
106 label={gt`Check for updates`}
107 disabled={busy}
108 loading={checking}
109 onPress={check}
110 />
111 {release?.targetVersion ? (
112 <SettingsActionRow
113 icon="arrow.up.circle"
114 label={
115 state.status === "failed"
116 ? gt`Retry update`
117 : gt`Update to ${{ version: release.targetVersion }}`
118 }
119 disabled={busy}
120 loading={state.status === "running"}
121 onPress={requestUpdate}
122 />
123 ) : null}
124 </SettingsSection>
125 );
126}