zh-web-sdk 1.0.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/.eslintrc.js +14 -0
- package/.github/workflows/pr.yaml +13 -0
- package/.github/workflows/release.yaml +57 -0
- package/.github/workflows/tag.yaml +55 -0
- package/README.md +36 -0
- package/dist/constants.d.ts +2 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +7 -0
- package/dist/onboarding/OnboardingApp.d.ts +13 -0
- package/dist/onboarding/constants.d.ts +1 -0
- package/dist/redux/actions/index.d.ts +8 -0
- package/dist/redux/reducers/app.d.ts +13 -0
- package/dist/redux/reducers/constants.d.ts +3 -0
- package/dist/redux/reducers/index.d.ts +4 -0
- package/dist/redux/store/index.d.ts +4 -0
- package/dist/styles.d.ts +15 -0
- package/dist/types.d.ts +101 -0
- package/dist/utils/strings.d.ts +6 -0
- package/dist/utils.d.ts +8 -0
- package/package.json +46 -0
- package/scripts/build.js +34 -0
- package/scripts/zip.js +49 -0
- package/src/constants.ts +2 -0
- package/src/index.tsx +197 -0
- package/src/onboarding/OnboardingApp.tsx +128 -0
- package/src/onboarding/constants.ts +2 -0
- package/src/redux/actions/index.ts +12 -0
- package/src/redux/reducers/app.ts +61 -0
- package/src/redux/reducers/constants.ts +3 -0
- package/src/redux/reducers/index.ts +8 -0
- package/src/redux/store/index.ts +6 -0
- package/src/styles.ts +85 -0
- package/src/types.ts +110 -0
- package/src/utils/strings.ts +19 -0
- package/src/utils.ts +13 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACTION_SET_ONBOARDING_MODAL_STATE,
|
|
3
|
+
ACTION_ONBOARDING_APP_LOADED,
|
|
4
|
+
ACTION_SET_USER_ONBOARDING_JWT
|
|
5
|
+
} from "./constants";
|
|
6
|
+
|
|
7
|
+
export interface IState {
|
|
8
|
+
isOnboardingAppActive: boolean;
|
|
9
|
+
isOnboardingAppLoaded: boolean;
|
|
10
|
+
userOnboardingJWT: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface IAction {
|
|
14
|
+
type: string;
|
|
15
|
+
isOnboardingAppActive?: boolean;
|
|
16
|
+
isOnboardingAppLoaded?: boolean;
|
|
17
|
+
userOnboardingJWT?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const INITIAL_STATE: IState = {
|
|
21
|
+
isOnboardingAppActive: false,
|
|
22
|
+
isOnboardingAppLoaded: false,
|
|
23
|
+
userOnboardingJWT: ""
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const applySetIsOnboardingAppActive = (state: IState, action: IAction) : IState => {
|
|
27
|
+
return {
|
|
28
|
+
...state,
|
|
29
|
+
isOnboardingAppActive: !!action.isOnboardingAppActive,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const applySetSendJWTToApp = (state: IState, action: IAction) : IState => {
|
|
34
|
+
return {
|
|
35
|
+
...state,
|
|
36
|
+
isOnboardingAppLoaded: !!action.isOnboardingAppLoaded,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const applySetUserOnboardingJWT = (state: IState, action: IAction) : IState => {
|
|
41
|
+
return {
|
|
42
|
+
...state,
|
|
43
|
+
userOnboardingJWT: action.userOnboardingJWT as string,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const reducerMap: { [actionType: string]: (state: IState, action: IAction) => IState } = {
|
|
48
|
+
[ACTION_SET_ONBOARDING_MODAL_STATE]: applySetIsOnboardingAppActive,
|
|
49
|
+
[ACTION_ONBOARDING_APP_LOADED]: applySetSendJWTToApp,
|
|
50
|
+
[ACTION_SET_USER_ONBOARDING_JWT]: applySetUserOnboardingJWT,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const appReducer = (state = INITIAL_STATE, action: IAction) => {
|
|
54
|
+
if (!!action.type && !!reducerMap[action.type]) {
|
|
55
|
+
return reducerMap[action.type](state, action);
|
|
56
|
+
} else {
|
|
57
|
+
return state;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default appReducer;
|
package/src/styles.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {CSSProperties} from "react";
|
|
2
|
+
|
|
3
|
+
const MTE_SM = 540;
|
|
4
|
+
|
|
5
|
+
export const screenSizes: { id: string, size: number }[] = [
|
|
6
|
+
{
|
|
7
|
+
id: "MTE_SM",
|
|
8
|
+
size: MTE_SM,
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
export const minWidthMatcher = (): string => {
|
|
13
|
+
for (let i = 0; i < screenSizes.length; i++) {
|
|
14
|
+
const {id, size} = screenSizes[i];
|
|
15
|
+
if (window.matchMedia(`(min-width: ${size}px`).matches) {
|
|
16
|
+
return id
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return "ANY"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const containerStyle: CSSProperties = {
|
|
23
|
+
paddingRight: 0,
|
|
24
|
+
paddingLeft: 0,
|
|
25
|
+
marginRight: "auto",
|
|
26
|
+
marginLeft: "auto",
|
|
27
|
+
maxHeight: 882,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const containerMediaStyles: { [id: string]: CSSProperties } = {
|
|
31
|
+
MTE_SM: {
|
|
32
|
+
width: 540
|
|
33
|
+
},
|
|
34
|
+
ANY: {
|
|
35
|
+
width: "100%",
|
|
36
|
+
height: "100%",
|
|
37
|
+
maxWidth: "100%",
|
|
38
|
+
borderRadius: 0,
|
|
39
|
+
maxHeight: "100%"
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const appWrapperStyle: CSSProperties = {
|
|
44
|
+
position: "absolute",
|
|
45
|
+
zIndex: 999999,
|
|
46
|
+
display: "flex",
|
|
47
|
+
justifyContent: "center",
|
|
48
|
+
alignItems: "center",
|
|
49
|
+
flexDirection: "column",
|
|
50
|
+
width: "100vw",
|
|
51
|
+
height: "100vh",
|
|
52
|
+
background: "rgba(0,0,0,0.5)",
|
|
53
|
+
cursor: "pointer",
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const appWrapperHiddenStyle: CSSProperties = {
|
|
57
|
+
display: "none",
|
|
58
|
+
width: 0,
|
|
59
|
+
height: 0
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const modalStyle: CSSProperties = {
|
|
63
|
+
padding: 0,
|
|
64
|
+
borderRadius: 5,
|
|
65
|
+
backgroundColor: "#FFF",
|
|
66
|
+
height: "calc(100% - 100px)",
|
|
67
|
+
maxWidth: "calc(100% - 30px)",
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const iframeWrapperStyle: CSSProperties = {
|
|
71
|
+
width: "100%",
|
|
72
|
+
height: "100%",
|
|
73
|
+
border: "none",
|
|
74
|
+
overflow: "hidden"
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const iframeStyle: CSSProperties = {
|
|
78
|
+
width: "100%",
|
|
79
|
+
height: "100%",
|
|
80
|
+
border: "none",
|
|
81
|
+
margin: 0,
|
|
82
|
+
padding: 0,
|
|
83
|
+
overflow: "hidden",
|
|
84
|
+
borderRadius: "0 0 15px 15px"
|
|
85
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
zerohash: any
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* IInitializeParameters describes the parameters
|
|
10
|
+
* required for initializing the platform SDK
|
|
11
|
+
*/
|
|
12
|
+
export interface IInitializeParameters {
|
|
13
|
+
/**
|
|
14
|
+
* zeroHashOnboardingURL should be set to the URL of
|
|
15
|
+
* the webapp for onboarding users onto the ZeroHash
|
|
16
|
+
* platform.
|
|
17
|
+
*/
|
|
18
|
+
zeroHashOnboardingURL: string
|
|
19
|
+
/**
|
|
20
|
+
* rootQuerySelector is a query selector string that
|
|
21
|
+
* allows creating the ZeroHash UI subtree under a
|
|
22
|
+
* custom element.
|
|
23
|
+
*
|
|
24
|
+
* The default #zh-root query selector will be used
|
|
25
|
+
* instead otherwise. #zh-root will be a <div> appended
|
|
26
|
+
* as a child of the body in no specified order.
|
|
27
|
+
*/
|
|
28
|
+
rootQuerySelector?: string
|
|
29
|
+
/**
|
|
30
|
+
* userOnboardingJWT is the JWT that you received from
|
|
31
|
+
* the ZeroHash HTTP API that pertains to the customer
|
|
32
|
+
* to be onboarded.
|
|
33
|
+
*
|
|
34
|
+
* This is optional in the constructor and can be
|
|
35
|
+
* deferred to setUserOnboardingJWT() at a later time
|
|
36
|
+
* but must be provided before opening the onboarding
|
|
37
|
+
* UI in order for the onboarding UI to be loaded.
|
|
38
|
+
*/
|
|
39
|
+
userOnboardingJWT?: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* ISetUserOnboardingJWTParameters describes the
|
|
44
|
+
* parameters required for setting the user onboarding
|
|
45
|
+
* JWT.
|
|
46
|
+
*/
|
|
47
|
+
export interface ISetUserOnboardingJWTParameters {
|
|
48
|
+
/**
|
|
49
|
+
* userOnboardingJWT is the JWT that you received from
|
|
50
|
+
* the ZeroHash HTTP API that pertains to the customer
|
|
51
|
+
* to be onboarded.
|
|
52
|
+
*/
|
|
53
|
+
userOnboardingJWT: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface IOpenOnboardingModalParameters {
|
|
57
|
+
/**
|
|
58
|
+
* userOnboardingJWT is the JWT that you received from
|
|
59
|
+
* the ZeroHash HTTP API that pertains to the customer
|
|
60
|
+
* to be onboarded.
|
|
61
|
+
*/
|
|
62
|
+
userOnboardingJWT?: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* IncomingMessageType are enums of message types that
|
|
67
|
+
* are registered that the host can handle.
|
|
68
|
+
*/
|
|
69
|
+
export enum IncomingMessageType {
|
|
70
|
+
/**
|
|
71
|
+
* OnboardingAppLoaded is received when the onboarding
|
|
72
|
+
* app has initialized.
|
|
73
|
+
*/
|
|
74
|
+
OnboardingAppLoaded = "ONBOARDING_APP_LOADED",
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* OnboardingCloseButtonClicked is received when the close button
|
|
78
|
+
* has been clicked in the onboarding app
|
|
79
|
+
*/
|
|
80
|
+
OnboardingCloseButtonClicked = "ONBOARDING_CLOSE_BUTTON_CLICKED",
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* IncomingMessage defines the structure of an incoming
|
|
85
|
+
* message
|
|
86
|
+
*/
|
|
87
|
+
export interface IncomingMessage {
|
|
88
|
+
/**
|
|
89
|
+
* type is the type of message that is used for routing
|
|
90
|
+
* the request to the appropriate handler
|
|
91
|
+
*/
|
|
92
|
+
type: IncomingMessageType
|
|
93
|
+
/**
|
|
94
|
+
* payload is any freeform value that pertains to the
|
|
95
|
+
* request
|
|
96
|
+
*/
|
|
97
|
+
payload: unknown
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type IncomingMessageHandler = (payload: unknown) => void;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* IZeroHashSDK is the interface that you may use to interact with the ZeroHash SDK.
|
|
104
|
+
*/
|
|
105
|
+
export interface IZeroHashSDK {
|
|
106
|
+
setUserOnboardingJWT(params: ISetUserOnboardingJWTParameters): void
|
|
107
|
+
isOnboardingModalOpen(): boolean
|
|
108
|
+
openOnboardingModal(params: IOpenOnboardingModalParameters): void
|
|
109
|
+
closeOnboardingModal(): void
|
|
110
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* randomString generates a random string of specified
|
|
3
|
+
* @param length
|
|
4
|
+
* @param characters - charset to be used.
|
|
5
|
+
*/
|
|
6
|
+
export const randomString = (
|
|
7
|
+
length: number,
|
|
8
|
+
characters: string = 'abcdefghijklmnopqrstuvwxyz0123456789'
|
|
9
|
+
) => {
|
|
10
|
+
let result = '';
|
|
11
|
+
const charactersLength = characters.length;
|
|
12
|
+
let counter = 0;
|
|
13
|
+
while (counter < length) {
|
|
14
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
15
|
+
counter += 1;
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {randomString} from "./utils/strings";
|
|
2
|
+
import {DEFAULT_ROOT_ID_PREFIX} from "./constants";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* generateRootID attempts to create a dynamic ID to be
|
|
6
|
+
* used with a HTML element that is relatively unique
|
|
7
|
+
* and will not cause namespace conflicts.
|
|
8
|
+
*
|
|
9
|
+
* E.g. 'zerohash-49vt8y'
|
|
10
|
+
*/
|
|
11
|
+
export const generateRootID = () : string => {
|
|
12
|
+
return `${DEFAULT_ROOT_ID_PREFIX}-${randomString(6)}`
|
|
13
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true,
|
|
4
|
+
"target": "esnext",
|
|
5
|
+
"lib": [
|
|
6
|
+
"dom",
|
|
7
|
+
"dom.iterable",
|
|
8
|
+
"esnext"
|
|
9
|
+
],
|
|
10
|
+
"allowJs": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"allowSyntheticDefaultImports": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"noFallthroughCasesInSwitch": true,
|
|
17
|
+
"module": "esnext",
|
|
18
|
+
"moduleResolution": "node",
|
|
19
|
+
"resolveJsonModule": true,
|
|
20
|
+
"isolatedModules": true,
|
|
21
|
+
"jsx": "react-jsx"
|
|
22
|
+
},
|
|
23
|
+
"include": [
|
|
24
|
+
"src"
|
|
25
|
+
]
|
|
26
|
+
}
|