@constructor-io/constructorio-ui-autocomplete 1.20.1 → 1.20.2
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.
- package/dist/constructorio-ui-autocomplete-bundled.js +11 -11
- package/lib/cjs/hooks/useCioAutocomplete.js +5 -2
- package/lib/cjs/hooks/useFetchRecommendationPod.js +6 -2
- package/lib/cjs/hooks/useSections.js +18 -2
- package/lib/cjs/utils.js +8 -3
- package/lib/cjs/version.js +1 -1
- package/lib/mjs/hooks/useCioAutocomplete.js +6 -3
- package/lib/mjs/hooks/useFetchRecommendationPod.js +6 -2
- package/lib/mjs/hooks/useSections.js +18 -3
- package/lib/mjs/utils.js +6 -1
- package/lib/mjs/version.js +1 -1
- package/lib/types/components/Autocomplete/CioAutocompleteProvider.d.ts +1 -0
- package/lib/types/hooks/useCioAutocomplete.d.ts +1 -0
- package/lib/types/hooks/useSections.d.ts +1 -1
- package/lib/types/types.d.ts +1 -0
- package/lib/types/utils.d.ts +2 -1
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
|
@@ -58,6 +58,7 @@ const useCioAutocomplete = (options) => {
|
|
|
58
58
|
const cioClient = (0, useCioClient_1.default)({ apiKey, cioJsClient, cioJsClientOptions });
|
|
59
59
|
// Get autocomplete sections (autocomplete + recommendations + custom)
|
|
60
60
|
const { activeSections, activeSectionsWithData, zeroStateActiveSections, request } = (0, useSections_1.default)(query, cioClient, sections, zeroStateSections, advancedParameters);
|
|
61
|
+
const features = (0, react_1.useMemo)(() => (0, utils_1.getFeatures)(request), [request]);
|
|
61
62
|
// Get dropdown items array from active sections (autocomplete + recommendations + custom)
|
|
62
63
|
const items = (0, react_1.useMemo)(() => (0, utils_1.getItemsForActiveSections)(activeSectionsWithData), [activeSectionsWithData]);
|
|
63
64
|
const { isOpen, getMenuProps, getLabelProps, openMenu, closeMenu, highlightedIndex, getInputProps, getItemProps, } = (0, useDownShift_1.default)({ setQuery, items, onSubmit, cioClient, previousQuery });
|
|
@@ -69,7 +70,7 @@ const useCioAutocomplete = (options) => {
|
|
|
69
70
|
query,
|
|
70
71
|
sections: activeSectionsWithData,
|
|
71
72
|
request,
|
|
72
|
-
featureToggles:
|
|
73
|
+
featureToggles: features,
|
|
73
74
|
isOpen: isOpen && (items === null || items === void 0 ? void 0 : items.length) > 0,
|
|
74
75
|
getMenuProps: () => (Object.assign(Object.assign({}, getMenuProps()), { className: 'cio-results', 'data-testid': 'cio-results' })),
|
|
75
76
|
getLabelProps,
|
|
@@ -92,7 +93,9 @@ const useCioAutocomplete = (options) => {
|
|
|
92
93
|
if (options.onFocus) {
|
|
93
94
|
options.onFocus();
|
|
94
95
|
}
|
|
95
|
-
if (zeroStateActiveSections &&
|
|
96
|
+
if (zeroStateActiveSections &&
|
|
97
|
+
openOnFocus !== false &&
|
|
98
|
+
features.featureDisplayZeroStateRecommendations) {
|
|
96
99
|
openMenu();
|
|
97
100
|
}
|
|
98
101
|
if (query === null || query === void 0 ? void 0 : query.length) {
|
|
@@ -15,14 +15,18 @@ const useFetchRecommendationPod = (cioClient, recommendationPods) => {
|
|
|
15
15
|
}));
|
|
16
16
|
const recommendationsPodResults = {};
|
|
17
17
|
const recommendationsPodsData = {};
|
|
18
|
-
responses.forEach(({ response }, index) => {
|
|
18
|
+
responses.forEach(({ response, request }, index) => {
|
|
19
19
|
const { pod, results } = response;
|
|
20
20
|
if (pod === null || pod === void 0 ? void 0 : pod.id) {
|
|
21
21
|
recommendationsPodResults[pod.id] = results === null || results === void 0 ? void 0 : results.map((item) => {
|
|
22
22
|
var _a, _b;
|
|
23
23
|
return (Object.assign(Object.assign({}, item), { id: (_a = item === null || item === void 0 ? void 0 : item.data) === null || _a === void 0 ? void 0 : _a.id, section: (_b = recommendationPods[index]) === null || _b === void 0 ? void 0 : _b.indexSectionName, podId: pod.id }));
|
|
24
24
|
});
|
|
25
|
-
recommendationsPodsData[pod.id] = {
|
|
25
|
+
recommendationsPodsData[pod.id] = {
|
|
26
|
+
displayName: pod.display_name,
|
|
27
|
+
podId: pod.id,
|
|
28
|
+
request,
|
|
29
|
+
};
|
|
26
30
|
}
|
|
27
31
|
});
|
|
28
32
|
try {
|
|
@@ -8,9 +8,9 @@ const useDebouncedFetchSections_1 = tslib_1.__importDefault(require("./useDeboun
|
|
|
8
8
|
const useFetchRecommendationPod_1 = tslib_1.__importDefault(require("./useFetchRecommendationPod"));
|
|
9
9
|
const typeGuards_1 = require("../typeGuards");
|
|
10
10
|
function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
|
|
11
|
-
const zeroStateActiveSections = !query.length && zeroStateSections;
|
|
11
|
+
const zeroStateActiveSections = !query.length && (zeroStateSections === null || zeroStateSections === void 0 ? void 0 : zeroStateSections.length);
|
|
12
12
|
// Define All Sections
|
|
13
|
-
const activeSections = zeroStateActiveSections ? zeroStateSections : sections;
|
|
13
|
+
const [activeSections, setActiveSections] = (0, react_1.useState)(zeroStateActiveSections ? zeroStateSections : sections);
|
|
14
14
|
const sectionsRefs = (0, react_1.useRef)(activeSections.map(() => (0, react_1.createRef)()));
|
|
15
15
|
const [activeSectionsWithData, setActiveSectionsWithData] = (0, react_1.useState)([]);
|
|
16
16
|
const autocompleteSections = (0, react_1.useMemo)(() => activeSections === null || activeSections === void 0 ? void 0 : activeSections.filter((config) => (0, typeGuards_1.isAutocompleteSection)(config)), [activeSections]);
|
|
@@ -19,6 +19,22 @@ function useSections(query, cioClient, sections, zeroStateSections, advancedPara
|
|
|
19
19
|
const { sectionsData: autocompleteResults, request } = (0, useDebouncedFetchSections_1.default)(query, cioClient, autocompleteSections, advancedParameters);
|
|
20
20
|
// Fetch Recommendations Results
|
|
21
21
|
const { recommendationsResults, podsData } = (0, useFetchRecommendationPod_1.default)(cioClient, recommendationsSections);
|
|
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]);
|
|
22
38
|
// Merge Recommendation Pods Display Name from Dashboard
|
|
23
39
|
const activeSectionConfigs = (0, react_1.useMemo)(() => activeSections.map((config) => {
|
|
24
40
|
const mergedConfig = config;
|
package/lib/cjs/utils.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.translate = exports.getItemsForActiveSections = exports.trackRecommendationView = exports.escapeRegExp = exports.getActiveSectionsWithData = exports.getCioClient = exports.disableStoryActions = exports.stringifyWithDefaults = exports.functionStrings = exports.getStoryParams = exports.sleep = exports.clearConstructorRequests = exports.isTrackingRequestSent = exports.toKebabCase = exports.camelToStartCase = exports.getItemPosition = exports.
|
|
3
|
+
exports.translate = exports.getItemsForActiveSections = exports.trackRecommendationView = exports.escapeRegExp = exports.getActiveSectionsWithData = exports.getCioClient = exports.disableStoryActions = exports.stringifyWithDefaults = exports.functionStrings = exports.getStoryParams = exports.sleep = exports.clearConstructorRequests = exports.isTrackingRequestSent = exports.toKebabCase = exports.camelToStartCase = exports.getItemPosition = exports.getFeatures = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const constructorio_client_javascript_1 = tslib_1.__importDefault(require("@constructor-io/constructorio-client-javascript"));
|
|
6
6
|
const typeGuards_1 = require("./typeGuards");
|
|
7
7
|
const version_1 = tslib_1.__importDefault(require("./version"));
|
|
8
|
-
function
|
|
8
|
+
function getFeatures(request) {
|
|
9
9
|
var _a, _b;
|
|
10
10
|
let featureDisplaySearchSuggestionImages = false;
|
|
11
11
|
let featureDisplaySearchSuggestionResultCounts = false;
|
|
12
|
+
let featureDisplayZeroStateRecommendations = true;
|
|
12
13
|
if (((_a = request === null || request === void 0 ? void 0 : request.features) === null || _a === void 0 ? void 0 : _a.custom_autosuggest_ui) === true) {
|
|
13
14
|
switch ((_b = request === null || request === void 0 ? void 0 : request.feature_variants) === null || _b === void 0 ? void 0 : _b.custom_autosuggest_ui) {
|
|
14
15
|
case 'custom_autosuggest_ui_result_count':
|
|
@@ -21,6 +22,9 @@ function getSearchSuggestionFeatures(request) {
|
|
|
21
22
|
featureDisplaySearchSuggestionImages = true;
|
|
22
23
|
featureDisplaySearchSuggestionResultCounts = true;
|
|
23
24
|
break;
|
|
25
|
+
case 'custom_autosuggest_ui_disable_recommendations_in_zero_state':
|
|
26
|
+
featureDisplayZeroStateRecommendations = false;
|
|
27
|
+
break;
|
|
24
28
|
default:
|
|
25
29
|
break;
|
|
26
30
|
}
|
|
@@ -28,9 +32,10 @@ function getSearchSuggestionFeatures(request) {
|
|
|
28
32
|
return {
|
|
29
33
|
featureDisplaySearchSuggestionImages,
|
|
30
34
|
featureDisplaySearchSuggestionResultCounts,
|
|
35
|
+
featureDisplayZeroStateRecommendations,
|
|
31
36
|
};
|
|
32
37
|
}
|
|
33
|
-
exports.
|
|
38
|
+
exports.getFeatures = getFeatures;
|
|
34
39
|
const getItemPosition = ({ item, items }) => {
|
|
35
40
|
var _a;
|
|
36
41
|
const index = items.findIndex((itemInFlatList) => (itemInFlatList === null || itemInFlatList === void 0 ? void 0 : itemInFlatList.id) === (item === null || item === void 0 ? void 0 : item.id));
|
package/lib/cjs/version.js
CHANGED
|
@@ -3,7 +3,7 @@ import { useMemo, useState } from 'react';
|
|
|
3
3
|
import useCioClient from './useCioClient';
|
|
4
4
|
import useDownShift from './useDownShift';
|
|
5
5
|
import usePrevious from './usePrevious';
|
|
6
|
-
import { getItemPosition, getItemsForActiveSections,
|
|
6
|
+
import { getItemPosition, getItemsForActiveSections, getFeatures, trackRecommendationView, toKebabCase, } from '../utils';
|
|
7
7
|
import useConsoleErrors from './useConsoleErrors';
|
|
8
8
|
import useSections from './useSections';
|
|
9
9
|
import useRecommendationsObserver from './useRecommendationsObserver';
|
|
@@ -54,6 +54,7 @@ const useCioAutocomplete = (options) => {
|
|
|
54
54
|
const cioClient = useCioClient({ apiKey, cioJsClient, cioJsClientOptions });
|
|
55
55
|
// Get autocomplete sections (autocomplete + recommendations + custom)
|
|
56
56
|
const { activeSections, activeSectionsWithData, zeroStateActiveSections, request } = useSections(query, cioClient, sections, zeroStateSections, advancedParameters);
|
|
57
|
+
const features = useMemo(() => getFeatures(request), [request]);
|
|
57
58
|
// Get dropdown items array from active sections (autocomplete + recommendations + custom)
|
|
58
59
|
const items = useMemo(() => getItemsForActiveSections(activeSectionsWithData), [activeSectionsWithData]);
|
|
59
60
|
const { isOpen, getMenuProps, getLabelProps, openMenu, closeMenu, highlightedIndex, getInputProps, getItemProps, } = useDownShift({ setQuery, items, onSubmit, cioClient, previousQuery });
|
|
@@ -65,7 +66,7 @@ const useCioAutocomplete = (options) => {
|
|
|
65
66
|
query,
|
|
66
67
|
sections: activeSectionsWithData,
|
|
67
68
|
request,
|
|
68
|
-
featureToggles:
|
|
69
|
+
featureToggles: features,
|
|
69
70
|
isOpen: isOpen && items?.length > 0,
|
|
70
71
|
getMenuProps: () => ({
|
|
71
72
|
...getMenuProps(),
|
|
@@ -98,7 +99,9 @@ const useCioAutocomplete = (options) => {
|
|
|
98
99
|
if (options.onFocus) {
|
|
99
100
|
options.onFocus();
|
|
100
101
|
}
|
|
101
|
-
if (zeroStateActiveSections &&
|
|
102
|
+
if (zeroStateActiveSections &&
|
|
103
|
+
openOnFocus !== false &&
|
|
104
|
+
features.featureDisplayZeroStateRecommendations) {
|
|
102
105
|
openMenu();
|
|
103
106
|
}
|
|
104
107
|
if (query?.length) {
|
|
@@ -12,7 +12,7 @@ const useFetchRecommendationPod = (cioClient, recommendationPods) => {
|
|
|
12
12
|
})));
|
|
13
13
|
const recommendationsPodResults = {};
|
|
14
14
|
const recommendationsPodsData = {};
|
|
15
|
-
responses.forEach(({ response }, index) => {
|
|
15
|
+
responses.forEach(({ response, request }, index) => {
|
|
16
16
|
const { pod, results } = response;
|
|
17
17
|
if (pod?.id) {
|
|
18
18
|
recommendationsPodResults[pod.id] = results?.map((item) => ({
|
|
@@ -21,7 +21,11 @@ const useFetchRecommendationPod = (cioClient, recommendationPods) => {
|
|
|
21
21
|
section: recommendationPods[index]?.indexSectionName,
|
|
22
22
|
podId: pod.id,
|
|
23
23
|
}));
|
|
24
|
-
recommendationsPodsData[pod.id] = {
|
|
24
|
+
recommendationsPodsData[pod.id] = {
|
|
25
|
+
displayName: pod.display_name,
|
|
26
|
+
podId: pod.id,
|
|
27
|
+
request,
|
|
28
|
+
};
|
|
25
29
|
}
|
|
26
30
|
});
|
|
27
31
|
try {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/* eslint-disable max-params */
|
|
2
2
|
import { createRef, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
-
import { getActiveSectionsWithData } from '../utils';
|
|
3
|
+
import { getActiveSectionsWithData, getFeatures } from '../utils';
|
|
4
4
|
import useDebouncedFetchSection from './useDebouncedFetchSections';
|
|
5
5
|
import useFetchRecommendationPod from './useFetchRecommendationPod';
|
|
6
6
|
import { isAutocompleteSection, isRecommendationsSection } from '../typeGuards';
|
|
7
7
|
export default function useSections(query, cioClient, sections, zeroStateSections, advancedParameters) {
|
|
8
|
-
const zeroStateActiveSections = !query.length && zeroStateSections;
|
|
8
|
+
const zeroStateActiveSections = !query.length && zeroStateSections?.length;
|
|
9
9
|
// Define All Sections
|
|
10
|
-
const activeSections = zeroStateActiveSections ? zeroStateSections : sections;
|
|
10
|
+
const [activeSections, setActiveSections] = useState(zeroStateActiveSections ? zeroStateSections : sections);
|
|
11
11
|
const sectionsRefs = useRef(activeSections.map(() => createRef()));
|
|
12
12
|
const [activeSectionsWithData, setActiveSectionsWithData] = useState([]);
|
|
13
13
|
const autocompleteSections = useMemo(() => activeSections?.filter((config) => isAutocompleteSection(config)), [activeSections]);
|
|
@@ -16,6 +16,21 @@ export default function useSections(query, cioClient, sections, zeroStateSection
|
|
|
16
16
|
const { sectionsData: autocompleteResults, request } = useDebouncedFetchSection(query, cioClient, autocompleteSections, advancedParameters);
|
|
17
17
|
// Fetch Recommendations Results
|
|
18
18
|
const { recommendationsResults, podsData } = useFetchRecommendationPod(cioClient, recommendationsSections);
|
|
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]);
|
|
19
34
|
// Merge Recommendation Pods Display Name from Dashboard
|
|
20
35
|
const activeSectionConfigs = useMemo(() => activeSections.map((config) => {
|
|
21
36
|
const mergedConfig = config;
|
package/lib/mjs/utils.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
|
|
2
2
|
import { isRecommendationsSection } from './typeGuards';
|
|
3
3
|
import version from './version';
|
|
4
|
-
export function
|
|
4
|
+
export function getFeatures(request) {
|
|
5
5
|
let featureDisplaySearchSuggestionImages = false;
|
|
6
6
|
let featureDisplaySearchSuggestionResultCounts = false;
|
|
7
|
+
let featureDisplayZeroStateRecommendations = true;
|
|
7
8
|
if (request?.features?.custom_autosuggest_ui === true) {
|
|
8
9
|
switch (request?.feature_variants?.custom_autosuggest_ui) {
|
|
9
10
|
case 'custom_autosuggest_ui_result_count':
|
|
@@ -16,6 +17,9 @@ export function getSearchSuggestionFeatures(request) {
|
|
|
16
17
|
featureDisplaySearchSuggestionImages = true;
|
|
17
18
|
featureDisplaySearchSuggestionResultCounts = true;
|
|
18
19
|
break;
|
|
20
|
+
case 'custom_autosuggest_ui_disable_recommendations_in_zero_state':
|
|
21
|
+
featureDisplayZeroStateRecommendations = false;
|
|
22
|
+
break;
|
|
19
23
|
default:
|
|
20
24
|
break;
|
|
21
25
|
}
|
|
@@ -23,6 +27,7 @@ export function getSearchSuggestionFeatures(request) {
|
|
|
23
27
|
return {
|
|
24
28
|
featureDisplaySearchSuggestionImages,
|
|
25
29
|
featureDisplaySearchSuggestionResultCounts,
|
|
30
|
+
featureDisplayZeroStateRecommendations,
|
|
26
31
|
};
|
|
27
32
|
}
|
|
28
33
|
export const getItemPosition = ({ item, items }) => {
|
package/lib/mjs/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '1.20.
|
|
1
|
+
export default '1.20.2';
|
|
@@ -7,6 +7,7 @@ export declare const CioAutocompleteContext: React.Context<{
|
|
|
7
7
|
featureToggles: {
|
|
8
8
|
featureDisplaySearchSuggestionImages: boolean;
|
|
9
9
|
featureDisplaySearchSuggestionResultCounts: boolean;
|
|
10
|
+
featureDisplayZeroStateRecommendations: boolean;
|
|
10
11
|
};
|
|
11
12
|
isOpen: boolean;
|
|
12
13
|
getMenuProps: () => any;
|
|
@@ -8,6 +8,7 @@ declare const useCioAutocomplete: (options: UseCioAutocompleteOptions) => {
|
|
|
8
8
|
featureToggles: {
|
|
9
9
|
featureDisplaySearchSuggestionImages: boolean;
|
|
10
10
|
featureDisplaySearchSuggestionResultCounts: boolean;
|
|
11
|
+
featureDisplayZeroStateRecommendations: boolean;
|
|
11
12
|
};
|
|
12
13
|
isOpen: boolean;
|
|
13
14
|
getMenuProps: () => any;
|
|
@@ -4,6 +4,6 @@ import { AdvancedParameters, UserDefinedSection, Section } from '../types';
|
|
|
4
4
|
export default function useSections(query: string, cioClient: Nullable<ConstructorIO>, sections: UserDefinedSection[], zeroStateSections: UserDefinedSection[] | undefined, advancedParameters?: AdvancedParameters): {
|
|
5
5
|
activeSections: UserDefinedSection[];
|
|
6
6
|
activeSectionsWithData: Section[];
|
|
7
|
-
zeroStateActiveSections:
|
|
7
|
+
zeroStateActiveSections: number | false | undefined;
|
|
8
8
|
request: Partial<import("@constructor-io/constructorio-client-javascript/lib/types").AutocompleteRequestType>;
|
|
9
9
|
};
|
package/lib/types/types.d.ts
CHANGED
package/lib/types/utils.d.ts
CHANGED
|
@@ -8,9 +8,10 @@ export type GetItemPosition = (args: {
|
|
|
8
8
|
index: number;
|
|
9
9
|
sectionId: string;
|
|
10
10
|
};
|
|
11
|
-
export declare function
|
|
11
|
+
export declare function getFeatures(request: Partial<AutocompleteRequestType>): {
|
|
12
12
|
featureDisplaySearchSuggestionImages: boolean;
|
|
13
13
|
featureDisplaySearchSuggestionResultCounts: boolean;
|
|
14
|
+
featureDisplayZeroStateRecommendations: boolean;
|
|
14
15
|
};
|
|
15
16
|
export declare const getItemPosition: GetItemPosition;
|
|
16
17
|
type CamelToStartCase = (camelCaseString: string) => string;
|
package/lib/types/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "1.20.
|
|
1
|
+
declare const _default: "1.20.2";
|
|
2
2
|
export default _default;
|
package/package.json
CHANGED