@ninetailed/experience.js-react 2.0.0-beta.22 → 2.0.0-beta.25

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/index.esm.js CHANGED
@@ -3,6 +3,7 @@ import { Ninetailed, selectVariant, selectHasExperienceVariants, selectActiveExp
3
3
  import { useInView } from 'react-intersection-observer';
4
4
  import { isBrowser } from '@ninetailed/experience.js-shared';
5
5
  import get$1 from 'lodash/get';
6
+ import has$1 from 'lodash/has';
6
7
 
7
8
  const NinetailedContext = /*#__PURE__*/createContext(undefined);
8
9
 
@@ -2333,6 +2334,31 @@ const MergeTag = ({
2333
2334
  return /*#__PURE__*/React.createElement(React.Fragment, null, value);
2334
2335
  };
2335
2336
 
2337
+ const TrackExperience = ({
2338
+ children,
2339
+ experience,
2340
+ variant
2341
+ }) => {
2342
+ const ninetailed = useNinetailed();
2343
+ const {
2344
+ ref,
2345
+ inView
2346
+ } = useInView({
2347
+ triggerOnce: true
2348
+ });
2349
+ useEffect(() => {
2350
+ if (isBrowser() && inView) {
2351
+ ninetailed.trackExperience({
2352
+ experience,
2353
+ variant
2354
+ });
2355
+ }
2356
+ }, [inView]);
2357
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
2358
+ ref: ref
2359
+ }), children);
2360
+ };
2361
+
2336
2362
  var global$2 = global$w;
2337
2363
  var aCallable = aCallable$2;
2338
2364
  var toObject = toObject$5;
@@ -2569,7 +2595,10 @@ const Experience = _a => {
2569
2595
 
2570
2596
  if (status === 'loading') {
2571
2597
  if (LoadingComponent) {
2572
- return /*#__PURE__*/React.createElement(LoadingComponent, null);
2598
+ return /*#__PURE__*/React.createElement(LoadingComponent, Object.assign({}, baseline, {
2599
+ experiences: experiences,
2600
+ component: Component
2601
+ }));
2573
2602
  }
2574
2603
 
2575
2604
  return /*#__PURE__*/React.createElement("div", {
@@ -2588,19 +2617,7 @@ const Experience = _a => {
2588
2617
  }
2589
2618
 
2590
2619
  if (!experience || !variant) {
2591
- return /*#__PURE__*/React.createElement(TrackHasSeenComponent, {
2592
- variant: {
2593
- id: baseline.id,
2594
- audience: {
2595
- id: 'baseline'
2596
- },
2597
- hidden: false
2598
- },
2599
- audience: {
2600
- id: 'baseline'
2601
- },
2602
- isPersonalized: false
2603
- }, /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline, {
2620
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline, {
2604
2621
  key: baseline.id,
2605
2622
  ninetailed: {
2606
2623
  isPersonalized: false,
@@ -2608,34 +2625,71 @@ const Experience = _a => {
2608
2625
  id: 'baseline'
2609
2626
  }
2610
2627
  }
2611
- })));
2628
+ }));
2612
2629
  }
2613
2630
 
2614
- return (
2615
- /*#__PURE__*/
2616
- // TODO this needs to be resolved when we move away from the old standard
2617
- React.createElement(TrackHasSeenComponent, {
2618
- variant: {
2619
- id: variant.id,
2620
- audience: {
2621
- id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2622
- },
2623
- hidden: variant.hidden || false
2624
- },
2631
+ return /*#__PURE__*/React.createElement(TrackExperience, {
2632
+ experience: experience,
2633
+ variant: {
2634
+ id: variant.id,
2635
+ hidden: variant.hidden || false
2636
+ }
2637
+ }, (variant === null || variant === void 0 ? void 0 : variant.hidden) ? null : /*#__PURE__*/React.createElement(Component, Object.assign({}, variant, {
2638
+ key: `${experience.id}-${variant.id}`,
2639
+ ninetailed: {
2640
+ isPersonalized,
2625
2641
  audience: {
2626
2642
  id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2627
- },
2628
- isPersonalized: isPersonalized
2629
- }, (variant === null || variant === void 0 ? void 0 : variant.hidden) ? null : /*#__PURE__*/React.createElement(Component, Object.assign({}, variant, {
2630
- key: `${experience.id}-${variant.id}`,
2631
- ninetailed: {
2632
- isPersonalized,
2633
- audience: {
2634
- id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2635
- }
2636
2643
  }
2637
- })))
2638
- );
2644
+ }
2645
+ })));
2646
+ };
2647
+
2648
+ const ESRContext = /*#__PURE__*/React.createContext(undefined);
2649
+ const useESR = () => {
2650
+ const context = React.useContext(ESRContext);
2651
+
2652
+ if (context === undefined) {
2653
+ throw new Error('The component using the the context must be a descendant of the ESRProvider');
2654
+ }
2655
+
2656
+ return {
2657
+ experienceVariantsMap: context.experienceVariantsMap
2658
+ };
2659
+ };
2660
+ const ESRLoadingComponent = _a => {
2661
+ var {
2662
+ experiences,
2663
+ component: Component
2664
+ } = _a,
2665
+ baseline = __rest(_a, ["experiences", "component"]);
2666
+
2667
+ const {
2668
+ experienceVariantsMap
2669
+ } = useESR();
2670
+ const experience = experiences.find(experience => has$1(experienceVariantsMap, experience.id));
2671
+
2672
+ if (!experience) {
2673
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline));
2674
+ }
2675
+
2676
+ const component = experience.components.find(component => component.baseline.id === baseline.id);
2677
+
2678
+ if (!component) {
2679
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline));
2680
+ }
2681
+
2682
+ if (experienceVariantsMap[experience.id] === 0) {
2683
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline));
2684
+ }
2685
+
2686
+ const variant = component.variants[experienceVariantsMap[experience.id] - 1];
2687
+
2688
+ if (!variant) {
2689
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, baseline));
2690
+ }
2691
+
2692
+ return /*#__PURE__*/React.createElement(Component, Object.assign({}, variant));
2639
2693
  };
