forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
217 lines (192 loc) · 6.89 KB
/
index.tsx
File metadata and controls
217 lines (192 loc) · 6.89 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
import {
IConfigurableActionConfiguration,
ConfigurableForm,
FormIdentifier,
IConfigurableFormComponent,
IToolboxComponent,
useAuth,
useForm,
useGlobalState,
useSidebarMenu,
useSheshaApplication,
} from '@/index';
import { useConfigurableActionDispatcher } from '@/providers/configurableActionsDispatcher';
import { useAvailableConstantsData } from '@/providers/form/utils';
import { IFullAuditedEntity } from '@/publicJsApis/entities';
import {
ButtonGroupItemProps,
IButtonGroup,
isGroup,
isItem,
} from '@/providers/buttonGroupConfigurator/models';
import { getStyle, validateConfigurableComponentSettings } from '@/providers/form/utils';
import { DownOutlined, UserOutlined } from '@ant-design/icons';
import { Avatar, Dropdown, Popover } from 'antd';
import React, { CSSProperties, useMemo, useState } from 'react';
import { getSettings } from './settingsForm';
import { useStyles } from './styles';
import { getAccountMenuItems, getMenuItem } from './utils';
import {
getDynamicActionsItemsLevel,
getItemsWithResolved,
IDynamicItemsEvaluationStore,
IResolvedDynamicItem,
} from '@/providers/dynamicActions/evaluator/utils';
import { SingleDynamicItemEvaluator } from '@/providers/dynamicActions/evaluator/singleDynamicItemEvaluator';
import ConditionalWrap from '@/components/conditionalWrapper';
interface IProfileDropdown extends IConfigurableFormComponent {
items?: IButtonGroup[];
subText?: string;
subTextColor?: string;
subTextFontSize?: string;
subTextFontWeight?: string;
subTextFontFamily?: string;
subTextTextAlign?: CSSProperties['textAlign'];
subTextStyle?: string;
showUserInfo?: boolean;
popOverTitle?: string;
popOverFormId?: FormIdentifier;
popOverContentStyle?: string;
}
const ProfileDropdown: IToolboxComponent<IProfileDropdown> = {
type: 'profileDropdown',
name: 'Profile Dropdown',
isInput: false,
canBeJsSetting: false,
icon: <UserOutlined />,
Factory: ({ model }) => {
const [numResolved, setNumResolved] = useState(0);
const {
subText,
subTextColor,
subTextFontSize,
subTextFontWeight,
subTextFontFamily,
subTextTextAlign,
subTextStyle,
showUserInfo,
popOverTitle,
popOverFormId,
popOverContentStyle,
} = model;
const { styles } = useStyles({
subText,
});
const { loginInfo, logoutUser } = useAuth();
const { formData } = useForm();
const { globalState } = useGlobalState();
const { executeAction } = useConfigurableActionDispatcher();
const { anyOfPermissionsGranted } = useSheshaApplication();
const allData = useAvailableConstantsData();
const sidebar = useSidebarMenu(false);
const { accountDropdownListItems } = sidebar || {};
const subTextStyling = {
color: subTextColor,
fontSize: subTextFontSize,
fontWeight: subTextFontWeight,
fontFamily: subTextFontFamily,
textAlign: subTextTextAlign,
...getStyle(subTextStyle, formData, globalState),
};
const evaluation = useMemo<IDynamicItemsEvaluationStore>(() => {
const dynamicItems: IResolvedDynamicItem[] = [];
const preparedItems = getDynamicActionsItemsLevel(model.items ?? [], (dynamicItem) => {
dynamicItems.push(dynamicItem);
});
return {
dynamicItems,
items: preparedItems,
};
}, [model.items]);
const finalItems = useMemo(() => {
return getItemsWithResolved(evaluation.items);
}, [evaluation.items, numResolved]);
const isVisibleBase = (item: ButtonGroupItemProps): boolean => {
const { permissions, hidden } = item;
if (hidden)
return false;
const granted = anyOfPermissionsGranted(permissions || []);
return granted;
};
type ItemVisibilityFunc = (item: ButtonGroupItemProps) => boolean;
const isGroupVisible = (group: IButtonGroup, itemVisibilityFunc: ItemVisibilityFunc): boolean => {
if (!isVisibleBase(group))
return false;
if (group.hideWhenEmpty) {
const firstVisibleItem = group.childItems?.find((item) => {
// analyze buttons and groups only
const isButton = isItem(item) && (item.itemSubType === 'button');
return (isButton || isGroup(item)) && itemVisibilityFunc(item);
});
if (!firstVisibleItem)
return false;
}
return true;
};
const getIsVisible = (item: ButtonGroupItemProps): boolean => {
return (isItem(item) && isVisibleBase(item)) || (isGroup(item) && isGroupVisible(item, getIsVisible));
};
// Custom execute function that includes dynamicItem in the context
const executeActionWithDynamicContext = (actionConfiguration: IConfigurableActionConfiguration, dynamicItem?: IFullAuditedEntity): void => {
if (actionConfiguration) {
executeAction({
actionConfiguration,
argumentsEvaluationContext: { ...allData, dynamicItem },
});
}
};
const menuItems = getMenuItem(finalItems, executeActionWithDynamicContext, getIsVisible);
const accountMenuItems = getAccountMenuItems(accountDropdownListItems, logoutUser);
const onDynamicItemEvaluated = (): void => {
setNumResolved((prev) => prev + 1);
};
if (model.hidden) return null;
const popoverContent = popOverFormId ? (
<div style={getStyle(popOverContentStyle, formData, globalState)}>
<ConfigurableForm formId={popOverFormId} mode="readonly" />
</div>
) : (
<div>Select Popover Form</div>
);
return (
<div className={styles.shaProfileDropdownWrapper}>
{subText && <div style={subTextStyling}>{subText}</div>}
{evaluation.dynamicItems.map((item) => (
<SingleDynamicItemEvaluator item={item} onEvaluated={onDynamicItemEvaluated} key={item.id} />
))}
<div className={styles.shaProfileDropdown}>
<ConditionalWrap
condition={showUserInfo}
wrap={(children) => {
return (
<Popover title={popOverTitle} content={popoverContent} placement="bottomRight">
{children}
</Popover>
);
}}
>
<Dropdown menu={{ items: [...menuItems, ...accountMenuItems] }} trigger={['click']}>
<a className="ant-dropdown-link" onClick={(e) => e.preventDefault()}>
{loginInfo?.fullName} <DownOutlined />
</a>
</Dropdown>
</ConditionalWrap>
<Avatar icon={<UserOutlined />} />
</div>
</div>
);
},
settingsFormMarkup: getSettings,
migrator: (m) => m
.add<IProfileDropdown>(1, (prev) => (
{
...prev, subTextFontWeight: 'normal',
subTextFontFamily: 'Arial',
subTextTextAlign: 'left',
subTextColor: '#000000',
subTextFontSize: '12px',
}
)),
validateSettings: (model) => validateConfigurableComponentSettings(getSettings, model),
};
export default ProfileDropdown;