|
|
@@ -1,12 +1,13 @@
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
-import { useNavigate } from 'react-router'
|
|
|
+import { Link, useNavigate } from 'react-router'
|
|
|
import { motion } from 'framer-motion'
|
|
|
import { RefreshCw, ShieldCheck } from 'lucide-react'
|
|
|
import { AlertItem } from '@/components/AlertItem'
|
|
|
import { SectionHeader } from '@/components/SectionHeader'
|
|
|
import { useNetPulse } from '@/data/DataProvider'
|
|
|
import { isHiddenByLevel } from '@/data/alertConfig'
|
|
|
+import { readUnreadOnly, writeUnreadOnly } from '@/lib/alertViewPrefs'
|
|
|
import { cn } from '@/lib/utils'
|
|
|
|
|
|
/** ⑥ Alertas recientes (home.md §⑥) */
|
|
|
@@ -28,30 +29,26 @@ export function RecentAlerts() {
|
|
|
[alerts, alertsConfig],
|
|
|
)
|
|
|
const recent = feed.slice(0, 4)
|
|
|
- // #916: toggle "solo no leídas" para mantener la tarjeta limpia en
|
|
|
- // operación normal. Persistente en localStorage (preferencia de vista,
|
|
|
- // no de negocio).
|
|
|
- const [unreadOnly, setUnreadOnly] = useState(() => {
|
|
|
- try {
|
|
|
- return localStorage.getItem('netpulse-recentalerts-unread-only') === '1'
|
|
|
- } catch {
|
|
|
- return false
|
|
|
- }
|
|
|
- })
|
|
|
+ // #916/#2217: toggle "solo no leídas", activado por defecto y compartido con
|
|
|
+ // la página Alertas (misma clave y mismo helper). Persistente en
|
|
|
+ // localStorage (preferencia de vista, no de negocio).
|
|
|
+ const [unreadOnly, setUnreadOnly] = useState(readUnreadOnly)
|
|
|
const toggleUnreadOnly = () => {
|
|
|
setUnreadOnly((prev) => {
|
|
|
const next = !prev
|
|
|
- try {
|
|
|
- localStorage.setItem('netpulse-recentalerts-unread-only', next ? '1' : '0')
|
|
|
- } catch {
|
|
|
- /* modo privado */
|
|
|
- }
|
|
|
+ writeUnreadOnly(next)
|
|
|
return next
|
|
|
})
|
|
|
}
|
|
|
const visible = (unreadOnly ? feed.filter((a) => !a.read && !readIds.has(a.id)) : recent).slice(0, 4)
|
|
|
- // El badge muestra el total de no leídas (como la campana), no solo de las 4 visibles
|
|
|
- const unread = Math.max(0, unreadAlerts - readIds.size)
|
|
|
+ // El badge muestra el total de no leídas (como la campana), no solo de las
|
|
|
+ // visibles. `readIds` descuenta lo marcado aquí mientras llega el snapshot
|
|
|
+ // del servidor (#891), sin restar dos veces cuando el provider ya recalculó
|
|
|
+ // el contador (#2217).
|
|
|
+ const unread = useMemo(
|
|
|
+ () => Math.max(0, unreadAlerts - feed.filter((a) => readIds.has(a.id) && !a.read).length),
|
|
|
+ [unreadAlerts, feed, readIds],
|
|
|
+ )
|
|
|
|
|
|
const markReadAndGo = (id: string) => {
|
|
|
setReadIds((prev) => new Set(prev).add(id))
|
|
|
@@ -84,11 +81,31 @@ export function RecentAlerts() {
|
|
|
<div className="-mx-3 flex flex-1 flex-col space-y-1">
|
|
|
{visible.length === 0 && (
|
|
|
// Estado vacío (#992): icono + microtexto, centrado en la tarjeta.
|
|
|
- <div className="flex flex-1 flex-col items-center justify-center gap-2 px-3 py-6 text-center">
|
|
|
- <ShieldCheck className="h-8 w-8 text-ok" strokeWidth={1.5} aria-hidden="true" />
|
|
|
- <p className="text-sm font-medium text-text-secondary">{t('home.recentAlerts.emptyTitle')}</p>
|
|
|
- <p className="text-caption text-text-muted">{t('home.recentAlerts.allRead')}</p>
|
|
|
- </div>
|
|
|
+ // #2217: con el filtro activo y todo leído, el estado lo dice y
|
|
|
+ // ofrece ver todas (desactiva el filtro) o abrir el registro.
|
|
|
+ unreadOnly && feed.length > 0 ? (
|
|
|
+ <div className="flex flex-1 flex-col items-center justify-center gap-2 px-3 py-6 text-center">
|
|
|
+ <ShieldCheck className="h-8 w-8 text-ok" strokeWidth={1.5} aria-hidden="true" />
|
|
|
+ <p className="text-sm font-medium text-text-secondary">{t('home.recentAlerts.emptyUnreadTitle')}</p>
|
|
|
+ <p className="text-caption text-text-muted">{t('home.recentAlerts.emptyUnreadDesc')}</p>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ onClick={toggleUnreadOnly}
|
|
|
+ className="mt-1 rounded-lg border border-accent/40 bg-accent/15 px-2.5 py-1 text-[11px] font-medium text-accent transition-colors hover:bg-accent/20"
|
|
|
+ >
|
|
|
+ {t('alerts.emptyFilteredAction')}
|
|
|
+ </button>
|
|
|
+ <Link to="/alerts" className="text-caption font-medium text-accent transition-colors hover:text-accent/80">
|
|
|
+ {t('home.recentAlerts.openLog')}
|
|
|
+ </Link>
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <div className="flex flex-1 flex-col items-center justify-center gap-2 px-3 py-6 text-center">
|
|
|
+ <ShieldCheck className="h-8 w-8 text-ok" strokeWidth={1.5} aria-hidden="true" />
|
|
|
+ <p className="text-sm font-medium text-text-secondary">{t('home.recentAlerts.emptyTitle')}</p>
|
|
|
+ <p className="text-caption text-text-muted">{t('home.recentAlerts.allRead')}</p>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
)}
|
|
|
{visible.map((a, i) => (
|
|
|
<motion.div
|