@ninetailed/experience.js-plugin-preview 4.2.0-beta.0 → 4.2.0-beta.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/index.cjs CHANGED
@@ -3,11 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var omit = require('lodash/omit');
6
- var experience_js = require('@ninetailed/experience.js');
7
- var jsxRuntime = require('react/jsx-runtime');
8
- var react = require('react');
9
- var uuid = require('uuid');
10
6
  var unionBy = require('lodash/unionBy');
7
+ var experience_js = require('@ninetailed/experience.js');
11
8
 
12
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
10
 
@@ -3028,79 +3025,6 @@ class WidgetContainer {
3028
3025
  }
3029
3026
  }
3030
3027
 
3031
- const PreviewRenderPlugin = ctx => {
3032
- const PreviewRenderPlugin = props => {
3033
- const [pluginApi, setPluginApi] = react.useState(ctx.pluginApi);
3034
- const {
3035
- children,
3036
- setComponentProps,
3037
- experiences,
3038
- baseline
3039
- } = props;
3040
- const {
3041
- current: id
3042
- } = react.useRef(uuid.v4());
3043
- react.useEffect(() => {
3044
- ctx.registerSetPluginApi(id, setPluginApi);
3045
- return () => {
3046
- ctx.unregisterSetPluginApi(id);
3047
- };
3048
- }, [id]);
3049
- react.useEffect(() => {
3050
- const experienceIds = Object.keys(pluginApi.experienceVariantIndexes);
3051
- const experience = experiences.find(experience => {
3052
- const hasActiveAudience = pluginApi.activeAudiences.some(activeAudienceId => {
3053
- var _a;
3054
- return ((_a = experience.audience) === null || _a === void 0 ? void 0 : _a.id) === activeAudienceId;
3055
- });
3056
- return hasActiveAudience && experienceIds.includes(experience.id);
3057
- });
3058
- if (!experience) {
3059
- setComponentProps(baseline);
3060
- return;
3061
- }
3062
- const baselineComponent = experience.components.find(component => component.baseline.id === baseline.id);
3063
- if (!baselineComponent) {
3064
- setComponentProps(baseline);
3065
- return;
3066
- }
3067
- const allVariants = [baseline, ...baselineComponent.variants];
3068
- const variantIndex = pluginApi.experienceVariantIndexes[experience.id];
3069
- if (allVariants.length <= variantIndex) {
3070
- setComponentProps(baseline);
3071
- return;
3072
- }
3073
- const variant = allVariants[variantIndex];
3074
- if (!variant) {
3075
- setComponentProps(baseline);
3076
- return;
3077
- }
3078
- setComponentProps(variant);
3079
- }, [experiences, baseline, setComponentProps, pluginApi.experienceVariantIndexes, pluginApi.activeAudiences]);
3080
- return jsxRuntime.jsx(jsxRuntime.Fragment, {
3081
- children: children
3082
- });
3083
- };
3084
- return PreviewRenderPlugin;
3085
- };
3086
-
3087
- class PreviewRenderPluginContext {
3088
- constructor(pluginApi) {
3089
- this.pluginApi = pluginApi;
3090
- this.setPluginApiCallbacks = new Map();
3091
- this.registerSetPluginApi = (id, setPluginApi) => {
3092
- this.setPluginApiCallbacks.set(id, setPluginApi);
3093
- };
3094
- this.unregisterSetPluginApi = id => {
3095
- this.setPluginApiCallbacks.delete(id);
3096
- };
3097
- this.setPluginApi = pluginApi => {
3098
- this.pluginApi = pluginApi;
3099
- this.setPluginApiCallbacks.forEach(callback => callback(pluginApi));
3100
- };
3101
- }
3102
- }
3103
-
3104
3028
  var _a;
3105
3029
  class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3106
3030
  constructor(options) {
@@ -3115,8 +3039,9 @@ class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3115
3039
  this.profile = null;
3116
3040
  this.container = null;
3117
3041
  this.bridge = null;
3118
- this.renderPluginContext = new PreviewRenderPluginContext(this.pluginApi);
3119
3042
  this.onChangeEmitter = new experience_js.OnChangeEmitter();
3043
+ this.clientId = null;
3044
+ this.environment = null;
3120
3045
  this.initialize = () => __awaiter(this, void 0, void 0, function* () {
3121
3046
  var _b;
3122
3047
  if (typeof window !== 'undefined') {
@@ -3140,9 +3065,6 @@ class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3140
3065
  });
3141
3066
  }
3142
3067
  });
3143
- this.getRenderWrapper = () => {
3144
- return this.renderPluginComponent;
3145
- };
3146
3068
  this.loaded = () => true;
