forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.tsx
More file actions
99 lines (86 loc) · 2.79 KB
/
utils.tsx
File metadata and controls
99 lines (86 loc) · 2.79 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
import {
IConfigurableActionConfiguration,
IconType,
IHeaderAction,
ShaIcon,
ShaLink,
} from '@/index';
import {
ButtonGroupItemProps,
IButtonGroup,
isGroup,
isButtonItem,
} from '@/providers/buttonGroupConfigurator/models';
import { IFullAuditedEntity } from '@/publicJsApis/entities';
import { IAuthenticator } from '@/providers/auth';
import { LoginOutlined } from '@ant-design/icons';
import { MenuProps } from 'antd';
import { ItemType } from 'antd/es/menu/interface';
import React, { Fragment } from 'react';
type MenuItem = MenuProps['items'][number];
type ItemVisibilityFunc = (item: ButtonGroupItemProps) => boolean;
const filterVisibleItems = (
items: ButtonGroupItemProps[] = [],
visibilityChecker: ItemVisibilityFunc,
): ButtonGroupItemProps[] => {
return items.reduce<ButtonGroupItemProps[]>((acc, item) => {
if (!visibilityChecker(item)) {
return acc;
}
if (isGroup(item)) {
const filteredChildren = item.childItems
? filterVisibleItems(item.childItems, visibilityChecker)
: undefined;
acc.push({ ...item, childItems: filteredChildren });
return acc;
}
acc.push(item);
return acc;
}, []);
};
export const getMenuItem = (
items: ButtonGroupItemProps[] = [],
execute: (payload: IConfigurableActionConfiguration, dynamicItem?: IFullAuditedEntity) => void,
visibilityChecker?: ItemVisibilityFunc,
): ItemType[] => {
// Filter items based on visibility if checker is provided
const visibleItems = visibilityChecker ? filterVisibleItems(items, visibilityChecker) : items;
return visibleItems.map((item) => {
const { id, icon, label } = item;
const childItems = isGroup(item) ? (item as IButtonGroup).childItems : undefined;
const dynamicItem = isButtonItem(item) ? item.dynamicItem : undefined;
return {
key: id,
label: (
<Fragment>
{icon && <ShaIcon iconName={icon as IconType} />} {label}
</Fragment>
),
children: childItems ? getMenuItem(childItems, execute, visibilityChecker) : undefined,
onClick: () => isButtonItem(item) ? execute(item.actionConfiguration, dynamicItem) : undefined,
};
});
};
export const getAccountMenuItems = (
accountDropdownListItems: IHeaderAction[],
logoutUser: IAuthenticator['logoutUser'],
): MenuItem[] => {
const result = (accountDropdownListItems ?? []).map<MenuItem>(({ icon, text, url: link, onClick }, index) => ({
key: index,
onClick: onClick,
label: link ? (
<ShaLink icon={icon} linkTo={link}>
{text}
</ShaLink>
) : (
<Fragment>{icon}</Fragment>
),
}));
if (result.length > 0) result.push({ key: 'divider', type: 'divider' });
result.push({
key: 'logout',
onClick: logoutUser,
label: <><LoginOutlined /> Logout</>,
});
return result;
};