@@ -9,10 +9,19 @@ import { SymbolView } from "../../components/AppSymbol";
99import * as Effect from "effect/Effect" ;
1010import { AsyncResult } from "effect/unstable/reactivity" ;
1111import { useCallback , useEffect , useMemo , useState , useSyncExternalStore } from "react" ;
12- import { Alert , Linking , Platform , ScrollView , View } from "react-native" ;
12+ import {
13+ ActivityIndicator ,
14+ Alert ,
15+ Linking ,
16+ Platform ,
17+ Pressable ,
18+ ScrollView ,
19+ View ,
20+ } from "react-native" ;
1321import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1422
1523import {
24+ type AtomCommandResult ,
1625 isAtomCommandInterrupted ,
1726 reportAtomCommandResult ,
1827 settleAsyncResult ,
@@ -570,8 +579,11 @@ function BetaSettingsSection() {
570579 ) ;
571580}
572581
582+ type UpdateCheckState = "idle" | "checking" | "downloading" | "restarting" | "current" ;
583+
573584function AppSettingsSection ( ) {
574585 const icon = useThemeColor ( "--color-icon" ) ;
586+ const [ updateState , setUpdateState ] = useState < UpdateCheckState > ( "idle" ) ;
575587
576588 const version = Constants . expoConfig ?. version ?? "0.0.0" ;
577589 // Fall back to "production" to match resolveAppVariant in app.config.ts, so a
@@ -590,30 +602,124 @@ function AppSettingsSection() {
590602 : null
591603 : null ;
592604
605+ const busy =
606+ updateState === "checking" || updateState === "downloading" || updateState === "restarting" ;
607+
608+ // "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?".
610+ useEffect ( ( ) => {
611+ if ( updateState !== "current" ) return ;
612+ const timer = setTimeout ( ( ) => setUpdateState ( "idle" ) , 3000 ) ;
613+ return ( ) => clearTimeout ( timer ) ;
614+ } , [ updateState ] ) ;
615+
616+ const checkForUpdate = useCallback ( async ( ) => {
617+ setUpdateState ( "checking" ) ;
618+ const check = await settlePromise ( ( ) => Updates . checkForUpdateAsync ( ) ) ;
619+ if ( check . _tag === "Failure" ) {
620+ reportUpdateFailure ( check , "Could not check for updates." ) ;
621+ setUpdateState ( "idle" ) ;
622+ return ;
623+ }
624+ if ( ! check . value . isAvailable ) {
625+ setUpdateState ( "current" ) ;
626+ return ;
627+ }
628+
629+ setUpdateState ( "downloading" ) ;
630+ const fetched = await settlePromise ( ( ) => Updates . fetchUpdateAsync ( ) ) ;
631+ if ( fetched . _tag === "Failure" ) {
632+ reportUpdateFailure ( fetched , "Could not download the update." ) ;
633+ setUpdateState ( "idle" ) ;
634+ return ;
635+ }
636+ if ( ! fetched . value . isNew ) {
637+ setUpdateState ( "current" ) ;
638+ return ;
639+ }
640+
641+ setUpdateState ( "restarting" ) ;
642+ // reloadAsync never resolves on success — the JS context is torn down — so
643+ // reaching the failure branch below is the only way this returns.
644+ const reloaded = await settlePromise ( ( ) => Updates . reloadAsync ( ) ) ;
645+ if ( reloaded . _tag === "Failure" ) {
646+ reportUpdateFailure ( reloaded , "Downloaded, but could not restart the app." ) ;
647+ setUpdateState ( "idle" ) ;
648+ }
649+ } , [ ] ) ;
650+
651+ const statusLabel =
652+ updateState === "checking"
653+ ? "Checking…"
654+ : updateState === "downloading"
655+ ? "Downloading…"
656+ : updateState === "restarting"
657+ ? "Restarting…"
658+ : updateState === "current"
659+ ? "Up to date"
660+ : bundleLabel ;
661+
662+ const versionRow = (
663+ < View className = "flex-row items-center gap-4 p-4" >
664+ < SymbolView
665+ name = "info.circle"
666+ size = { 22 }
667+ tintColor = { icon }
668+ type = "monochrome"
669+ weight = "regular"
670+ />
671+ < Text className = "flex-1 text-lg text-foreground" > Version</ Text >
672+ < View className = "items-end" >
673+ < Text className = "text-lg text-foreground-muted" > { versionLabel } </ Text >
674+ { statusLabel ? (
675+ < Text className = "text-xs text-foreground-muted/70" > { statusLabel } </ Text >
676+ ) : null }
677+ </ View >
678+ { Updates . isEnabled ? (
679+ < View className = "w-[22px] items-center" >
680+ { busy ? (
681+ < ActivityIndicator color = { icon } size = "small" />
682+ ) : (
683+ < SymbolView
684+ name = "arrow.clockwise"
685+ size = { 18 }
686+ tintColor = { icon }
687+ type = "monochrome"
688+ weight = "semibold"
689+ />
690+ ) }
691+ </ View >
692+ ) : null }
693+ </ View >
694+ ) ;
695+
593696 return (
594697 < SettingsSection title = "App" >
595698 < SettingsRow icon = "internaldrive" label = "Client Storage" target = "SettingsClientStorage" />
596699 < SettingsRow icon = "doc.text" label = "Legal" fullScreenTarget = "SettingsLegal" />
597- < View className = "flex-row items-center gap-4 p-4" >
598- < SymbolView
599- name = "info.circle"
600- size = { 22 }
601- tintColor = { icon }
602- type = "monochrome"
603- weight = "regular"
604- />
605- < Text className = "flex-1 text-lg text-foreground" > Version</ Text >
606- < View className = "items-end" >
607- < Text className = "text-lg text-foreground-muted" > { versionLabel } </ Text >
608- { bundleLabel ? (
609- < Text className = "text-xs text-foreground-muted/70" > { bundleLabel } </ Text >
610- ) : null }
611- </ View >
612- </ View >
700+ { Updates . isEnabled ? (
701+ < Pressable
702+ accessibilityLabel = "Check for updates"
703+ accessibilityRole = "button"
704+ disabled = { busy }
705+ onPress = { ( ) => void checkForUpdate ( ) }
706+ >
707+ { versionRow }
708+ </ Pressable >
709+ ) : (
710+ versionRow
711+ ) }
613712 </ SettingsSection >
614713 ) ;
615714}
616715
716+ function reportUpdateFailure ( result : AtomCommandResult < unknown , unknown > , fallback : string ) : void {
717+ reportAtomCommandResult ( result , { label : "app update check" } ) ;
718+ if ( result . _tag !== "Failure" || isAtomCommandInterrupted ( result ) ) return ;
719+ const error = squashAtomCommandFailure ( result ) ;
720+ Alert . alert ( "Update failed" , error instanceof Error ? error . message : fallback ) ;
721+ }
722+
617723function capitalize ( value : string ) : string {
618724 return value . length > 0 ? value . charAt ( 0 ) . toUpperCase ( ) + value . slice ( 1 ) : value ;
619725}
0 commit comments