3147
3069
  this[_a] = ({
3148
3070
  payload
@@ -3215,9 +3137,16 @@ class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3215
3137
  this.profile = profile;
3216
3138
  this.onChange();
3217
3139
  };
3140
+ this.setCredentials = ({
3141
+ clientId,
3142
+ environment
3143
+ }) => {
3144
+ this.clientId = clientId;
3145
+ this.environment = environment;
3146
+ };
3218
3147
  this.experiences = options.experiences || [];
3219
3148
  this.audienceDefinitions = options.audiences || [];
3220
- this.renderPluginComponent = PreviewRenderPlugin(this.renderPluginContext);
3149
+ this.onOpenExperienceEditor = options.onOpenExperienceEditor;
3221
3150
  }
3222
3151
  open() {
3223
3152
  var _b;
@@ -3333,6 +3262,12 @@ class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3333
3262
  window.ninetailed.reset();
3334
3263
  }
3335
3264
  }
3265
+ openExperienceEditor(experience) {
3266
+ if (this.onOpenExperienceEditor && typeof this.onOpenExperienceEditor === 'function') return this.onOpenExperienceEditor(experience);
3267
+ }
3268
+ openExperienceAnalytics(experience) {
3269
+ window.open(`https://app.ninetailed.io/${this.clientId}/${this.environment}/experiences/${experience.id}`, '_blank');
3270
+ }
3336
3271
  get pluginApi() {
3337
3272
  var _b;
3338
3273
  return {
@@ -3353,7 +3288,9 @@ class NinetailedPreviewPlugin extends experience_js.NinetailedPlugin {
3353
3288
  experienceVariantIndexes: Object.assign(Object.assign({}, this.experienceVariantIndexes), this.experienceVariantIndexOverwrites),
3354
3289
  experienceVariantIndexOverwrites: this.experienceVariantIndexOverwrites,
3355
3290
  reset: this.reset.bind(this),
3356
- experiences: this.experiences
3291
+ experiences: this.experiences,
3292
+ openExperienceEditor: this.onOpenExperienceEditor ? this.openExperienceEditor.bind(this) : undefined,
3293
+ openExperienceAnalytics: this.openExperienceAnalytics.bind(this)
3357
3294
  };
3358
3295
  }
3359
3296
  get windowApi() {
package/index.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import omit from 'lodash/omit';
2
- import { NinetailedPlugin, OnChangeEmitter, selectActiveExperiments, selectEligibleExperiences, isExperienceMatch, selectDistribution, PROFILE_CHANGE } from '@ninetailed/experience.js';
3
- import { jsx, Fragment } from 'react/jsx-runtime';
4
- import { useState, useRef, useEffect } from 'react';
5
- import { v4 } from 'uuid';
6
2
  import unionBy from 'lodash/unionBy';
3
+ import { NinetailedPlugin, OnChangeEmitter, selectActiveExperiments, selectEligibleExperiences, isExperienceMatch, selectDistribution, PROFILE_CHANGE } from '@ninetailed/experience.js';
7
4
 
8
5
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
9
6
 
@@ -3001,79 +2998,6 @@ class WidgetContainer {
3001
2998
  }
3002
2999
  }
3003
3000
 
3004
- const PreviewRenderPlugin = ctx => {
3005
- const PreviewRenderPlugin = props => {
3006
- const [pluginApi, setPluginApi] = useState(ctx.pluginApi);
3007
- const {
3008
- children,
3009
- setComponentProps,
3010
- experiences,
3011
- baseline
3012
- } = props;
3013
- const {
3014
- current: id
3015
- } = useRef(v4());
3016
- useEffect(() => {
3017
- ctx.registerSetPluginApi(id, setPluginApi);
3018
- return () => {
3019
- ctx.unregisterSetPluginApi(id);
3020
- };
3021
- }, [id]);
3022
- useEffect(() => {
3023
- const experienceIds = Object.keys(pluginApi.experienceVariantIndexes);
3024
- const experience = experiences.find(experience => {
3025
- const hasActiveAudience = pluginApi.activeAudiences.some(activeAudienceId => {
3026
- var _a;
3027
- return ((_a = experience.audience) === null || _a === void 0 ? void 0 : _a.id) === activeAudienceId;
3028
- });
3029
- return hasActiveAudience && experienceIds.includes(experience.id);
3030
- });
3031
- if (!experience) {
3032
- setComponentProps(baseline);
3033
- return;
3034
- }
3035
- const baselineComponent = experience.components.find(component => component.baseline.id === baseline.id);
3036
- if (!baselineComponent) {
3037
- setComponentProps(baseline);
3038
- return;
3039
- }
3040
- const allVariants = [baseline, ...baselineComponent.variants];
3041
- const variantIndex = pluginApi.experienceVariantIndexes[experience.id];
3042
- if (allVariants.length <= variantIndex) {
3043
- setComponentProps(baseline);
3044
- return;
3045
- }
3046
- const variant = allVariants[variantIndex];
3047
- if (!variant) {
3048
- setComponentProps(baseline);
3049
- return;
3050
- }
3051
- setComponentProps(variant);
3052
- }, [experiences, baseline, setComponentProps, pluginApi.experienceVariantIndexes, pluginApi.activeAudiences]);
3053
- return jsx(Fragment, {
3054
- children: children
3055
- });
3056
- };
3057
- return PreviewRenderPlugin;
3058
- };
3059
-
3060
- class PreviewRenderPluginContext {
3061
- constructor(pluginApi) {
3062
- this.pluginApi = pluginApi;
3063
- this.setPluginApiCallbacks = new Map();
3064
- this.registerSetPluginApi = (id, setPluginApi) => {
3065
- this.setPluginApiCallbacks.set(id, setPluginApi);
3066
- };
3067
- this.unregisterSetPluginApi = id => {
3068
- this.setPluginApiCallbacks.delete(id);
3069
- };
3070
- this.setPluginApi = pluginApi => {
3071
- this.pluginApi = pluginApi;
3072
- this.setPluginApiCallbacks.forEach(callback => callback(pluginApi));
3073
- };
3074
- }
3075
- }
3076
-
3077
3001
  var _a;
3078
3002
  class NinetailedPreviewPlugin extends NinetailedPlugin {
3079
3003
  constructor(options) {
@@ -3088,8 +3012,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
3088
3012
  this.profile = null;
3089
3013
  this.container = null;
3090
3014
  this.bridge = null;
3091
- this.renderPluginContext = new PreviewRenderPluginContext(this.pluginApi);
3092
3015
  this.onChangeEmitter = new OnChangeEmitter();
3016
+ this.clientId = null;
3017
+ this.environment = null;
3093
3018
  this.initialize = () => __awaiter(this, void 0, void 0, function* () {
3094
3019
  var _b;
3095
3020
  if (typeof window !== 'undefined') {
@@ -3113,9 +3038,6 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
3113
3038
  });
3114
3039
  }
3115
3040
  });
3116
- this.getRenderWrapper = () => {
3117
- return this.renderPluginComponent;
3118
- };
3119
3041
  this.loaded = () => true;
3120
3042
  this[_a] = ({
3121
3043
  payload
@@ -3188,9 +3110,16 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
3188
3110
  this.profile = profile;
3189
3111
  this.onChange();
3190
3112
  };
3113
+ this.setCredentials = ({
3114
+ clientId,
3115
+ environment
3116
+ }) => {
3117
+ this.clientId = clientId;
3118
+ this.environment = environment;
3119
+ };
3191
3120
  this.experiences = options.experiences || [];
3192
3121
  this.audienceDefinitions = options.audiences || [];
3193
- this.renderPluginComponent = PreviewRenderPlugin(this.renderPluginContext);
3122
+ this.onOpenExperienceEditor = options.onOpenExperienceEditor;
3194
3123
  }
3195
3124
  open() {
3196
3125
  var _b;
@@ -3306,6 +3235,12 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
3306
3235
  window.ninetailed.reset();
3307
3236
  }
3308
3237
  }
