Files
n8n-enterprise-unlocked/packages/editor-ui/src/modules/templates.ts
Oliver Trajceski cfa91cda27 Add Templates (#2720)
* Templates Bugs / Fixed Various Bugs / Multiply Api Request, Carousel Gradient, Core Nodes Filters ...

* Updated MainSidebar Paddings

* N8N-Templates Bugfixing - Remove Unnecesairy Icon (Shape), Refatctor infiniteScrollEnabled Prop + updated infiniterScroll functinality

* N8N-2853 Fixed Carousel Arrows Bug after Cleaning the SearchBar

* fix telemetry init

* fix search tracking issues

* N8N-2853 Created FilterTemplateNode Constant Array, Filter PlayButton and WebhookRespond from Nodes, Added Box for showing more nodes inside TemplateList, Updated NewWorkflowButton to primary, Fixed Markdown issue with Code

* N8N-2853 Removed Placeholder if Workflows Or Collections are not found, Updated the Logic

* fix telemetry events

* clean up session id

* update user inserted event

* N8N-2853 Fixed Categories to Moving if the names are long

* Add todos

* Update Routes on loading

* fix spacing

* Update Border Color

* Update Border Readius

* fix filter fn

* fix constant, console error

* N8N-2853 PR Fixes, Refactoring, Removing unnecesairy code ..

* N8N-2853 PR Fixes - Editor-ui Fixes, Refactoring, Removing Dead Code ...

* N8N-2853 Refactor Card to LongCard

* clean up spacing, replace css var

* clean up spacing

* set categories as optional in node

* replace vars

* refactor store

* remove unnesssary import

* fix error

* fix templates view to start

* add to cache

* fix coll view data

* fix categories

* fix category event

* fix collections carousel

* fix initial load and search

* fix infinite load

* fix query param

* fix scrolling issues

* fix scroll to top

* fix search

* fix collections search

* fix navigation bug

* rename view

* update package lock

* rename workflow view

* rename coll view

* update routes

* add wrapper component

* set session id

* fix search tracking

* fix session tracking

* remove deleted mutation

* remove check for unsupported nodes

* refactor filters

* lazy load template

* clean up types

* refactor infinte scroll

* fix end of search

* Fix spacing

* fix coll loading

* fix types

* fix coll view list

* fix navigation

* rename types

* rename state

* fix search responsiveness

* fix coll view spacing

* fix search view spacing

* clean up views

* set background color

* center page not vert

* fix workflow view

* remove import

* fix background color

* fix background

* clean props

* clean up imports

* refactor button

* update background color

* fix spacing issue

* rename event

* update telemetry event

* update endpoints, add loading view, check for endpoint health

* remove conolse log

* N8N-2853 Fixed Menu Items Padding

* replace endpoints

* fix type issues

* fix categories

* N8N-2853 Fixed ParameterInput Placeholder after ElementUI Upgrade

* update createdAt

*  Fix placeholder in creds config modal

* ✏️ Adjust docstring to `credText` placeholder version

* N8N-2853 Optimized

* N8N-2853 Optimized code

*  Add deployment type to FE settings

*  Add deployment type to interfaces

* N8N-2853 Removed Animated prop from components

*  Add deployment type to store module

*  Create hiring banner

*  Display hiring banner

*  Undo unrelated change

* N8N-2853 Refactor TemplateFilters

*  Fix indentation

* N8N-2853 Reorder items / TemplateList

* 👕 Fix lint

* N8N-2853 Refactor TemplateFilters Component

* N8N-2853 Reorder TemplateList

* refactor template card

* update timeout

* fix removelistener

* fix spacing

* split enabled from offline

* add spacing to go back

* N8N-2853 Fixed Screens for Tablet & Mobile

* N8N-2853 Update Stores Order

* remove image componet

* remove placeholder changes

* N8N-2853 Fixed Chinnese Placeholders for El Select Component that comes from the Library Upgrade

* N8N-2853 Fixed Vue Agile Console Warnings

* N8N-2853 Update Collection Route

* ✏️ Update jobs URL

* 🚚 Move logging to root component

*  Refactor `deploymentType` to `isInternalUser`

*  Improve syntax

* fix cut bug in readonly view

* N8N-3012 Fixed Details section in templates with lots of description, Fixed Mardown Block with overflox-x

* N8N-3012 Increased Font-size, Spacing and Line-height of the Categories Items

* N8N-3012 Fixed Vue-agile client width error on resize

* only delay redirect for root path

* N8N-3012 Fixed Carousel Arrows that Disappear

* N8N-3012 Make Loading Screen same color as Templates

* N8N-3012 Markdown renders inline block as block code

* add offline warning

* hide log from workflow iframe

* update text

* make search button larger

* N8N-3012 Categories / Tags extended all the way in details section

* load data in cred modals

* remove deleted message

* add external hook

* remove import

* update env variable description

* fix markdown width issue

* disable telemetry for demo, add session id to template pages

* fix telemetery bugs

* N8N-3012 Not found Collections/Wokrkflow

* N8N-3012 Checkboxes change order when categories are changed

* N8N-3012 Refactor SortedCategories inside TemplateFilters component

* fix firefox bug

* add telemetry requirements

* add error check

* N8N-3012 Update GoBackButton to check if Route History is present

* N8N-3012 Fixed WF Nodes Icons

* hide workflow screenshots

* remove unnessary mixins

* rename prop

* fix design a bit

* rename data

* clear workspace on destroy

* fix copy paste bug

* fix disabled state

* N8N-3012 Fixed Saving/Leave without saving Modal

* fix telemetry issue

* fix telemetry issues, error bug

* fix error notification

* disable workflow menu items on templates

* fix i18n elementui issue

* Remove Emit - NodeType from HoverableNodeIcon component

* TechnicalFixes: NavigateTo passed down as function should be helper

* TechnicalFixes: Update NavigateTo function

* TechnicalFixes: Add FilterCoreNodes directly as function

* check for empty connecitions

* fix titles

* respect new lines

* increase categories to be sliced

* rename prop

* onUseWorkflow

* refactor click event

* fix bug, refactor

* fix loading story

* add default

* fix styles at right level of abstraction

* add wrapper with width

* remove loading blocks component

* add story

* rename prop

* fix spacing

* refactor tag, add story

* move margin to container

* fix tag redirect, remove unnessary check

* make version optional

* rename view

* move from workflows to templates store

* remove unnessary change

* remove unnessary css

* rename component

* refactor collection card

* add boolean to prevent shrink

* clean up carousel

* fix redirection bug on save

* remove listeners to fix multiple listeners bug

* remove unnessary types

* clean up boolean set

* fix node select bug

* rename component

* remove unnessary class

* fix redirection bug

* remove unnessary error

* fix typo

* fix blockquotes, pre

* refactor markdown rendering

* remove console log

* escape markdown

* fix safari bug

* load active workflows to fix modal bug

* ⬆️ Update package-lock.json file

*  Add n8n version as header

Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>
Co-authored-by: Mutasem <mutdmour@gmail.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-02-28 10:57:44 +01:00

283 lines
10 KiB
TypeScript

import { getCategories, getCollectionById, getCollections, getTemplateById, getWorkflows, getWorkflowTemplate } from '@/api/templates';
import { ActionContext, Module } from 'vuex';
import {
IRootState,
ITemplatesCollection,
ITemplatesWorkflow,
ITemplatesCategory,
ITemplateState,
ITemplatesQuery,
ITemplatesWorkflowFull,
ITemplatesCollectionFull,
IWorkflowTemplate,
} from '../Interface';
import Vue from 'vue';
const TEMPLATES_PAGE_SIZE = 10;
function getSearchKey(query: ITemplatesQuery): string {
return JSON.stringify([query.search || '', [...query.categories].sort()]);
}
const module: Module<ITemplateState, IRootState> = {
namespaced: true,
state: {
categories: {},
collections: {},
workflows: {},
collectionSearches: {},
workflowSearches: {},
currentSessionId: '',
previousSessionId: '',
},
getters: {
allCategories(state: ITemplateState) {
return Object.values(state.categories).sort((a: ITemplatesCategory, b: ITemplatesCategory) => a.name > b.name ? 1: -1);
},
getTemplateById(state: ITemplateState) {
return (id: string): null | ITemplatesWorkflow => state.workflows[id];
},
getCollectionById(state: ITemplateState) {
return (id: string): null | ITemplatesCollection => state.collections[id];
},
getCategoryById(state: ITemplateState) {
return (id: string): null | ITemplatesCategory => state.categories[id];
},
getSearchedCollections(state: ITemplateState) {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = state.collectionSearches[searchKey];
if (!search) {
return null;
}
return search.collectionIds.map((collectionId: string) => state.collections[collectionId]);
};
},
getSearchedWorkflows(state: ITemplateState) {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = state.workflowSearches[searchKey];
if (!search) {
return null;
}
return search.workflowIds.map((workflowId: string) => state.workflows[workflowId]);
};
},
getSearchedWorkflowsTotal(state: ITemplateState) {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = state.workflowSearches[searchKey];
return search ? search.totalWorkflows : 0;
};
},
isSearchLoadingMore(state: ITemplateState) {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = state.workflowSearches[searchKey];
return Boolean(search && search.loadingMore);
};
},
isSearchFinished(state: ITemplateState) {
return (query: ITemplatesQuery) => {
const searchKey = getSearchKey(query);
const search = state.workflowSearches[searchKey];
return Boolean(search && !search.loadingMore && search.totalWorkflows === search.workflowIds.length);
};
},
currentSessionId(state: ITemplateState) {
return state.currentSessionId;
},
previousSessionId(state: ITemplateState) {
return state.previousSessionId;
},
},
mutations: {
addCategories(state: ITemplateState, categories: ITemplatesCategory[]) {
categories.forEach((category: ITemplatesCategory) => {
Vue.set(state.categories, category.id, category);
});
},
addCollections(state: ITemplateState, collections: Array<ITemplatesCollection | ITemplatesCollectionFull>) {
collections.forEach((collection) => {
const workflows = (collection.workflows || []).map((workflow) => ({id: workflow.id}));
const cachedCollection = state.collections[collection.id] || {};
Vue.set(state.collections, collection.id, {
...cachedCollection,
...collection,
workflows,
});
});
},
addWorkflows(state: ITemplateState, workflows: Array<ITemplatesWorkflow | ITemplatesWorkflowFull>) {
workflows.forEach((workflow: ITemplatesWorkflow) => {
const cachedWorkflow = state.workflows[workflow.id] || {};
Vue.set(state.workflows, workflow.id, {
...cachedWorkflow,
...workflow,
});
});
},
addCollectionSearch(state: ITemplateState, data: {collections: ITemplatesCollection[], query: ITemplatesQuery}) {
const collectionIds = data.collections.map((collection) => collection.id);
const searchKey = getSearchKey(data.query);
Vue.set(state.collectionSearches, searchKey, {
collectionIds,
});
},
addWorkflowsSearch(state: ITemplateState, data: {totalWorkflows: number; workflows: ITemplatesWorkflow[], query: ITemplatesQuery}) {
const workflowIds = data.workflows.map((workflow) => workflow.id);
const searchKey = getSearchKey(data.query);
const cachedResults = state.workflowSearches[searchKey];
if (!cachedResults) {
Vue.set(state.workflowSearches, searchKey, {
workflowIds,
totalWorkflows: data.totalWorkflows,
});
return;
}
Vue.set(state.workflowSearches, searchKey, {
workflowIds: [...cachedResults.workflowIds, ...workflowIds],
totalWorkflows: data.totalWorkflows,
});
},
setWorkflowSearchLoading(state: ITemplateState, query: ITemplatesQuery) {
const searchKey = getSearchKey(query);
const cachedResults = state.workflowSearches[searchKey];
if (!cachedResults) {
return;
}
Vue.set(state.workflowSearches[searchKey], 'loadingMore', true);
},
setWorkflowSearchLoaded(state: ITemplateState, query: ITemplatesQuery) {
const searchKey = getSearchKey(query);
const cachedResults = state.workflowSearches[searchKey];
if (!cachedResults) {
return;
}
Vue.set(state.workflowSearches[searchKey], 'loadingMore', false);
},
resetSessionId(state: ITemplateState) {
state.previousSessionId = state.currentSessionId;
state.currentSessionId = '';
},
setSessionId(state: ITemplateState) {
if (!state.currentSessionId) {
state.currentSessionId = `templates-${Date.now()}`;
}
},
},
actions: {
async getTemplateById(context: ActionContext<ITemplateState, IRootState>, templateId: string): Promise<ITemplatesWorkflowFull> {
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
const response = await getTemplateById(apiEndpoint, templateId, { 'n8n-version': versionCli });
const template: ITemplatesWorkflowFull = {
...response.workflow,
full: true,
};
context.commit('addWorkflows', [template]);
return template;
},
async getCollectionById(context: ActionContext<ITemplateState, IRootState>, collectionId: string): Promise<ITemplatesCollection> {
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
const response = await getCollectionById(apiEndpoint, collectionId, { 'n8n-version': versionCli });
const collection: ITemplatesCollectionFull = {
...response.collection,
full: true,
};
context.commit('addCollections', [collection]);
context.commit('addWorkflows', response.collection.workflows);
return context.getters.getCollectionById(collectionId);
},
async getCategories(context: ActionContext<ITemplateState, IRootState>): Promise<ITemplatesCategory[]> {
const cachedCategories: ITemplatesCategory[] = context.getters.allCategories;
if (cachedCategories.length) {
return cachedCategories;
}
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
const response = await getCategories(apiEndpoint, { 'n8n-version': versionCli });
const categories = response.categories;
context.commit('addCategories', categories);
return categories;
},
async getCollections(context: ActionContext<ITemplateState, IRootState>, query: ITemplatesQuery): Promise<ITemplatesCollection[]> {
const cachedResults: ITemplatesCollection[] | null = context.getters.getSearchedCollections(query);
if (cachedResults) {
return cachedResults;
}
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
const response = await getCollections(apiEndpoint, query, { 'n8n-version': versionCli });
const collections = response.collections;
context.commit('addCollections', collections);
context.commit('addCollectionSearch', {query, collections});
collections.forEach((collection: ITemplatesCollection) => context.commit('addWorkflows', collection.workflows));
return collections;
},
async getWorkflows(context: ActionContext<ITemplateState, IRootState>, query: ITemplatesQuery): Promise<ITemplatesWorkflow[]> {
const cachedResults: ITemplatesWorkflow[] = context.getters.getSearchedWorkflows(query);
if (cachedResults) {
return cachedResults;
}
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
const payload = await getWorkflows(apiEndpoint, {...query, skip: 0, limit: TEMPLATES_PAGE_SIZE}, { 'n8n-version': versionCli });
context.commit('addWorkflows', payload.workflows);
context.commit('addWorkflowsSearch', {...payload, query});
return context.getters.getSearchedWorkflows(query);
},
async getMoreWorkflows(context: ActionContext<ITemplateState, IRootState>, query: ITemplatesQuery): Promise<ITemplatesWorkflow[]> {
if (context.getters.isSearchLoadingMore(query) && !context.getters.isSearchFinished(query)) {
return [];
}
const cachedResults: ITemplatesWorkflow[] = context.getters.getSearchedWorkflows(query) || [];
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
context.commit('setWorkflowSearchLoading', query);
try {
const payload = await getWorkflows(apiEndpoint, {...query, skip: cachedResults.length, limit: TEMPLATES_PAGE_SIZE});
context.commit('setWorkflowSearchLoaded', query);
context.commit('addWorkflows', payload.workflows);
context.commit('addWorkflowsSearch', {...payload, query});
return context.getters.getSearchedWorkflows(query);
} catch (e) {
context.commit('setWorkflowSearchLoaded', query);
throw e;
}
},
getWorkflowTemplate: async (context: ActionContext<ITemplateState, IRootState>, templateId: string): Promise<IWorkflowTemplate> => {
const apiEndpoint: string = context.rootGetters['settings/templatesHost'];
const versionCli: string = context.rootGetters['versionCli'];
return await getWorkflowTemplate(apiEndpoint, templateId, { 'n8n-version': versionCli });
},
},
};
export default module;