2640
2694
 
2641
- export { Experience, MergeTag, NinetailedProvider, Personalize, TrackHasSeenComponent, useNinetailed, usePersonalize, useProfile };
2695
+ export { ESRLoadingComponent, Experience, MergeTag, NinetailedProvider, Personalize, TrackHasSeenComponent, useNinetailed, usePersonalize, useProfile };
package/index.umd.js CHANGED
@@ -1,13 +1,14 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@ninetailed/experience.js'), require('react-intersection-observer'), require('@ninetailed/experience.js-shared'), require('lodash/get')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'react', '@ninetailed/experience.js', 'react-intersection-observer', '@ninetailed/experience.js-shared', 'lodash/get'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.React = {}, global.React, global.experience_js, global.reactIntersectionObserver, global.experience_jsShared, global.get$1));
5
- })(this, (function (exports, React, experience_js, reactIntersectionObserver, experience_jsShared, get$1) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@ninetailed/experience.js'), require('react-intersection-observer'), require('@ninetailed/experience.js-shared'), require('lodash/get'), require('lodash/has')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'react', '@ninetailed/experience.js', 'react-intersection-observer', '@ninetailed/experience.js-shared', 'lodash/get', 'lodash/has'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.React = {}, global.React, global.experience_js, global.reactIntersectionObserver, global.experience_jsShared, global.get$1, global.has$1));
5
+ })(this, (function (exports, React, experience_js, reactIntersectionObserver, experience_jsShared, get$1, has$1) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
9
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
10
  var get__default = /*#__PURE__*/_interopDefaultLegacy(get$1);
11
+ var has__default = /*#__PURE__*/_interopDefaultLegacy(has$1);
11
12
 
12
13
  var NinetailedContext = /*#__PURE__*/React.createContext(undefined);
13
14
 
@@ -1883,6 +1884,31 @@
1883
1884
  return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, value);
1884
1885
  };
1885
1886
 
1887
+ var TrackExperience = function TrackExperience(_a) {
1888
+ var children = _a.children,
1889
+ experience = _a.experience,
1890
+ variant = _a.variant;
1891
+ var ninetailed = useNinetailed();
1892
+
1893
+ var _b = reactIntersectionObserver.useInView({
1894
+ triggerOnce: true
1895
+ }),
1896
+ ref = _b.ref,
1897
+ inView = _b.inView;
1898
+
1899
+ React.useEffect(function () {
1900
+ if (experience_jsShared.isBrowser() && inView) {
1901
+ ninetailed.trackExperience({
1902
+ experience: experience,
1903
+ variant: variant
1904
+ });
1905
+ }
1906
+ }, [inView]);
1907
+ return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("div", {
1908
+ ref: ref
1909
+ }), children);
1910
+ };
1911
+
1886
1912
  var global$2 = global$t;
1887
1913
  var aCallable = aCallable$2;
1888
1914
  var toObject = toObject$3;
@@ -2114,7 +2140,10 @@
2114
2140
 
