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

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.
Files changed (35) hide show
  1. package/dist/constructorio-ui-autocomplete-bundled.js +11 -11
  2. package/lib/cjs/hooks/useCioAutocomplete.js +9 -34
  3. package/lib/cjs/hooks/useDownShift.js +44 -45
  4. package/lib/cjs/hooks/useNormalizedProps.js +53 -0
  5. package/lib/cjs/hooks/useSections/index.js +38 -0
  6. package/lib/cjs/hooks/useSections/useActiveSections.js +25 -0
  7. package/lib/cjs/hooks/useSections/useActiveSectionsWithData.js +16 -0
  8. package/lib/cjs/hooks/useSections/useRemoveSections.js +24 -0
  9. package/lib/cjs/hooks/useSections/useSectionsResults.js +28 -0
  10. package/lib/cjs/utils.js +8 -4
  11. package/lib/cjs/version.js +1 -1
  12. package/lib/mjs/hooks/useCioAutocomplete.js +13 -35
  13. package/lib/mjs/hooks/useDownShift.js +2 -2
  14. package/lib/mjs/hooks/useNormalizedProps.js +50 -0
  15. package/lib/mjs/hooks/useSections/index.js +34 -0
  16. package/lib/mjs/hooks/useSections/useActiveSections.js +22 -0
  17. package/lib/mjs/hooks/useSections/useActiveSectionsWithData.js +13 -0
  18. package/lib/mjs/hooks/useSections/useRemoveSections.js +20 -0
  19. package/lib/mjs/hooks/useSections/useSectionsResults.js +24 -0
  20. package/lib/mjs/utils.js +8 -4
  21. package/lib/mjs/version.js +1 -1
  22. package/lib/types/hooks/useCioAutocomplete.d.ts +1 -2
  23. package/lib/types/hooks/useDownShift.d.ts +3 -5
  24. package/lib/types/hooks/useNormalizedProps.d.ts +9 -0
  25. package/lib/types/hooks/{useSections.d.ts → useSections/index.d.ts} +3 -3
  26. package/lib/types/hooks/useSections/useActiveSections.d.ts +6 -0
  27. package/lib/types/hooks/useSections/useActiveSectionsWithData.d.ts +3 -0
  28. package/lib/types/hooks/useSections/useRemoveSections.d.ts +2 -0
  29. package/lib/types/hooks/useSections/useSectionsResults.d.ts +15 -0
  30. package/lib/types/types.d.ts +6 -3
  31. package/lib/types/utils.d.ts +1 -1
  32. package/lib/types/version.d.ts +1 -1
  33. package/package.json +1 -1
  34. package/lib/cjs/hooks/useSections.js +0 -63
  35. package/lib/mjs/hooks/useSections.js +0 -58
@@ -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.20';
@@ -1,6 +1,5 @@
1
- import { CioAutocompleteProps, Section, UserDefinedSection, HTMLPropsWithCioDataAttributes, Item } from '../types';
1
+ import { Section, UserDefinedSection, HTMLPropsWithCioDataAttributes, Item, UseCioAutocompleteOptions } from '../types';
2
2
  export declare const defaultSections: UserDefinedSection[];
3
- export type UseCioAutocompleteOptions = Omit<CioAutocompleteProps, 'children'>;
4
3
  declare const useCioAutocomplete: (options: UseCioAutocompleteOptions) => {
5
4
  query: string;
6
5
  sections: Section[];
@@ -1,16 +1,14 @@
1
1
  /// <reference types="react" />
2
- import { UseComboboxReturnValue, UseComboboxStateChange } from 'downshift';
2
+ import { UseComboboxProps, UseComboboxReturnValue } from 'downshift';
3
3
  import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
4
4
  import { Nullable } from '@constructor-io/constructorio-client-javascript/lib/types';
5
5
  import { Item, OnSubmit } from '../types';
6
- type UseDownShiftOptions = {
6
+ interface UseDownShiftOptions extends UseComboboxProps<Item> {
7
7
  setQuery: React.Dispatch<React.SetStateAction<string>>;
8
- items: Item[];
9
8
  onSubmit: OnSubmit;
10
9
  previousQuery?: string;
11
10
  cioClient: Nullable<ConstructorIOClient>;
12
- onIsOpenChange?: (changes: UseComboboxStateChange<Item>) => void;
13
- };
11
+ }
14
12
  export type DownShift = UseComboboxReturnValue<Item>;
15
13
  type UseDownShift = (options: UseDownShiftOptions) => DownShift;
16
14
  declare const useDownShift: UseDownShift;
@@ -0,0 +1,9 @@
1
+ import { UseCioAutocompleteOptions, UserDefinedSection } from '../types';
2
+ export declare const defaultSections: UserDefinedSection[];
3
+ declare const useNormalizedProps: (options: UseCioAutocompleteOptions) => {
4
+ sections: UserDefinedSection[];
5
+ zeroStateSections: UserDefinedSection[] | undefined;
6
+ cioJsClientOptions: import("@constructor-io/constructorio-client-javascript").ConstructorClientOptions | undefined;
7
+ advancedParameters: import("../types").AdvancedParameters | undefined;
8
+ };
9
+ export default useNormalizedProps;
@@ -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
+ };
@@ -1,4 +1,4 @@
1
- import { GetItemPropsOptions, UseComboboxStateChange } from 'downshift';
1
+ import { GetItemPropsOptions, UseComboboxProps } from 'downshift';
2
2
  import { ReactNode } from 'react';
3
3
  import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
4
4
  import { IAutocompleteParameters, SearchSuggestion as SearchSuggestionFromClient, Product as ProductFromClient, Item as ItemBase, AutocompleteRequestType, ConstructorClientOptions, RecommendationsParameters } from '@constructor-io/constructorio-client-javascript/lib/types';
@@ -19,13 +19,16 @@ export interface AdvancedParametersBase {
19
19
  fetchZeroStateOnFocus?: boolean;
20
20
  }
21
21
  export type AdvancedParameters = AdvancedParametersBase & Omit<IAutocompleteParameters, 'resultsPerSection'>;
22
- export type CioAutocompleteProps = CioClientConfig & {
22
+ type OptionalItemsComboboxProps<Item> = Partial<UseComboboxProps<Item>> & {
23
+ items?: Item[];
24
+ };
25
+ export type UseCioAutocompleteOptions = Omit<CioAutocompleteProps, 'children'>;
26
+ export type CioAutocompleteProps = CioClientConfig & OptionalItemsComboboxProps<Item> & {
23
27
  openOnFocus?: boolean;
24
28
  getSearchResultsUrl?: (item: SearchSuggestion) => string;
25
29
  onSubmit: OnSubmit;
26
30
  onFocus?: () => void;
27
31
  onChange?: (input: string) => void;
28
- onIsOpenChange?: (changes: UseComboboxStateChange<Item>) => void;
29
32
  placeholder?: string;
30
33
  children?: ReactNode;
31
34
  sections?: UserDefinedSection[];
@@ -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.20";
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.20",
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
- }