Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -36,7 +40,7 @@ export default function DashboardLayout({ children }: { children: React.ReactNod
<Sun className="h-4 w-4 rotate-0 scale-100 dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-4 w-4 rotate-90 scale-0 dark:rotate-0 dark:scale-100" />
</Button>
<Link href="/dashboard/notifications"><Button variant="ghost" size="icon" className="relative"><Bell className="h-4 w-4" /><span className="absolute top-1 right-1 h-2 w-2 rounded-full bg-red-500" /></Button></Link>
<Link href="/dashboard/notifications"><Button variant="ghost" size="icon" className="relative"><Bell className="h-4 w-4" />{unreadCount > 0 && <span className="absolute top-1 right-1 flex h-5 w-5 items-center justify-center rounded-full bg-red-500 text-white text-xs font-semibold">{unreadCount > 9 ? '9+' : unreadCount}</span>}</Button></Link>
</div>
</header>
<main className="flex-1 p-4 lg:p-8 pb-24 lg:pb-8">{children}</main>
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/use-notifications.ts
Original file line number Diff line number Diff line change
@@ -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<Notification[]>([])
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<Error | null>(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 }
}
Loading