Просмотр исходного кода

A4(D65-5 app): displayName en AuthContext + refresh bajo demanda en AuthGate + saludo HeroStrip (live y demo localStorage)

agent 4 месяцев назад
Родитель
Сommit
76fe77ae73
3 измененных файлов с 43 добавлено и 4 удалено
  1. 22 2
      app/src/components/AuthGate.tsx
  2. 2 0
      app/src/data/AuthContext.tsx
  3. 19 2
      app/src/sections/HeroStrip.tsx

+ 22 - 2
app/src/components/AuthGate.tsx

@@ -26,9 +26,17 @@ async function checkSession(): Promise<{ state: GateState; auth: AuthUser | null
     if (!res.ok) return { state: 'demo', auth: null }
     // La preview estática responde HTML con 200 (SPA fallback): no es sesión
     if (!(res.headers.get('content-type') ?? '').includes('application/json')) return { state: 'demo', auth: null }
-    const data = (await res.json()) as { user?: string; role?: 'admin' | 'user'; language?: 'auto' | 'es' | 'en' }
+    const data = (await res.json()) as {
+      user?: string
+      role?: 'admin' | 'user'
+      language?: 'auto' | 'es' | 'en'
+      displayName?: string
+    }
     if (!data?.user) return { state: 'demo', auth: null }
-    return { state: 'authed', auth: { user: data.user, role: data.role ?? 'user', language: data.language ?? 'auto' } }
+    return {
+      state: 'authed',
+      auth: { user: data.user, role: data.role ?? 'user', language: data.language ?? 'auto', displayName: data.displayName ?? '' },
+    }
   } catch {
     return { state: 'demo', auth: null }
   }
@@ -82,6 +90,18 @@ export function AuthGate({ children }: { children: React.ReactNode }) {
     // eslint-disable-next-line react-hooks/exhaustive-deps
   }, [])
 
+  // Refetch bajo demanda (SPEC-65 D65-5): tras cambiar displayName/idioma, la
+  // página emite 'netpulse-auth-refresh' y el contexto se actualiza sin reload.
+  useEffect(() => {
+    const onRefresh = () => {
+      void checkSession().then(({ state: s, auth: a }) => {
+        if (s === 'authed') setAuth(a)
+      })
+    }
+    window.addEventListener('netpulse-auth-refresh', onRefresh)
+    return () => window.removeEventListener('netpulse-auth-refresh', onRefresh)
+  }, [])
+
   if (state === 'loading') return <GateSkeleton />
   if (state === 'login') return <Navigate to="/login" replace />
   return <AuthProvider auth={auth}>{children}</AuthProvider>

+ 2 - 0
app/src/data/AuthContext.tsx

@@ -9,6 +9,8 @@ export interface AuthUser {
   role: 'admin' | 'user'
   /** 'auto' (navegador) | 'es' | 'en' — fuente de verdad: users.language */
   language?: 'auto' | 'es' | 'en'
+  /** Nombre para saludar (SPEC-65 D65-5); '' = usar username */
+  displayName?: string
 }
 
 const AuthContext = createContext<AuthUser | null>(null)

+ 19 - 2
app/src/sections/HeroStrip.tsx

@@ -1,5 +1,5 @@
 import { motion, useReducedMotion } from 'framer-motion'
-import { Fragment } from 'react'
+import { Fragment, useEffect, useState } from 'react'
 import { ArrowDown, ArrowUp, Gauge, MonitorSmartphone } from 'lucide-react'
 import { useTranslation } from 'react-i18next'
 import { healthLabel } from '@/i18n'
@@ -53,7 +53,24 @@ export function HeroStrip() {
   const { refreshKey } = useDashboard()
   const { deviceTotals, health: healthScore, wan } = useNetPulse()
   const auth = useAuth()
-  const greeting = t(greetingKey()) + (auth?.user ? t('home.greetingName', { name: auth.user }) : '')
+  // SPEC-65 D65-5: saluda con displayName || username. En demo (sin backend)
+  // el nombre vive en localStorage ('netpulse-displayname').
+  const [demoName, setDemoName] = useState('')
+  useEffect(() => {
+    if (auth) return
+    const read = () => {
+      try {
+        setDemoName(localStorage.getItem('netpulse-displayname') ?? '')
+      } catch {
+        /* modo privado */
+      }
+    }
+    read()
+    window.addEventListener('netpulse-auth-refresh', read)
+    return () => window.removeEventListener('netpulse-auth-refresh', read)
+  }, [auth])
+  const name = auth ? auth.displayName || auth.user : demoName
+  const greeting = t(greetingKey()) + (name ? t('home.greetingName', { name }) : '')
   const words = greeting.split(' ')
 
   return (