-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
NodesMode.vue
241 lines (205 loc) · 6.77 KB
/
NodesMode.vue
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<script setup lang="ts">
import { camelCase } from 'lodash-es';
import { computed } from 'vue';
import type { INodeCreateElement, NodeFilterType } from '@/Interface';
import {
TRIGGER_NODE_CREATOR_VIEW,
HTTP_REQUEST_NODE_TYPE,
WEBHOOK_NODE_TYPE,
REGULAR_NODE_CREATOR_VIEW,
AI_NODE_CREATOR_VIEW,
AI_OTHERS_NODE_CREATOR_VIEW,
} from '@/constants';
import type { BaseTextKey } from '@/plugins/i18n';
import { useRootStore } from '@/stores/n8nRoot.store';
import { useNodeCreatorStore } from '@/stores/nodeCreator.store';
import { TriggerView, RegularView, AIView, AINodesView } from '../viewsData';
import { flattenCreateElements, transformNodeType } from '../utils';
import { useViewStacks } from '../composables/useViewStacks';
import { useKeyboardNavigation } from '../composables/useKeyboardNavigation';
import ItemsRenderer from '../Renderers/ItemsRenderer.vue';
import CategorizedItemsRenderer from '../Renderers/CategorizedItemsRenderer.vue';
import NoResults from '../Panel/NoResults.vue';
import { useI18n } from '@/composables/useI18n';
import { useTelemetry } from '@/composables/useTelemetry';
export interface Props {
rootView: 'trigger' | 'action';
}
const emit = defineEmits({
nodeTypeSelected: (nodeTypes: string[]) => true,
});
const i18n = useI18n();
const telemetry = useTelemetry();
const { mergedNodes, actions } = useNodeCreatorStore();
const { baseUrl } = useRootStore();
const { pushViewStack, popViewStack } = useViewStacks();
const { registerKeyHook } = useKeyboardNavigation();
const activeViewStack = computed(() => useViewStacks().activeViewStack);
const globalSearchItemsDiff = computed(() => useViewStacks().globalSearchItemsDiff);
function selectNodeType(nodeTypes: string[]) {
emit('nodeTypeSelected', nodeTypes);
}
function onSelected(item: INodeCreateElement) {
if (item.type === 'subcategory') {
const subcategoryKey = camelCase(item.properties.title);
const title = i18n.baseText(`nodeCreator.subcategoryNames.${subcategoryKey}` as BaseTextKey);
pushViewStack({
subcategory: item.key,
title,
mode: 'nodes',
...(item.properties.icon
? {
nodeIcon: {
icon: item.properties.icon,
iconType: 'icon',
},
}
: {}),
...(item.properties.panelClass ? { panelClass: item.properties.panelClass } : {}),
rootView: activeViewStack.value.rootView,
forceIncludeNodes: item.properties.forceIncludeNodes,
baseFilter: baseSubcategoriesFilter,
itemsMapper: subcategoriesMapper,
sections: item.properties.sections,
});
telemetry.trackNodesPanel('nodeCreateList.onSubcategorySelected', {
subcategory: item.key,
});
}
if (item.type === 'node') {
const nodeActions = actions?.[item.key] || [];
if (nodeActions.length <= 1) {
selectNodeType([item.key]);
return;
}
const icon = item.properties.iconUrl
? `${baseUrl}${item.properties.iconUrl}`
: item.properties.icon?.split(':')[1];
const transformedActions = nodeActions?.map((a) =>
transformNodeType(a, item.properties.displayName, 'action'),
);
pushViewStack({
subcategory: item.properties.displayName,
title: item.properties.displayName,
nodeIcon: {
color: item.properties.defaults?.color || '',
icon,
iconType: item.properties.iconUrl ? 'file' : 'icon',
},
rootView: activeViewStack.value.rootView,
hasSearch: true,
mode: 'actions',
items: transformedActions,
});
}
if (item.type === 'view') {
const views = {
[TRIGGER_NODE_CREATOR_VIEW]: TriggerView,
[REGULAR_NODE_CREATOR_VIEW]: RegularView,
[AI_NODE_CREATOR_VIEW]: AIView,
[AI_OTHERS_NODE_CREATOR_VIEW]: AINodesView,
};
const itemKey = item.key as keyof typeof views;
const matchedView = views[itemKey];
if (!matchedView) {
console.warn(`No view found for ${itemKey}`);
return;
}
const view = matchedView(mergedNodes);
pushViewStack({
title: view.title,
subtitle: view?.subtitle ?? '',
info: view?.info ?? '',
items: view.items as INodeCreateElement[],
hasSearch: true,
rootView: view.value as NodeFilterType,
mode: 'nodes',
// Root search should include all nodes
searchItems: mergedNodes,
});
}
}
function subcategoriesMapper(item: INodeCreateElement) {
if (item.type !== 'node') return item;
const hasTriggerGroup = item.properties.group.includes('trigger');
const nodeActions = actions?.[item.key] || [];
const hasActions = nodeActions.length > 0;
if (hasTriggerGroup && hasActions) {
if (item.properties?.codex) {
// Store the original name in the alias so we can search for it
item.properties.codex.alias = [
...(item.properties.codex?.alias || []),
item.properties.displayName,
];
}
item.properties.displayName = item.properties.displayName.replace(' Trigger', '');
}
return item;
}
function baseSubcategoriesFilter(item: INodeCreateElement): boolean {
if (item.type === 'section') return item.children.every(baseSubcategoriesFilter);
if (item.type !== 'node') return false;
const hasTriggerGroup = item.properties.group.includes('trigger');
const nodeActions = actions?.[item.key] || [];
const hasActions = nodeActions.length > 0;
const isTriggerRootView = activeViewStack.value.rootView === TRIGGER_NODE_CREATOR_VIEW;
if (isTriggerRootView) {
return hasActions || hasTriggerGroup;
}
return hasActions || !hasTriggerGroup;
}
function arrowLeft() {
popViewStack();
}
function onKeySelect(activeItemId: string) {
const mergedItems = flattenCreateElements([
...(activeViewStack.value.items ?? []),
...(globalSearchItemsDiff.value ?? []),
]);
const item = mergedItems.find((i) => i.uuid === activeItemId);
if (!item) return;
onSelected(item);
}
registerKeyHook('MainViewArrowRight', {
keyboardKeys: ['ArrowRight', 'Enter'],
condition: (type) => ['subcategory', 'node', 'view'].includes(type),
handler: onKeySelect,
});
registerKeyHook('MainViewArrowLeft', {
keyboardKeys: ['ArrowLeft'],
condition: (type) => ['subcategory', 'node', 'view'].includes(type),
handler: arrowLeft,
});
</script>
<template>
<span>
<!-- Main Node Items -->
<ItemsRenderer :elements="activeViewStack.items" @selected="onSelected" :class="$style.items">
<template
#empty
v-if="(activeViewStack.items || []).length === 0 && globalSearchItemsDiff.length === 0"
>
<NoResults
:rootView="activeViewStack.rootView"
showIcon
showRequest
@addWebhookNode="selectNodeType([WEBHOOK_NODE_TYPE])"
@addHttpNode="selectNodeType([HTTP_REQUEST_NODE_TYPE])"
/>
</template>
</ItemsRenderer>
<!-- Results in other categories -->
<CategorizedItemsRenderer
v-if="globalSearchItemsDiff.length > 0"
:elements="globalSearchItemsDiff"
:category="$locale.baseText('nodeCreator.categoryNames.otherCategories')"
@selected="onSelected"
>
</CategorizedItemsRenderer>
</span>
</template>
<style lang="scss" module>
.items {
margin-bottom: var(--spacing-s);
}
</style>