-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.js
More file actions
114 lines (101 loc) · 2.97 KB
/
utils.js
File metadata and controls
114 lines (101 loc) · 2.97 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
// src/utils/utils.js
import { useThemeConfig } from "@docusaurus/theme-common";
// import { useAuthenticator } from "@aws-amplify/ui-react";
import { useAuth } from "react-oidc-context";
import {
LOGIN_BUTTON,
LOGIN_PATH,
LOGOUT_BUTTON,
LOGOUT_PATH,
} from "./constants";
import { isEnvLocalLoaded } from './env';
export function useNavbarItemsMobile() {
if (!isEnvLocalLoaded()) {
// If .env.local is missing, do not render auth-related navbar items
return useThemeConfig().navbar.items;
}
const auth = useAuth();
let authElement;
// Btw this is disgusting code, it's just how Docusaurus expects any HTML content for parsing, as a string...
let html = auth.user?.profile.name + '  <img class="profile_icon" src="' + auth.user?.profile.picture + '" alt="Profile Picture"/>';
if (auth.isAuthenticated) {
authElement = {
id: 'logged-in-menu',
type: 'dropdown',
html: html,
position: 'right',
items: [
{
label: "Your Account",
to: "/Account"
},
{
label: LOGOUT_BUTTON,
to: LOGOUT_PATH
}
]
}
} else {
authElement = {
id: 'login-button',
label: LOGIN_BUTTON,
to: LOGIN_PATH
}
}
// TODO temporary casting until ThemeConfig type is improved
// return useThemeConfig().navbar.items;
let items = useThemeConfig().navbar.items;
items.push(authElement);
// remove irrelevant items
if (auth.isAuthenticated)
items = items.filter((x) => x.label !== LOGIN_BUTTON);
else {
items = items.filter((x) => x.type !== 'dropdown');
}
const uniqueItems = [...new Map(items.map((x) => [x.label, x])).values()];
return uniqueItems;
}
export function useNavbarItemsDesktop() {
if (!isEnvLocalLoaded()) {
// If .env.local is missing, do not render auth-related navbar items
return useThemeConfig().navbar.items;
}
const auth = useAuth();
let authElement;
// Btw this is disgusting code, it's just how Docusaurus expects any HTML content for parsing, as a string...
let html = '<img class="profile_icon" src="' + auth.user?.profile.picture + '" alt="Profile Picture"/>';
if (auth.isAuthenticated) {
authElement = {
type: 'dropdown',
html: html,
position: 'right',
items: [
{
label: "Your Account",
to: "/Account"
},
{
label: LOGOUT_BUTTON,
to: LOGOUT_PATH
}
]
}
} else {
authElement = {
label: LOGIN_BUTTON,
to: LOGIN_PATH
}
}
// TODO temporary casting until ThemeConfig type is improved
// return useThemeConfig().navbar.items;
let items = useThemeConfig().navbar.items;
items.push(authElement);
// remove irrelevant items
if (auth.isAuthenticated)
items = items.filter((x) => x.label !== LOGIN_BUTTON);
else {
items = items.filter((x) => x.type !== 'dropdown');
}
const uniqueItems = [...new Map(items.map((x) => [x.label, x])).values()];
return uniqueItems;
}