|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { useUnifiedData } from '../../hooks/useUnifiedData'; |
| 4 | +import { useDashboardConfig } from '../../hooks/useDashboardConfig'; |
| 5 | +import { useFinancialSummary } from '../../hooks/useFinancialSummary'; |
| 6 | +import { AnomalyAlerts } from './AnomalyAlerts'; |
| 7 | + |
| 8 | +type DashboardAlertsDropdownProps = { |
| 9 | + selectedMonth: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export function DashboardAlertsDropdown({ selectedMonth }: DashboardAlertsDropdownProps) { |
| 13 | + const { t } = useTranslation(); |
| 14 | + const [open, setOpen] = useState(false); |
| 15 | + const rootRef = useRef<HTMLDivElement>(null); |
| 16 | + |
| 17 | + const { data: unifiedTransactions, isLoading } = useUnifiedData(); |
| 18 | + const { config } = useDashboardConfig(); |
| 19 | + const transactions = unifiedTransactions || []; |
| 20 | + const summary = useFinancialSummary( |
| 21 | + transactions, |
| 22 | + selectedMonth, |
| 23 | + config.ccPaymentDate, |
| 24 | + config.forecastMonths ?? 6, |
| 25 | + config.customCCKeywords ?? [] |
| 26 | + ); |
| 27 | + |
| 28 | + const anomalies = summary.anomalies ?? []; |
| 29 | + const count = anomalies.length; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (!open) return; |
| 33 | + const onDoc = (e: MouseEvent) => { |
| 34 | + if (rootRef.current && !rootRef.current.contains(e.target as Node)) { |
| 35 | + setOpen(false); |
| 36 | + } |
| 37 | + }; |
| 38 | + const onKey = (e: KeyboardEvent) => { |
| 39 | + if (e.key === 'Escape') setOpen(false); |
| 40 | + }; |
| 41 | + document.addEventListener('mousedown', onDoc); |
| 42 | + document.addEventListener('keydown', onKey); |
| 43 | + return () => { |
| 44 | + document.removeEventListener('mousedown', onDoc); |
| 45 | + document.removeEventListener('keydown', onKey); |
| 46 | + }; |
| 47 | + }, [open]); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="relative shrink-0" ref={rootRef}> |
| 51 | + <button |
| 52 | + type="button" |
| 53 | + onClick={() => setOpen((v) => !v)} |
| 54 | + className={`relative p-2.5 rounded-full transition-colors ${open ? 'bg-indigo-100 text-indigo-600' : 'text-gray-400 hover:text-gray-600 hover:bg-gray-100'}`} |
| 55 | + title={t('dashboard.toggle_alerts')} |
| 56 | + aria-expanded={open} |
| 57 | + aria-haspopup="true" |
| 58 | + > |
| 59 | + <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 60 | + <path |
| 61 | + strokeLinecap="round" |
| 62 | + strokeLinejoin="round" |
| 63 | + strokeWidth={2} |
| 64 | + d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" |
| 65 | + /> |
| 66 | + </svg> |
| 67 | + {count > 0 && !open && ( |
| 68 | + <span className="absolute top-1.5 end-1.5 w-2 h-2 bg-red-500 rounded-full animate-pulse border border-white" /> |
| 69 | + )} |
| 70 | + </button> |
| 71 | + |
| 72 | + {open && ( |
| 73 | + <div |
| 74 | + className="absolute end-0 top-full mt-1.5 z-50 w-[min(100vw-2rem,22rem)] rounded-xl border border-gray-200 bg-white shadow-lg ring-1 ring-black/5" |
| 75 | + role="menu" |
| 76 | + > |
| 77 | + <div className="border-b border-gray-100 px-3 py-2"> |
| 78 | + <p className="text-xs font-semibold uppercase tracking-wide text-gray-500"> |
| 79 | + {t('dashboard.alerts_title')} |
| 80 | + </p> |
| 81 | + </div> |
| 82 | + <div className="max-h-[min(60vh,24rem)] overflow-y-auto p-2"> |
| 83 | + {isLoading ? ( |
| 84 | + <p className="px-2 py-6 text-center text-sm text-gray-400">{t('common.loading')}</p> |
| 85 | + ) : transactions.length === 0 ? ( |
| 86 | + <p className="px-2 py-6 text-center text-sm text-gray-500">{t('dashboard.select_data')}</p> |
| 87 | + ) : count === 0 ? ( |
| 88 | + <p className="px-2 py-6 text-center text-sm text-gray-500">{t('dashboard.no_alerts')}</p> |
| 89 | + ) : ( |
| 90 | + <AnomalyAlerts anomalies={anomalies} className="space-y-2 mb-0" /> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments