PromucFlow_constructor/app/client/src/selectors/templatesSelectors.tsx
Rahul Barwal acfa51aced
feat: Updates templates filters section in appsmith platform (#23361)
## Description
Updates templates filters
1. Removes data sources from filters
2. Renames `functions` to `teams`

#### PR fixes following issue(s)
Fixes # (issue number)
#22959

#### Media
<img width="713" alt="Screenshot 2023-05-15 at 10 14 30 PM"
src="https://github.com/appsmithorg/appsmith/assets/6761673/d9021aa6-a21b-4b71-97f9-e59e6a77a93e">

#### Type of change

- Breaking change (fix or feature that would cause existing
functionality to not work as expected)

## Testing
- [x] Cypress

#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Test-plan-implementation#speedbreaker-features-to-consider-for-every-change)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans/_edit#areas-of-interest)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-06-05 11:46:00 +05:30

215 lines
6.0 KiB
TypeScript

import type { FilterKeys, Template } from "api/TemplatesApi";
import Fuse from "fuse.js";
import type { AppState } from "@appsmith/reducers";
import { createSelector } from "reselect";
import { getWorkspaceCreateApplication } from "@appsmith/selectors/applicationSelectors";
import { getDefaultPlugins } from "./entitiesSelector";
import type { Filter } from "pages/Templates/Filters";
const fuzzySearchOptions = {
keys: ["title", "id", "datasources", "widgets"],
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 isImportingTemplateToAppSelector = (state: AppState) =>
state.ui.templates.isImportingTemplateToApp;
export const showTemplateNotificationSelector = (state: AppState) =>
state.ui.templates.templateNotificationSeen;
export const getWorkspaceForTemplates = createSelector(
getWorkspaceCreateApplication,
(workspaceList) => {
if (workspaceList.length) {
return workspaceList[0];
}
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;
export const isFetchingTemplateSelector = (state: AppState) =>
state.ui.templates.gettingTemplate;
export const getTemplateById = (id: string) => (state: AppState) => {
return state.ui.templates.templates.find((template) => template.id === id);
};
export const getActiveTemplateSelector = (state: AppState) =>
state.ui.templates.activeTemplate;
export const getFilteredTemplateList = createSelector(
getTemplatesSelector,
getTemplateFilterSelector,
getTemplateFiltersLength,
(templates, templatesFilters, numberOfFiltersApplied) => {
const result: Template[] = [];
const activeTemplateIds: string[] = [];
if (!numberOfFiltersApplied) {
return templates;
}
if (!Object.keys(templatesFilters).length) {
return templates;
}
Object.keys(templatesFilters).map((filter) => {
templates.map((template) => {
if (activeTemplateIds.includes(template.id)) {
return;
}
if (
template[filter as FilterKeys].some((templateFilter) => {
return templatesFilters[filter].includes(templateFilter);
})
) {
result.push(template);
activeTemplateIds.push(template.id);
}
});
});
return result;
},
);
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);
},
);
// Get the list of datasources which are used by templates
export const templatesDatasourceFiltersSelector = createSelector(
getTemplatesSelector,
getDefaultPlugins,
(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,
});
}
}
});
});
return datasourceFilters;
},
);
export const templatesFiltersSelector = (state: AppState) =>
state.ui.templates.allFilters;
// 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(
getTemplatesSelector,
templatesFiltersSelector,
(templates, allTemplateFilters) => {
const FUNCTIONS_FILTER = "functions";
const filters: Record<string, Filter[]> = {
[FUNCTIONS_FILTER]: [],
};
const allFunctions = allTemplateFilters.functions.map((item) => {
return {
label: item,
value: item,
};
});
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.forEach((template) => {
filterFilters(FUNCTIONS_FILTER, allFunctions, template);
});
return filters;
},
);
export const getForkableWorkspaces = createSelector(
getWorkspaceCreateApplication,
(workspaces) => {
return workspaces.map((workspace) => {
return {
label: workspace.workspace.name,
value: workspace.workspace.id,
};
});
},
);
export const templateModalOpenSelector = (state: AppState) =>
state.ui.templates.showTemplatesModal;
export const templatesCountSelector = (state: AppState) =>
state.ui.templates.templates.length;