@constructor-io/constructorio-ui-autocomplete 1.23.18 → 1.23.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -108,7 +108,7 @@ const useCioAutocomplete = (options) => {
108
108
  openMenu();
109
109
  }
110
110
  try {
111
- if ((advancedParameters === null || advancedParameters === void 0 ? void 0 : advancedParameters.fetchZeroStateOnFocus) && (items === null || items === void 0 ? void 0 : items.length) === 0) {
111
+ if (advancedParameters === null || advancedParameters === void 0 ? void 0 : advancedParameters.fetchZeroStateOnFocus) {
112
112
  fetchRecommendationResults();
113
113
  }
114
114
  (_a = cioClient === null || cioClient === void 0 ? void 0 : cioClient.tracker) === null || _a === void 0 ? void 0 : _a.trackInputFocus();
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /* eslint-disable max-params */
5
+ const react_1 = require("react");
6
+ const useActiveSections_1 = tslib_1.__importDefault(require("./useActiveSections"));
7
+ const useSectionsResults_1 = tslib_1.__importDefault(require("./useSectionsResults"));
8
+ const useActiveSectionsWithData_1 = tslib_1.__importDefault(require("./useActiveSectionsWithData"));
9
+ const useRemoveSections_1 = tslib_1.__importDefault(require("./useRemoveSections"));
10
+ function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
11
+ const [podsData, setPodsData] = (0, react_1.useState)({});
12
+ // Get Active Sections defined by configuration object
13
+ const { activeSections, showZeroStateSections, setActiveSections } = (0, useActiveSections_1.default)(query, sections, podsData, zeroStateSections);
14
+ // create refs for active sections
15
+ const sectionsRefs = (0, react_1.useRef)(activeSections.map(() => (0, react_1.createRef)()));
16
+ // Get API results for each active section
17
+ const { recommendations, autocomplete } = (0, useSectionsResults_1.default)(query, cioClient, activeSections, advancedParameters);
18
+ // Access the Pods Data from the Recommendations response to update Active Sections configuration
19
+ (0, react_1.useEffect)(() => {
20
+ if (!recommendations.podsData) {
21
+ setPodsData(recommendations.podsData);
22
+ }
23
+ }, [recommendations.podsData]);
24
+ (0, useRemoveSections_1.default)(sections, podsData, setActiveSections, showZeroStateSections, zeroStateSections);
25
+ // Combine recommendations and autocomplete results in sectionsResults
26
+ const sectionsResults = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, autocomplete.results), recommendations.results)), [autocomplete.results, recommendations.results]);
27
+ // Return current active sections populated with data from the API response sectionsResults
28
+ const activeSectionsWithData = (0, useActiveSectionsWithData_1.default)(sectionsResults, activeSections, sectionsRefs, query);
29
+ return {
30
+ fetchRecommendationResults: recommendations.fetchRecommendationResults,
31
+ activeSections,
32
+ activeSectionsWithData,
33
+ zeroStateActiveSections: showZeroStateSections,
34
+ request: autocomplete.request,
35
+ totalNumResultsPerSection: autocomplete.totalNumResultsPerSection,
36
+ };
37
+ }
38
+ exports.default = useSections;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const typeGuards_1 = require("../../typeGuards");
5
+ function useActiveSections(query, sections, podsData, zeroStateSections) {
6
+ const showZeroStateSections = !query.length && !!(zeroStateSections === null || zeroStateSections === void 0 ? void 0 : zeroStateSections.length);
7
+ const [activeSections, setActiveSections] = (0, react_1.useState)(showZeroStateSections ? zeroStateSections : sections);
8
+ // Merge Recommendation Pods Display Name from Dashboard
9
+ const activeSectionsTransformed = (0, react_1.useMemo)(() => activeSections.map((config) => {
10
+ const mergedConfig = config;
11
+ if ((0, typeGuards_1.isRecommendationsSection)(config)) {
12
+ const podData = podsData === null || podsData === void 0 ? void 0 : podsData[config.podId];
13
+ const libraryDisplayName = config.displayName;
14
+ const dashboardDisplayName = podData === null || podData === void 0 ? void 0 : podData.displayName;
15
+ mergedConfig.displayName = libraryDisplayName || dashboardDisplayName;
16
+ }
17
+ return mergedConfig;
18
+ }), [activeSections, podsData]);
19
+ return {
20
+ activeSections: activeSectionsTransformed,
21
+ showZeroStateSections,
22
+ setActiveSections,
23
+ };
24
+ }
25
+ exports.default = useActiveSections;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("../../utils");
5
+ function useActiveSectionsWithData(sectionsResults, activeSections, sectionsRefs, query) {
6
+ const [activeSectionsWithData, setActiveSectionsWithData] = (0, react_1.useState)([]);
7
+ // Add to active sections the results data and refs when autocomplete results or recommendation results fetched
8
+ (0, react_1.useEffect)(() => {
9
+ const activeSectionsWithDataValue = (0, utils_1.getActiveSectionsWithData)(activeSections, sectionsResults, sectionsRefs);
10
+ if (activeSectionsWithDataValue.length || !query) {
11
+ setActiveSectionsWithData(activeSectionsWithDataValue);
12
+ }
13
+ }, [activeSections, sectionsResults, sectionsRefs, query]);
14
+ return activeSectionsWithData;
15
+ }
16
+ exports.default = useActiveSectionsWithData;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable max-params */
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../../utils");
6
+ function useRemoveSections(sections, podsData, setActiveSections, showZeroStateSections, zeroStateSections) {
7
+ // Remove sections if necessary
8
+ (0, react_1.useEffect)(() => {
9
+ var _a, _b;
10
+ const features = (0, utils_1.getFeatures)((_b = (_a = Object.values(podsData || {})) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.request);
11
+ if (showZeroStateSections) {
12
+ if (!features.featureDisplayZeroStateRecommendations) {
13
+ setActiveSections([]);
14
+ }
15
+ else {
16
+ setActiveSections(zeroStateSections);
17
+ }
18
+ }
19
+ else {
20
+ setActiveSections(sections);
21
+ }
22
+ }, [zeroStateSections, showZeroStateSections, sections, podsData, setActiveSections]);
23
+ }
24
+ exports.default = useRemoveSections;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const react_1 = require("react");
5
+ const useDebouncedFetchSections_1 = tslib_1.__importDefault(require("../useDebouncedFetchSections"));
6
+ const useFetchRecommendationPod_1 = tslib_1.__importDefault(require("../useFetchRecommendationPod"));
7
+ const typeGuards_1 = require("../../typeGuards");
8
+ function useSectionsResults(query, cioClient, activeSections, advancedParameters) {
9
+ // Fetch Autocomplete Results
10
+ const activeAutocompleteSections = (0, react_1.useMemo)(() => activeSections === null || activeSections === void 0 ? void 0 : activeSections.filter((config) => (0, typeGuards_1.isAutocompleteSection)(config)), [activeSections]);
11
+ const { sectionsData: autocompleteResults, request, totalNumResultsPerSection, } = (0, useDebouncedFetchSections_1.default)(query, cioClient, activeAutocompleteSections, advancedParameters);
12
+ // Fetch Recommendations Results
13
+ const activeRecommendationsSections = (0, react_1.useMemo)(() => activeSections === null || activeSections === void 0 ? void 0 : activeSections.filter((config) => (0, typeGuards_1.isRecommendationsSection)(config)), [activeSections]);
14
+ const { fetchRecommendationResults, recommendationsResults, podsData } = (0, useFetchRecommendationPod_1.default)(cioClient, activeRecommendationsSections, advancedParameters === null || advancedParameters === void 0 ? void 0 : advancedParameters.fetchZeroStateOnFocus);
15
+ return {
16
+ recommendations: {
17
+ results: recommendationsResults,
18
+ fetchRecommendationResults,
19
+ podsData,
20
+ },
21
+ autocomplete: {
22
+ results: autocompleteResults,
23
+ request,
24
+ totalNumResultsPerSection,
25
+ },
26
+ };
27
+ }
28
+ exports.default = useSectionsResults;
package/lib/cjs/utils.js CHANGED
@@ -144,14 +144,14 @@ const getCioClient = (apiKey, cioJsClientOptions) => {
144
144
  return null;
145
145
  };
146
146
  exports.getCioClient = getCioClient;
147
- const getActiveSectionsWithData = (activeSections, sectionResults, sectionsRefs) => {
147
+ const getActiveSectionsWithData = (activeSections, sectionsResults, sectionsRefs) => {
148
148
  const activeSectionsWithData = [];
149
- activeSections === null || activeSections === void 0 ? void 0 : activeSections.forEach((sectionConfig, index) => {
149
+ const getSectionData = (sectionConfig) => {
150
150
  const { type } = sectionConfig;
151
151
  let sectionData;
152
152
  switch (type) {
153
153
  case 'recommendations':
154
- sectionData = sectionResults[sectionConfig.podId];
154
+ sectionData = sectionsResults[sectionConfig.podId];
155
155
  break;
156
156
  case 'custom':
157
157
  // Copy id from data to the top level
@@ -162,8 +162,12 @@ const getActiveSectionsWithData = (activeSections, sectionResults, sectionsRefs)
162
162
  break;
163
163
  default:
164
164
  // Autocomplete
165
- sectionData = sectionResults[sectionConfig.indexSectionName];
165
+ sectionData = sectionsResults[sectionConfig.indexSectionName];
166
166
  }
167
+ return sectionData;
168
+ };
169
+ activeSections === null || activeSections === void 0 ? void 0 : activeSections.forEach((sectionConfig, index) => {
170
+ const sectionData = getSectionData(sectionConfig);
167
171
  if (Array.isArray(sectionData)) {
168
172
  const section = Object.assign(Object.assign({}, sectionConfig), { data: sectionData });
169
173
  // If ref passed as part of `SectionConfiguration`, use it.
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = '1.23.18';
3
+ exports.default = '1.23.19';
@@ -117,7 +117,7 @@ const useCioAutocomplete = (options) => {
117
117
  openMenu();
118
118
  }
119
119
  try {
120
- if (advancedParameters?.fetchZeroStateOnFocus && items?.length === 0) {
120
+ if (advancedParameters?.fetchZeroStateOnFocus) {
121
121
  fetchRecommendationResults();
122
122
  }
123
123
  cioClient?.tracker?.trackInputFocus();
@@ -0,0 +1,34 @@
1
+ /* eslint-disable max-params */
2
+ import { createRef, useEffect, useMemo, useRef, useState } from 'react';
3
+ import useActiveSections from './useActiveSections';
4
+ import useSectionsResults from './useSectionsResults';
5
+ import useActiveSectionsWithData from './useActiveSectionsWithData';
6
+ import useRemoveSections from './useRemoveSections';
7
+ export default function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
8
+ const [podsData, setPodsData] = useState({});
9
+ // Get Active Sections defined by configuration object
10
+ const { activeSections, showZeroStateSections, setActiveSections } = useActiveSections(query, sections, podsData, zeroStateSections);
11
+ // create refs for active sections
12
+ const sectionsRefs = useRef(activeSections.map(() => createRef()));
13
+ // Get API results for each active section
14
+ const { recommendations, autocomplete } = useSectionsResults(query, cioClient, activeSections, advancedParameters);
15
+ // Access the Pods Data from the Recommendations response to update Active Sections configuration
16
+ useEffect(() => {
17
+ if (!recommendations.podsData) {
18
+ setPodsData(recommendations.podsData);
19
+ }
20
+ }, [recommendations.podsData]);
21
+ useRemoveSections(sections, podsData, setActiveSections, showZeroStateSections, zeroStateSections);
22
+ // Combine recommendations and autocomplete results in sectionsResults
23
+ const sectionsResults = useMemo(() => ({ ...autocomplete.results, ...recommendations.results }), [autocomplete.results, recommendations.results]);
24
+ // Return current active sections populated with data from the API response sectionsResults
25
+ const activeSectionsWithData = useActiveSectionsWithData(sectionsResults, activeSections, sectionsRefs, query);
26
+ return {
27
+ fetchRecommendationResults: recommendations.fetchRecommendationResults,
28
+ activeSections,
29
+ activeSectionsWithData,
30
+ zeroStateActiveSections: showZeroStateSections,
31
+ request: autocomplete.request,
32
+ totalNumResultsPerSection: autocomplete.totalNumResultsPerSection,
33
+ };
34
+ }
@@ -0,0 +1,22 @@
1
+ import { useMemo, useState } from 'react';
2
+ import { isRecommendationsSection } from '../../typeGuards';
3
+ export default function useActiveSections(query, sections, podsData, zeroStateSections) {
4
+ const showZeroStateSections = !query.length && !!zeroStateSections?.length;
5
+ const [activeSections, setActiveSections] = useState(showZeroStateSections ? zeroStateSections : sections);
6
+ // Merge Recommendation Pods Display Name from Dashboard
7
+ const activeSectionsTransformed = useMemo(() => activeSections.map((config) => {
8
+ const mergedConfig = config;
9
+ if (isRecommendationsSection(config)) {
10
+ const podData = podsData?.[config.podId];
11
+ const libraryDisplayName = config.displayName;
12
+ const dashboardDisplayName = podData?.displayName;
13
+ mergedConfig.displayName = libraryDisplayName || dashboardDisplayName;
14
+ }
15
+ return mergedConfig;
16
+ }), [activeSections, podsData]);
17
+ return {
18
+ activeSections: activeSectionsTransformed,
19
+ showZeroStateSections,
20
+ setActiveSections,
21
+ };
22
+ }
@@ -0,0 +1,13 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { getActiveSectionsWithData } from '../../utils';
3
+ export default function useActiveSectionsWithData(sectionsResults, activeSections, sectionsRefs, query) {
4
+ const [activeSectionsWithData, setActiveSectionsWithData] = useState([]);
5
+ // Add to active sections the results data and refs when autocomplete results or recommendation results fetched
6
+ useEffect(() => {
7
+ const activeSectionsWithDataValue = getActiveSectionsWithData(activeSections, sectionsResults, sectionsRefs);
8
+ if (activeSectionsWithDataValue.length || !query) {
9
+ setActiveSectionsWithData(activeSectionsWithDataValue);
10
+ }
11
+ }, [activeSections, sectionsResults, sectionsRefs, query]);
12
+ return activeSectionsWithData;
13
+ }
@@ -0,0 +1,20 @@
1
+ /* eslint-disable max-params */
2
+ import { useEffect } from 'react';
3
+ import { getFeatures } from '../../utils';
4
+ export default function useRemoveSections(sections, podsData, setActiveSections, showZeroStateSections, zeroStateSections) {
5
+ // Remove sections if necessary
6
+ useEffect(() => {
7
+ const features = getFeatures(Object.values(podsData || {})?.[0]?.request);
8
+ if (showZeroStateSections) {
9
+ if (!features.featureDisplayZeroStateRecommendations) {
10
+ setActiveSections([]);
11
+ }
12
+ else {
13
+ setActiveSections(zeroStateSections);
14
+ }
15
+ }
16
+ else {
17
+ setActiveSections(sections);
18
+ }
19
+ }, [zeroStateSections, showZeroStateSections, sections, podsData, setActiveSections]);
20
+ }
@@ -0,0 +1,24 @@
1
+ import { useMemo } from 'react';
2
+ import useDebouncedFetchSection from '../useDebouncedFetchSections';
3
+ import useFetchRecommendationPod from '../useFetchRecommendationPod';
4
+ import { isAutocompleteSection, isRecommendationsSection } from '../../typeGuards';
5
+ export default function useSectionsResults(query, cioClient, activeSections, advancedParameters) {
6
+ // Fetch Autocomplete Results
7
+ const activeAutocompleteSections = useMemo(() => activeSections?.filter((config) => isAutocompleteSection(config)), [activeSections]);
8
+ const { sectionsData: autocompleteResults, request, totalNumResultsPerSection, } = useDebouncedFetchSection(query, cioClient, activeAutocompleteSections, advancedParameters);
9
+ // Fetch Recommendations Results
10
+ const activeRecommendationsSections = useMemo(() => activeSections?.filter((config) => isRecommendationsSection(config)), [activeSections]);
11
+ const { fetchRecommendationResults, recommendationsResults, podsData } = useFetchRecommendationPod(cioClient, activeRecommendationsSections, advancedParameters?.fetchZeroStateOnFocus);
12
+ return {
13
+ recommendations: {
14
+ results: recommendationsResults,
15
+ fetchRecommendationResults,
16
+ podsData,
17
+ },
18
+ autocomplete: {
19
+ results: autocompleteResults,
20
+ request,
21
+ totalNumResultsPerSection,
22
+ },
23
+ };
24
+ }
package/lib/mjs/utils.js CHANGED
@@ -130,14 +130,14 @@ export const getCioClient = (apiKey, cioJsClientOptions) => {
130
130
  }
131
131
  return null;
132
132
  };
133
- export const getActiveSectionsWithData = (activeSections, sectionResults, sectionsRefs) => {
133
+ export const getActiveSectionsWithData = (activeSections, sectionsResults, sectionsRefs) => {
134
134
  const activeSectionsWithData = [];
135
- activeSections?.forEach((sectionConfig, index) => {
135
+ const getSectionData = (sectionConfig) => {
136
136
  const { type } = sectionConfig;
137
137
  let sectionData;
138
138
  switch (type) {
139
139
  case 'recommendations':
140
- sectionData = sectionResults[sectionConfig.podId];
140
+ sectionData = sectionsResults[sectionConfig.podId];
141
141
  break;
142
142
  case 'custom':
143
143
  // Copy id from data to the top level
@@ -148,8 +148,12 @@ export const getActiveSectionsWithData = (activeSections, sectionResults, sectio
148
148
  break;
149
149
  default:
150
150
  // Autocomplete
151
- sectionData = sectionResults[sectionConfig.indexSectionName];
151
+ sectionData = sectionsResults[sectionConfig.indexSectionName];
152
152
  }
153
+ return sectionData;
154
+ };
155
+ activeSections?.forEach((sectionConfig, index) => {
156
+ const sectionData = getSectionData(sectionConfig);
153
157
  if (Array.isArray(sectionData)) {
154
158
  const section = {
155
159
  ...sectionConfig,
@@ -1 +1 @@
1
- export default '1.23.18';
1
+ export default '1.23.19';
@@ -1,11 +1,11 @@
1
1
  import ConstructorIO from '@constructor-io/constructorio-client-javascript';
2
2
  import { Nullable } from '@constructor-io/constructorio-client-javascript/lib/types';
3
- import { AdvancedParameters, UserDefinedSection, Section } from '../types';
3
+ import { AdvancedParameters, UserDefinedSection } from '../../types';
4
4
  export default function useSections(query: string, cioClient: Nullable<ConstructorIO>, sections: UserDefinedSection[], zeroStateSections?: UserDefinedSection[], advancedParameters?: AdvancedParameters): {
5
5
  fetchRecommendationResults: () => Promise<void>;
6
6
  activeSections: UserDefinedSection[];
7
- activeSectionsWithData: Section[];
8
- zeroStateActiveSections: number | false | undefined;
7
+ activeSectionsWithData: import("../../types").Section[];
8
+ zeroStateActiveSections: boolean;
9
9
  request: Partial<import("@constructor-io/constructorio-client-javascript").AutocompleteRequestType>;
10
10
  totalNumResultsPerSection: Record<string, number>;
11
11
  };
@@ -0,0 +1,6 @@
1
+ import { PodData, UserDefinedSection } from '../../types';
2
+ export default function useActiveSections(query: string, sections: UserDefinedSection[], podsData?: Record<string, PodData>, zeroStateSections?: UserDefinedSection[]): {
3
+ activeSections: UserDefinedSection[];
4
+ showZeroStateSections: boolean;
5
+ setActiveSections: import("react").Dispatch<import("react").SetStateAction<UserDefinedSection[]>>;
6
+ };
@@ -0,0 +1,3 @@
1
+ import { RefObject } from 'react';
2
+ import { UserDefinedSection, Section, SectionsData } from '../../types';
3
+ export default function useActiveSectionsWithData(sectionsResults: SectionsData, activeSections: UserDefinedSection[], sectionsRefs: React.MutableRefObject<RefObject<HTMLLIElement>[]>, query: string): Section[];
@@ -0,0 +1,2 @@
1
+ import { PodData, UserDefinedSection } from '../../types';
2
+ export default function useRemoveSections(sections: UserDefinedSection[], podsData: Record<string, PodData>, setActiveSections: any, showZeroStateSections: boolean, zeroStateSections?: UserDefinedSection[]): void;
@@ -0,0 +1,15 @@
1
+ import ConstructorIO from '@constructor-io/constructorio-client-javascript';
2
+ import { Nullable } from '@constructor-io/constructorio-client-javascript/lib/types';
3
+ import { AdvancedParameters, UserDefinedSection } from '../../types';
4
+ export default function useSectionsResults(query: string, cioClient: Nullable<ConstructorIO>, activeSections: UserDefinedSection[], advancedParameters?: AdvancedParameters): {
5
+ recommendations: {
6
+ results: import("../../types").SectionsData;
7
+ fetchRecommendationResults: () => Promise<void>;
8
+ podsData: Record<string, import("../../types").PodData>;
9
+ };
10
+ autocomplete: {
11
+ results: import("../../types").SectionsData;
12
+ request: Partial<import("@constructor-io/constructorio-client-javascript").AutocompleteRequestType>;
13
+ totalNumResultsPerSection: Record<string, number>;
14
+ };
15
+ };
@@ -37,7 +37,7 @@ export declare const functionStrings: {
37
37
  export declare const stringifyWithDefaults: (obj: any) => string;
38
38
  export declare const disableStoryActions: (story: any) => void;
39
39
  export declare const getCioClient: (apiKey?: string, cioJsClientOptions?: ConstructorClientOptions) => ConstructorIOClient | null;
40
- export declare const getActiveSectionsWithData: (activeSections: UserDefinedSection[], sectionResults: SectionsData, sectionsRefs: React.MutableRefObject<React.RefObject<HTMLLIElement>[]>) => Section[];
40
+ export declare const getActiveSectionsWithData: (activeSections: UserDefinedSection[], sectionsResults: SectionsData, sectionsRefs: React.MutableRefObject<React.RefObject<HTMLLIElement>[]>) => Section[];
41
41
  export declare const escapeRegExp: (string: string) => string;
42
42
  export declare const trackRecommendationView: (target: HTMLElement, activeSectionsWithData: Section[], cioClient: Nullable<ConstructorIOClient>) => void;
43
43
  export declare const getItemsForActiveSections: (activeSectionsWithData: Section[]) => Item[];
@@ -1,2 +1,2 @@
1
- declare const _default: "1.23.18";
1
+ declare const _default: "1.23.19";
2
2
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-autocomplete",
3
- "version": "1.23.18",
3
+ "version": "1.23.19",
4
4
  "description": "Constructor.io Autocomplete UI library for web applications",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- /* eslint-disable max-params */
5
- const react_1 = require("react");
6
- const utils_1 = require("../utils");
7
- const useDebouncedFetchSections_1 = tslib_1.__importDefault(require("./useDebouncedFetchSections"));
8
- const useFetchRecommendationPod_1 = tslib_1.__importDefault(require("./useFetchRecommendationPod"));
9
- const typeGuards_1 = require("../typeGuards");
10
- function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
11
- const zeroStateActiveSections = !query.length && (zeroStateSections === null || zeroStateSections === void 0 ? void 0 : zeroStateSections.length);
12
- // Define All Sections
13
- const [activeSections, setActiveSections] = (0, react_1.useState)(zeroStateActiveSections ? zeroStateSections : sections);
14
- const sectionsRefs = (0, react_1.useRef)(activeSections.map(() => (0, react_1.createRef)()));
15
- const [activeSectionsWithData, setActiveSectionsWithData] = (0, react_1.useState)([]);
16
- const autocompleteSections = (0, react_1.useMemo)(() => activeSections === null || activeSections === void 0 ? void 0 : activeSections.filter((config) => (0, typeGuards_1.isAutocompleteSection)(config)), [activeSections]);
17
- const recommendationsSections = (0, react_1.useMemo)(() => activeSections === null || activeSections === void 0 ? void 0 : activeSections.filter((config) => (0, typeGuards_1.isRecommendationsSection)(config)), [activeSections]);
18
- // Fetch Autocomplete Results
19
- const { sectionsData: autocompleteResults, request, totalNumResultsPerSection, } = (0, useDebouncedFetchSections_1.default)(query, cioClient, autocompleteSections, advancedParameters);
20
- // Fetch Recommendations Results
21
- const { fetchRecommendationResults, recommendationsResults, podsData } = (0, useFetchRecommendationPod_1.default)(cioClient, recommendationsSections, advancedParameters === null || advancedParameters === void 0 ? void 0 : advancedParameters.fetchZeroStateOnFocus);
22
- // Remove sections if necessary
23
- (0, react_1.useEffect)(() => {
24
- var _a, _b;
25
- const features = (0, utils_1.getFeatures)((_b = (_a = Object.values(podsData || {})) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.request);
26
- if (zeroStateActiveSections) {
27
- if (!features.featureDisplayZeroStateRecommendations) {
28
- setActiveSections([]);
29
- }
30
- else {
31
- setActiveSections(zeroStateSections);
32
- }
33
- }
34
- else {
35
- setActiveSections(sections);
36
- }
37
- }, [zeroStateSections, zeroStateActiveSections, sections, podsData]);
38
- // Merge Recommendation Pods Display Name from Dashboard
39
- const activeSectionConfigs = (0, react_1.useMemo)(() => activeSections.map((config) => {
40
- const mergedConfig = config;
41
- if ((0, typeGuards_1.isRecommendationsSection)(config)) {
42
- const podData = podsData[config.podId];
43
- const libraryDisplayName = config.displayName;
44
- const dashboardDisplayName = podData === null || podData === void 0 ? void 0 : podData.displayName;
45
- mergedConfig.displayName = libraryDisplayName || dashboardDisplayName;
46
- }
47
- return mergedConfig;
48
- }), [activeSections, podsData]);
49
- // Add to active sections the results data and refs when autocomplete results or recommendation results fetched
50
- (0, react_1.useEffect)(() => {
51
- const sectionsResults = Object.assign(Object.assign({}, autocompleteResults), recommendationsResults);
52
- setActiveSectionsWithData((0, utils_1.getActiveSectionsWithData)(activeSectionConfigs, sectionsResults, sectionsRefs));
53
- }, [autocompleteResults, recommendationsResults, activeSectionConfigs]);
54
- return {
55
- fetchRecommendationResults,
56
- activeSections,
57
- activeSectionsWithData,
58
- zeroStateActiveSections,
59
- request,
60
- totalNumResultsPerSection,
61
- };
62
- }
63
- exports.default = useSections;
@@ -1,58 +0,0 @@
1
- /* eslint-disable max-params */
2
- import { createRef, useEffect, useMemo, useRef, useState } from 'react';
3
- import { getActiveSectionsWithData, getFeatures } from '../utils';
4
- import useDebouncedFetchSection from './useDebouncedFetchSections';
5
- import useFetchRecommendationPod from './useFetchRecommendationPod';
6
- import { isAutocompleteSection, isRecommendationsSection } from '../typeGuards';
7
- export default function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
8
- const zeroStateActiveSections = !query.length && zeroStateSections?.length;
9
- // Define All Sections
10
- const [activeSections, setActiveSections] = useState(zeroStateActiveSections ? zeroStateSections : sections);
11
- const sectionsRefs = useRef(activeSections.map(() => createRef()));
12
- const [activeSectionsWithData, setActiveSectionsWithData] = useState([]);
13
- const autocompleteSections = useMemo(() => activeSections?.filter((config) => isAutocompleteSection(config)), [activeSections]);
14
- const recommendationsSections = useMemo(() => activeSections?.filter((config) => isRecommendationsSection(config)), [activeSections]);
15
- // Fetch Autocomplete Results
16
- const { sectionsData: autocompleteResults, request, totalNumResultsPerSection, } = useDebouncedFetchSection(query, cioClient, autocompleteSections, advancedParameters);
17
- // Fetch Recommendations Results
18
- const { fetchRecommendationResults, recommendationsResults, podsData } = useFetchRecommendationPod(cioClient, recommendationsSections, advancedParameters?.fetchZeroStateOnFocus);
19
- // Remove sections if necessary
20
- useEffect(() => {
21
- const features = getFeatures(Object.values(podsData || {})?.[0]?.request);
22
- if (zeroStateActiveSections) {
23
- if (!features.featureDisplayZeroStateRecommendations) {
24
- setActiveSections([]);
25
- }
26
- else {
27
- setActiveSections(zeroStateSections);
28
- }
29
- }
30
- else {
31
- setActiveSections(sections);
32
- }
33
- }, [zeroStateSections, zeroStateActiveSections, sections, podsData]);
34
- // Merge Recommendation Pods Display Name from Dashboard
35
- const activeSectionConfigs = useMemo(() => activeSections.map((config) => {
36
- const mergedConfig = config;
37
- if (isRecommendationsSection(config)) {
38
- const podData = podsData[config.podId];
39
- const libraryDisplayName = config.displayName;
40
- const dashboardDisplayName = podData?.displayName;
41
- mergedConfig.displayName = libraryDisplayName || dashboardDisplayName;
42
- }
43
- return mergedConfig;
44
- }), [activeSections, podsData]);
45
- // Add to active sections the results data and refs when autocomplete results or recommendation results fetched
46
- useEffect(() => {
47
- const sectionsResults = { ...autocompleteResults, ...recommendationsResults };
48
- setActiveSectionsWithData(getActiveSectionsWithData(activeSectionConfigs, sectionsResults, sectionsRefs));
49
- }, [autocompleteResults, recommendationsResults, activeSectionConfigs]);
50
- return {
51
- fetchRecommendationResults,
52
- activeSections,
53
- activeSectionsWithData,
54
- zeroStateActiveSections,
55
- request,
56
- totalNumResultsPerSection,
57
- };
58
- }