fix: implement global app-refocus listener and data recovery pattern in critical views to prevent infinite loading after app suspension

This commit is contained in:
2026-03-03 15:04:16 -05:00
parent cfe9286fcb
commit df0a4397f6
6 changed files with 96 additions and 12 deletions

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { computed, onMounted, onUnmounted } from 'vue'
import { RouterView, useRoute } from "vue-router";
import { useI18n } from 'vue-i18n'
import MainLayout from "./components/layouts/MainLayout.vue";
@ -20,6 +20,24 @@ const isAuthScreen = computed(() => {
return route.path === '/login' || route.path === '/register' || route.name === 'auth'
})
let lastHiddenAt: number | null = null
function handleVisibilityChange() {
if (document.visibilityState === 'hidden') {
lastHiddenAt = Date.now()
return
}
if (document.visibilityState === 'visible' && lastHiddenAt !== null) {
const secondsAway = (Date.now() - lastHiddenAt) / 1000
// Si pasaron más de 3 segundos, asumimos que el proceso pudo ser suspendido
if (secondsAway > 3) {
console.log(`SIBU | App recuperada tras ${secondsAway.toFixed(1)}s. Disparando refocus...`)
window.dispatchEvent(new CustomEvent('app-refocus'))
}
lastHiddenAt = null
}
}
onMounted(() => {
themeStore.applyTheme()
analyticsService.logEvent({
@ -30,6 +48,11 @@ onMounted(() => {
if (authStore.isAuthenticated) {
favoritesStore.loadFavorites()
}
document.addEventListener('visibilitychange', handleVisibilityChange)
})
onUnmounted(() => {
document.removeEventListener('visibilitychange', handleVisibilityChange)
})
</script>