PromucFlow_constructor/app/client/src/selectors/templatesSelectors.tsx
Ilia d6f249b42d
chore: add blank line eslint rule (#36369)
## Description
Added ESLint rule to force blank lines between statements. 


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]
> 🔴 🔴 🔴 Some tests have failed.
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10924926728>
> Commit: 34f57714a1575ee04e94e03cbcaf95e57a96c86c
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10924926728&attempt=1&selectiontype=test&testsstatus=failed&specsstatus=fail"
target="_blank">Cypress dashboard</a>.
> Tags: @tag.All
> Spec: 
> The following are new failures, please fix them before merging the PR:
<ol>
> <li>cypress/e2e/Regression/ClientSide/Anvil/AnvilModal_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCheckboxGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCurrencyInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilIconButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInlineButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilParagraphWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilPhoneInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilStatsWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilTableWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilToolbarButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilZoneSectionWidgetSnapshot_spec.ts</ol>
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/identified-flaky-tests-65890b3c81d7400d08fa9ee3?branch=master"
target="_blank">List of identified flaky tests</a>.
> <hr>Wed, 18 Sep 2024 16:33:36 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No

---------

Co-authored-by: Valera Melnikov <valera@appsmith.com>
2024-09-18 19:35:28 +03:00

276 lines
8.1 KiB
TypeScript

import type { Workspace } from "ee/constants/workspaceConstants";
import type { AppState } from "ee/reducers";
import { getDefaultPlugins } from "ee/selectors/entitiesSelector";
import { getFetchedWorkspaces } from "ee/selectors/workspaceSelectors";
import { hasCreateNewAppPermission } from "ee/utils/permissionHelpers";
import type { FilterKeys, Template } from "api/TemplatesApi";
import {
BUILDING_BLOCK_EXPLORER_TYPE,
DEFAULT_COLUMNS_FOR_EXPLORER_BUILDING_BLOCKS,
DEFAULT_ROWS_FOR_EXPLORER_BUILDING_BLOCKS,
WIDGET_TAGS,
} from "constants/WidgetConstants";
import Fuse from "fuse.js";
import type { Filter } from "pages/Templates/TemplateFilters";
import { TEMPLATE_BUILDING_BLOCKS_FILTER_FUNCTION_VALUE } from "pages/Templates/constants";
import { createSelector } from "reselect";
import type { WidgetCardProps } from "widgets/BaseWidget";
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 currentForkingBuildingBlockName = (state: AppState) =>
state.ui.templates.currentForkingTemplateInfo.buildingBlock.name;
export const buildingBlocksSourcePageIdSelector = (state: AppState) =>
state.ui.templates.buildingBlockSourcePageId;
export const showTemplateNotificationSelector = (state: AppState) =>
state.ui.templates.templateNotificationSeen;
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 getBuildingBlocksList = (state: AppState) => {
return state.ui.templates.templates.filter(
(template) =>
template.functions[0] === TEMPLATE_BUILDING_BLOCKS_FILTER_FUNCTION_VALUE,
);
};
export const getBuildingBlockExplorerCards = createSelector(
getBuildingBlocksList,
(buildingBlocks) => {
const adjustedBuildingBlocks: WidgetCardProps[] = buildingBlocks.map(
(buildingBlock) => ({
rows:
buildingBlock.templateGridRowSize ||
DEFAULT_ROWS_FOR_EXPLORER_BUILDING_BLOCKS,
columns:
buildingBlock.templateGridColumnSize ||
DEFAULT_COLUMNS_FOR_EXPLORER_BUILDING_BLOCKS,
type: BUILDING_BLOCK_EXPLORER_TYPE,
displayName: buildingBlock.title,
icon:
buildingBlock.screenshotUrls.length > 1
? buildingBlock.screenshotUrls[1]
: buildingBlock.screenshotUrls[0],
thumbnail:
buildingBlock.screenshotUrls.length > 1
? buildingBlock.screenshotUrls[1]
: buildingBlock.screenshotUrls[0],
tags: [WIDGET_TAGS.BUILDING_BLOCKS],
}),
);
return adjustedBuildingBlocks;
},
);
export const getFilteredTemplateList = createSelector(
getTemplatesSelector,
getTemplateFilterSelector,
getTemplateFiltersLength,
(templates, templatesFilters, numberOfFiltersApplied) => {
const result: Template[] = [];
const activeTemplateIds: string[] = [];
const ALL_TEMPLATES_FILTER_VALUE = "All";
if (!numberOfFiltersApplied) {
return templates;
}
if (!Object.keys(templatesFilters).length) {
return templates;
}
// If only "All Templates" is selected, return all templates
if (
numberOfFiltersApplied === 1 &&
templatesFilters.functions?.includes(ALL_TEMPLATES_FILTER_VALUE)
) {
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(
getFetchedWorkspaces,
(workspaces: Workspace[]) => {
return workspaces
.filter((workspace) =>
hasCreateNewAppPermission(workspace.userPermissions ?? []),
)
.map((workspace) => {
return {
label: workspace.name,
value: workspace.id,
};
});
},
);
export const templateModalSelector = (state: AppState) =>
state.ui.templates.templatesModal;
export const templatesCountSelector = (state: AppState) =>
state.ui.templates.templates.length;
export const activeLoadingTemplateId = (state: AppState) =>
state.ui.templates.activeLoadingTemplateId;