@screeb/sdk-react 0.1.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/README.md ADDED
@@ -0,0 +1,101 @@
1
+ <p align="center">
2
+ <a href="https://screeb.app" alt="Screeb">
3
+ <img src="https://raw.githubusercontent.com/ScreebApp/sdk-js/master/packages/screeb-sdk-react/readme/screeb-logo.svg?token=GHSAT0AAAAAAB2OOPMGT2QD5TL3IRJN3CKCZDEYHJA" alt="Logo" height="120px" style="margin-top: 20px;"/>
4
+ </a>
5
+ </p>
6
+ <h1 align="center">@screeb/sdk-react</h1>
7
+ <p align="center">
8
+ Screeb's browser sdk, optimized for React.
9
+
10
+ <b>Continuous Product Discovery, Without the Time Sink.</b>
11
+
12
+ <a href="https://screeb.app" alt="Screeb">Screeb</a> is the only Continuous Product Discovery platform that lets you analyse users' behaviour, ask in-app questions, recruit people for interviews and analyse data in a blink with AI.
13
+ </p>
14
+
15
+ <p align="center">
16
+ <a href="https://github.com/ScreebApp/sdk-js/actions/workflows/node.js.yml" alt="ci">
17
+ <img alt="ci" src="https://github.com/ScreebApp/sdk-js/actions/workflows/node.js.yml/badge.svg">
18
+ </a>
19
+ <a href="https://www.npmjs.com/package/@screeb/sdk-react" alt="version">
20
+ <img alt="version" src="https://img.shields.io/npm/v/@screeb/sdk-react.svg" />
21
+ </a>
22
+ <a href="https://bundlephobia.com/package/@screeb/sdk-react" alt="min size">
23
+ <img alt="min size" src="https://img.shields.io/bundlephobia/min/@screeb/sdk-react">
24
+ </a>
25
+ <a href="https://bundlephobia.com/package/@screeb/sdk-react" alt="minzipped size">
26
+ <img alt="minzipped size" src="https://img.shields.io/bundlephobia/minzip/@screeb/sdk-react">
27
+ </a>
28
+ <img alt="downloads" src="https://badgen.net/npm/dw/@screeb/sdk-react" />
29
+ </p>
30
+
31
+
32
+ ## Installation
33
+
34
+ This library is published in the NPM registry and can be installed using any compatible package manager.
35
+
36
+ ```bash
37
+ npm install @screeb/sdk-react --save
38
+
39
+ # For Yarn, use the command below.
40
+ yarn add @screeb/sdk-react
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ Basic usage:
46
+ ```ts
47
+ import * as React from 'react';
48
+
49
+ import { ScreebProvider, useScreeb } from '@screeb/sdk-react';
50
+
51
+ const SCREEB_APP_ID = 'your-screeb-website-id';
52
+
53
+ const App = () => (
54
+ <ScreebProvider
55
+ autoInit
56
+ websiteId={SCREEB_APP_ID}
57
+ userId="dev@screeb.app"
58
+ userProperties={{
59
+ firstname: "John",
60
+ lastname: "Smith",
61
+ last_seen_at: new Date(),
62
+ authenticated: true,
63
+ org_size: 20,
64
+ }}
65
+ >
66
+ <HomePage />
67
+ </ScreebProvider>
68
+ );
69
+
70
+ // Anywhere in your app
71
+ const HomePage = () => {
72
+ const { trackEvent } = useScreeb();
73
+
74
+ const onButtonClicked = React.useCallback(
75
+ () => eventTrack(
76
+ "screeb-sdk-react-example started",
77
+ { test: 123 }
78
+ );
79
+ );
80
+
81
+ return (
82
+ <button onClick={onButtonClicked}>
83
+ Track event!
84
+ </button>
85
+ );
86
+ };
87
+ ```
88
+
89
+ For a more advanced usage and a complete API documentation, see [documentation generated from source files](https://github.com/ScreebApp/sdk-js/tree/master/packages/screeb-sdk-react/docs).
90
+
91
+ For further information, see [our developper documentation](https://github.com/ScreebApp/developers).
92
+
93
+ ## Support
94
+ For any issues, please contact our support team at support@screeb.com.
95
+
96
+ ## Contributing
97
+ All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.
98
+
99
+ ## License
100
+
101
+ Released under [MIT License](https://github.com/ScreebApp/sdk-js/blob/master/LICENSE).
@@ -0,0 +1,4 @@
1
+ import * as React from "react";
2
+ import { ScreebContextValues } from "./types";
3
+ declare const ScreebContext: React.Context<ScreebContextValues | undefined>;
4
+ export default ScreebContext;
@@ -0,0 +1,3 @@
1
+ export { ScreebProvider } from "./provider";
2
+ export { useScreeb } from "./useScreeb";
3
+ export * from "./types";
@@ -0,0 +1,347 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import * as Screeb from '@screeb/sdk-browser';
3
+ import * as React from 'react';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise */
20
+
21
+
22
+ var __assign = function() {
23
+ __assign = Object.assign || function __assign(t) {
24
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
25
+ s = arguments[i];
26
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
27
+ }
28
+ return t;
29
+ };
30
+ return __assign.apply(this, arguments);
31
+ };
32
+
33
+ function __rest(s, e) {
34
+ var t = {};
35
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
36
+ t[p] = s[p];
37
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
38
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
39
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
40
+ t[p[i]] = s[p[i]];
41
+ }
42
+ return t;
43
+ }
44
+
45
+ function __awaiter(thisArg, _arguments, P, generator) {
46
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
47
+ return new (P || (P = Promise))(function (resolve, reject) {
48
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
49
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
51
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
52
+ });
53
+ }
54
+
55
+ function __generator(thisArg, body) {
56
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
57
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
58
+ function verb(n) { return function (v) { return step([n, v]); }; }
59
+ function step(op) {
60
+ if (f) throw new TypeError("Generator is already executing.");
61
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
62
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
63
+ if (y = 0, t) op = [op[0] & 2, t.value];
64
+ switch (op[0]) {
65
+ case 0: case 1: t = op; break;
66
+ case 4: _.label++; return { value: op[1], done: false };
67
+ case 5: _.label++; y = op[1]; op = [0]; continue;
68
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
69
+ default:
70
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
71
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
72
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
73
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
74
+ if (t[2]) _.ops.pop();
75
+ _.trys.pop(); continue;
76
+ }
77
+ op = body.call(thisArg, _);
78
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
79
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
80
+ }
81
+ }
82
+
83
+ var ScreebContext = React.createContext(undefined);
84
+
85
+ /**
86
+ * Logs messages in the console with a corresponding urgency
87
+ *
88
+ * @param level the urgency of the message
89
+ * @param message the message to log in the console
90
+ */
91
+ var log = function (level, message) {
92
+ var packageName = "[@screeb/react-sdk]";
93
+ switch (level) {
94
+ case "info":
95
+ // eslint-disable-next-line no-console
96
+ console.log("".concat(packageName, " ").concat(message));
97
+ break;
98
+ case "warn":
99
+ // eslint-disable-next-line no-console
100
+ console.warn("".concat(packageName, " ").concat(message));
101
+ break;
102
+ case "error":
103
+ // eslint-disable-next-line no-console
104
+ console.error("".concat(packageName, " ").concat(message));
105
+ break;
106
+ default:
107
+ // eslint-disable-next-line no-console
108
+ console.log("".concat(packageName, " ").concat(message));
109
+ }
110
+ };
111
+
112
+ var isSSR = typeof window === "undefined";
113
+
114
+ var isInitialized = false;
115
+ var ScreebProvider = function (_a) {
116
+ var websiteId = _a.websiteId, userId = _a.userId, userProperties = _a.userProperties, children = _a.children, _b = _a.shouldLoad, shouldLoad = _b === void 0 ? !isSSR : _b, _c = _a.autoInit, autoInit = _c === void 0 ? false : _c, options = _a.options, rest = __rest(_a, ["websiteId", "userId", "userProperties", "children", "shouldLoad", "autoInit", "options"]);
117
+ var isLoaded = React.useRef(Screeb.isLoaded());
118
+ // Allow data-x attributes, see https://github.com/devrnt/react-use-screeb/issues/478
119
+ var invalidPropKeys = Object.keys(rest).filter(function (key) { return !key.startsWith("data-"); });
120
+ if (invalidPropKeys.length > 0) {
121
+ log("warn", [
122
+ "some invalid props were passed to ScreebProvider. ",
123
+ "Please check following props: ".concat(invalidPropKeys.join(", "), "."),
124
+ ].join(""));
125
+ }
126
+ var ensureScreeb = React.useCallback(
127
+ // eslint-disable-next-line no-unused-vars
128
+ function (functionName, callback, onlyLoaded) {
129
+ if (onlyLoaded === void 0) { onlyLoaded = false; }
130
+ return __awaiter(this, void 0, void 0, function () {
131
+ var message, message;
132
+ return __generator(this, function (_a) {
133
+ if (!Screeb.isLoaded() && !shouldLoad) {
134
+ message = "Screeb instance is not loaded because `shouldLoad` is set to `false` in `ScreebProvider`";
135
+ log("warn", message);
136
+ return [2 /*return*/, Promise.reject(message)];
137
+ }
138
+ if (!isInitialized && !onlyLoaded) {
139
+ message = [
140
+ "\"".concat(functionName, "\" was called but Screeb has not been initialized yet. "),
141
+ "Please call 'init' before calling '".concat(functionName, "' or "),
142
+ "set 'autoInit' to true in the ScreebProvider.",
143
+ ].join("");
144
+ log("warn", message);
145
+ return [2 /*return*/, Promise.reject(message)];
146
+ }
147
+ return [2 /*return*/, Promise.resolve(callback())];
148
+ });
149
+ });
150
+ }, [shouldLoad]);
151
+ var close = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
152
+ return __generator(this, function (_a) {
153
+ switch (_a.label) {
154
+ case 0:
155
+ if (!isLoaded.current) return [3 /*break*/, 2];
156
+ return [4 /*yield*/, Screeb.close()];
157
+ case 1:
158
+ _a.sent();
159
+ isLoaded.current = false;
160
+ _a.label = 2;
161
+ case 2: return [2 /*return*/];
162
+ }
163
+ });
164
+ }); }, [isLoaded.current]);
165
+ var debug = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
166
+ switch (_a.label) {
167
+ case 0: return [4 /*yield*/, ensureScreeb("debug", function () { return Screeb.debug(); })];
168
+ case 1: return [2 /*return*/, _a.sent()];
169
+ }
170
+ }); }); }, []);
171
+ var eventTrack = React.useCallback(function (eventName, eventProperties) { return __awaiter(void 0, void 0, void 0, function () {
172
+ return __generator(this, function (_a) {
173
+ switch (_a.label) {
174
+ case 0: return [4 /*yield*/, ensureScreeb("eventTrack", function () {
175
+ return Screeb.eventTrack(eventName, eventProperties);
176
+ })];
177
+ case 1: return [2 /*return*/, _a.sent()];
178
+ }
179
+ });
180
+ }); }, []);
181
+ var identity = React.useCallback(function (userId, userProperties) { return __awaiter(void 0, void 0, void 0, function () {
182
+ return __generator(this, function (_a) {
183
+ switch (_a.label) {
184
+ case 0: return [4 /*yield*/, ensureScreeb("identity", function () {
185
+ return Screeb.identity(userId, userProperties);
186
+ })];
187
+ case 1: return [2 /*return*/, _a.sent()];
188
+ }
189
+ });
190
+ }); }, []);
191
+ var identityGet = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
192
+ switch (_a.label) {
193
+ case 0: return [4 /*yield*/, ensureScreeb("identityGet", function () { return Screeb.identityGet(); })];
194
+ case 1: return [2 /*return*/, _a.sent()];
195
+ }
196
+ }); }); }, []);
197
+ var identityGroupAssign = React.useCallback(function (groupName, groupType, groupProperties) { return __awaiter(void 0, void 0, void 0, function () {
198
+ return __generator(this, function (_a) {
199
+ switch (_a.label) {
200
+ case 0: return [4 /*yield*/, ensureScreeb("identityGroupAssign", function () {
201
+ return Screeb.identityGroupAssign(groupName, groupType, groupProperties);
202
+ })];
203
+ case 1: return [2 /*return*/, _a.sent()];
204
+ }
205
+ });
206
+ }); }, []);
207
+ var identityGroupUnassign = React.useCallback(function (groupName, groupType) { return __awaiter(void 0, void 0, void 0, function () {
208
+ return __generator(this, function (_a) {
209
+ switch (_a.label) {
210
+ case 0: return [4 /*yield*/, ensureScreeb("identityGroupUnassign", function () {
211
+ return Screeb.identityGroupUnassign(groupName, groupType);
212
+ })];
213
+ case 1: return [2 /*return*/, _a.sent()];
214
+ }
215
+ });
216
+ }); }, []);
217
+ var identityProperties = React.useCallback(function (userProperties) { return __awaiter(void 0, void 0, void 0, function () {
218
+ return __generator(this, function (_a) {
219
+ switch (_a.label) {
220
+ case 0: return [4 /*yield*/, ensureScreeb("identityProperties", function () {
221
+ return Screeb.identityProperties(userProperties);
222
+ })];
223
+ case 1: return [2 /*return*/, _a.sent()];
224
+ }
225
+ });
226
+ }); }, []);
227
+ var identityReset = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
228
+ switch (_a.label) {
229
+ case 0: return [4 /*yield*/, ensureScreeb("identityReset", function () { return Screeb.identityReset(); })];
230
+ case 1: return [2 /*return*/, _a.sent()];
231
+ }
232
+ }); }); }, []);
233
+ var init = React.useCallback(function (websiteId, userId, userProperties) { return __awaiter(void 0, void 0, void 0, function () {
234
+ return __generator(this, function (_a) {
235
+ switch (_a.label) {
236
+ case 0: return [4 /*yield*/, ensureScreeb("init", function () {
237
+ if (!isInitialized) {
238
+ Screeb.init(websiteId, userId, userProperties);
239
+ isInitialized = true;
240
+ }
241
+ }, true)];
242
+ case 1:
243
+ _a.sent();
244
+ return [2 /*return*/];
245
+ }
246
+ });
247
+ }); }, [ensureScreeb, websiteId, shouldLoad]);
248
+ var load = React.useCallback(function (options) { return __awaiter(void 0, void 0, void 0, function () {
249
+ return __generator(this, function (_a) {
250
+ switch (_a.label) {
251
+ case 0:
252
+ if (!!isLoaded.current) return [3 /*break*/, 3];
253
+ Screeb.load(options);
254
+ isLoaded.current = true;
255
+ if (!autoInit) return [3 /*break*/, 3];
256
+ if (!websiteId) return [3 /*break*/, 2];
257
+ return [4 /*yield*/, init(websiteId, userId, userProperties)];
258
+ case 1:
259
+ _a.sent();
260
+ return [3 /*break*/, 3];
261
+ case 2:
262
+ log("warn", "autoInit is set to true, but no websiteId have been provided.");
263
+ _a.label = 3;
264
+ case 3: return [2 /*return*/];
265
+ }
266
+ });
267
+ }); }, [isLoaded.current, autoInit]);
268
+ var surveyClose = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
269
+ switch (_a.label) {
270
+ case 0: return [4 /*yield*/, ensureScreeb("surveyClose", function () { return Screeb.surveyClose(); })];
271
+ case 1: return [2 /*return*/, _a.sent()];
272
+ }
273
+ }); }); }, []);
274
+ var surveyStart = React.useCallback(function (surveyId, allowMultipleResponses, hiddenFields) { return __awaiter(void 0, void 0, void 0, function () {
275
+ return __generator(this, function (_a) {
276
+ switch (_a.label) {
277
+ case 0: return [4 /*yield*/, ensureScreeb("surveyStart", function () {
278
+ return Screeb.surveyStart(surveyId, allowMultipleResponses, hiddenFields);
279
+ })];
280
+ case 1: return [2 /*return*/, _a.sent()];
281
+ }
282
+ });
283
+ }); }, []);
284
+ var targetingCheck = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
285
+ switch (_a.label) {
286
+ case 0: return [4 /*yield*/, ensureScreeb("targetingCheck", function () { return Screeb.targetingCheck(); })];
287
+ case 1: return [2 /*return*/, _a.sent()];
288
+ }
289
+ }); }); }, []);
290
+ var targetingDebug = React.useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
291
+ switch (_a.label) {
292
+ case 0: return [4 /*yield*/, ensureScreeb("targetingDebug", function () { return Screeb.targetingDebug(); })];
293
+ case 1: return [2 /*return*/, _a.sent()];
294
+ }
295
+ }); }); }, []);
296
+ var providerValue = React.useMemo(function () { return ({
297
+ close: close,
298
+ debug: debug,
299
+ eventTrack: eventTrack,
300
+ identity: identity,
301
+ identityGet: identityGet,
302
+ identityGroupAssign: identityGroupAssign,
303
+ identityGroupUnassign: identityGroupUnassign,
304
+ identityProperties: identityProperties,
305
+ identityReset: identityReset,
306
+ init: init,
307
+ load: load,
308
+ surveyClose: surveyClose,
309
+ surveyStart: surveyStart,
310
+ targetingCheck: targetingCheck,
311
+ targetingDebug: targetingDebug,
312
+ }); }, [
313
+ close,
314
+ debug,
315
+ eventTrack,
316
+ identity,
317
+ identityGet,
318
+ identityGroupAssign,
319
+ identityGroupUnassign,
320
+ identityProperties,
321
+ identityReset,
322
+ init,
323
+ load,
324
+ surveyClose,
325
+ surveyStart,
326
+ targetingCheck,
327
+ targetingDebug,
328
+ ]);
329
+ if (!isSSR && shouldLoad) {
330
+ load(options);
331
+ }
332
+ return (jsx(ScreebContext.Provider, __assign({ value: providerValue }, { children: children })));
333
+ };
334
+ var useScreebContext = function () {
335
+ var context = React.useContext(ScreebContext);
336
+ if (context === undefined) {
337
+ // eslint-disable-next-line quotes
338
+ throw new Error('"useScreeb" must be used within `ScreebProvider`.');
339
+ }
340
+ return context;
341
+ };
342
+
343
+ var useScreeb = function () {
344
+ return useScreebContext();
345
+ };
346
+
347
+ export { ScreebProvider, useScreeb };
@@ -0,0 +1,9 @@
1
+ type LogLevel = "info" | "error" | "warn";
2
+ /**
3
+ * Logs messages in the console with a corresponding urgency
4
+ *
5
+ * @param level the urgency of the message
6
+ * @param message the message to log in the console
7
+ */
8
+ export declare const log: (level: LogLevel, message: string) => void;
9
+ export {};
@@ -0,0 +1,4 @@
1
+ import * as React from "react";
2
+ import { ScreebContextValues, ScreebProviderProps } from "./types";
3
+ export declare const ScreebProvider: React.FC<React.PropsWithChildren<ScreebProviderProps>>;
4
+ export declare const useScreebContext: () => ScreebContextValues;