2115
2141
  if (status === 'loading') {
2116
2142
  if (LoadingComponent) {
2117
- return /*#__PURE__*/React__default["default"].createElement(LoadingComponent, null);
2143
+ return /*#__PURE__*/React__default["default"].createElement(LoadingComponent, __assign({}, baseline, {
2144
+ experiences: experiences,
2145
+ component: Component
2146
+ }));
2118
2147
  }
2119
2148
 
2120
2149
  return /*#__PURE__*/React__default["default"].createElement("div", {
@@ -2133,19 +2162,7 @@
2133
2162
  }
2134
2163
 
2135
2164
  if (!experience || !variant) {
2136
- return /*#__PURE__*/React__default["default"].createElement(TrackHasSeenComponent, {
2137
- variant: {
2138
- id: baseline.id,
2139
- audience: {
2140
- id: 'baseline'
2141
- },
2142
- hidden: false
2143
- },
2144
- audience: {
2145
- id: 'baseline'
2146
- },
2147
- isPersonalized: false
2148
- }, /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline, {
2165
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline, {
2149
2166
  key: baseline.id,
2150
2167
  ninetailed: {
2151
2168
  isPersonalized: false,
@@ -2153,36 +2170,74 @@
2153
2170
  id: 'baseline'
2154
2171
  }
2155
2172
  }
2156
- })));
2173
+ }));
2157
2174
  }
2158
2175
 
2159
- return (
2160
- /*#__PURE__*/
2161
- // TODO this needs to be resolved when we move away from the old standard
2162
- React__default["default"].createElement(TrackHasSeenComponent, {
2163
- variant: {
2164
- id: variant.id,
2165
- audience: {
2166
- id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2167
- },
2168
- hidden: variant.hidden || false
2169
- },
2176
+ return /*#__PURE__*/React__default["default"].createElement(TrackExperience, {
2177
+ experience: experience,
2178
+ variant: {
2179
+ id: variant.id,
2180
+ hidden: variant.hidden || false
2181
+ }
2182
+ }, (variant === null || variant === void 0 ? void 0 : variant.hidden) ? null : /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, variant, {
2183
+ key: "".concat(experience.id, "-").concat(variant.id),
2184
+ ninetailed: {
2185
+ isPersonalized: isPersonalized,
2170
2186
  audience: {
2171
2187
  id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2172
- },
2173
- isPersonalized: isPersonalized
2174
- }, (variant === null || variant === void 0 ? void 0 : variant.hidden) ? null : /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, variant, {
2175
- key: "".concat(experience.id, "-").concat(variant.id),
2176
- ninetailed: {
2177
- isPersonalized: isPersonalized,
2178
- audience: {
2179
- id: (audience === null || audience === void 0 ? void 0 : audience.id) || 'baseline'
2180
- }
2181
2188
  }
2182
- })))
2183
- );
2189
+ }
2190
+ })));
2191
+ };
2192
+
2193
+ var ESRContext = /*#__PURE__*/React__default["default"].createContext(undefined);
2194
+ var useESR = function useESR() {
2195
+ var context = React__default["default"].useContext(ESRContext);
2196
+
2197
+ if (context === undefined) {
2198
+ throw new Error('The component using the the context must be a descendant of the ESRProvider');
2199
+ }
2200
+
2201
+ return {
2202
+ experienceVariantsMap: context.experienceVariantsMap
2203
+ };
2204
+ };
2205
+ var ESRLoadingComponent = function ESRLoadingComponent(_a) {
2206
+ var experiences = _a.experiences,
2207
+ Component = _a.component,
2208
+ baseline = __rest(_a, ["experiences", "component"]);
2209
+
2210
+ var experienceVariantsMap = useESR().experienceVariantsMap;
2211
+ var experience = experiences.find(function (experience) {
2212
+ return has__default["default"](experienceVariantsMap, experience.id);
2213
+ });
2214
+
2215
+ if (!experience) {
2216
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline));
2217
+ }
2218
+
2219
+ var component = experience.components.find(function (component) {
2220
+ return component.baseline.id === baseline.id;
2221
+ });
2222
+
2223
+ if (!component) {
2224
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline));
2225
+ }
2226
+
2227
+ if (experienceVariantsMap[experience.id] === 0) {
2228
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline));
2229
+ }
2230
+
2231
+ var variant = component.variants[experienceVariantsMap[experience.id] - 1];
2232
+
2233
+ if (!variant) {
2234
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, baseline));
2235
+ }
2236
+
2237
+ return /*#__PURE__*/React__default["default"].createElement(Component, __assign({}, variant));
2184
2238
  };
2185
2239
 
2240
+ exports.ESRLoadingComponent = ESRLoadingComponent;
2186
2241
  exports.Experience = Experience;
2187
2242
  exports.MergeTag = MergeTag;
2188
2243
  exports.NinetailedProvider = NinetailedProvider;
