-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathuseConvertBackendItemToCommandMenuItemConfig.tsx
More file actions
158 lines (140 loc) · 5.47 KB
/
useConvertBackendItemToCommandMenuItemConfig.tsx
File metadata and controls
158 lines (140 loc) · 5.47 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
import { EngineCommandMenuItem } from '@/command-menu-item/display/components/EngineCommandMenuItem';
import { FrontComponentCommandMenuItem } from '@/command-menu-item/display/components/FrontComponentCommandMenuItem';
import { HeadlessFrontComponentCommandMenuItem } from '@/command-menu-item/display/components/HeadlessFrontComponentCommandMenuItem';
import { WorkflowCommandMenuItem } from '@/command-menu-item/display/components/WorkflowCommandMenuItem';
import { CommandMenuItemScope } from '@/command-menu-item/types/CommandMenuItemScope';
import { CommandMenuItemType } from '@/command-menu-item/types/CommandMenuItemType';
import { contextStoreIsPageInEditModeComponentState } from '@/context-store/states/contextStoreIsPageInEditModeComponentState';
import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/states/contextStoreTargetedRecordsRuleComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { COMMAND_MENU_DEFAULT_ICON } from '@/workflow/workflow-trigger/constants/CommandMenuDefaultIcon';
import { useCallback } from 'react';
import { type CommandMenuContextApi } from 'twenty-shared/types';
import {
evaluateConditionalAvailabilityExpression,
interpolateCommandMenuItemLabel,
isDefined,
} from 'twenty-shared/utils';
import { useIcons } from 'twenty-ui/display';
import {
CommandMenuItemAvailabilityType,
type CommandMenuItemFieldsFragment,
} from '~/generated-metadata/graphql';
const resolveScope = (
availabilityType: CommandMenuItemAvailabilityType,
): CommandMenuItemScope => {
if (availabilityType === CommandMenuItemAvailabilityType.RECORD_SELECTION) {
return CommandMenuItemScope.RecordSelection;
}
return CommandMenuItemScope.Global;
};
const resolveType = (
item: CommandMenuItemFieldsFragment,
): CommandMenuItemType => {
if (item.availabilityType === CommandMenuItemAvailabilityType.FALLBACK) {
return CommandMenuItemType.Fallback;
}
if (isDefined(item.engineComponentKey)) {
return CommandMenuItemType.Standard;
}
if (isDefined(item.frontComponentId)) {
return CommandMenuItemType.FrontComponent;
}
if (isDefined(item.workflowVersionId)) {
return CommandMenuItemType.WorkflowRun;
}
return CommandMenuItemType.Standard;
};
// TODO: Remove this hook once we finish refactoring and we use
// the new types to build command menu items.
export const useConvertBackendItemToCommandMenuItemConfig = () => {
const { getIcon } = useIcons();
const contextStoreIsPageInEditMode = useAtomComponentStateValue(
contextStoreIsPageInEditModeComponentState,
);
const contextStoreTargetedRecordsRule = useAtomComponentStateValue(
contextStoreTargetedRecordsRuleComponentState,
);
const selectedRecordIds =
contextStoreTargetedRecordsRule.mode === 'selection'
? contextStoreTargetedRecordsRule.selectedRecordIds
: [];
const hasRecordSelection =
selectedRecordIds.length >= 1 ||
contextStoreTargetedRecordsRule.mode === 'exclusion';
const convertBackendItemToCommandMenuItemConfig = useCallback(
(
item: CommandMenuItemFieldsFragment,
commandMenuContextApi: CommandMenuContextApi,
) => {
const scope = resolveScope(item.availabilityType);
if (
scope === CommandMenuItemScope.RecordSelection &&
!hasRecordSelection
) {
return null;
}
const isEditModeItem =
item.conditionalAvailabilityExpression?.includes('isPageInEditMode') ??
false;
const isPinned =
item.availabilityType !== CommandMenuItemAvailabilityType.FALLBACK &&
(!contextStoreIsPageInEditMode || isEditModeItem) &&
item.isPinned;
const Icon = getIcon(item.icon, COMMAND_MENU_DEFAULT_ICON);
const label = interpolateCommandMenuItemLabel({
label: item.label,
context: commandMenuContextApi,
});
const shortLabel = interpolateCommandMenuItemLabel({
label: item.shortLabel,
context: commandMenuContextApi,
});
const component = isDefined(item.engineComponentKey) ? (
<EngineCommandMenuItem
commandMenuItemId={item.id}
engineComponentKey={item.engineComponentKey}
/>
) : isDefined(item.frontComponentId) ? (
item.frontComponent?.isHeadless === true ? (
<HeadlessFrontComponentCommandMenuItem
frontComponentId={item.frontComponentId}
commandMenuItemId={item.id}
/>
) : (
<FrontComponentCommandMenuItem
frontComponentId={item.frontComponentId}
/>
)
) : isDefined(item.workflowVersionId) ? (
<WorkflowCommandMenuItem
workflowVersionId={item.workflowVersionId}
availabilityType={item.availabilityType}
availabilityObjectMetadataId={item.availabilityObjectMetadataId}
/>
) : null;
if (!isDefined(component)) {
return;
}
return {
type: resolveType(item),
key: `command-menu-item-${item.id}`,
scope,
label,
shortLabel,
position: item.position,
isPinned,
Icon,
hotKeys: item.hotKeys,
shouldBeRegistered: () =>
evaluateConditionalAvailabilityExpression(
item.conditionalAvailabilityExpression,
commandMenuContextApi,
),
component,
};
},
[getIcon, contextStoreIsPageInEditMode, hasRecordSelection],
);
return { convertBackendItemToCommandMenuItemConfig };
};