2022-03-31 05:16:04 +00:00
|
|
|
import { FilterKeys, Template } from "api/TemplatesApi";
|
2022-03-03 10:56:53 +00:00
|
|
|
import Fuse from "fuse.js";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2022-03-03 10:56:53 +00:00
|
|
|
import { createSelector } from "reselect";
|
2022-06-15 15:37:41 +00:00
|
|
|
import { getWorkspaceCreateApplication } from "./applicationSelectors";
|
2022-03-10 14:39:05 +00:00
|
|
|
import { getWidgetCards } from "./editorSelectors";
|
2022-03-03 10:56:53 +00:00
|
|
|
import { getDefaultPlugins } from "./entitiesSelector";
|
2022-03-10 14:39:05 +00:00
|
|
|
import { Filter } from "pages/Templates/Filters";
|
2022-03-03 10:56:53 +00:00
|
|
|
|
|
|
|
|
const fuzzySearchOptions = {
|
2022-04-13 08:48:40 +00:00
|
|
|
keys: ["title", "id", "datasources", "widgets"],
|
2022-03-03 10:56:53 +00:00
|
|
|
shouldSort: true,
|
|
|
|
|
threshold: 0.5,
|
|
|
|
|
location: 0,
|
|
|
|
|
distance: 100,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getTemplatesSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.templates;
|
|
|
|
|
export const isImportingTemplateSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.isImportingTemplate;
|
|
|
|
|
export const showTemplateNotificationSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.templateNotificationSeen;
|
|
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
export const getWorkspaceForTemplates = createSelector(
|
|
|
|
|
getWorkspaceCreateApplication,
|
|
|
|
|
(workspaceList) => {
|
|
|
|
|
if (workspaceList.length) {
|
|
|
|
|
return workspaceList[0];
|
2022-03-03 10:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const getTemplateFilterSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.filters;
|
|
|
|
|
|
|
|
|
|
export const getTemplateFiltersLength = createSelector(
|
|
|
|
|
getTemplateFilterSelector,
|
|
|
|
|
(filters) => {
|
|
|
|
|
return Object.values(filters)
|
|
|
|
|
.map((filterList) => filterList.length)
|
|
|
|
|
.reduce((c, a) => c + a, 0);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const isFetchingTemplatesSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.gettingAllTemplates;
|
2022-03-31 05:16:04 +00:00
|
|
|
export const isFetchingTemplateSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.gettingTemplate;
|
2022-03-03 10:56:53 +00:00
|
|
|
|
|
|
|
|
export const getTemplateById = (id: string) => (state: AppState) => {
|
|
|
|
|
return state.ui.templates.templates.find((template) => template.id === id);
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-31 05:16:04 +00:00
|
|
|
export const getActiveTemplateSelector = (state: AppState) =>
|
|
|
|
|
state.ui.templates.activeTemplate;
|
|
|
|
|
|
2022-03-03 10:56:53 +00:00
|
|
|
export const getFilteredTemplateList = createSelector(
|
|
|
|
|
getTemplatesSelector,
|
|
|
|
|
getTemplateFilterSelector,
|
2022-03-31 05:16:04 +00:00
|
|
|
getTemplateFiltersLength,
|
|
|
|
|
(templates, templatesFilters, numberOfFiltersApplied) => {
|
|
|
|
|
const result: Template[] = [];
|
|
|
|
|
|
|
|
|
|
if (!numberOfFiltersApplied) {
|
|
|
|
|
return templates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Object.keys(templatesFilters).length) {
|
|
|
|
|
return templates;
|
2022-03-03 10:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 05:16:04 +00:00
|
|
|
Object.keys(templatesFilters).map((filter) => {
|
|
|
|
|
templates.map((template) => {
|
|
|
|
|
if (
|
|
|
|
|
template[filter as FilterKeys].some((templateFilter) => {
|
|
|
|
|
return templatesFilters[filter].includes(templateFilter);
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
result.push(template);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
2022-03-03 10:56:53 +00:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const getTemplateSearchQuery = (state: AppState) =>
|
|
|
|
|
state.ui.templates.templateSearchQuery;
|
|
|
|
|
|
|
|
|
|
export const getSearchedTemplateList = createSelector(
|
|
|
|
|
getFilteredTemplateList,
|
|
|
|
|
getTemplateSearchQuery,
|
|
|
|
|
(templates, query) => {
|
|
|
|
|
if (!query) {
|
|
|
|
|
return templates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fuzzy = new Fuse(templates, fuzzySearchOptions);
|
|
|
|
|
return fuzzy.search(query);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2022-09-02 13:17:17 +00:00
|
|
|
// Get the list of datasources which are used by templates
|
2022-03-03 10:56:53 +00:00
|
|
|
export const templatesDatasourceFiltersSelector = createSelector(
|
2022-09-02 13:17:17 +00:00
|
|
|
getTemplatesSelector,
|
2022-03-03 10:56:53 +00:00
|
|
|
getDefaultPlugins,
|
2022-09-02 13:17:17 +00:00
|
|
|
(templates, plugins) => {
|
|
|
|
|
const datasourceFilters: Filter[] = [];
|
|
|
|
|
templates.map((template) => {
|
|
|
|
|
template.datasources.map((pluginIdentifier) => {
|
|
|
|
|
if (
|
|
|
|
|
!datasourceFilters.find((filter) => filter.value === pluginIdentifier)
|
|
|
|
|
) {
|
|
|
|
|
const matchedPlugin = plugins.find(
|
|
|
|
|
(plugin) =>
|
|
|
|
|
plugin.id === pluginIdentifier ||
|
|
|
|
|
plugin.packageName === pluginIdentifier,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (matchedPlugin) {
|
|
|
|
|
datasourceFilters.push({
|
|
|
|
|
label: matchedPlugin.name,
|
|
|
|
|
value: pluginIdentifier,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-03-03 10:56:53 +00:00
|
|
|
});
|
2022-09-02 13:17:17 +00:00
|
|
|
|
|
|
|
|
return datasourceFilters;
|
2022-03-03 10:56:53 +00:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2022-03-10 14:39:05 +00:00
|
|
|
// Get all filters which is associated with atleast one template
|
|
|
|
|
// If no template is associated with a filter, then the filter shouldn't be in the filter list
|
|
|
|
|
export const getFilterListSelector = createSelector(
|
|
|
|
|
getWidgetCards,
|
|
|
|
|
templatesDatasourceFiltersSelector,
|
|
|
|
|
getTemplatesSelector,
|
2022-09-26 08:14:39 +00:00
|
|
|
(widgetConfigs, allDatasources, templates) => {
|
2022-03-10 14:39:05 +00:00
|
|
|
const filters: Record<string, Filter[]> = {
|
|
|
|
|
datasources: [],
|
2022-09-26 08:14:39 +00:00
|
|
|
widgets: [],
|
2022-03-10 14:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
2022-09-26 08:14:39 +00:00
|
|
|
const allWidgets = widgetConfigs.map((widget) => {
|
2022-03-10 14:39:05 +00:00
|
|
|
return {
|
2022-09-26 08:14:39 +00:00
|
|
|
label: widget.displayName,
|
|
|
|
|
value: widget.type,
|
2022-03-10 14:39:05 +00:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const filterFilters = (
|
|
|
|
|
key: "datasources" | "widgets" | "useCases" | "functions",
|
|
|
|
|
dataReference: Filter[],
|
|
|
|
|
template: Template,
|
|
|
|
|
) => {
|
|
|
|
|
template[key].map((templateValue) => {
|
|
|
|
|
if (
|
|
|
|
|
!filters[key].some((filter) => {
|
|
|
|
|
if (filter.value) {
|
|
|
|
|
return filter.value === templateValue;
|
|
|
|
|
}
|
|
|
|
|
return filter.label === templateValue;
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
const filteredData = dataReference.find((datum) => {
|
|
|
|
|
if (datum.value) {
|
|
|
|
|
return datum.value === templateValue;
|
|
|
|
|
}
|
|
|
|
|
return datum.label === templateValue;
|
|
|
|
|
});
|
|
|
|
|
filteredData && filters[key].push(filteredData);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
templates.map((template) => {
|
|
|
|
|
filterFilters("datasources", allDatasources, template);
|
2022-09-26 08:14:39 +00:00
|
|
|
filterFilters("widgets", allWidgets, template);
|
2022-03-10 14:39:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
export const getForkableWorkspaces = createSelector(
|
|
|
|
|
getWorkspaceCreateApplication,
|
|
|
|
|
(workspaces) => {
|
|
|
|
|
return workspaces.map((workspace) => {
|
2022-03-03 10:56:53 +00:00
|
|
|
return {
|
2022-06-15 15:37:41 +00:00
|
|
|
label: workspace.workspace.name,
|
|
|
|
|
value: workspace.workspace.id,
|
2022-03-03 10:56:53 +00:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|