@@ -0,0 +1,15 @@
1
+ import React from 'react';
2
+ import { ExperienceLoadingComponent } from './Experience';
3
+ declare type ESRContextValue = {
4
+ experienceVariantsMap: Record<string, number>;
5
+ };
6
+ export declare const ESRContext: React.Context<ESRContextValue | undefined>;
7
+ declare type ESRProviderProps = {
8
+ experienceVariantsMap: Record<string, number>;
9
+ };
10
+ export declare const ESRProvider: React.FC<ESRProviderProps>;
11
+ export declare const useESR: () => {
12
+ experienceVariantsMap: Record<string, number>;
13
+ };
14
+ export declare const ESRLoadingComponent: ExperienceLoadingComponent;
15
+ export {};
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { Baseline, ExperienceConfiguration } from '@ninetailed/experience.js';
3
- declare type ExperienceComponent<P extends Baseline> = React.ComponentType<Omit<P, 'id'> & {
3
+ export declare type ExperienceComponent<P extends Baseline> = React.ComponentType<Omit<P, 'id'> & {
4
4
  ninetailed?: {
5
5
  isPersonalized: boolean;
6
6
  audience: {
@@ -8,12 +8,16 @@ declare type ExperienceComponent<P extends Baseline> = React.ComponentType<Omit<
8
8
  };
9
9
  };
10
10
  }>;
11
- declare type ExperienceProps<P extends Baseline> = P & {
11
+ export declare type ExperienceBaseProps<P extends Baseline = Baseline> = P & {
12
12
  experiences: ExperienceConfiguration[];
13
13
  component: ExperienceComponent<P> | React.ComponentType<P>;
14
- loadingComponent?: React.ComponentType;
14
+ };
15
+ export declare type ExperienceLoadingComponent<P extends Baseline = Baseline> = React.ComponentType<ExperienceBaseProps<P>>;
16
+ export declare type ExperienceProps<P extends Baseline> = ExperienceBaseProps<P> & {
17
+ experiences: ExperienceConfiguration[];
18
+ component: ExperienceComponent<P> | React.ComponentType<P>;
19
+ loadingComponent?: ExperienceLoadingComponent<P>;
15
20
  };
16
21
  export declare const Experience: <P extends {
17
22
  id: string;
18
23
  }>({ experiences, component: Component, loadingComponent: LoadingComponent, ...baseline }: ExperienceProps<P>) => JSX.Element;
19
- export {};
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ import { TrackExperiencePayload } from '@ninetailed/experience.js';
3
+ export declare const TrackExperience: React.FC<React.PropsWithChildren<TrackExperiencePayload>>;
@@ -1 +1,3 @@
1
1
  export { Experience } from './Experience';
2
+ export type { ExperienceProps, ExperienceBaseProps, ExperienceComponent, ExperienceLoadingComponent, } from './Experience';
3
+ export { ESRLoadingComponent } from './ESRLoadingComponent';
package/lib/index.d.ts CHANGED
@@ -9,4 +9,5 @@ export { Personalize } from './Personalize';
9
9
  export type { PersonalizedComponent } from './Personalize';
10
10
  export { MergeTag } from './MergeTag';
11
11
  export { TrackHasSeenComponent } from './TrackHasSeenComponent';
12
- export { Experience } from './Experience';
12
+ export { Experience, ESRLoadingComponent } from './Experience';
13
+ export type { ExperienceProps, ExperienceBaseProps, ExperienceComponent, ExperienceLoadingComponent, } from './Experience';
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-react",
3
- "version": "2.0.0-beta.22",
3
+ "version": "2.0.0-beta.25",
4
4
  "peerDependencies": {
5
5
  "react": ">=16.8.0"
6
6
  },
7
7
  "dependencies": {
8
8
  "@analytics/google-analytics": "0.5.3",
9
9
  "react-visibility-sensor": "5.1.1",
10
- "@ninetailed/experience.js": "2.0.0-beta.22",
10
+ "lodash": "^4.17.21",
11
+ "@ninetailed/experience.js": "2.0.0-beta.25",
11
12
  "analytics": "^0.8.0",
12
- "@ninetailed/experience.js-shared": "2.0.0-beta.22",
13
+ "@ninetailed/experience.js-shared": "2.0.0-beta.25",
13
14
  "uuid": "^8.3.2",
14
15
  "ts-toolbelt": "^9.6.0",
15
16
  "locale-enum": "^1.1.1",
16
17
  "i18n-iso-countries": "^7.3.0",
17
- "lodash": "^4.17.21",
18
18
  "loglevel": "^1.8.0",
19
19
  "murmurhash-js": "^1.0.0",
20
20
  "react-intersection-observer": "^8.33.1"