Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 6154b46

Browse files
Check for mobile app updates on launch (pingdotgg#4958)
Co-authored-by: codex <[email protected]>
1 parent abc409c commit 6154b46

4 files changed

Lines changed: 512 additions & 91 deletions

File tree

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useSavedRemoteConnections } from "../../state/use-remote-environment-re
1111
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
1212
import { WorkspaceEmptyDetail } from "../layout/WorkspaceEmptyDetail";
1313
import { WorkspaceSidebarToolbar } from "../layout/workspace-sidebar-toolbar";
14+
import { checkForAppUpdateOnLaunch } from "../updates/app-updates";
1415
import { AndroidHomeFabLayout } from "./AndroidHomeFab";
1516
import { HomeScreen } from "./HomeScreen";
1617
import { HomeHeader } from "./HomeHeader";
@@ -29,6 +30,11 @@ export function HomeRouteScreen() {
2930
const { savedConnectionsById } = useSavedRemoteConnections();
3031
const navigation = useNavigation();
3132
const [searchQuery, setSearchQuery] = useState("");
33+
34+
useEffect(() => {
35+
void checkForAppUpdateOnLaunch();
36+
}, []);
37+
3238
const { archiveThread, confirmDeleteThread, settleThread, unsettleThread } =
3339
useThreadListActions();
3440
const pendingTasks = usePendingNewTasks();

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@ import { SymbolView } from "../../components/AppSymbol";
99
import * as Effect from "effect/Effect";
1010
import { AsyncResult } from "effect/unstable/reactivity";
1111
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
12-
import {
13-
ActivityIndicator,
14-
Alert,
15-
Linking,
16-
Platform,
17-
Pressable,
18-
ScrollView,
19-
View,
20-
} from "react-native";
12+
import { Alert, Linking, Platform, Pressable, ScrollView, View } from "react-native";
2113
import { useSafeAreaInsets } from "react-native-safe-area-context";
2214

2315
import {
24-
type AtomCommandResult,
2516
isAtomCommandInterrupted,
2617
reportAtomCommandResult,
2718
settleAsyncResult,
@@ -47,6 +38,11 @@ import { runtime } from "../../lib/runtime";
4738
import { useThemeColor } from "../../lib/useThemeColor";
4839
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
4940
import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
41+
import {
42+
type AppUpdateCheckState,
43+
registerHiddenUpdateTap,
44+
runAppUpdateCheck,
45+
} from "../updates/app-updates";
5046
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
5147
import { SettingsRow } from "./components/SettingsRow";
5248
import { SettingsSection } from "./components/SettingsSection";
@@ -578,35 +574,23 @@ function BetaSettingsSection() {
578574
);
579575
}
580576

581-
type UpdateCheckState = "idle" | "checking" | "downloading" | "restarting" | "current";
582-
583577
function AppSettingsSection() {
584578
const icon = useThemeColor("--color-icon");
585-
const [updateState, setUpdateState] = useState<UpdateCheckState>("idle");
579+
const [updateState, setUpdateState] = useState<AppUpdateCheckState>("idle");
586580
const updateInFlight = useRef(false);
581+
const hiddenUpdateTapCount = useRef(0);
587582

588583
const version = Constants.expoConfig?.version ?? "0.0.0";
589584
// Fall back to "production" to match resolveAppVariant in app.config.ts, so a
590585
// missing variant never mislabels a production build as development.
591586
const variant = (Constants.expoConfig?.extra?.appVariant as string | undefined) ?? "production";
592587
const variantLabel = variant === "production" ? "" : capitalize(variant);
593588
const versionLabel = variantLabel ? `${version} · ${variantLabel}` : version;
594-
// Which JS is actually running: the bundle shipped in the binary, or an OTA
595-
// update downloaded on top of it. Surfacing this makes "am I even on the
596-
// right build?" answerable at a glance.
597-
const bundleLabel = Updates.isEnabled
598-
? Updates.isEmbeddedLaunch
599-
? "Embedded"
600-
: Updates.updateId
601-
? `OTA ${Updates.updateId.slice(0, 7)}`
602-
: null
603-
: null;
604-
605589
const busy =
606590
updateState === "checking" || updateState === "downloading" || updateState === "restarting";
607591

608592
// "Up to date" is a transient acknowledgement, not a state worth persisting —
609-
// drop back to the bundle label so the row keeps answering "what am I running?".
593+
// return the version row to its normal, deliberately quiet state.
610594
useEffect(() => {
611595
if (updateState !== "current") return;
612596
const timer = setTimeout(() => setUpdateState("idle"), 3000);
@@ -619,12 +603,24 @@ function AppSettingsSection() {
619603
if (updateInFlight.current) return;
620604
updateInFlight.current = true;
621605
try {
622-
await runUpdateCheck(setUpdateState);
606+
await runAppUpdateCheck({
607+
onFailure: (message) => Alert.alert("Update failed", message),
608+
onStateChange: setUpdateState,
609+
});
623610
} finally {
624611
updateInFlight.current = false;
625612
}
626613
}, []);
627614

615+
const handleVersionPress = useCallback(() => {
616+
if (!Updates.isEnabled || updateInFlight.current) return;
617+
const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
618+
hiddenUpdateTapCount.current = tap.nextCount;
619+
if (tap.shouldCheck) {
620+
void checkForUpdate();
621+
}
622+
}, [checkForUpdate]);
623+
628624
const statusLabel =
629625
updateState === "checking"
630626
? "Checking…"
@@ -634,7 +630,7 @@ function AppSettingsSection() {
634630
? "Restarting…"
635631
: updateState === "current"
636632
? "Up to date"
637-
: bundleLabel;
633+
: null;
638634

639635
const versionRow = (
640636
<View className="flex-row items-center gap-4 p-4">
@@ -652,21 +648,6 @@ function AppSettingsSection() {
652648
<Text className="text-xs text-foreground-muted/70">{statusLabel}</Text>
653649
) : null}
654650
</View>
655-
{Updates.isEnabled ? (
656-
<View className="w-[22px] items-center">
657-
{busy ? (
658-
<ActivityIndicator color={icon} size="small" />
659-
) : (
660-
<SymbolView
661-
name="arrow.clockwise"
662-
size={18}
663-
tintColor={icon}
664-
type="monochrome"
665-
weight="semibold"
666-
/>
667-
)}
668-
</View>
669-
) : null}
670651
</View>
671652
);
672653

@@ -676,10 +657,10 @@ function AppSettingsSection() {
676657
<SettingsRow icon="doc.text" label="Legal" fullScreenTarget="SettingsLegal" />
677658
{Updates.isEnabled ? (
678659
<Pressable
679-
accessibilityLabel="Check for updates"
680-
accessibilityRole="button"
660+
accessibilityLabel={`Version ${versionLabel}`}
661+
accessibilityRole="text"
681662
disabled={busy}
682-
onPress={() => void checkForUpdate()}
663+
onPress={handleVersionPress}
683664
>
684665
{versionRow}
685666
</Pressable>
@@ -690,52 +671,6 @@ function AppSettingsSection() {
690671
);
691672
}
692673

693-
async function runUpdateCheck(setUpdateState: (state: UpdateCheckState) => void): Promise<void> {
694-
setUpdateState("checking");
695-
const check = await settlePromise(() => Updates.checkForUpdateAsync());
696-
if (check._tag === "Failure") {
697-
reportUpdateFailure(check, "Could not check for updates.");
698-
setUpdateState("idle");
699-
return;
700-
}
701-
// A rollback directive (`eas update:rollback`) arrives as isAvailable: false
702-
// with isRollBackToEmbedded: true — there is nothing newer to install, but the
703-
// running OTA still has to be dropped for the embedded bundle.
704-
if (!check.value.isAvailable && !check.value.isRollBackToEmbedded) {
705-
setUpdateState("current");
706-
return;
707-
}
708-
709-
setUpdateState("downloading");
710-
const fetched = await settlePromise(() => Updates.fetchUpdateAsync());
711-
if (fetched._tag === "Failure") {
712-
reportUpdateFailure(fetched, "Could not download the update.");
713-
setUpdateState("idle");
714-
return;
715-
}
716-
// isNew is always false for a rollback, so it can't be the sole gate here either.
717-
if (!fetched.value.isNew && !fetched.value.isRollBackToEmbedded) {
718-
setUpdateState("current");
719-
return;
720-
}
721-
722-
setUpdateState("restarting");
723-
// reloadAsync never resolves on success — the JS context is torn down — so
724-
// reaching the failure branch below is the only way this returns.
725-
const reloaded = await settlePromise(() => Updates.reloadAsync());
726-
if (reloaded._tag === "Failure") {
727-
reportUpdateFailure(reloaded, "Downloaded, but could not restart the app.");
728-
setUpdateState("idle");
729-
}
730-
}
731-
732-
function reportUpdateFailure(result: AtomCommandResult<unknown, unknown>, fallback: string): void {
733-
reportAtomCommandResult(result, { label: "app update check" });
734-
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
735-
const error = squashAtomCommandFailure(result);
736-
Alert.alert("Update failed", error instanceof Error ? error.message : fallback);
737-
}
738-
739674
function capitalize(value: string): string {
740675
return value.length > 0 ? value.charAt(0).toUpperCase() + value.slice(1) : value;
741676
}

0 commit comments

Comments
 (0)