@streamlayer/sdk-web-core 1.4.3 → 1.6.0

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/lib/ui/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
1
+ import { createMapStore, StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
2
2
  import { QuestionType } from '@streamlayer/sdk-web-types';
3
3
  export type UIData = {
4
4
  stage: 'activate' | 'deactivate';
@@ -8,22 +8,35 @@ export type UIData = {
8
8
  type: 'advertisement' | 'insight' | 'poll' | 'trivia' | 'prediction' | 'tweet' | 'question';
9
9
  };
10
10
  export type OnContentActivateCallback = (renderData: UIData) => void;
11
+ export type UIState = {
12
+ promotionSidebar?: boolean;
13
+ promotionOverlay?: boolean;
14
+ promotionBanner?: boolean;
15
+ promotionNotification?: boolean;
16
+ app?: boolean;
17
+ appNotification?: boolean;
18
+ };
11
19
  export interface UIContext {
12
- state: Set<string>;
20
+ disableApp: () => void;
21
+ enableApp: () => void;
22
+ disableAppNotification: () => void;
23
+ enableAppNotification: () => void;
13
24
  onContentActivate?: OnContentActivateCallback;
14
25
  }
15
26
  declare module '@streamlayer/sdk-web-interfaces' {
16
27
  interface StreamLayerContext {
17
28
  ui: UIContext;
18
- }
19
- interface StreamLayerSDK {
20
- onAdvertisementActivate: (params: Omit<UIData, 'type'>) => void;
21
29
  onQuestionActivate: (params: Omit<UIData, 'type'> & {
22
30
  type?: string;
23
31
  questionType?: QuestionType;
24
32
  }) => void;
25
33
  }
34
+ interface StreamLayerSDK {
35
+ uiState: ReturnType<typeof createMapStore<UIState>>;
36
+ }
26
37
  }
27
38
  export declare const ui: (instance: StreamLayerContext, opts: {
28
39
  onContentActivate?: OnContentActivateCallback;
40
+ withAdNotification?: boolean;
41
+ withAd?: boolean;
29
42
  }, done: () => void) => void;
package/lib/ui/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createMapStore, eventBus, } from '@streamlayer/sdk-web-interfaces';
1
2
  import { QuestionType } from '@streamlayer/sdk-web-types';
2
3
  const questionTypes = {
3
4
  [QuestionType.FACTOID]: 'insight',
@@ -7,25 +8,123 @@ const questionTypes = {
7
8
  [QuestionType.TWEET]: 'tweet',
8
9
  };
9
10
  export const ui = (instance, opts, done) => {
11
+ instance.sdk.uiState = createMapStore({});
10
12
  instance.ui = {
11
- state: new Set(),
13
+ enableAppNotification: function () {
14
+ instance.sdk.uiState.setKey('appNotification', true);
15
+ },
16
+ disableAppNotification: function () {
17
+ if (!instance.sdk.uiState.get().app) {
18
+ instance.sdk.uiState.setKey('appNotification', false);
19
+ }
20
+ },
21
+ enableApp: function () {
22
+ instance.sdk.uiState.set({
23
+ app: true,
24
+ appNotification: true,
25
+ });
26
+ },
27
+ disableApp: function () {
28
+ instance.sdk.uiState.set({
29
+ ...instance.sdk.uiState.get(),
30
+ appNotification: false,
31
+ app: false,
32
+ });
33
+ },
12
34
  };
13
35
  if (opts.onContentActivate) {
14
36
  instance.ui.onContentActivate = opts.onContentActivate;
15
37
  }
16
- instance.sdk.onAdvertisementActivate = (params) => {
38
+ const onAdvertisementActivate = (params) => {
17
39
  if (instance.ui.onContentActivate) {
18
40
  instance.ui.onContentActivate({ ...params, type: 'advertisement' });
19
41
  }
20
42
  };
21
- instance.sdk.onQuestionActivate = ({ questionType, ...params }) => {
43
+ instance.onQuestionActivate = ({ questionType, ...params }) => {
22
44
  if (instance.ui.onContentActivate) {
23
45
  const type = params.type || (questionType && questionTypes[questionType]) || 'question';
24
- // temporary skip other question types
25
- if (type === 'insight') {
26
- instance.ui.onContentActivate({ ...params, type });
27
- }
46
+ instance.ui.onContentActivate({ ...params, type });
28
47
  }
29
48
  };
49
+ instance.sdk.onMount({ name: 'ui' }, () => {
50
+ const listener = (event) => {
51
+ if (event.slEventBus?.type === 'notification') {
52
+ const action = event.slEventBus.action;
53
+ switch (action) {
54
+ case 'received':
55
+ instance.ui.enableAppNotification();
56
+ break;
57
+ case 'closed':
58
+ instance.ui.disableAppNotification();
59
+ break;
60
+ }
61
+ }
62
+ if (event.slEventBus?.type === 'advertisement' && opts.withAd) {
63
+ const ctx = event.slEventBus;
64
+ const { hasNotification, id, isViewed } = ctx.payload;
65
+ switch (ctx.action) {
66
+ case 'received':
67
+ instance.sdk.closeFeature();
68
+ onAdvertisementActivate({
69
+ stage: 'activate',
70
+ id,
71
+ hasNotification,
72
+ isViewed,
73
+ });
74
+ if (!opts.withAdNotification) {
75
+ instance.sdk.uiState.set({
76
+ promotionBanner: true,
77
+ promotionOverlay: true,
78
+ promotionSidebar: true,
79
+ });
80
+ break;
81
+ }
82
+ if (hasNotification) {
83
+ instance.sdk.uiState.set({
84
+ promotionNotification: true,
85
+ });
86
+ }
87
+ else {
88
+ instance.sdk.uiState.set({
89
+ promotionBanner: true,
90
+ promotionOverlay: true,
91
+ promotionSidebar: true,
92
+ });
93
+ }
94
+ break;
95
+ case 'closed':
96
+ instance.sdk.uiState.set({
97
+ ...instance.sdk.uiState.get(),
98
+ promotionNotification: false,
99
+ promotionBanner: false,
100
+ promotionOverlay: false,
101
+ promotionSidebar: false,
102
+ });
103
+ onAdvertisementActivate({
104
+ stage: 'deactivate',
105
+ id,
106
+ hasNotification,
107
+ isViewed,
108
+ });
109
+ break;
110
+ }
111
+ }
112
+ };
113
+ const cancel1 = eventBus.listen(listener);
114
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
115
+ // @ts-ignore
116
+ const cancel2 = instance.activeFeature.subscribe((feature) => {
117
+ if (feature) {
118
+ instance.ui.enableApp();
119
+ }
120
+ else {
121
+ instance.ui.disableApp();
122
+ }
123
+ });
124
+ return () => {
125
+ cancel1();
126
+ cancel2();
127
+ };
128
+ });
30
129
  done();
31
130
  };
package/package.json CHANGED
@@ -5,13 +5,13 @@
5
5
  },
6
6
  "peerDependencies": {
7
7
  "@nanostores/query": "^0.2.10",
8
- "@streamlayer/sl-eslib": "^5.123.1",
8
+ "@streamlayer/sl-eslib": "^5.130.0",
9
9
  "nanostores": "^0.10.3",
10
- "@streamlayer/sdk-web-api": "^1.5.3",
11
- "@streamlayer/sdk-web-interfaces": "^1.1.15",
12
- "@streamlayer/sdk-web-logger": "^1.0.20",
13
- "@streamlayer/sdk-web-storage": "^1.0.20",
14
- "@streamlayer/sdk-web-types": "^1.6.3"
10
+ "@streamlayer/sdk-web-api": "^1.6.1",
11
+ "@streamlayer/sdk-web-interfaces": "^1.2.1",
12
+ "@streamlayer/sdk-web-storage": "^1.0.22",
13
+ "@streamlayer/sdk-web-logger": "^1.0.22",
14
+ "@streamlayer/sdk-web-types": "^1.7.1"
15
15
  },
16
16
  "exports": {
17
17
  ".": {
@@ -40,7 +40,7 @@
40
40
  "default": "./lib/auth/index.js"
41
41
  }
42
42
  },
43
- "version": "1.4.3",
43
+ "version": "1.6.0",
44
44
  "type": "module",
45
45
  "main": "./lib/index.js",
46
46
  "module": "./lib/index.js",