-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNavigationBar.tsx
More file actions
279 lines (270 loc) · 8.1 KB
/
Copy pathNavigationBar.tsx
File metadata and controls
279 lines (270 loc) · 8.1 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
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
import {
Link,
useNavigate,
useLocation,
useSearchParams,
} from "react-router-dom";
import { FaGlobe, FaSearch } from "react-icons/fa";
import { useAuth } from "../../config/AuthContext.tsx";
import { useAuth0 } from "@auth0/auth0-react";
import {
ACCESS_TOKEN,
LANGUAGE,
LOGGED_IN_VIA,
REFRESH_TOKEN,
} from "../../utils/constants.ts";
import { useTolgee, useTranslate } from "@tolgee/react";
import { setFontVariables } from "../../config/commonConfigs.ts";
import { useQueryClient } from "react-query";
import { useState, type FormEvent } from "react";
import { useCollectionColor } from "../../context/CollectionColorContext.tsx";
import { Button } from "../../components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "../../components/ui/dropdown-menu";
import NavSmallerScreen from "./NavSmallerScreen.tsx";
export const invalidateQueries = async (queryClient: any) => {
const queriesToInvalidate = [
"texts",
"topics",
"sheets",
"sidePanel",
"works",
"texts-versions",
"texts-content",
"sheets-user-profile",
"table-of-contents",
"collections",
"sub-collections",
"versions",
];
await Promise.all(
queriesToInvalidate.map((query) => queryClient.invalidateQueries(query)),
);
};
export const changeLanguage = async (
lng: string,
queryClient: any,
tolgee: any,
) => {
await tolgee.changeLanguage(lng);
sessionStorage.setItem("textLanguage", lng);
localStorage.setItem(LANGUAGE, lng);
setFontVariables(lng);
await invalidateQueries(queryClient);
};
const Navigation = () => {
const navigate = useNavigate();
const location = useLocation();
const { t } = useTranslate();
const {
isLoggedIn,
logout: pechaLogout,
isAuthLoading,
} = useAuth() as {
isLoggedIn: boolean;
logout: () => void;
isAuthLoading: boolean;
};
const { isAuthenticated, logout, isLoading: isAuth0Loading } = useAuth0();
const tolgee = useTolgee(["language"]);
const queryClient = useQueryClient();
const { collectionColor } = useCollectionColor();
const [searchTerm, setSearchTerm] = useState("");
const [params, setParams] = useSearchParams();
const navItems = [
{ to: "/", label: t("header.plans"), key: "plans" },
{ to: "/collections", label: t("header.text"), key: "collections" },
];
const currentLanguage = tolgee.getLanguage();
const isTibetan = currentLanguage === "bo-IN";
const routesWithoutColorBorder = [
"/",
"/collections",
"/login",
"/register",
"/signup",
"/community",
"/user",
];
const shouldHideColorBorder = routesWithoutColorBorder.includes(
location.pathname,
);
const handleLogout = (e: any) => {
e.preventDefault();
localStorage.removeItem(LOGGED_IN_VIA);
sessionStorage.removeItem(ACCESS_TOKEN);
localStorage.removeItem(REFRESH_TOKEN);
isLoggedIn && pechaLogout();
isAuthenticated && logout();
if (isLoggedIn && !isAuthenticated) {
navigate("/login");
}
};
const handleSearchSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (searchTerm.trim()) {
navigate(`/search?q=${encodeURIComponent(searchTerm.trim())}`);
setSearchTerm("");
}
};
const handleLangSelect = (lng: string) => {
changeLanguage(lng, queryClient, tolgee);
setParams((prev) => {
prev.set("lang", lng);
return prev;
});
};
const renderAuthButtons = (variant: "desktop" | "mobile") => {
if (isAuth0Loading || isAuthLoading) {
return <div className="text-sm text-faded-grey">Loading...</div>;
}
if (!isLoggedIn && !isAuthenticated) {
return (
<div
className={
variant === "desktop"
? "hidden md:flex items-center gap-2.5 text-sm"
: "flex flex-col gap-2 text-sm"
}
>
<Button
variant="outline"
onClick={() => navigate("/login")}
className="rounded text-faded-grey"
aria-label="Go to login"
>
{t("login.form.button.login_in")}
</Button>
<Button
variant="ghost"
onClick={() => navigate("/register")}
className="rounded text-faded-grey"
aria-label="Go to sign up"
>
{t("common.sign_up")}
</Button>
</div>
);
}
return (
<Button
variant="outline"
className={
variant === "desktop"
? "rounded text-faded-grey"
: "w-full rounded text-faded-grey"
}
onClick={handleLogout}
>
{t("profile.log_out")}
</Button>
);
};
const renderLanguageDropdown = () => {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
className="flex items-center justify-center p-1.5 rounded hover:bg-accent transition-colors"
aria-label="Change language"
>
<FaGlobe className="text-faded-grey" />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="min-w-[120px]">
<DropdownMenuItem onClick={() => handleLangSelect("en")}>
English
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleLangSelect("bo-IN")}>
བོད་ཡིག
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleLangSelect("zh-Hans-CN")}>
中文
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
};
return (
<div
className={`${isTibetan && "text-sm"} overalltext bg-navbar h-[60px] flex justify-between items-center w-full px-4 md:px-7`}
style={{
borderBottom: `2px solid ${shouldHideColorBorder ? "#E7E5E4" : collectionColor || "#E7E5E4"}`,
}}
>
<div className="flex items-center gap-x-4">
<Link
to="/"
className="flex items-center"
onClick={(e) => {
if (location.pathname === "/") {
e.preventDefault();
window.location.reload();
}
}}
>
<img
className="h-[30px]"
src="/img/webuddhist_logo.svg"
alt="Webuddhist"
/>
</Link>
<div className={`hidden md:flex space-x-8`}>
{navItems.map((navItem) => (
<Link
key={navItem.key}
className={`no-underline text-faded-grey font-medium ${isTibetan ? "text-sm" : "text-base"} hover:underline transition-all`}
to={navItem.to}
>
{navItem.label}
</Link>
))}
</div>
</div>
<div className="flex items-center space-x-2">
<form
className="hidden md:flex items-center rounded-lg border border-custom-border bg-search-background"
onSubmit={handleSearchSubmit}
>
<FaSearch className="ml-1.5 text-faded-grey" />
<input
type="search"
placeholder={t("common.placeholder.search")}
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full border-none bg-transparent outline-none px-1 py-1.5 content"
/>
</form>
{renderAuthButtons("desktop")}
<div className="hidden md:block">
{(isAuthenticated || isLoggedIn) && (
<Button
variant="ghost"
onClick={() => navigate("/profile")}
className="rounded text-faded-grey"
>
{t("header.profileMenu.profile")}
</Button>
)}
</div>
{renderLanguageDropdown()}
<NavSmallerScreen
searchTerm={searchTerm}
onSearchTermChange={setSearchTerm}
onSearchSubmit={handleSearchSubmit}
navItems={navItems}
renderAuthButtons={renderAuthButtons}
isAuthenticated={isAuthenticated}
isLoggedIn={isLoggedIn}
onProfileNavigate={() => navigate("/profile")}
translate={t}
/>
</div>
</div>
);
};
export default Navigation;