-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHarmonySidebar.js
More file actions
599 lines (576 loc) · 18 KB
/
HarmonySidebar.js
File metadata and controls
599 lines (576 loc) · 18 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import React, { useState } from "react";
import {
Box,
ListItem,
ListItemButton,
ListItemIcon,
Typography,
Avatar,
Tooltip,
Menu,
MenuItem,
Divider,
useMediaQuery,
useTheme,
} from "@mui/material";
import { Link, useLocation } from "react-router-dom";
import { LayoutGrid } from "lucide-react";
import { useAuth } from "../contexts/AuthContext";
import { useData } from "../contexts/DataContext";
import { Logout, JoinInner } from "@mui/icons-material/";
import GoogleIcon from "@mui/icons-material/Google";
import GitHubIcon from "@mui/icons-material/GitHub";
import TwitterIcon from "@mui/icons-material/Twitter";
// Get current domain for dynamic links
const getCurrentDomain = () => {
if (typeof window !== "undefined") {
// Handle local development with different ports
if (
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
) {
// React app runs on port 3000, DiscoveryNext runs on port 3222
return "http://localhost:3222";
}
return window.location.origin;
}
return "https://harmonydata.ac.uk"; // fallback for SSR
};
// Get the correct path for DiscoveryNext links
const getDiscoveryNextPath = (path) => {
if (typeof window !== "undefined") {
// Handle local development - DiscoveryNext is on root
if (
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
) {
return path; // e.g., "/studies" becomes "/studies"
}
// Production - DiscoveryNext is under /search
return `/search${path}`; // e.g., "/studies" becomes "/search/studies"
}
return `/search${path}`; // fallback for SSR
};
const navigationItems = [
{
text: "Search",
icon: "/app/icons/discover.svg",
activeIcon: "/app/icons/discover-active.svg",
href: `${getCurrentDomain()}${getDiscoveryNextPath("/")}`,
},
{
text: "Browse",
icon: "",
activeIcon: "",
href: `${getCurrentDomain()}${getDiscoveryNextPath("/studies")}`,
},
{
text: "Explore",
icon: "/app/icons/explore.svg",
activeIcon: "/app/icons/explore-active.svg",
href: `${getCurrentDomain()}${getDiscoveryNextPath("/explore")}`,
},
{
text: "Compare",
icon: "/app/icons/compare.svg",
activeIcon: "/app/icons/compare-active.svg",
href: `${getCurrentDomain()}${getDiscoveryNextPath("/compare")}`,
},
{
text: "Saves",
icon: "/app/icons/saves.svg",
activeIcon: "/app/icons/saves-active.svg",
href: `${getCurrentDomain()}${getDiscoveryNextPath("/saves")}`,
},
{
text: "Harmonise",
icon: "/app/icons/harmonise.svg",
activeIcon: "/app/icons/harmonise-active.svg",
href: "/",
},
];
const settings = ["My Harmony", "Logout"];
const SettingsIcons = {
"My Harmony": <JoinInner />,
Logout: <Logout />,
};
export default function HarmonySidebar() {
const location = useLocation();
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("md"));
const [anchorUser, setAnchorUser] = useState(null);
const [apiVersion, setApiVersion] = useState(null);
const {
currentUser,
logout,
signInWithGoogle,
signInWithGitHub,
signInWithTwitter,
} = useAuth();
const { getVersion } = useData();
// Determine if an item is active
const isActive = (item) => {
if (item.text === "Harmonise") {
// Harmonise is active when we're on any /app path
return location.pathname.startsWith("/app");
} else {
// Other items are active when the pathname matches exactly
return location.pathname === item.href;
}
};
// User menu handlers
const handleOpenUserMenu = (event) => {
setAnchorUser(event.currentTarget);
};
const handleUserMenuClick = (menuItem) => {
switch (menuItem) {
case "Logout":
handleCloseUserMenu();
console.log("logging out");
logout();
break;
default:
}
};
const handleCloseUserMenu = () => {
setAnchorUser(null);
};
// Load API version
React.useEffect(() => {
getVersion()
.then((ver) => {
setApiVersion(ver);
})
.catch((e) => console.error("ERROR: API unreachable", e));
}, [getVersion]);
return (
<>
{/* Mobile Sidebar */}
<Box
component="nav"
sx={{
width: "100%",
height: 64,
position: "fixed",
top: 0,
left: 0,
bgcolor: "#FAF8FF",
borderBottom: "1px solid",
borderColor: "grey.200",
zIndex: 1200,
display: "flex",
alignItems: "center",
justifyContent: "center",
gap: 2,
px: 2,
// Hide on desktop
"@media (min-width: 900px)": {
display: "none",
},
}}
>
{/* Logo */}
<Box
sx={{
position: "absolute",
left: 2,
display: "flex",
alignItems: "center",
}}
>
<Link to="/app/" style={{ textDecoration: "none" }}>
<img
src="/app/harmony.png"
alt="Harmony Logo"
width={40}
height={40}
style={{ objectFit: "contain" }}
/>
</Link>
</Box>
{/* Navigation Items - Hidden on mobile, moved to avatar menu */}
<Box
sx={{
display: { xs: "none", sm: "flex" },
gap: 2,
alignItems: "center",
}}
>
{navigationItems.map((item) => {
const isExternal = item.href.startsWith("http");
return (
<ListItemButton
disabled={isExternal}
key={item.text}
component={isExternal ? "a" : Link}
href={isExternal ? item.href : undefined}
to={isExternal ? undefined : item.href}
selected={isActive(item)}
sx={{
flexDirection: "column",
minWidth: 48,
minHeight: 48,
px: 1,
py: 0.5,
borderRadius: 2,
color: isActive(item) ? "#2E5FFF" : "#444653",
display: "flex",
alignItems: "center",
justifyContent: "center",
bgcolor: isActive(item)
? "rgba(46, 95, 255, 0.08)"
: "inherit",
"&.Mui-selected": {
bgcolor: "rgba(46, 95, 255, 0.08)",
},
}}
>
<ListItemIcon
sx={{
minWidth: 0,
color: "inherit",
position: "relative",
mb: 0.5,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
{item.text === "Browse" ? (
<LayoutGrid
size={16}
color={isActive(item) ? "#2E5FFF" : "#444653"}
style={{
position: "relative",
zIndex: 1,
}}
/>
) : (
<img
src={
item.text === "Harmonise"
? item.activeIcon
: isActive(item)
? item.activeIcon
: item.icon
}
alt={`${item.text} icon`}
width={16}
height={16}
style={{
position: "relative",
zIndex: 1,
}}
/>
)}
</ListItemIcon>
<Typography
variant="caption"
sx={{
color: "#444653",
fontSize: "10px",
fontWeight: 500,
textAlign: "center",
}}
>
{item.text}
</Typography>
</ListItemButton>
);
})}
</Box>
{/* User Avatar on Mobile - Right Side */}
<Box sx={{ position: "absolute", right: 16 }}>
<Tooltip title="My Harmony">
<Avatar
key={(currentUser && currentUser.uid) || "anonUser"}
src={currentUser && currentUser.photoURL}
imgProps={{ referrerPolicy: "no-referrer" }}
onClick={handleOpenUserMenu}
sx={{
display: "flex",
cursor: "pointer",
width: 40,
height: 40,
}}
>
{currentUser &&
!currentUser.photoURL &&
currentUser.email.substring(1).toUpperCase()}
</Avatar>
</Tooltip>
</Box>
</Box>
{/* Desktop Sidebar */}
<Box
component="nav"
sx={{
width: 72,
borderRight: "1px solid",
borderColor: "grey.200",
height: "100vh",
position: "fixed",
left: 0,
top: 0,
bgcolor: "#FAF8FF",
overflow: "hidden",
// Hide on mobile
"@media (max-width: 899px)": {
display: "none",
},
}}
>
{/* Logo */}
<Box sx={{ p: 1, pt: 3, display: "flex", justifyContent: "center" }}>
<Link to="/app/" style={{ textDecoration: "none" }}>
<img
src="/app/harmony.png"
alt="Harmony Logo"
width={64}
height={64}
style={{ objectFit: "contain" }}
/>
</Link>
</Box>
{/* Navigation Items */}
<Box
sx={{
position: "absolute",
top: "50%",
transform: "translateY(-50%)",
width: "100%",
}}
>
{navigationItems.map((item) => {
const isExternal = item.href.startsWith("http");
return (
<ListItem key={item.text} disablePadding>
<ListItemButton
disabled={isExternal}
component={isExternal ? "a" : Link}
href={isExternal ? item.href : undefined}
to={isExternal ? undefined : item.href}
selected={isActive(item)}
sx={{
minHeight: 48,
justifyContent: "center",
px: 2.5,
flexDirection: "column",
color: isActive(item) ? "#2E5FFF" : "#444653",
"&.Mui-selected": {
bgcolor: "rgba(46, 95, 255, 0.08)",
},
}}
>
<ListItemIcon
sx={{
minWidth: 0,
color: "inherit",
position: "relative",
width: "100%",
display: "flex",
justifyContent: "center",
}}
>
{item.text === "Browse" ? (
<LayoutGrid
size={20}
style={{
position: "relative",
zIndex: 1,
color: isActive(item) ? "#2E5FFF" : "#444653",
}}
/>
) : (
<img
src={
item.text === "Harmonise"
? item.activeIcon
: isActive(item)
? item.activeIcon
: item.icon
}
alt={`${item.text} icon`}
width={20}
height={20}
style={{
position: "relative",
zIndex: 1,
}}
/>
)}
</ListItemIcon>
<Typography
variant="caption"
sx={{
color: "#444653",
fontSize: "12px",
fontWeight: 500,
textAlign: "center",
}}
>
{item.text}
</Typography>
</ListItemButton>
</ListItem>
);
})}
</Box>
{/* User Avatar at Bottom */}
<Box
sx={{
position: "absolute",
bottom: 16,
left: "50%",
transform: "translateX(-50%)",
display: "flex",
justifyContent: "center",
}}
>
<Tooltip title="My Harmony">
<Avatar
key={(currentUser && currentUser.uid) || "anonUser"}
src={currentUser && currentUser.photoURL}
imgProps={{ referrerPolicy: "no-referrer" }}
onClick={handleOpenUserMenu}
sx={{
display: "flex",
cursor: "pointer",
width: 48,
height: 48,
}}
>
{currentUser &&
!currentUser.photoURL &&
currentUser.email.substring(1).toUpperCase()}
</Avatar>
</Tooltip>
<Menu
sx={{
mt: isMobile ? "45px" : "-8px",
maxWidth: "50%",
"& .MuiPaper-root": {
transform: isMobile ? "none" : "translateY(-100%)",
},
}}
id="menu-appbar"
anchorEl={anchorUser}
anchorOrigin={{
vertical: isMobile ? "bottom" : "top",
horizontal: "right",
}}
transformOrigin={{
vertical: isMobile ? "top" : "bottom",
horizontal: "right",
}}
open={Boolean(anchorUser)}
onClose={handleCloseUserMenu}
>
{/* Navigation items for mobile */}
{isMobile &&
navigationItems.map((item) => {
const isExternal = item.href.startsWith("http");
const itemIsActive = isActive(item);
return (
<MenuItem
key={item.text}
component={isExternal ? "a" : Link}
href={isExternal ? item.href : undefined}
to={isExternal ? undefined : item.href}
onClick={handleCloseUserMenu}
sx={{
color: itemIsActive ? "#2E5FFF" : "inherit",
bgcolor: itemIsActive
? "rgba(46, 95, 255, 0.08)"
: "inherit",
}}
>
{item.text === "Browse" ? (
<LayoutGrid
size={20}
color={itemIsActive ? "#2E5FFF" : "#444653"}
/>
) : (
<img
src={itemIsActive ? item.activeIcon : item.icon}
alt={`${item.text} icon`}
width={20}
height={20}
/>
)}
<Typography textAlign="center" sx={{ pl: 1 }}>
{item.text}
</Typography>
</MenuItem>
);
})}
{/* Divider between navigation and settings */}
{isMobile && <Divider />}
{settings.map((setting) => (
<MenuItem
key={setting}
onClick={() => handleUserMenuClick(setting)}
disabled={!currentUser}
>
{SettingsIcons[setting]}
<Typography textAlign="center" sx={{ pl: 1 }}>
{setting}
</Typography>
</MenuItem>
))}
{!currentUser && [
<Divider key="oauthSigninDiv" />,
<p
key="oauthSigninText"
style={{ margin: "0 0.5rem", textAlign: "center" }}
>
Signing in with one of the OAuth providers below allows you
access to My Harmony where you can save and share your
harmonisations.
</p>,
]}
{!currentUser && (
<MenuItem
key="SSOGoogle"
onClick={() => signInWithGoogle().then(handleCloseUserMenu)}
>
<GoogleIcon />
<Typography textAlign="center" sx={{ pl: 1 }}>
Sign in with Google
</Typography>
</MenuItem>
)}
{!currentUser && (
<MenuItem
key="SSOGithub"
onClick={() => signInWithGitHub().then(handleCloseUserMenu)}
>
<GitHubIcon />
<Typography textAlign="center" sx={{ pl: 1 }}>
Sign in with GitHub
</Typography>
</MenuItem>
)}
{!currentUser && (
<MenuItem
key="SSOTwitter"
onClick={() => signInWithTwitter().then(handleCloseUserMenu)}
>
<TwitterIcon />
<Typography textAlign="center" sx={{ pl: 1 }}>
Sign in with Twitter
</Typography>
</MenuItem>
)}
<Divider key="versionDiv" />
{apiVersion && (
<Typography sx={{ mx: 1 }}>
Harmony API version: {apiVersion}
</Typography>
)}
</Menu>
</Box>
</Box>
</>
);
}