Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OutdatedServerUpdateSection.tsx

118 lines4.2 KB

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