3238
+ openExperienceEditor(experience) {
3239
+ if (this.onOpenExperienceEditor && typeof this.onOpenExperienceEditor === 'function') return this.onOpenExperienceEditor(experience);
3240
+ }
3241
+ openExperienceAnalytics(experience) {
3242
+ window.open(`https://app.ninetailed.io/${this.clientId}/${this.environment}/experiences/${experience.id}`, '_blank');
3243
+ }
3309
3244
  get pluginApi() {
3310
3245
  var _b;
3311
3246
  return {
@@ -3326,7 +3261,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
3326
3261
  experienceVariantIndexes: Object.assign(Object.assign({}, this.experienceVariantIndexes), this.experienceVariantIndexOverwrites),
3327
3262
  experienceVariantIndexOverwrites: this.experienceVariantIndexOverwrites,
3328
3263
  reset: this.reset.bind(this),
3329
- experiences: this.experiences
3264
+ experiences: this.experiences,
3265
+ openExperienceEditor: this.onOpenExperienceEditor ? this.openExperienceEditor.bind(this) : undefined,
3266
+ openExperienceAnalytics: this.openExperienceAnalytics.bind(this)
3330
3267
  };
3331
3268
  }
3332
3269
  get windowApi() {
@@ -1,16 +1,15 @@
1
1
  import { Reference } from '@ninetailed/experience.js-shared';
2
- import { ExperienceConfiguration, PROFILE_CHANGE, NinetailedPlugin, HasExperienceSelectionMiddleware, ExperienceSelectionMiddleware } from '@ninetailed/experience.js';
2
+ import { ExperienceConfiguration, PROFILE_CHANGE, HasExperienceSelectionMiddleware, ExperienceSelectionMiddleware, NinetailedPlugin } from '@ninetailed/experience.js';
3
3
  import { Audience } from '@ninetailed/experience.js-utils';
4
- import { RenderPluginWrapperComponent } from '@ninetailed/experience.js-react';
5
4
  export declare const NINETAILED_PREVIEW_EVENTS: {
6
5
  previewAudiences: string;
7
6
  previewTraits: string;
8
7
  };
9
8
  type NinetailedPreviewPluginOptions = {
10
9
  url?: string;
11
- environment?: string;
12
10
  experiences: ExperienceConfiguration[];
13
11
  audiences: Audience[];
12
+ onOpenExperienceEditor?: (experience: ExperienceConfiguration) => void;
14
13
  ui?: {
15
14
  opener?: {
16
15
  hide: boolean;
@@ -28,12 +27,12 @@ export declare class NinetailedPreviewPlugin extends NinetailedPlugin implements
28
27
  private profile;
29
28
  private container;
30
29
  private bridge;
31
- private renderPluginContext;
32
- private renderPluginComponent;
33
30
  private onChangeEmitter;
31
+ private readonly onOpenExperienceEditor;
32
+ private clientId;
33
+ private environment;
34
34
  constructor(options: NinetailedPreviewPluginOptions);
35
35
  initialize: () => Promise<void>;
36
- getRenderWrapper: () => RenderPluginWrapperComponent;
37
36
  loaded: () => boolean;
38
37
  [PROFILE_CHANGE]: ({ payload }: {
39
38
  payload: any;
@@ -51,6 +50,8 @@ export declare class NinetailedPreviewPlugin extends NinetailedPlugin implements
51
50
  resetExperience(experienceId: string): void;
52
51
  reset(): void;
53
52
  getExperienceSelectionMiddleware: ExperienceSelectionMiddleware<Reference>;
53
+ openExperienceEditor(experience: ExperienceConfiguration): void;
54
+ openExperienceAnalytics(experience: ExperienceConfiguration): void;
54
55
  private get pluginApi();
55
56
  private get windowApi();
56
57
  private isKnownAudience;
@@ -59,5 +60,6 @@ export declare class NinetailedPreviewPlugin extends NinetailedPlugin implements
59
60
  private get experienceVariantIndexes();
60
61
  private onChange;
61
62
  private onProfileChange;
63
+ private setCredentials;
62
64
  }
63
65
  export {};
package/package.json CHANGED
@@ -1,20 +1,16 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-plugin-preview",
3
- "version": "4.2.0-beta.0",
3
+ "version": "4.2.0-beta.2",
4
4
  "module": "./index.js",
5
5
  "main": "./index.cjs",
6
6
  "type": "module",
7
7
  "types": "./index.d.ts",
8
8
  "dependencies": {
9
9
  "lodash": "4.17.21",
10
- "@ninetailed/experience.js-shared": "4.2.0-beta.0",
11
- "@ninetailed/experience.js": "4.2.0-beta.0",
12
- "@ninetailed/experience.js-utils": "4.1.0-beta.3",
13
- "@ninetailed/experience.js-react": "4.2.0-beta.0",
14
- "@ninetailed/experience.js-preview-bridge": "4.2.0-beta.0",
15
- "react": "18.2.0",
16
- "uuid": "9.0.0",
17
- "ts-toolbelt": "9.6.0"
10
+ "@ninetailed/experience.js-shared": "4.2.0-beta.2",
11
+ "@ninetailed/experience.js": "4.2.0-beta.2",
12
+ "@ninetailed/experience.js-utils": "4.2.0-beta.1",
13
+ "@ninetailed/experience.js-preview-bridge": "4.2.0-beta.2"
18
14
  },
19
15
  "peerDependencies": {}
20
16
  }
@@ -1,5 +0,0 @@
1
- import { RenderPluginWrapperComponent } from '@ninetailed/experience.js-react';
2
- import { PreviewRenderPluginContext } from './PreviewRenderPluginContext';
3
- type PreviewRenderPlugin = (ctx: PreviewRenderPluginContext) => RenderPluginWrapperComponent;
4
- export declare const PreviewRenderPlugin: PreviewRenderPlugin;
5
- export {};
@@ -1,9 +0,0 @@
1
- import { PreviewPluginApi } from '@ninetailed/experience.js-preview-bridge';
2
- export declare class PreviewRenderPluginContext {
3
- pluginApi: PreviewPluginApi;
4
- private setPluginApiCallbacks;
5
- constructor(pluginApi: PreviewPluginApi);
6
- registerSetPluginApi: (id: string, setPluginApi: (luginApi: PreviewPluginApi) => void) => void;
7
- unregisterSetPluginApi: (id: string) => void;
8
- setPluginApi: (pluginApi: PreviewPluginApi) => void;
9
- }