Skip to content

Commit d02e9ca

Browse files
committed
added pairing and permission handling
1 parent 0cbb55b commit d02e9ca

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

index.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { InitData, FormField, FieldValue } from 'global-input-message';
2+
import { InitData, FormField, FieldValue, Sender, PermissionRequestMessage } from 'global-input-message';
33
export * from 'global-input-message';
44
export function useGlobalInputApp(config: ConfigData | (() => ConfigData)): GlobalInputData;
55

@@ -13,9 +13,15 @@ export interface ConfigData {
1313
}
1414
export interface ConnectOptions {
1515
apikey?: string;
16+
url?: string;
1617
securityGroup?: string;
1718
client?: string;
18-
url?: string;
19+
onRegistered?: (connectionCode: string) => void;
20+
onRegisterFailed?: () => void;
21+
onSenderConnected?: (sender: Sender, senders: Sender[]) => void;
22+
onSenderDisconnected?: (sender: Sender, senders: Sender[]) => void;
23+
onInputPermission?: (permissionMessage: PermissionRequestMessage, senders: Sender[], deny: (reason?: string) => void) => void;
24+
onError?: (message: string) => void;
1925
}
2026
interface FieldChanged {
2127
field: FormField;
@@ -38,6 +44,7 @@ type ConnectQRProps = {
3844
};
3945
export interface GlobalInputData {
4046
ConnectQR: React.FC<ConnectQRProps>,
47+
PairingQR: React.FC<ConnectQRProps>,
4148
connectionCode: string;
4249
field: FormField;
4350
errorMessage: string;
@@ -47,6 +54,7 @@ export interface GlobalInputData {
4754
isDisconnected: boolean;
4855
isConnected: boolean;
4956
initData: InitData;
57+
senders: Sender[];
5058
sendValue: SendValueFunction;
5159
sendInitData: SendInitDataFunction;
5260
setOnchange: (onchange: OnchangeFunction) => void;

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "global-input-react",
3-
"version": "4.2.3",
3+
"version": "4.3.0",
44
"description": "global input react component",
55
"main": "./dist/index",
66
"repository": {
@@ -69,7 +69,7 @@
6969
"webpack": "^5.4.0"
7070
},
7171
"dependencies": {
72-
"global-input-message": "^2.0.9",
72+
"global-input-message": "^2.1.0",
7373
"qrcode.react": "^1.0.0"
7474
}
7575
}

src/globalinput.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const initialState = {
3434
isDisconnected: false,
3535
isConnected: false,
3636
initData: null,
37+
connected: []
3738
};
3839

3940

@@ -43,8 +44,8 @@ const mobileData = {
4344
fields: [],
4445
values: [],
4546
setters: [],
46-
clients: [],
47-
client: null,
47+
senders: [],
48+
sender: null,
4849
mobileConfig: null
4950
};
5051

@@ -180,39 +181,41 @@ const buildMobileConfig = (initData, options, notify) => {
180181
notify({ type: ACTION_TYPES.REGISTER_FAILED, errorMessage });
181182

182183
},
183-
onSenderConnected: (client, clients) => {
184+
onSenderConnected: (sender, senders) => {
184185
mobileData.mobileState = MobileState.MOBILE_CONNECTED;
185-
mobileData.clients = clients;
186-
mobileData.client = client;
187-
options && options.onSenderConnected && options.onSenderConnected(client, clients);
186+
mobileData.senders = senders;
187+
mobileData.sender = sender;
188+
options && options.onSenderConnected && options.onSenderConnected(sender, senders);
188189
notify({ type: ACTION_TYPES.SENDER_CONNECTED });
189190
},
190-
onSenderDisconnected: (client, clients) => {
191-
mobileData.clients = clients;
192-
mobileData.client = client;
193-
if (clients && clients.length) {
194-
console.log("-multi-client-");
191+
onSenderDisconnected: (sender, senders) => {
192+
mobileData.senders = senders;
193+
mobileData.sender = sender;
194+
if (senders && senders.length) {
195+
console.log("-multi-senders-");
195196
}
196197
else {
197198
mobileData.mobileState = MobileState.INITIALIZING;
198199
mobileData.session.connect(mobileData.mobileConfig);
199-
console.log("-client-disconnected-");
200+
console.log("-sender-disconnected-");
200201
}
201-
options && options.onSenderDisconnected && options.onSenderDisconnected(client, clients);
202+
options && options.onSenderDisconnected && options.onSenderDisconnected(sender, senders);
202203
notify({ type: ACTION_TYPES.SENDER_DISCONNECTED });
203204

204205
},
206+
onInputPermission: (permissionMessage, senders, deny) => {
207+
if (options && options.onInputPermission) {
208+
options.onInputPermission(permissionMessage, senders, deny);
209+
}
210+
},
205211
onError: errorMessage => {
206212
closeConnection();
207213
mobileData.mobileState = MobileState.ERROR;
208214
notify({ type: ACTION_TYPES.CONNECTION_ERROR, errorMessage });
209215
},
210216
url: options && options.url,
211217
apikey: options && options.apikey,
212-
securityGroup: options && options.securityGroup,
213-
aes: options && options.aes,
214-
onInput: options && options.onInput,
215-
onInputPermissionResult: options && options.onInputPermissionResult
218+
securityGroup: options && options.securityGroup
216219
};
217220
};
218221

@@ -242,14 +245,16 @@ export const startConnect = (config, configId, notify) => {
242245
if (!mobileData.session) {
243246
mobileData.session = createMessageConnector();
244247
}
245-
if(config.codeAES){
248+
if (config.codeAES) {
246249
mobileData.session.setCodeAES(config.codeAES);
247250
}
248251
mobileData.session.connect(mobileData.mobileConfig);
249252
notify({ type: ACTION_TYPES.START_CONNECT });
250253
};
251254

252-
255+
export const getParingCode = () => {
256+
return mobileData.session && mobileData.session.buildPairingData();
257+
};
253258

254259

255260
export const reducer = (state, action) => {
@@ -281,7 +286,8 @@ export const reducer = (state, action) => {
281286
isError: mobileData.mobileState === MobileState.ERROR,
282287
isDisconnected: mobileData.mobileState === MobileState.DISCONNECTED,
283288
isConnected: mobileData.mobileState === MobileState.MOBILE_CONNECTED,
284-
initData: mobileData.mobileConfig && mobileData.mobileConfig.initData
289+
initData: mobileData.mobileConfig && mobileData.mobileConfig.initData,
290+
senders: mobileData.senders
285291
};
286292
};
287293

@@ -307,22 +313,22 @@ export const qrCodeLabel = (
307313
</div>
308314
);
309315

310-
export const displayQRCode = (connectionCode, level, size, label, maxSize, marginTop, marginLeft) => {
311-
if ((!connectionCode) || size === 0) {
316+
export const displayQRCode = (codeContent, level, size, label, maxSize, marginTop, marginLeft) => {
317+
if ((!codeContent) || size === 0) {
312318
return null;
313319
}
314320
if (size) {
315321
return (
316322
<>
317-
<QRCode value={connectionCode} level={level} size={size} />
323+
<QRCode value={codeContent} level={level} size={size} />
318324
{label}
319325
</>
320326
);
321327
}
322328
else {
323329
return (
324330
<>
325-
<ResizeQRCode value={connectionCode} level={level} maxSize={maxSize} marginTop={marginTop} marginLeft={marginLeft} />
331+
<ResizeQRCode value={codeContent} level={level} maxSize={maxSize} marginTop={marginTop} marginLeft={marginLeft} />
326332
{label}
327333
</>
328334
);

src/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const useGlobalInputApp = (config) => {
1313
isDisconnected,
1414
isConnected,
1515
initData,
16+
senders
1617
}, dispatch] = useReducer(globalInput.reducer, globalInput.initialState);
1718

1819
const attached = useRef(true);
@@ -102,11 +103,24 @@ export const useGlobalInputApp = (config) => {
102103
}
103104
return globalInput.displayQRCode(connectionCode, level, size, label, maxSize, marginTop, marginLeft);
104105
}, [connectionCode, isReady, isLoading]);
105-
106+
const PairingQR = useCallback(({ level = 'H', size, label = globalInput.qrCodeLabel, loading = globalInput.loading, maxSize = 400, marginTop = 90, marginLeft = 10 }) => {
107+
if (isLoading) {
108+
return loading;
109+
}
110+
if (!isReady) {
111+
return null;
112+
}
113+
const pairingCode = globalInput.getParingCode();
114+
if ((!pairingCode) || size === 0) {
115+
return null;
116+
}
117+
return globalInput.displayQRCode(pairingCode, level, size, label, maxSize, marginTop, marginLeft);
118+
}, [isReady, isLoading]);
106119

107120

108121
return {
109122
ConnectQR,
123+
PairingQR,
110124
connectionCode,
111125
field,
112126
errorMessage,
@@ -116,6 +130,7 @@ export const useGlobalInputApp = (config) => {
116130
isDisconnected,
117131
isConnected,
118132
initData,
133+
senders,
119134
sendValue,
120135
sendInitData,
121136
setOnchange,

0 commit comments

Comments
 (0)