File size: 15,412 Bytes
eb0499d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | import { useState, useEffect, useContext } from 'react';
import { QueryClientContext } from '@tanstack/react-query';
import { useRealTimeFraudStream } from '../../hooks/useRealTimeFraudStream';
import {
apiClient,
formatApiError,
advanceInstitutionalSessionEpoch,
} from '../../api/client';
import { Activity, Radio, ShieldCheck, LogIn, LogOut } from 'lucide-react';
interface HeaderProps {
onMenuClick: () => void;
}
interface SessionUserProfile {
username: string;
bank_id: string;
roles: string[];
clearance_level?: number;
}
export default function Header({ onMenuClick }: HeaderProps) {
const docsUrl = (import.meta.env.VITE_API_URL ?? '').replace(/\/+$/, '') + '/docs';
const { status, streamSource, streamProvenance, latencyMs, totalStreamedTransactions } = useRealTimeFraudStream();
const queryClient = useContext(QueryClientContext);
const [sessionUser, setSessionUser] = useState<SessionUserProfile | null>(() => {
try {
const raw = localStorage.getItem('cfi_session_user');
if (raw) {
return JSON.parse(raw) as SessionUserProfile;
}
} catch {
// ignore storage errors
}
return null;
});
const [isLoginModalOpen, setIsLoginModalOpen] = useState(false);
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [loginError, setLoginError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
useEffect(() => {
const syncAuthFromStorage = async () => {
try {
const token = localStorage.getItem('cfi_token');
const rawUser = localStorage.getItem('cfi_session_user');
if (!token) {
setSessionUser(null);
return;
}
if (rawUser) {
setSessionUser(JSON.parse(rawUser) as SessionUserProfile);
}
const { data } = await apiClient.get<SessionUserProfile>('/api/v1/auth/me');
if (data?.username) {
localStorage.setItem('cfi_session_user', JSON.stringify(data));
if (data.bank_id) {
localStorage.setItem('cfi_tenant_id', data.bank_id);
}
setSessionUser(data);
}
} catch (err: unknown) {
const status = (err as { response?: { status?: number } })?.response?.status;
if (status === 401) {
await advanceInstitutionalSessionEpoch(queryClient, { preserveAuthCredentials: false });
setSessionUser(null);
}
}
};
void syncAuthFromStorage();
const handleStorageChange = () => {
void syncAuthFromStorage();
};
window.addEventListener('storage', handleStorageChange);
window.addEventListener('cfi-auth-changed', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
window.removeEventListener('cfi-auth-changed', handleStorageChange);
};
}, [queryClient]);
const handleLoginSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!username.trim() || !password) return;
setLoginError(null);
setIsSubmitting(true);
try {
const { data } = await apiClient.post<{
access_token: string;
refresh_token?: string;
user: SessionUserProfile;
}>('/api/v1/auth/login', {
username: username.trim(),
password,
});
await advanceInstitutionalSessionEpoch(queryClient, { preserveAuthCredentials: false });
localStorage.setItem('cfi_token', data.access_token);
if (data.refresh_token) {
localStorage.setItem('cfi_refresh_token', data.refresh_token);
}
if (data.user?.bank_id) {
localStorage.setItem('cfi_tenant_id', data.user.bank_id);
}
localStorage.setItem('cfi_session_user', JSON.stringify(data.user));
setSessionUser(data.user);
setPassword('');
setIsLoginModalOpen(false);
window.dispatchEvent(new Event('cfi-auth-changed'));
await queryClient?.invalidateQueries();
} catch (err: unknown) {
setLoginError(formatApiError(err, 'Institutional authentication failed.'));
} finally {
setIsSubmitting(false);
}
};
const handleSignOut = async () => {
try {
await apiClient.post('/api/v1/auth/logout');
} catch {
// Proceed with local session cleanup even if backend logout fails
}
await advanceInstitutionalSessionEpoch(queryClient, { preserveAuthCredentials: false });
setSessionUser(null);
window.dispatchEvent(new Event('cfi-auth-changed'));
await queryClient?.invalidateQueries();
};
return (
<>
<header className="h-14 border-b border-[var(--color-border)] bg-[var(--color-bg-secondary)]/80 backdrop-blur-md flex items-center px-4 md:px-6 sticky top-0 z-40">
{/* Menu toggle button on mobile */}
<button
onClick={onMenuClick}
className="p-2 -ml-2 mr-2 rounded-lg text-[var(--color-text-secondary)] hover:bg-[var(--color-bg-card)] md:hidden focus:outline-none focus:ring-1 focus:ring-[var(--color-accent-indigo)]/50"
aria-label="Open navigation menu"
>
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
</svg>
</button>
<div className="flex-1 min-w-0 flex items-center gap-3">
<h2 className="text-sm font-semibold text-slate-100 truncate">
Collaborative Fraud Intelligence Platform
</h2>
{/* Real-time WebSocket status badge */}
<div className="hidden sm:flex items-center gap-2">
{status === 'connected' ? (
streamSource === 'LIVE_CONNECTOR' ? (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-emerald-500/15 text-emerald-400 border border-emerald-500/30" title={`Live Ingestion Provider: ${streamProvenance}`}>
<span className="w-1.5 h-1.5 rounded-full bg-emerald-400 animate-ping" />
Live Connector WS ({latencyMs}ms)
</span>
) : streamSource === 'SIMULATED' ? (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-indigo-500/15 text-indigo-300 border border-indigo-500/30" title="Simulated Sandbox Demo Feed (Synthetic transactions)">
<Radio className="w-2.5 h-2.5 text-indigo-400 animate-pulse" />
Simulated Feed ({latencyMs}ms)
</span>
) : (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-amber-500/15 text-amber-300 border border-amber-500/30" title="WebSocket connected, but no live telemetry provider is currently configured.">
<span className="w-1.5 h-1.5 rounded-full bg-amber-400" />
Telemetry Standby ({latencyMs}ms)
</span>
)
) : status === 'mock_active' ? (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-indigo-500/15 text-indigo-300 border border-indigo-500/30" title="WebSocket disconnected. Deterministic simulated sandbox telemetry.">
<Radio className="w-2.5 h-2.5 text-indigo-400 animate-pulse" />
Simulated Stream (Offline)
</span>
) : status === 'connecting' ? (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-amber-500/15 text-amber-400 border border-amber-500/30">
<span className="w-1.5 h-1.5 rounded-full bg-amber-400 animate-pulse" />
Connecting...
</span>
) : (
<span className="flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold bg-slate-500/15 text-slate-400 border border-slate-500/30">
WS Offline
</span>
)}
{/* Live streamed transactions ticker */}
<span className="hidden lg:flex items-center gap-1 text-[11px] font-mono text-slate-400">
<Activity className="w-3 h-3 text-cyan-400" />
<span>{totalStreamedTransactions.toLocaleString()} txns</span>
</span>
</div>
</div>
<div className="flex items-center gap-2.5 md:gap-3 shrink-0">
{sessionUser ? (
<div className="flex items-center gap-2">
<span
data-testid="active-institutional-session"
className="flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-[11px] font-mono font-semibold bg-emerald-500/15 text-emerald-300 border border-emerald-500/30"
>
<ShieldCheck className="w-3.5 h-3.5 text-emerald-400" />
<span>{sessionUser.username}</span>
<span className="text-emerald-400/70">·</span>
<span data-testid="active-session-tenant">{sessionUser.bank_id.toUpperCase()}</span>
</span>
<button
type="button"
data-testid="institutional-sign-in-btn"
onClick={() => {
setLoginError(null);
setIsLoginModalOpen(true);
}}
className="px-2 py-1 rounded-lg text-[11px] font-medium bg-slate-800 hover:bg-slate-700 text-slate-300 border border-slate-700 transition-colors"
>
Switch Principal
</button>
<button
type="button"
data-testid="institutional-sign-out-btn"
onClick={handleSignOut}
className="p-1.5 rounded-lg text-slate-400 hover:text-red-300 hover:bg-red-500/10 border border-transparent hover:border-red-500/30 transition-colors"
title="Sign out of institutional session"
>
<LogOut className="w-3.5 h-3.5" />
</button>
</div>
) : (
<button
type="button"
data-testid="institutional-sign-in-btn"
onClick={() => {
setLoginError(null);
setIsLoginModalOpen(true);
}}
className="flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs font-medium bg-indigo-600/20 hover:bg-indigo-600/30 text-indigo-300 border border-indigo-500/40 transition-colors"
>
<LogIn className="w-3.5 h-3.5" />
<span>Institutional Sign In</span>
</button>
)}
<div className="w-px h-4 bg-[var(--color-border)]" />
<a
href={docsUrl}
target="_blank"
rel="noopener noreferrer"
className="text-xs text-[var(--color-text-muted)] hover:text-[var(--color-text-primary)] transition-colors"
>
API Docs ↗
</a>
<div className="w-px h-4 bg-[var(--color-border)]" />
<span className="text-xs font-mono text-[var(--color-text-muted)]">v2.4.1</span>
</div>
</header>
{isLoginModalOpen && (
<div
data-testid="institutional-login-modal"
className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm p-4"
>
<div className="w-full max-w-md rounded-xl border border-slate-700 bg-slate-900 p-6 shadow-2xl">
<div className="flex items-center justify-between mb-4">
<div>
<h3 className="text-base font-bold text-slate-100">Institutional Session Authentication</h3>
<p className="text-xs text-slate-400 mt-0.5">
Authenticate with your sovereign institutional credentials to bind tenant context.
</p>
</div>
<button
type="button"
onClick={() => setIsLoginModalOpen(false)}
className="text-slate-400 hover:text-slate-200 text-sm"
>
✕
</button>
</div>
{loginError && (
<div
data-testid="institutional-login-error"
className="mb-4 rounded-lg border border-red-500/40 bg-red-950/40 p-3 text-xs text-red-300"
>
{loginError}
</div>
)}
<form onSubmit={handleLoginSubmit} className="space-y-4">
<div>
<label
htmlFor="institutional-username-input"
className="block text-xs font-medium text-slate-300 mb-1"
>
Principal Username
</label>
<input
id="institutional-username-input"
data-testid="input-institutional-username"
type="text"
autoComplete="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="e.g., analyst_bank_a"
className="w-full rounded-lg border border-slate-700 bg-slate-950 px-3 py-2 text-sm text-slate-100 font-mono focus:border-indigo-500 focus:outline-none"
required
/>
</div>
<div>
<label
htmlFor="institutional-password-input"
className="block text-xs font-medium text-slate-300 mb-1"
>
Institutional Password
</label>
<input
id="institutional-password-input"
data-testid="input-institutional-password"
type="password"
autoComplete="current-password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Enter institutional credential"
className="w-full rounded-lg border border-slate-700 bg-slate-950 px-3 py-2 text-sm text-slate-100 font-mono focus:border-indigo-500 focus:outline-none"
required
/>
</div>
<div className="flex justify-end gap-2 pt-2">
<button
type="button"
data-testid="btn-cancel-institutional-login"
onClick={() => setIsLoginModalOpen(false)}
className="rounded-lg border border-slate-700 bg-slate-800 px-3.5 py-1.5 text-xs font-medium text-slate-300 hover:bg-slate-700"
>
Cancel
</button>
<button
type="submit"
data-testid="btn-submit-institutional-login"
disabled={isSubmitting || !username.trim() || !password}
className="rounded-lg bg-indigo-600 px-4 py-1.5 text-xs font-semibold text-white hover:bg-indigo-500 disabled:opacity-50"
>
{isSubmitting ? 'Authenticating...' : 'Sign In'}
</button>
</div>
</form>
</div>
</div>
)}
</>
);
}
|