File size: 5,165 Bytes
e1f81b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import {useEffect, useRef, useState} from "react"
import {TouchableOpacity, View} from "react-native"
import {Icon, Text, type IconTypes} from "@/components/ignite"
import {useEngineSnapshot} from "@/hooks/useEngineSnapshot"
import {translate} from "@/i18n"
import {engine, useRefresh} from "@mentra/engine"
import {BgTimer} from "@mentra/engine"
import {useAppTheme} from "@/contexts/ThemeContext"
import {SETTINGS, useSetting} from "@mentra/engine"
import {useNavigationStore} from "@/stores/navigation"
type DisplayStatus = "connected" | "warning" | "disconnected"
const STATUS_CONFIG: Record<DisplayStatus, {icon: IconTypes; label: () => string; bgClass: string; iconColor: string}> = {
connected: {
icon: "wifi",
label: () => translate("connection:connected"),
bgClass: "bg-primary",
iconColor: "#fff",
},
warning: {
icon: "wifi",
label: () => translate("connection:connecting"),
bgClass: "bg-chart-3",
iconColor: "#fff",
},
disconnected: {
icon: "wifi-off",
label: () => translate("connection:disconnected"),
bgClass: "bg-destructive",
iconColor: "#fff",
},
}
export default function WebsocketStatus() {
// Cloud-v2 runtime connection (the Cloud V1 websocket this pill used to
// render is retired).
const connectionStatus = useEngineSnapshot(
() => engine.session.status().status,
(onChange) => engine.session.onStatus(() => onChange()),
)
const [displayStatus, setDisplayStatus] = useState<DisplayStatus>("connected")
const [offlineMode] = useSetting(SETTINGS.offline_mode.key)
const [superMode] = useSetting(SETTINGS.super_mode.key)
const refreshApplets = useRefresh()
const {theme} = useAppTheme()
const disconnectionTimerRef = useRef<number | null>(null)
const DISCONNECTION_DELAY = 3000
const prevConnectionStatusRef = useRef(connectionStatus)
const {push} = useNavigationStore.getState()
// Track whether the WS was observed as disconnected long enough that we
// might genuinely have missed applet state changes. Flipped true by the
// DISCONNECTION_DELAY timer below. Cleared on the next CONNECTED after
// refresh fires. Under a reconnect storm (issue 101), the WS can flap
// CONNECTED → DISCONNECTED → CONNECTED within a sub-second cycle; a flap
// is not evidence we lost applet state, so refreshing on every CONNECTED
// was amplifying the storm into a matching REST-call storm. This ref only
// allows the refresh when the prior disconnect actually persisted past
// the 3-second threshold the user-visible "warning → disconnected"
// transition already respects.
const wasSustainedDisconnectedRef = useRef(false)
useEffect(() => {
const prevStatus = prevConnectionStatusRef.current
prevConnectionStatusRef.current = connectionStatus
console.log(`CLOUD-STATUS: useEffect: connectionStatus: ${connectionStatus}`)
if (connectionStatus === "connected") {
if (disconnectionTimerRef.current) {
BgTimer.clearTimeout(disconnectionTimerRef.current)
disconnectionTimerRef.current = null
}
setDisplayStatus("connected")
if (wasSustainedDisconnectedRef.current) {
wasSustainedDisconnectedRef.current = false
refreshApplets()
}
return
}
// Now you can compare:
if (prevStatus === "connected") {
// we just disconnected
setDisplayStatus("warning")
if (disconnectionTimerRef.current) {
BgTimer.clearTimeout(disconnectionTimerRef.current)
disconnectionTimerRef.current = null
}
disconnectionTimerRef.current = BgTimer.setTimeout(() => {
setDisplayStatus("disconnected")
wasSustainedDisconnectedRef.current = true
refreshApplets()
}, DISCONNECTION_DELAY)
return
}
return () => {
if (disconnectionTimerRef.current) {
BgTimer.clearTimeout(disconnectionTimerRef.current)
disconnectionTimerRef.current = null
}
}
}, [connectionStatus])
const config = STATUS_CONFIG[displayStatus]
if (offlineMode) {
return (
<TouchableOpacity
onPress={() => {
push("/miniapps/settings/speech")
}}>
<View
className={`flex-row items-center self-center align-middle justify-center py-1 px-2 rounded-full bg-destructive`}>
<Icon name="wifi-off" size={14} color={theme.colors.secondary_foreground} />
<Text className="text-secondary-foreground text-sm font-medium ml-2">
{translate("offlineMode:offlineMode")}
</Text>
</View>
</TouchableOpacity>
)
}
if (!superMode && displayStatus == "connected") {
return null
}
return (
<View
className={`flex-row items-center self-center align-middle justify-center py-1 px-2 rounded-full ${config.bgClass}`}>
<Icon name={config.icon} size={14} color={theme.colors.secondary_foreground} />
<Text className="text-secondary-foreground text-sm font-medium ml-2">{config.label()}</Text>
</View>
)
}
|