@go1/go1-embedding-react-sdk 0.0.1

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,175 @@
1
+ # Go1 Embedding React SDK
2
+
3
+ A react hook to embed a Go1 content view in your site. Includes two-way messaging between your site and the Go1 content view.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @go1/go1-embedding-react-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Import the hook from the library and use it in your functional components.
14
+
15
+ ```tsx
16
+ import { useMemo, useCallback } from "react";
17
+ import { ContentViewProps, useGo1ContentView } from "@go1/go1-embedding-react-sdk";
18
+
19
+ export function YourHostingComponent() {
20
+ const onGo1MessageReceived = useCallback(message => {
21
+ console.log('Received postMessage from Go1');
22
+ console.log(message);
23
+ }, []);
24
+
25
+ const options = useMemo<ContentViewProps>(() => {
26
+ return {
27
+ feature: 'metrics',
28
+ portalName: 'learning-incorporated-usa',
29
+ onGo1MessageReceived,
30
+ };
31
+ }, []);
32
+
33
+ const { sendMessageToGo1, ContentView } = useGo1ContentView(options);
34
+
35
+ return (
36
+ <div style={{ width: "100vw", height: "100vh" }}>
37
+ <ContentView />
38
+ </div>
39
+ );
40
+ }
41
+ ```
42
+
43
+ # APIs
44
+
45
+ ### Types
46
+
47
+ ```typescript
48
+ type OnGo1MessageReceived = (message: Go1Message) => void;
49
+
50
+ type SendMessageToGo1 = (message: Go1Message) => void;
51
+
52
+ interface Go1Message {
53
+ type: string;
54
+ payload?: any; //optional
55
+ }
56
+
57
+ interface ThemeInformation {
58
+ /** Hex string, starting with a #. Example #0437F2 */
59
+ primaryColor: string;
60
+ }
61
+
62
+ /**
63
+ * Used to specify how the feature should appear, or what data it should work with.
64
+ * example:
65
+ * {
66
+ * id: 341134,
67
+ * shouldShowHeading: false
68
+ * }
69
+ */
70
+ interface FeatureAttributes {
71
+ [key: string]: number | string
72
+ }
73
+
74
+ interface InitOptions {
75
+ feature: string; // 'ai-chat' | 'explore' | 'preview';
76
+ featureAttributes?: FeatureAttributes; // optional
77
+ portalName?: string; //optional
78
+ onGo1MessageReceived?: OnGo1MessageReceived; //optional
79
+ additionalUserInfo?: any; //optional
80
+ themeInformation?: ThemeInformation; //optional
81
+ oneTimeToken?: string; //optional
82
+ }
83
+ ```
84
+
85
+ ## Function: `useGo1ContentView`
86
+
87
+ A react hook that will return
88
+
89
+ - `sendMessageToGo1`: A function that allows you to send a message to the Go1 content view
90
+ - `ContentView`: A react component to render the Go1 content view
91
+
92
+ ```typescript
93
+ function useGo1ContentView(options: InitOptions): {
94
+ sendMessageToGo1: SendMessageToGo1;
95
+ ContentView: () => React.JSX.Element;
96
+ };
97
+ ```
98
+
99
+ ### Usage
100
+
101
+ ```typescript
102
+ const onGo1MessageReceived = useCallback(message => {
103
+ console.log('Received postMessage from Go1');
104
+ console.log(message);
105
+ }, []);
106
+
107
+ const options = useMemo<ContentViewProps>(() => {
108
+ return {
109
+ feature: 'ai-chat',
110
+ portalName: 'learning-incorporated-usa',
111
+ featureAttributes: {
112
+ id: 32973,
113
+ },
114
+ onGo1MessageReceived,
115
+ additionalUserInfo: {
116
+ department: 'sales',
117
+ goals: ['leadership', 'management']
118
+ },
119
+ themeInformation: {
120
+ primaryColor: '#0437F2'
121
+ },
122
+ oneTimeToken: '907687a6e81a458190fe4c21f05ee689'
123
+ };
124
+ }, []);
125
+
126
+ const { sendMessageToGo1, ContentView } = useGo1ContentView(options);
127
+
128
+ // now to render the <ContentView>
129
+ ```
130
+
131
+ ## Function: `sendMessageToGo1`
132
+
133
+ Send a message to the Go1 content view. Useful to control the user experience when an event starts on your website and the Go1 content view needs to be aware of it (and potentially perform other actions - this is `feature` dependent.)
134
+
135
+ ```typescript
136
+ function sendMessageToGo1(message: Go1Message): void;
137
+ ```
138
+
139
+ ### Usage
140
+
141
+ ```typescript
142
+ const message = {
143
+ type: "return_to_home_screen",
144
+ };
145
+ sendMessageToGo1(message);
146
+ ```
147
+
148
+ ## Function: `onGo1MessageReceived`
149
+
150
+ Respond to events that have occurred in the Go1 content view. Useful to control the user experience when an event starts in the Go1 content view and your website needs to be aware of it.
151
+
152
+ ### Usage
153
+
154
+ ```typescript
155
+ const onGo1MessageReceived = (message) => {
156
+ switch (message.type) {
157
+ case "enrolment_created": {
158
+ const { userId, courseId } = message.payload;
159
+ // perform appropriate actions for when a new enrolment is created
160
+ break;
161
+ }
162
+ case "category_favourite_added": {
163
+ const { userId, category } = message.payload;
164
+ // perform appropriate actions for when a new category is favourited
165
+ break;
166
+ }
167
+ default:
168
+ break;
169
+ }
170
+ };
171
+ ```
172
+
173
+ ## Support
174
+
175
+ https://www.go1.com/developers
package/dist/README.md ADDED
@@ -0,0 +1,175 @@
1
+ # Go1 Embedding React SDK
2
+
3
+ A react hook to embed a Go1 content view in your site. Includes two-way messaging between your site and the Go1 content view.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @go1/go1-embedding-react-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Import the hook from the library and use it in your functional components.
14
+
15
+ ```tsx
16
+ import { useMemo, useCallback } from "react";
17
+ import { ContentViewProps, useGo1ContentView } from "@go1/go1-embedding-react-sdk";
18
+
19
+ export function YourHostingComponent() {
20
+ const onGo1MessageReceived = useCallback(message => {
21
+ console.log('Received postMessage from Go1');
22
+ console.log(message);
23
+ }, []);
24
+
25
+ const options = useMemo<ContentViewProps>(() => {
26
+ return {
27
+ feature: 'metrics',
28
+ portalName: 'learning-incorporated-usa',
29
+ onGo1MessageReceived,
30
+ };
31
+ }, []);
32
+
33
+ const { sendMessageToGo1, ContentView } = useGo1ContentView(options);
34
+
35
+ return (
36
+ <div style={{ width: "100vw", height: "100vh" }}>
37
+ <ContentView />
38
+ </div>
39
+ );
40
+ }
41
+ ```
42
+
43
+ # APIs
44
+
45
+ ### Types
46
+
47
+ ```typescript
48
+ type OnGo1MessageReceived = (message: Go1Message) => void;
49
+
50
+ type SendMessageToGo1 = (message: Go1Message) => void;
51
+
52
+ interface Go1Message {
53
+ type: string;
54
+ payload?: any; //optional
55
+ }
56
+
57
+ interface ThemeInformation {
58
+ /** Hex string, starting with a #. Example #0437F2 */
59
+ primaryColor: string;
60
+ }
61
+
62
+ /**
63
+ * Used to specify how the feature should appear, or what data it should work with.
64
+ * example:
65
+ * {
66
+ * id: 341134,
67
+ * shouldShowHeading: false
68
+ * }
69
+ */
70
+ interface FeatureAttributes {
71
+ [key: string]: number | string
72
+ }
73
+
74
+ interface InitOptions {
75
+ feature: string; // 'ai-chat' | 'explore' | 'preview';
76
+ featureAttributes?: FeatureAttributes; // optional
77
+ portalName?: string; //optional
78
+ onGo1MessageReceived?: OnGo1MessageReceived; //optional
79
+ additionalUserInfo?: any; //optional
80
+ themeInformation?: ThemeInformation; //optional
81
+ oneTimeToken?: string; //optional
82
+ }
83
+ ```
84
+
85
+ ## Function: `useGo1ContentView`
86
+
87
+ A react hook that will return
88
+
89
+ - `sendMessageToGo1`: A function that allows you to send a message to the Go1 content view
90
+ - `ContentView`: A react component to render the Go1 content view
91
+
92
+ ```typescript
93
+ function useGo1ContentView(options: InitOptions): {
94
+ sendMessageToGo1: SendMessageToGo1;
95
+ ContentView: () => React.JSX.Element;
96
+ };
97
+ ```
98
+
99
+ ### Usage
100
+
101
+ ```typescript
102
+ const onGo1MessageReceived = useCallback(message => {
103
+ console.log('Received postMessage from Go1');
104
+ console.log(message);
105
+ }, []);
106
+
107
+ const options = useMemo<ContentViewProps>(() => {
108
+ return {
109
+ feature: 'ai-chat',
110
+ portalName: 'learning-incorporated-usa',
111
+ featureAttributes: {
112
+ id: 32973,
113
+ },
114
+ onGo1MessageReceived,
115
+ additionalUserInfo: {
116
+ department: 'sales',
117
+ goals: ['leadership', 'management']
118
+ },
119
+ themeInformation: {
120
+ primaryColor: '#0437F2'
121
+ },
122
+ oneTimeToken: '907687a6e81a458190fe4c21f05ee689'
123
+ };
124
+ }, []);
125
+
126
+ const { sendMessageToGo1, ContentView } = useGo1ContentView(options);
127
+
128
+ // now to render the <ContentView>
129
+ ```
130
+
131
+ ## Function: `sendMessageToGo1`
132
+
133
+ Send a message to the Go1 content view. Useful to control the user experience when an event starts on your website and the Go1 content view needs to be aware of it (and potentially perform other actions - this is `feature` dependent.)
134
+
135
+ ```typescript
136
+ function sendMessageToGo1(message: Go1Message): void;
137
+ ```
138
+
139
+ ### Usage
140
+
141
+ ```typescript
142
+ const message = {
143
+ type: "return_to_home_screen",
144
+ };
145
+ sendMessageToGo1(message);
146
+ ```
147
+
148
+ ## Function: `onGo1MessageReceived`
149
+
150
+ Respond to events that have occurred in the Go1 content view. Useful to control the user experience when an event starts in the Go1 content view and your website needs to be aware of it.
151
+
152
+ ### Usage
153
+
154
+ ```typescript
155
+ const onGo1MessageReceived = (message) => {
156
+ switch (message.type) {
157
+ case "enrolment_created": {
158
+ const { userId, courseId } = message.payload;
159
+ // perform appropriate actions for when a new enrolment is created
160
+ break;
161
+ }
162
+ case "category_favourite_added": {
163
+ const { userId, category } = message.payload;
164
+ // perform appropriate actions for when a new category is favourited
165
+ break;
166
+ }
167
+ default:
168
+ break;
169
+ }
170
+ };
171
+ ```
172
+
173
+ ## Support
174
+
175
+ https://www.go1.com/developers
@@ -0,0 +1 @@
1
+ export declare const GO1_MESSAGE = "go1Message";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GO1_MESSAGE = void 0;
4
+ exports.GO1_MESSAGE = 'go1Message';
@@ -0,0 +1,29 @@
1
+ export type OnGo1MessageReceived = (message: Go1Message) => void;
2
+ export type SendMessageToGo1 = (message: Go1Message) => void;
3
+ export type IframesMap = {
4
+ [key: string]: HTMLIFrameElement;
5
+ };
6
+ export interface AdditionalInfoMessage {
7
+ additionalUserInfo?: any;
8
+ themeInformation?: ThemeInformation;
9
+ }
10
+ export interface ThemeInformation {
11
+ primaryColor: string;
12
+ }
13
+ export interface Go1Message {
14
+ type: string;
15
+ payload?: any;
16
+ }
17
+ export interface FeatureAttributes {
18
+ [key: string]: number | string;
19
+ }
20
+ export interface InitOptions {
21
+ feature: string;
22
+ iframeParentId: string;
23
+ featureAttributes?: FeatureAttributes;
24
+ portalName?: string;
25
+ onGo1MessageReceived?: OnGo1MessageReceived;
26
+ additionalUserInfo?: any;
27
+ themeInformation?: ThemeInformation;
28
+ oneTimeToken?: string;
29
+ }
@@ -0,0 +1,9 @@
1
+ import * as React from 'react';
2
+ import { InitOptions, SendMessageToGo1 } from '../../common/types';
3
+ export interface ContentViewProps extends Omit<InitOptions, 'iframeParentId'> {
4
+ baseURL?: string;
5
+ }
6
+ export declare function useGo1ContentView(props: ContentViewProps): {
7
+ sendMessageToGo1: SendMessageToGo1;
8
+ ContentView: (userProps?: React.IframeHTMLAttributes<HTMLIFrameElement>) => React.JSX.Element;
9
+ };
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useGo1ContentView = void 0;
4
+ const React = require("react");
5
+ const react_1 = require("react");
6
+ const constants_1 = require("../../common/constants");
7
+ function useGo1ContentView(props) {
8
+ const { feature, featureAttributes, portalName, oneTimeToken, onGo1MessageReceived, additionalUserInfo, themeInformation, baseURL, } = props;
9
+ const iframeRef = (0, react_1.useRef)(null);
10
+ const url = baseURL || 'https://embedding.go1.com';
11
+ let { iframeURL, iframeID } = (0, react_1.useMemo)(() => {
12
+ let iframeURL = new URL(`${url}/${feature}`);
13
+ if (featureAttributes) {
14
+ const { id, ...rest } = featureAttributes;
15
+ if (id) {
16
+ iframeURL.pathname += `/${id}`;
17
+ }
18
+ const entries = Object.entries(rest);
19
+ for (let [key, value] of entries) {
20
+ iframeURL.searchParams.append(key, `${value}`);
21
+ }
22
+ }
23
+ if (portalName) {
24
+ iframeURL.searchParams.append('portal_id', portalName);
25
+ }
26
+ if (oneTimeToken) {
27
+ iframeURL.searchParams.append('oneTimeToken', oneTimeToken);
28
+ }
29
+ const iframeID = `go1-iframe-${feature}`;
30
+ return { iframeURL, iframeID };
31
+ }, [url, feature, oneTimeToken, portalName, featureAttributes]);
32
+ const sendMessageToGo1 = (0, react_1.useCallback)((message) => {
33
+ iframeRef.current?.contentWindow?.postMessage(message, url);
34
+ }, [url]);
35
+ const eventListenerInstance = (0, react_1.useCallback)((event) => {
36
+ const { _type, ...rest } = event.data;
37
+ if (_type === constants_1.GO1_MESSAGE) {
38
+ if (rest.type === 'go1_app_initialised') {
39
+ sendMessageToGo1({
40
+ type: 'set_additional_embedding_data',
41
+ payload: {
42
+ additionalUserInfo,
43
+ themeInformation,
44
+ },
45
+ });
46
+ }
47
+ else {
48
+ if (onGo1MessageReceived) {
49
+ onGo1MessageReceived(rest);
50
+ }
51
+ }
52
+ }
53
+ }, [
54
+ additionalUserInfo,
55
+ sendMessageToGo1,
56
+ onGo1MessageReceived,
57
+ themeInformation,
58
+ ]);
59
+ (0, react_1.useEffect)(() => {
60
+ window.addEventListener('message', eventListenerInstance);
61
+ return () => {
62
+ window.removeEventListener('message', eventListenerInstance);
63
+ };
64
+ }, [eventListenerInstance]);
65
+ const ContentView = (0, react_1.useCallback)((userProps) => {
66
+ return Go1ContentView(iframeRef, iframeURL, iframeID, userProps);
67
+ }, [iframeRef, iframeURL, iframeID]);
68
+ return { sendMessageToGo1, ContentView };
69
+ }
70
+ exports.useGo1ContentView = useGo1ContentView;
71
+ function Go1ContentView(iframeRef, iframeURL, iframeID, userProps) {
72
+ return (React.createElement("iframe", { id: iframeID, ref: iframeRef, src: iframeURL.toString(), style: { border: 0, width: '100%', height: '100%' }, sandbox: 'allow-popups allow-popups-to-escape-sandbox allow-scripts allow-same-origin allow-forms', ...userProps }));
73
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@go1/go1-embedding-react-sdk",
3
+ "version": "0.0.1",
4
+ "description": "A React library to embed GO1 content into your website.",
5
+ "main": "dist/go1-embedding-react-sdk/src/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "author": {
10
+ "name": "Go1",
11
+ "email": "tim.sawtell@go1.com",
12
+ "url": "https://go1.com"
13
+ },
14
+ "keywords": [
15
+ "@go1/go1-embedding-js-sdk",
16
+ "Go1"
17
+ ],
18
+ "scripts": {
19
+ "test": "jest"
20
+ },
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "@testing-library/jest-dom": "5.16.5",
24
+ "@testing-library/react": "14.0.0",
25
+ "@testing-library/user-event": "14.4.3",
26
+ "jest": "^29.6.1",
27
+ "react": "18.2.0",
28
+ "react-dom": "^18.2.0",
29
+ "ts-jest": "^29.1.1",
30
+ "typescript": "^5.1.6"
31
+ },
32
+ "peerDependencies": {
33
+ "react": ">=16.8"
34
+ }
35
+ }