-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathnav-config.ts
155 lines (141 loc) · 3.51 KB
/
nav-config.ts
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
export type NavConfigRoute = {
title?: string;
path: string;
exact?: boolean;
requiresPlus?: boolean;
};
export type NavConfigGroup = {
title: string;
requiresSystems?: boolean;
requiresConnections?: boolean;
routes: NavConfigRoute[];
};
export const NAV_CONFIG: NavConfigGroup[] = [
// Goes last because its root path will match everything.
{
title: "Home",
routes: [
{
path: "/",
exact: true,
},
],
},
{
title: "Privacy requests",
requiresConnections: true,
routes: [
{ title: "Request manager", path: "/privacy-requests" },
{ title: "Connection manager", path: "/datastore-connection" },
{ title: "Configuration", path: "/privacy-requests/configure" },
],
},
{
title: "Data map",
requiresSystems: true,
routes: [
{ title: "View map", path: "/datamap", requiresPlus: true },
{ title: "View systems", path: "/system" },
{ title: "Add systems", path: "/add-systems" },
{ title: "Manage datasets", path: "/dataset" },
{
title: "Classify systems",
path: "/classify-systems",
requiresPlus: true,
},
],
},
{
title: "Management",
routes: [
{ title: "Taxonomy", path: "/taxonomy" },
{ title: "Users", path: "/user-management" },
{ title: "About Fides", path: "/management/about" },
],
},
];
export type NavGroupChild = {
title: string;
path: string;
exact?: boolean;
};
export type NavGroup = {
/**
* Title of the group. Displayed in NavTopBar.
*/
title: string;
/**
* The routes that are nested under this group. These are displayed in the NavSideBar. If this has
* only one child, the side bar should not be shown at all (such as for "Home").
*/
children: Array<NavGroupChild>;
};
export const configureNavGroups = ({
config,
hasPlus = false,
hasSystems = false,
hasConnections = false,
}: {
config: NavConfigGroup[];
hasPlus?: boolean;
hasSystems?: boolean;
hasConnections?: boolean;
}): NavGroup[] => {
const navGroups: NavGroup[] = [];
config.forEach((group) => {
// Skip groups with unmet requirements.
if (
(group.requiresConnections && !hasConnections) ||
(group.requiresSystems && !hasSystems)
) {
return;
}
const navGroup: NavGroup = {
title: group.title,
children: [],
};
navGroups.push(navGroup);
group.routes.forEach((route) => {
// If the target route would require plus in a non-plus environment,
// exclude it from the group.
if (route.requiresPlus && !hasPlus) {
return;
}
navGroup.children.push({
title: route.title ?? navGroup.title,
path: route.path,
exact: route.exact,
});
});
});
return navGroups;
};
export type ActiveNav = NavGroup & { path?: string };
export const findActiveNav = ({
navGroups,
path,
}: {
navGroups: NavGroup[];
path: string;
}): ActiveNav | undefined => {
let childMatch: NavGroupChild | undefined;
const groupMatch = navGroups.find((group) => {
childMatch = group.children.find((child) =>
child.exact ? path === child.path : path.startsWith(child.path)
);
return childMatch;
});
if (!(groupMatch && childMatch)) {
return undefined;
}
let activePath: string | undefined;
if (childMatch.exact && path === childMatch.path) {
activePath = path;
} else if (path.startsWith(childMatch.path)) {
activePath = childMatch.path;
}
return {
...groupMatch,
path: activePath,
};
};