diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx index fabd365..ee0bad8 100644 --- a/src/app/dashboard/layout.tsx +++ b/src/app/dashboard/layout.tsx @@ -9,11 +9,15 @@ import { Bell, Search, Moon, Sun } from "lucide-react" import { useTheme } from "next-themes" import Link from "next/link" import { DashboardSkeleton } from "@/components/shared/skeletons" +import { useNotifications } from "@/hooks/use-notifications" export default function DashboardLayout({ children }: { children: React.ReactNode }) { const { isAuthenticated, isLoading } = useAuth() const router = useRouter() const { theme, setTheme } = useTheme() + const { data: notifications } = useNotifications() + + const unreadCount = notifications?.filter(n => !n.read).length ?? 0 useEffect(() => { if (!isLoading && !isAuthenticated) router.push("/auth") @@ -36,7 +40,7 @@ export default function DashboardLayout({ children }: { children: React.ReactNod - +
{children}
diff --git a/src/hooks/use-notifications.ts b/src/hooks/use-notifications.ts new file mode 100644 index 0000000..1240116 --- /dev/null +++ b/src/hooks/use-notifications.ts @@ -0,0 +1,36 @@ +import { useEffect, useState } from "react" + +export interface Notification { + id: string + title: string + message: string + type: "warning" | "success" | "info" | "error" + read: boolean + createdAt: string +} + +export function useNotifications() { + const [data, setData] = useState([]) + const [isLoading, setIsLoading] = useState(true) + const [error, setError] = useState(null) + + useEffect(() => { + const fetchNotifications = async () => { + try { + setIsLoading(true) + const response = await fetch("/api/notifications") + if (!response.ok) throw new Error("Failed to fetch notifications") + const notifications = await response.json() + setData(notifications) + } catch (err) { + setError(err instanceof Error ? err : new Error("Unknown error")) + } finally { + setIsLoading(false) + } + } + + fetchNotifications() + }, []) + + return { data, isLoading, error } +}