-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbarActions.tsx
More file actions
145 lines (131 loc) · 4.34 KB
/
NavbarActions.tsx
File metadata and controls
145 lines (131 loc) · 4.34 KB
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
import { useState, useRef, useEffect } from "react";
import Image from "next/image";
import Link from "next/link";
import { Bell, Plus, Menu, X } from "lucide-react";
import { Button } from "@/components/ui/button";
import { UserProfileDropdown } from "./UserProfileDropdown";
interface Props {
createNewConfig: { text: string; href: string } | undefined;
mobileShowHamburger: boolean;
isMobileMenuOpen: boolean;
onToggleMobileMenu: () => void;
}
export const NavbarActions = ({
createNewConfig,
mobileShowHamburger,
isMobileMenuOpen,
onToggleMobileMenu,
}: Props) => {
const [hasUnreadNotifications, setHasUnreadNotifications] = useState(false);
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const handleNotificationClick = () => {
// TODO
setHasUnreadNotifications(false);
};
const handleAvatarClick = () => {
setIsDropdownOpen(!isDropdownOpen);
};
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
setIsDropdownOpen(false);
}
};
if (isDropdownOpen) {
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}
}, [isDropdownOpen]);
useEffect(() => {
const handleEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setIsDropdownOpen(false);
}
};
if (isDropdownOpen) {
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}
}, [isDropdownOpen]);
return (
<div className="flex items-center gap-2 sm:gap-3">
{createNewConfig && (
<>
{/* desktop only new listing button */}
<Button
variant="outline"
className="border-primary text-primary hover:bg-primary hover:text-primary-foreground hidden gap-2 md:flex"
asChild
>
<Link href={createNewConfig.href} aria-label={createNewConfig.text}>
<Plus className="h-4 w-4" />
<span>{createNewConfig.text}</span>
</Link>
</Button>
{/* mobile only new listing button (icon only) */}
<Button
variant="outline"
size="icon"
className="border-primary text-primary hover:bg-primary hover:text-primary-foreground md:hidden"
asChild
>
<Link href={createNewConfig.href} aria-label={createNewConfig.text}>
<Plus className="h-4 w-4" />
</Link>
</Button>
</>
)}
{/* notification bell */}
<Button
variant="ghost"
size="icon"
className="relative"
onClick={handleNotificationClick}
aria-label={`Notifications${hasUnreadNotifications ? " (unread)" : ""}`}
>
<Bell className="h-5 w-5" />
{hasUnreadNotifications && (
<span
className="bg-primary absolute top-1 right-1 h-2 w-2 rounded-full"
aria-hidden="true"
/>
)}
</Button>
{/* user avatar */}
<div className="relative" ref={containerRef}>
<Button
variant="ghost"
size="icon"
className="overflow-hidden rounded-full p-0 transition-opacity hover:opacity-80"
onClick={handleAvatarClick}
aria-label="User menu"
aria-expanded={isDropdownOpen}
>
<Image
src="/images/default-avatar.png"
alt="User avatar"
width={40}
height={40}
className="rounded-full object-cover"
/>
</Button>
<UserProfileDropdown isOpen={isDropdownOpen} onClose={() => setIsDropdownOpen(false)} />
</div>
{/* mobile only menu toggle */}
{mobileShowHamburger && (
<Button
variant="ghost"
size="icon"
className="md:hidden"
onClick={onToggleMobileMenu}
aria-label={isMobileMenuOpen ? "Close menu" : "Open menu"}
aria-expanded={isMobileMenuOpen}
aria-controls="mobile-menu"
>
{isMobileMenuOpen ? <X className="h-5 w-5" /> : <Menu className="h-5 w-5" />}
</Button>
)}
</div>
);
};