Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 1 addition & 143 deletions components/layout/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,12 @@
"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useMemo, useRef, useState } from "react";
import { apiFetch } from "@/lib/api";
import type { AlertListResponse, AlertItem } from "@/lib/types/alert";
import { useSidebar } from "./SidebarContext";
import { useNotifications } from "./NotificationContext";
import { useTheme } from "./ThemeContext";
import { User, Settings, Info, ChevronDown, Bell } from "lucide-react";

export function Header() {
const router = useRouter();
const { compactMode, toggle } = useSidebar();
const { notifHighRisk, notifMediumRisk } = useNotifications();
const { isDark, toggle: toggleTheme } = useTheme();
const [isProfileOpen, setIsProfileOpen] = useState(false);
const [isBellOpen, setIsBellOpen] = useState(false);
const [allAlerts, setAllAlerts] = useState<AlertItem[]>([]);

useEffect(() => {
apiFetch<AlertListResponse>("/alerts")
.then((res) => setAllAlerts(res.data))
.catch(() => {});
}, []);

function handleGoToAlerts() {
setIsBellOpen(false);
router.push("/alerts-settings");
}

const visibleNarratives = useMemo(() => {
return allAlerts.filter((n) => {
if (n.risk_level === "High" && notifHighRisk) return true;
if (n.risk_level === "Medium" && notifMediumRisk) return true;
return false;
});
}, [allAlerts, notifHighRisk, notifMediumRisk]);

const badgeCount = visibleNarratives.length;

const profileWrapRef = useRef<HTMLDivElement | null>(null);
const bellWrapRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
function onPointerDown(e: PointerEvent) {
if (profileWrapRef.current && e.target instanceof Node && !profileWrapRef.current.contains(e.target)) {
setIsProfileOpen(false);
}
if (bellWrapRef.current && e.target instanceof Node && !bellWrapRef.current.contains(e.target)) {
setIsBellOpen(false);
}
}
document.addEventListener("pointerdown", onPointerDown);
return () => document.removeEventListener("pointerdown", onPointerDown);
}, []);

return (
<header className="flex h-14 shrink-0 items-center justify-between border-b border-white/10 bg-[#1A1A1A] px-4 font-sans">
Expand Down Expand Up @@ -83,10 +35,8 @@ export function Header() {
</Link>
</div>

{/* Right: Theme toggle + Notifications + Profile */}
{/* Right: Theme toggle ONLY */}
<div className="flex items-center gap-4">

{/* Sun / Moon toggle */}
<button
type="button"
onClick={toggleTheme}
Expand All @@ -105,98 +55,6 @@ export function Header() {
</svg>
)}
</button>

<div className="relative" ref={bellWrapRef}>
<button
type="button"
onClick={() => setIsBellOpen((v) => !v)}
className="relative cursor-pointer rounded p-1.5 text-white transition-colors hover:bg-white/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/30"
>
<Bell className="size-5" />
{badgeCount > 0 && (
<span className="absolute -right-0.5 -top-0.5 flex size-4 items-center justify-center rounded-full bg-red-500 text-[9px] font-bold text-white">
{badgeCount}
</span>
)}
</button>

{isBellOpen && (
<div className="dropdown-enter absolute right-0 z-50 mt-2 w-80 overflow-hidden rounded-xl border border-zinc-200 bg-white shadow-lg dark:border-zinc-700 dark:bg-zinc-900">
<div className="border-b border-zinc-100 px-4 py-3 dark:border-zinc-700">
<p className="text-sm font-semibold text-zinc-900 dark:text-zinc-100">Notifications</p>
<p className="text-xs text-zinc-500 dark:text-zinc-400">{badgeCount} narrative alert{badgeCount !== 1 ? "s" : ""} detected</p>
</div>
<div className="max-h-72 divide-y divide-zinc-100 overflow-y-auto dark:divide-zinc-700">
{visibleNarratives.length === 0 ? (
<p className="px-4 py-4 text-center text-xs text-zinc-400 dark:text-zinc-500">No active alerts</p>
) : visibleNarratives.map((n) => (
<div key={n.id} className="flex items-start gap-3 px-4 py-3 transition-colors hover:bg-zinc-50 dark:hover:bg-zinc-800">
<span className={`mt-1 size-2 shrink-0 rounded-full ${n.risk_level === "High" ? "bg-red-500" : "bg-amber-400"}`} />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-zinc-900 line-clamp-1 dark:text-zinc-100">{n.title}</p>
<p className="mt-0.5 line-clamp-2 text-xs text-zinc-500 dark:text-zinc-400">{n.description}</p>
</div>
</div>
))}
</div>
<div className="border-t border-zinc-100 px-4 py-2.5 dark:border-zinc-700">
<button onClick={handleGoToAlerts} className="w-full rounded-lg bg-zinc-900 py-2 text-center text-xs font-medium text-white hover:bg-zinc-800 dark:bg-zinc-100 dark:text-zinc-900 dark:hover:bg-white">
View All Alerts
</button>
</div>
</div>
)}
</div>

{/* Profile Dropdown */}
<div className="relative" ref={profileWrapRef}>
<button
type="button"
onClick={() => setIsProfileOpen((v) => !v)}
className="flex items-center gap-3 hover:bg-white/10 p-1.5 rounded-lg transition-colors group"
>
<div className="hidden flex-col text-right sm:flex">
<span className="text-sm font-semibold text-white leading-none">UTDesign</span>
<span className="text-[10px] text-white/60">UT Dallas</span>
</div>
<div className="flex size-8 items-center justify-center rounded-full bg-zinc-700 border border-zinc-500 group-hover:border-white transition-all overflow-hidden">
<User className="text-white w-5 h-5" />
</div>
<ChevronDown className={`w-4 h-4 text-white/50 transition-transform ${isProfileOpen ? 'rotate-180' : ''}`} />
</button>

{isProfileOpen && (
<div className="dropdown-enter absolute right-0 mt-2 w-56 overflow-hidden rounded-xl border border-zinc-200 bg-white shadow-2xl z-[100] dark:border-zinc-700 dark:bg-zinc-900">
<div className="px-4 py-3 border-b border-zinc-100 bg-zinc-50/50 dark:border-zinc-700 dark:bg-zinc-800/50">
<p className="text-[10px] font-bold text-zinc-400 uppercase tracking-widest dark:text-zinc-500">Signed in as</p>
<p className="text-sm font-bold text-zinc-900 dark:text-zinc-100">admin@cs4485.com</p>
</div>

<div className="py-1">
<button
type="button"
onClick={() => { setIsProfileOpen(false); router.push('/alerts-settings'); }}
className="flex w-full items-center gap-3 px-4 py-2.5 text-left text-sm text-zinc-700 transition-colors hover:bg-zinc-50 dark:text-zinc-300 dark:hover:bg-zinc-800"
>
<Settings className="size-4 shrink-0 text-zinc-500 dark:text-zinc-400" />
System Settings
</button>
</div>

<div className="mx-2 h-px bg-zinc-100 dark:bg-zinc-700" />

<div className="py-1">
<button
onClick={() => { setIsProfileOpen(false); router.push('/help'); }}
className="w-full flex items-center gap-3 px-4 py-2.5 text-sm text-zinc-700 hover:bg-zinc-50 transition-colors dark:text-zinc-300 dark:hover:bg-zinc-800"
>
<Info className="w-4 h-4 text-zinc-500 dark:text-zinc-400" />
Help &amp; Support
</button>
</div>
</div>
)}
</div>
</div>
</header>
);
Expand Down
Loading