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 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("connected") const [offlineMode] = useSetting(SETTINGS.offline_mode.key) const [superMode] = useSetting(SETTINGS.super_mode.key) const refreshApplets = useRefresh() const {theme} = useAppTheme() const disconnectionTimerRef = useRef(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 ( { push("/miniapps/settings/speech") }}> {translate("offlineMode:offlineMode")} ) } if (!superMode && displayStatus == "connected") { return null } return ( {config.label()} ) }