@streamlayer/feature-gamification 0.33.2 → 0.33.4

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.
@@ -75,7 +75,7 @@ export class Gamification extends AbstractFeature {
75
75
  */
76
76
  this.background.activeQuestionId.listen((question) => {
77
77
  const onboardingStatus = this.onboardingStatus.$store.get();
78
- if (question && question.data && onboardingStatus) {
78
+ if (question && question.data && onboardingStatus && onboardingStatus !== OnboardingStatus.Unset) {
79
79
  if (question.data.question?.id !== undefined &&
80
80
  question.data.question.notification !== undefined &&
81
81
  question.data.question?.bypassNotifications?.inAppSilence !== SilenceSetting.ON &&
package/lib/onboarding.js CHANGED
@@ -19,44 +19,94 @@ export var OnboardingStatus;
19
19
  OnboardingStatus["Disabled"] = "disabled";
20
20
  OnboardingStatus["Unavailable"] = "unavailable";
21
21
  })(OnboardingStatus || (OnboardingStatus = {}));
22
+ const showOnboardingInApp = (service, background, notifications, storage) => {
23
+ const { inplayGame } = service.featureSettings.get();
24
+ const notificationId = background.getCurrentSessionId({ prefix: 'onboarding' });
25
+ if (inplayGame) {
26
+ notifications.add({
27
+ type: NotificationType.ONBOARDING,
28
+ id: notificationId,
29
+ action: service.openFeature,
30
+ close: () => {
31
+ notifications.markAsViewed(notificationId);
32
+ },
33
+ delay: 1000,
34
+ autoHideDuration: 1000000,
35
+ data: {
36
+ questionType: QuestionType.UNSET,
37
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
38
+ // @ts-ignore
39
+ onboarding: {
40
+ ...inplayGame,
41
+ instantOpen: !storage.getOnboardingInstantOpen({
42
+ userId: background.userId.get() || '',
43
+ organizationId: background.organizationId.get() || '',
44
+ eventId: background.slStreamId.get() || '',
45
+ }),
46
+ },
47
+ },
48
+ });
49
+ }
50
+ };
51
+ /**
52
+ * check onboarding status, sync with browser cache
53
+ * retrieve onboarding settings from api
54
+ */
55
+ const onboardingProcess = ($store, background, service, storage, listeners) => {
56
+ try {
57
+ const userId = background.userId.get();
58
+ if (!userId) {
59
+ return;
60
+ }
61
+ const onboardingStatus = storage.getOnboardingStatus({
62
+ userId,
63
+ organizationId: background.organizationId.get() || '',
64
+ eventId: background.slStreamId.get() || '',
65
+ });
66
+ if (onboardingStatus === OnboardingStatus.Completed) {
67
+ $store.set(OnboardingStatus.Completed);
68
+ for (const cancel of listeners) {
69
+ cancel();
70
+ }
71
+ return;
72
+ }
73
+ const moderation = background.moderation.getStore().value?.data;
74
+ const inplayGame = service.featureSettings.get().inplayGame;
75
+ if (moderation === undefined || inplayGame === undefined) {
76
+ return;
77
+ }
78
+ const onboardingEnabled = !!moderation?.options?.onboardingEnabled && inplayGame?.enable;
79
+ const optIn = !!inplayGame?.titleCard?.optIn;
80
+ if (onboardingEnabled) {
81
+ if (optIn) {
82
+ $store.set(OnboardingStatus.Required);
83
+ }
84
+ else {
85
+ $store.set(OnboardingStatus.Optional);
86
+ }
87
+ }
88
+ else {
89
+ if (optIn) {
90
+ $store.set(OnboardingStatus.Unavailable);
91
+ }
92
+ else {
93
+ $store.set(OnboardingStatus.Disabled);
94
+ }
95
+ }
96
+ }
97
+ catch (error) {
98
+ console.error(error);
99
+ }
100
+ };
22
101
  export const onboarding = (service, background, transport, notifications) => {
23
102
  const storage = new GamificationStorage();
24
103
  const $store = createSingleStore(OnboardingStatus.Unset);
25
- const showOnboardingInApp = () => {
26
- const { inplayGame } = service.featureSettings.get();
27
- const notificationId = background.getCurrentSessionId({ prefix: 'onboarding' });
28
- if (inplayGame && inplayGame.enable) {
29
- notifications.add({
30
- type: NotificationType.ONBOARDING,
31
- id: notificationId,
32
- action: service.openFeature,
33
- close: () => {
34
- notifications.markAsViewed(notificationId);
35
- },
36
- delay: 1000,
37
- autoHideDuration: 1000000,
38
- data: {
39
- questionType: QuestionType.UNSET,
40
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
- // @ts-ignore
42
- onboarding: {
43
- ...inplayGame,
44
- instantOpen: !storage.getOnboardingInstantOpen({
45
- userId: background.userId.get() || '',
46
- organizationId: background.organizationId.get() || '',
47
- eventId: background.slStreamId.get() || '',
48
- }),
49
- },
50
- },
51
- });
52
- }
53
- };
54
104
  $store.subscribe((onboardingStatus) => {
55
105
  if (onboardingStatus === OnboardingStatus.Unset) {
56
106
  return;
57
107
  }
58
108
  if (onboardingStatus === OnboardingStatus.Optional || onboardingStatus === OnboardingStatus.Required) {
59
- showOnboardingInApp();
109
+ showOnboardingInApp(service, background, notifications, storage);
60
110
  }
61
111
  if (onboardingStatus === OnboardingStatus.Completed) {
62
112
  background.activeQuestionId.invalidate();
@@ -67,64 +117,25 @@ export const onboarding = (service, background, transport, notifications) => {
67
117
  eventId: background.slStreamId.get() || '',
68
118
  });
69
119
  });
70
- /**
71
- * check onboarding status, sync with browser cache
72
- * retrieve onboarding settings from api
73
- */
74
- const onboardingProcess = async () => {
75
- try {
76
- const userId = background.userId.get();
77
- if (!userId) {
78
- $store.set(OnboardingStatus.Unset);
79
- return;
80
- }
81
- const onboardingStatus = storage.getOnboardingStatus({
82
- userId,
83
- organizationId: background.organizationId.get() || '',
84
- eventId: background.slStreamId.get() || '',
85
- });
86
- if (onboardingStatus === OnboardingStatus.Completed) {
87
- $store.set(OnboardingStatus.Completed);
88
- }
89
- const moderation = await background.moderation.getValue();
90
- if ($store.get() === OnboardingStatus.Completed) {
91
- return;
92
- }
93
- const onboardingEnabled = !!(moderation?.options?.onboardingEnabled && service.featureSettings.get().inplayGame?.onboarding?.completed);
94
- const optIn = !!service.featureSettings.get().inplayGame?.titleCard?.optIn;
95
- if (onboardingEnabled) {
96
- if (optIn) {
97
- $store.set(OnboardingStatus.Required);
98
- }
99
- else {
100
- $store.set(OnboardingStatus.Optional);
101
- }
102
- }
103
- else {
104
- if (optIn) {
105
- $store.set(OnboardingStatus.Unavailable);
106
- }
107
- else {
108
- $store.set(OnboardingStatus.Disabled);
109
- }
110
- }
111
- }
112
- catch (error) {
113
- console.error(error);
114
- }
115
- };
116
- void onboardingProcess().then(() => {
117
- background.userId.listen((userId) => {
120
+ const listeners = [];
121
+ onboardingProcess($store, background, service, storage, listeners);
122
+ if ($store.get() !== OnboardingStatus.Completed) {
123
+ listeners.push(background.userId.listen((userId) => {
118
124
  if (userId) {
119
- void onboardingProcess();
125
+ void onboardingProcess($store, background, service, storage, listeners);
120
126
  }
121
- });
122
- background.moderation.listen((value) => {
127
+ }));
128
+ listeners.push(background.moderation.listen((value) => {
123
129
  if (value.data) {
124
- void onboardingProcess();
130
+ void onboardingProcess($store, background, service, storage, listeners);
125
131
  }
126
- });
127
- });
132
+ }));
133
+ listeners.push(service.featureSettings.listen((value) => {
134
+ if (value) {
135
+ void onboardingProcess($store, background, service, storage, listeners);
136
+ }
137
+ }));
138
+ }
128
139
  const submitInplay = async () => {
129
140
  const eventId = background.slStreamId.get();
130
141
  if (eventId) {
package/lib/storage.js CHANGED
@@ -16,7 +16,7 @@ export class GamificationStorage extends Storage {
16
16
  };
17
17
  setOnboardingInstantOpen = ({ userId, eventId, organizationId }) => {
18
18
  const key = this.generateKey([KEY_PREFIX.ONBOARDING_IO, organizationId, userId, eventId]);
19
- window.sessionStorage.setItem(key, 'true');
19
+ window.sessionStorage.setItem(key, '1');
20
20
  };
21
21
  getOnboardingInstantOpen = ({ userId, eventId, organizationId }) => {
22
22
  const key = this.generateKey([KEY_PREFIX.ONBOARDING_IO, organizationId, userId, eventId]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamlayer/feature-gamification",
3
- "version": "0.33.2",
3
+ "version": "0.33.4",
4
4
  "peerDependencies": {
5
5
  "@bufbuild/protobuf": "^1.4.2",
6
6
  "@streamlayer/sl-eslib": "^5.61.1",
@@ -10,8 +10,8 @@
10
10
  "@streamlayer/sdk-web-core": "^0.0.1",
11
11
  "@streamlayer/sdk-web-interfaces": "^0.18.21",
12
12
  "@streamlayer/sdk-web-logger": "^0.0.1",
13
- "@streamlayer/sdk-web-notifications": "^0.0.6",
14
13
  "@streamlayer/sdk-web-storage": "^0.0.4",
14
+ "@streamlayer/sdk-web-notifications": "^0.0.6",
15
15
  "@streamlayer/sdk-web-types": "^0.1.0"
16
16
  },
17
17
  "devDependencies": {