Repository navigation
Expand file tree
/
Copy pathcreateCustomMenuItems.tsx
More file actions
226 lines (193 loc) · 7 KB
/
Copy pathcreateCustomMenuItems.tsx
File metadata and controls
226 lines (193 loc) · 7 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
import type { CustomMenuItem, LoadedClerk } from '@clerk/shared/types';
import { USER_BUTTON_ITEM_ID } from '../constants';
import type { LocalizationKey } from '../customizables';
import { Cog, SignOut } from '../icons';
import { localizationKeys } from '../localization';
import { ExternalElementMounter } from './ExternalElementMounter';
import { isDevelopmentSDK } from './runtimeEnvironment';
import { sanitizeCustomLinkURL } from './sanitizeCustomLinkURL';
export type DefaultItemIds = 'manageAccount' | 'addAccount' | 'signOut' | 'signOutAll';
export type MenuItem = {
id: string;
name: LocalizationKey | string;
icon: React.ComponentType;
onClick?: () => void;
open?: string;
path?: string;
};
type MenuReorderItem = {
label: 'manageAccount' | 'signOut';
};
type CustomMenuAction = {
label: string;
onClick: () => void;
mountIcon: (el: HTMLDivElement) => void;
unmountIcon: (el?: HTMLDivElement) => void;
mount: (el: HTMLDivElement) => void;
unmount: (el?: HTMLDivElement) => void;
};
type CustomMenuActionWithOpen = {
label: string;
open: string;
mountIcon: (el: HTMLDivElement) => void;
unmountIcon: (el?: HTMLDivElement) => void;
mount: (el: HTMLDivElement) => void;
unmount: (el?: HTMLDivElement) => void;
};
type CustomMenuLink = {
label: string;
href: string;
mountIcon: (el: HTMLDivElement) => void;
unmountIcon: (el?: HTMLDivElement) => void;
};
type CreateCustomMenuItemsParams = {
customMenuItems: CustomMenuItem[];
getDefaultMenuItems: () => { INITIAL_MENU_ITEMS: MenuItem[]; validReorderItemLabels: string[] };
};
export const createUserButtonCustomMenuItems = (customMenuItems: CustomMenuItem[], clerk: LoadedClerk) => {
return createCustomMenuItems({ customMenuItems, getDefaultMenuItems: getUserButtonDefaultMenuItems }, clerk);
};
const createCustomMenuItems = (
{ customMenuItems, getDefaultMenuItems }: CreateCustomMenuItemsParams,
clerk: LoadedClerk,
) => {
const { INITIAL_MENU_ITEMS, validReorderItemLabels } = getDefaultMenuItems();
const validCustomMenuItems = customMenuItems.filter(item => {
if (!isValidCustomMenuItem(item, validReorderItemLabels)) {
if (isDevelopmentSDK(clerk)) {
console.error('Clerk: Invalid custom menu item:', item);
}
return false;
}
return true;
});
if (isDevelopmentSDK(clerk)) {
checkForDuplicateUsageOfReorderingItems(customMenuItems, validReorderItemLabels);
warnForDuplicateLabels(validCustomMenuItems);
}
const { menuItems } = getMenuItems({ customMenuItems: validCustomMenuItems, defaultMenuItems: INITIAL_MENU_ITEMS });
return menuItems;
};
const getMenuItems = ({
customMenuItems,
defaultMenuItems,
}: {
customMenuItems: CustomMenuItem[];
defaultMenuItems: MenuItem[];
}) => {
let remainingDefaultItems: MenuItem[] = defaultMenuItems.map(r => r);
const items: MenuItem[] = customMenuItems.map((ci: CustomMenuItem, index) => {
if (isCustomMenuLink(ci)) {
return {
name: ci.label,
id: `custom-menutItem-${index}`,
icon: props => (
<ExternalElementMounter
mount={ci.mountIcon}
unmount={ci.unmountIcon}
{...props}
/>
),
path: sanitizeCustomLinkURL(ci.href),
};
}
if (isCustomMenuItem(ci)) {
return {
name: ci.label,
id: `custom-menutItem-${index}`,
icon: props => (
<ExternalElementMounter
mount={ci.mountIcon}
unmount={ci.unmountIcon}
{...props}
/>
),
onClick: ci?.onClick,
};
}
if (isCustomMenuItemWithOpen(ci)) {
return {
name: ci.label,
id: `custom-menutItem-${index}`,
icon: props => (
<ExternalElementMounter
mount={ci.mountIcon}
unmount={ci.unmountIcon}
{...props}
/>
),
open: ci.open,
};
}
const reorderItem = defaultMenuItems.find(item => item.id === ci.label) as MenuItem;
remainingDefaultItems = remainingDefaultItems.filter(item => item.id !== ci.label);
return { ...reorderItem };
});
const allItems = [...items, ...remainingDefaultItems];
const hasReorderedManageAccount = customMenuItems.some(item => item.label === USER_BUTTON_ITEM_ID.MANAGE_ACCOUNT);
// Ensure that the "Manage account" item is always at the top of the list if it's not included in the custom items
if (!hasReorderedManageAccount) {
allItems.sort(a => {
if (a.id === USER_BUTTON_ITEM_ID.MANAGE_ACCOUNT) {
return -1;
}
return 0;
});
}
return { menuItems: allItems };
};
const getUserButtonDefaultMenuItems = () => {
const INITIAL_MENU_ITEMS = [
{
name: localizationKeys('userButton.action__manageAccount'),
id: USER_BUTTON_ITEM_ID.MANAGE_ACCOUNT as 'manageAccount',
icon: Cog as React.ComponentType,
},
{
name: localizationKeys('userButton.action__signOut'),
id: USER_BUTTON_ITEM_ID.SIGN_OUT as 'signOut',
icon: SignOut as React.ComponentType,
},
];
const validReorderItemLabels: string[] = INITIAL_MENU_ITEMS.map(r => r.id);
return { INITIAL_MENU_ITEMS, validReorderItemLabels };
};
const isValidCustomMenuItem = (item: CustomMenuItem, validReorderItemLabels: string[]): item is CustomMenuItem => {
return (
(isCustomMenuLink(item) ||
isCustomMenuItem(item) ||
isCustomMenuItemWithOpen(item) ||
isReorderItem(item, validReorderItemLabels)) &&
typeof item.label === 'string'
);
};
const isCustomMenuItem = (ci: CustomMenuItem): ci is CustomMenuAction => {
return !!ci.label && !!ci.onClick && !ci.mount && !ci.unmount && !!ci.mountIcon && !!ci.unmountIcon;
};
const isCustomMenuItemWithOpen = (ci: CustomMenuItem): ci is CustomMenuActionWithOpen => {
return !!ci.label && !!ci.open && !ci.mount && !ci.unmount && !!ci.mountIcon && !!ci.unmountIcon;
};
const isCustomMenuLink = (ci: CustomMenuItem): ci is CustomMenuLink => {
return !!ci.href && !!ci.label && !ci.mount && !ci.unmount && !!ci.mountIcon && !!ci.unmountIcon;
};
const isReorderItem = (ci: CustomMenuItem, validItems: string[]): ci is MenuReorderItem => {
return !ci.mount && !ci.unmount && !ci.mountIcon && !ci.unmountIcon && validItems.some(v => v === ci.label);
};
const checkForDuplicateUsageOfReorderingItems = (customMenuItems: CustomMenuItem[], validReorderItems: string[]) => {
const reorderItems = customMenuItems.filter(ci => isReorderItem(ci, validReorderItems));
reorderItems.reduce((acc, ci) => {
if (acc.includes(ci.label)) {
console.error(
`Clerk: The "${ci.label}" item is used more than once when reordering pages. This may cause unexpected behavior.`,
);
}
return [...acc, ci.label];
}, [] as string[]);
};
const warnForDuplicateLabels = (items: CustomMenuItem[]) => {
const labels = items.map(item => item.label);
const duplicates = labels.filter((label, index) => labels.indexOf(label) !== index);
if (duplicates.length > 0) {
console.warn(`Clerk: Duplicate custom menu item labels found: ${duplicates.join(', ')}`);
}
};