forked from morpheus65535/bazarr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbar.tsx
More file actions
345 lines (304 loc) · 8.27 KB
/
Navbar.tsx
File metadata and controls
345 lines (304 loc) · 8.27 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
import React, {
createContext,
FunctionComponent,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { matchPath, NavLink, RouteObject, useLocation } from "react-router";
import {
Anchor,
AppShell,
Badge,
Collapse,
Divider,
Group,
Stack,
Text,
useComputedColorScheme,
useMantineColorScheme,
} from "@mantine/core";
import { useHover } from "@mantine/hooks";
import {
faHeart,
faMoon,
faSun,
IconDefinition,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import clsx from "clsx";
import { Action } from "@/components";
import { useNavbar } from "@/contexts/Navbar";
import { useRouteItems } from "@/Router";
import { CustomRouteObject, Route } from "@/Router/type";
import { BuildKey, pathJoin } from "@/utilities";
import { LOG } from "@/utilities/console";
import styles from "./Navbar.module.scss";
const Selection = createContext<{
selection: string | null;
select: (path: string | null) => void;
}>({
selection: null,
select: () => {
LOG("error", "Selection context not initialized");
},
});
function useSelection() {
return useContext(Selection);
}
function useBadgeValue(route: Route.Item) {
const { badge, children } = route;
return useMemo(() => {
if (typeof badge === "string") {
return badge;
}
let value = badge ?? 0;
if (children === undefined) {
return value;
}
value +=
children.reduce((acc, child: Route.Item) => {
const childBadgeValue = child.badge;
if (typeof childBadgeValue === "number" && child.hidden !== true) {
return acc + childBadgeValue;
}
return acc;
}, 0) ?? 0;
return value === 0 ? undefined : value;
}, [badge, children]);
}
function useIsActive(parent: string, route: RouteObject) {
const { path, children } = route;
const { pathname } = useLocation();
const root = useMemo(() => pathJoin(parent, path ?? ""), [parent, path]);
const paths = useMemo(
() => [root, ...(children?.map((v) => pathJoin(root, v.path ?? "")) ?? [])],
[root, children],
);
const selection = useSelection().selection;
return useMemo(
() =>
selection?.includes(root) ||
paths.some((path) => matchPath(path, pathname)),
[pathname, paths, root, selection],
);
}
const AppNavbar: FunctionComponent = () => {
const [selection, select] = useState<string | null>(null);
const { toggleColorScheme } = useMantineColorScheme();
const computedColorScheme = useComputedColorScheme("light");
const dark = computedColorScheme === "dark";
const routes = useRouteItems();
const { pathname } = useLocation();
useEffect(() => {
select(null);
}, [pathname]);
return (
<AppShell.Navbar p="xs" className={styles.nav}>
<Selection.Provider value={{ selection, select }}>
<AppShell.Section
grow
style={{ overflowY: "auto", scrollbarWidth: "none" }}
>
<Stack gap={0}>
{routes.map((route, idx) => (
<RouteItem
key={BuildKey("nav", idx)}
parent="/"
route={route}
></RouteItem>
))}
</Stack>
</AppShell.Section>
<Divider></Divider>
<AppShell.Section mt="xs">
<Group gap="xs">
<Action
label="Change Theme"
c={dark ? "yellow" : "indigo"}
onClick={() => toggleColorScheme()}
icon={dark ? faSun : faMoon}
></Action>
<Anchor
href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XHHRWXT9YB7WE&source=url"
target="_blank"
>
<Action label="Donate" icon={faHeart} c="red"></Action>
</Anchor>
</Group>
</AppShell.Section>
</Selection.Provider>
</AppShell.Navbar>
);
};
const RouteItem: FunctionComponent<{
route: CustomRouteObject;
parent: string;
}> = ({ route, parent }) => {
const { children, name, path, icon, hidden, element } = route;
const { select } = useSelection();
const link = useMemo(() => pathJoin(parent, path ?? ""), [parent, path]);
const badge = useBadgeValue(route);
const isOpen = useIsActive(parent, route);
// Ignore path if it is using match
if (hidden === true || path === undefined || path.includes(":")) {
return null;
}
if (children !== undefined) {
const elements = (
<Stack gap={0}>
{children.map((child, idx) => (
<RouteItem
parent={link}
key={BuildKey(link, "nav", idx)}
route={child}
></RouteItem>
))}
</Stack>
);
if (name) {
return (
<Stack gap={0}>
<NavbarItem
primary
name={name}
link={link}
icon={icon}
badge={badge}
onClick={(event) => {
LOG("info", "clicked", link);
const validated =
element !== undefined ||
children?.find((v) => v.index === true) !== undefined;
if (!validated) {
event.preventDefault();
}
if (isOpen) {
select(null);
} else {
select(link);
}
}}
></NavbarItem>
{children.length > 0 && (
<Collapse in={isOpen}>{elements}</Collapse>
)}
</Stack>
);
} else {
return elements;
}
} else {
return (
<NavbarItem
name={name ?? link}
link={link}
icon={icon}
badge={badge}
></NavbarItem>
);
}
};
interface NavbarItemProps {
name: string;
link: string;
icon?: IconDefinition;
badge?: number | string;
primary?: boolean;
onClick?: (event: React.MouseEvent<HTMLAnchorElement>) => void;
}
const NavbarItem: FunctionComponent<NavbarItemProps> = ({
icon,
link,
name,
badge,
onClick,
primary = false,
}) => {
const { show } = useNavbar();
const { ref, hovered } = useHover();
const shouldHideBadge = useMemo(() => {
if (typeof badge === "number") {
return badge === 0;
} else if (typeof badge === "string") {
return badge.length === 0;
}
return true;
}, [badge]);
const isSignalRBadge = useMemo(() => {
return link === "/series" || link === "/movies";
}, [link]);
// Compute explicit background and text style objects safely
const badgeStyle = useMemo(() => {
if (!isSignalRBadge) return {};
if (badge === "LIVE") {
return {
// Subtle background colours that adapt to light/dark mode
backgroundColor:
"light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6))",
// Softened high-contrast text colors
color:
"light-dark(var(--mantine-color-gray-7), var(--mantine-color-gray-5))",
border: "none",
};
}
if (badge === "DOWN") {
return {
// more noticeable background colors for "DOWN" status, still adapting to theme
backgroundColor:
"light-dark(var(--mantine-color-red-6), var(--mantine-color-red-8))",
color: "var(--mantine-color-white)",
};
}
return {};
}, [badge, isSignalRBadge]);
return (
<NavLink
to={link}
onClick={(event: React.MouseEvent<HTMLAnchorElement>) => {
onClick?.(event);
if (!event.isDefaultPrevented()) {
show(false);
}
}}
className={({ isActive }) =>
clsx(
clsx(styles.anchor, {
[styles.active]: isActive,
[styles.hover]: hovered,
}),
)
}
>
<Text
ref={ref}
inline
p="xs"
size="sm"
fw={primary ? "bold" : "normal"}
className={styles.text}
span
>
{icon && (
<FontAwesomeIcon
className={styles.icon}
icon={icon}
></FontAwesomeIcon>
)}
{name}
{!shouldHideBadge && (
<Badge
className={styles.badge}
radius="xs"
// We apply the explicit styling overrides here
style={badgeStyle}
>
{badge}
</Badge>
)}
</Text>
</NavLink>
);
};
export default AppNavbar;