PromucFlow_constructor/app/client/src/selectors/templatesSelectors.tsx
Rahul Barwal 95784c6d10
feat: replace blank canvas with starter templates. (#28284)
## Description
### Shows starter page templates instead of blank canvas
As part of first activation experiment, this PR implements changes for
showing starter page templates and allows user to fork a starter page
template when they click on any template.

#### PR fixes following issue(s)
Fixes #27884
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
- New feature (non-breaking change which adds functionality)
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] JUnit
- [ ] Jest
- [ ] 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
- [ ] I have performed a self-review of my own code
- [ ] 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
- [x] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#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

---------

Co-authored-by: Jacques Ikot <jacquesikot@gmail.com>
2023-10-30 11:15:05 +05:30

221 lines
6.4 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 "@appsmith/selectors/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 isImportingStarterBuildingBlockToAppSelector = (state: AppState) =>
state.ui.templates.isImportingStarterBuildingBlockToApp;
export const starterBuildingBlockDatasourcePromptSelector = (state: AppState) =>
state.ui.templates.starterBuildingBlockDatasourcePrompt;
export const buildingBlocksSourcePageIdSelector = (state: AppState) =>
state.ui.templates.buildingBlockSourcePageId;
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 allTemplatesFiltersSelector = (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,
allTemplatesFiltersSelector,
(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;