shogun-button-react 1.5.1 → 1.5.6
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/CHANGELOG.md +54 -0
- package/README.md +457 -103
- package/dist/components/ShogunButton.d.ts +17 -0
- package/dist/components/ShogunButton.js +97 -15
- package/dist/connector.js +17 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/plugins/GunAdvancedPlugin.d.ts +79 -0
- package/dist/plugins/GunAdvancedPlugin.js +498 -0
- package/dist/types/connector-options.d.ts +6 -0
- package/package.json +2 -2
|
@@ -2,6 +2,7 @@ import React from "react";
|
|
|
2
2
|
import { ShogunCore } from "shogun-core";
|
|
3
3
|
import { Observable } from "rxjs";
|
|
4
4
|
import "../styles/index.css";
|
|
5
|
+
import { GunAdvancedPlugin } from "../plugins/GunAdvancedPlugin";
|
|
5
6
|
type ShogunContextType = {
|
|
6
7
|
sdk: ShogunCore | null;
|
|
7
8
|
options: any;
|
|
@@ -16,6 +17,22 @@ type ShogunContextType = {
|
|
|
16
17
|
getPlugin: <T>(name: string) => T | undefined;
|
|
17
18
|
exportGunPair: (password?: string) => Promise<string>;
|
|
18
19
|
importGunPair: (pairData: string, password?: string) => Promise<boolean>;
|
|
20
|
+
gunPlugin: GunAdvancedPlugin | null;
|
|
21
|
+
useGunState: <T>(path: string, defaultValue?: T) => any;
|
|
22
|
+
useGunCollection: <T>(path: string, options?: any) => any;
|
|
23
|
+
useGunConnection: (path: string) => {
|
|
24
|
+
isConnected: boolean;
|
|
25
|
+
lastSeen: Date | null;
|
|
26
|
+
error: string | null;
|
|
27
|
+
};
|
|
28
|
+
useGunDebug: (path: string, enabled?: boolean) => void;
|
|
29
|
+
useGunRealtime: <T>(path: string, callback?: (data: T, key: string) => void) => {
|
|
30
|
+
data: T | null;
|
|
31
|
+
key: string | null;
|
|
32
|
+
};
|
|
33
|
+
put: (path: string, data: any) => Promise<void>;
|
|
34
|
+
get: (path: string) => any;
|
|
35
|
+
remove: (path: string) => Promise<void>;
|
|
19
36
|
};
|
|
20
37
|
export declare const useShogun: () => ShogunContextType;
|
|
21
38
|
type ShogunButtonProviderProps = {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React, { useContext, useState, createContext, useEffect, useRef, } from "react";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
3
|
import "../styles/index.css";
|
|
4
|
+
import { GunAdvancedPlugin } from "../plugins/GunAdvancedPlugin";
|
|
4
5
|
// Default context
|
|
5
6
|
const defaultShogunContext = {
|
|
6
7
|
sdk: null,
|
|
@@ -16,6 +17,15 @@ const defaultShogunContext = {
|
|
|
16
17
|
getPlugin: () => undefined,
|
|
17
18
|
exportGunPair: async () => "",
|
|
18
19
|
importGunPair: async () => false,
|
|
20
|
+
gunPlugin: null,
|
|
21
|
+
useGunState: () => ({}),
|
|
22
|
+
useGunCollection: () => ({}),
|
|
23
|
+
useGunConnection: () => ({ isConnected: false, lastSeen: null, error: null }),
|
|
24
|
+
useGunDebug: () => { },
|
|
25
|
+
useGunRealtime: () => ({ data: null, key: null }),
|
|
26
|
+
put: async () => { },
|
|
27
|
+
get: () => null,
|
|
28
|
+
remove: async () => { },
|
|
19
29
|
};
|
|
20
30
|
// Create context using React's createContext directly
|
|
21
31
|
const ShogunContext = createContext(defaultShogunContext);
|
|
@@ -330,22 +340,94 @@ export function ShogunButtonProvider({ children, sdk, options, onLoginSuccess, o
|
|
|
330
340
|
throw new Error(`Failed to import Gun pair: ${error.message}`);
|
|
331
341
|
}
|
|
332
342
|
};
|
|
343
|
+
// Inizializza il plugin
|
|
344
|
+
const gunPlugin = React.useMemo(() => {
|
|
345
|
+
if (!sdk)
|
|
346
|
+
return null;
|
|
347
|
+
return new GunAdvancedPlugin(sdk, {
|
|
348
|
+
enableDebug: options.enableGunDebug !== false,
|
|
349
|
+
enableConnectionMonitoring: options.enableConnectionMonitoring !== false,
|
|
350
|
+
defaultPageSize: options.defaultPageSize || 20,
|
|
351
|
+
connectionTimeout: options.connectionTimeout || 10000,
|
|
352
|
+
debounceInterval: options.debounceInterval || 100,
|
|
353
|
+
});
|
|
354
|
+
}, [sdk, options]);
|
|
355
|
+
// Effetto per pulizia del plugin
|
|
356
|
+
React.useEffect(() => {
|
|
357
|
+
return () => {
|
|
358
|
+
if (gunPlugin) {
|
|
359
|
+
gunPlugin.cleanup();
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}, [gunPlugin]);
|
|
363
|
+
// Crea gli hook del plugin
|
|
364
|
+
const pluginHooks = React.useMemo(() => {
|
|
365
|
+
if (!gunPlugin)
|
|
366
|
+
return {};
|
|
367
|
+
return gunPlugin.createHooks();
|
|
368
|
+
}, [gunPlugin]);
|
|
369
|
+
// Create a properly typed context value
|
|
370
|
+
const contextValue = React.useMemo(() => ({
|
|
371
|
+
sdk,
|
|
372
|
+
options,
|
|
373
|
+
isLoggedIn,
|
|
374
|
+
userPub,
|
|
375
|
+
username,
|
|
376
|
+
login,
|
|
377
|
+
signUp,
|
|
378
|
+
logout,
|
|
379
|
+
observe,
|
|
380
|
+
hasPlugin,
|
|
381
|
+
getPlugin,
|
|
382
|
+
exportGunPair,
|
|
383
|
+
importGunPair,
|
|
384
|
+
gunPlugin,
|
|
385
|
+
// Ensure all required hooks are present with proper fallbacks
|
|
386
|
+
useGunState: pluginHooks.useGunState || (() => ({
|
|
387
|
+
data: null,
|
|
388
|
+
isLoading: false,
|
|
389
|
+
error: null,
|
|
390
|
+
update: async () => { },
|
|
391
|
+
set: async () => { },
|
|
392
|
+
remove: async () => { },
|
|
393
|
+
refresh: () => { }
|
|
394
|
+
})),
|
|
395
|
+
useGunCollection: pluginHooks.useGunCollection || (() => ({
|
|
396
|
+
items: [],
|
|
397
|
+
currentPage: 0,
|
|
398
|
+
totalPages: 0,
|
|
399
|
+
hasNextPage: false,
|
|
400
|
+
hasPrevPage: false,
|
|
401
|
+
nextPage: () => { },
|
|
402
|
+
prevPage: () => { },
|
|
403
|
+
goToPage: () => { },
|
|
404
|
+
isLoading: false,
|
|
405
|
+
error: null,
|
|
406
|
+
refresh: () => { },
|
|
407
|
+
addItem: async () => { },
|
|
408
|
+
updateItem: async () => { },
|
|
409
|
+
removeItem: async () => { }
|
|
410
|
+
})),
|
|
411
|
+
useGunConnection: pluginHooks.useGunConnection || (() => ({
|
|
412
|
+
isConnected: false,
|
|
413
|
+
lastSeen: null,
|
|
414
|
+
error: null
|
|
415
|
+
})),
|
|
416
|
+
useGunDebug: pluginHooks.useGunDebug || (() => { }),
|
|
417
|
+
useGunRealtime: pluginHooks.useGunRealtime || (() => ({
|
|
418
|
+
data: null,
|
|
419
|
+
key: null
|
|
420
|
+
})),
|
|
421
|
+
put: (gunPlugin === null || gunPlugin === void 0 ? void 0 : gunPlugin.put.bind(gunPlugin)) || (async () => { }),
|
|
422
|
+
get: (gunPlugin === null || gunPlugin === void 0 ? void 0 : gunPlugin.get.bind(gunPlugin)) || (() => null),
|
|
423
|
+
remove: (gunPlugin === null || gunPlugin === void 0 ? void 0 : gunPlugin.remove.bind(gunPlugin)) || (async () => { }),
|
|
424
|
+
}), [
|
|
425
|
+
sdk, options, isLoggedIn, userPub, username, login, signUp, logout,
|
|
426
|
+
observe, hasPlugin, getPlugin, exportGunPair, importGunPair,
|
|
427
|
+
gunPlugin, pluginHooks
|
|
428
|
+
]);
|
|
333
429
|
// Provide the context value to children
|
|
334
|
-
return (React.createElement(ShogunContext.Provider, { value:
|
|
335
|
-
sdk,
|
|
336
|
-
options,
|
|
337
|
-
isLoggedIn,
|
|
338
|
-
userPub,
|
|
339
|
-
username,
|
|
340
|
-
login,
|
|
341
|
-
signUp,
|
|
342
|
-
logout,
|
|
343
|
-
observe,
|
|
344
|
-
hasPlugin,
|
|
345
|
-
getPlugin,
|
|
346
|
-
exportGunPair,
|
|
347
|
-
importGunPair,
|
|
348
|
-
} }, children));
|
|
430
|
+
return (React.createElement(ShogunContext.Provider, { value: contextValue }, children));
|
|
349
431
|
}
|
|
350
432
|
// SVG Icons Components
|
|
351
433
|
const WalletIcon = () => (React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" },
|
package/dist/connector.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ShogunCore } from "shogun-core";
|
|
2
|
+
import { GunAdvancedPlugin } from "./plugins/GunAdvancedPlugin";
|
|
2
3
|
export function shogunConnector(options) {
|
|
3
|
-
const { peers = ["https://gun-manhattan.herokuapp.com/gun"], appName, logging, timeouts, oauth,
|
|
4
|
+
const { peers = ["https://gun-manhattan.herokuapp.com/gun"], appName, logging, timeouts, oauth,
|
|
5
|
+
// Nuove opzioni per il plugin
|
|
6
|
+
enableGunDebug = true, enableConnectionMonitoring = true, defaultPageSize = 20, connectionTimeout = 10000, debounceInterval = 100, ...restOptions } = options;
|
|
4
7
|
const sdk = new ShogunCore({
|
|
5
8
|
peers,
|
|
6
9
|
scope: appName,
|
|
@@ -24,10 +27,23 @@ export function shogunConnector(options) {
|
|
|
24
27
|
const hasPlugin = (name) => {
|
|
25
28
|
return sdk ? sdk.hasPlugin(name) : false;
|
|
26
29
|
};
|
|
30
|
+
// Registra automaticamente il plugin Gun avanzato
|
|
31
|
+
let gunPlugin = null;
|
|
32
|
+
if (sdk) {
|
|
33
|
+
gunPlugin = new GunAdvancedPlugin(sdk, {
|
|
34
|
+
enableDebug: enableGunDebug,
|
|
35
|
+
enableConnectionMonitoring,
|
|
36
|
+
defaultPageSize,
|
|
37
|
+
connectionTimeout,
|
|
38
|
+
debounceInterval,
|
|
39
|
+
});
|
|
40
|
+
registerPlugin(gunPlugin);
|
|
41
|
+
}
|
|
27
42
|
return {
|
|
28
43
|
sdk,
|
|
29
44
|
options,
|
|
30
45
|
registerPlugin,
|
|
31
46
|
hasPlugin,
|
|
47
|
+
gunPlugin, // Esporta il plugin per uso esterno
|
|
32
48
|
};
|
|
33
49
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ShogunButton, ShogunButtonProvider, useShogun } from './components/ShogunButton.js';
|
|
2
2
|
import { ShogunConnectorOptions, ShogunConnectorResult } from './types/connector-options.js';
|
|
3
3
|
import { shogunConnector } from './connector.js';
|
|
4
|
+
import { GunAdvancedPlugin } from './plugins/GunAdvancedPlugin.js';
|
|
4
5
|
export { ShogunButton, ShogunButtonProvider, useShogun };
|
|
5
6
|
export { shogunConnector };
|
|
7
|
+
export { GunAdvancedPlugin };
|
|
6
8
|
export { ShogunConnectorOptions, ShogunConnectorResult };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ShogunButton, ShogunButtonProvider, useShogun } from './components/ShogunButton.js';
|
|
2
2
|
import { shogunConnector } from './connector.js';
|
|
3
|
+
import { GunAdvancedPlugin } from './plugins/GunAdvancedPlugin.js';
|
|
3
4
|
// Export components
|
|
4
5
|
export { ShogunButton, ShogunButtonProvider, useShogun };
|
|
5
6
|
// Export connector function
|
|
6
7
|
export { shogunConnector };
|
|
8
|
+
// Export plugin
|
|
9
|
+
export { GunAdvancedPlugin };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ShogunCore } from "shogun-core";
|
|
2
|
+
export interface GunAdvancedPluginConfig {
|
|
3
|
+
enableDebug?: boolean;
|
|
4
|
+
enableConnectionMonitoring?: boolean;
|
|
5
|
+
defaultPageSize?: number;
|
|
6
|
+
connectionTimeout?: number;
|
|
7
|
+
debounceInterval?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface GunCollectionOptions<T> {
|
|
10
|
+
pageSize?: number;
|
|
11
|
+
sortBy?: keyof T | ((a: T, b: T) => number);
|
|
12
|
+
sortOrder?: 'asc' | 'desc';
|
|
13
|
+
filter?: (item: T) => boolean;
|
|
14
|
+
enableRealtime?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface GunCollectionResult<T> {
|
|
17
|
+
items: T[];
|
|
18
|
+
currentPage: number;
|
|
19
|
+
totalPages: number;
|
|
20
|
+
hasNextPage: boolean;
|
|
21
|
+
hasPrevPage: boolean;
|
|
22
|
+
nextPage: () => void;
|
|
23
|
+
prevPage: () => void;
|
|
24
|
+
goToPage: (page: number) => void;
|
|
25
|
+
isLoading: boolean;
|
|
26
|
+
error: string | null;
|
|
27
|
+
refresh: () => void;
|
|
28
|
+
addItem: (item: T) => Promise<void>;
|
|
29
|
+
updateItem: (id: string, updates: Partial<T>) => Promise<void>;
|
|
30
|
+
removeItem: (id: string) => Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
export interface GunStateResult<T> {
|
|
33
|
+
data: T | null;
|
|
34
|
+
isLoading: boolean;
|
|
35
|
+
error: string | null;
|
|
36
|
+
update: (updates: Partial<T>) => Promise<void>;
|
|
37
|
+
set: (data: T) => Promise<void>;
|
|
38
|
+
remove: () => Promise<void>;
|
|
39
|
+
refresh: () => void;
|
|
40
|
+
}
|
|
41
|
+
export declare class GunAdvancedPlugin {
|
|
42
|
+
private sdk;
|
|
43
|
+
private config;
|
|
44
|
+
private debugEnabled;
|
|
45
|
+
private connectionMonitors;
|
|
46
|
+
private collectionCache;
|
|
47
|
+
constructor(sdk: ShogunCore, config?: GunAdvancedPluginConfig);
|
|
48
|
+
setDebugEnabled(enabled: boolean): void;
|
|
49
|
+
private log;
|
|
50
|
+
createHooks(): {
|
|
51
|
+
useGunState: any;
|
|
52
|
+
useGunCollection: any;
|
|
53
|
+
useGunConnection: any;
|
|
54
|
+
useGunDebug: any;
|
|
55
|
+
useGunRealtime: any;
|
|
56
|
+
};
|
|
57
|
+
useGunState<T>(path: string, defaultValue?: T): GunStateResult<T>;
|
|
58
|
+
useGunCollection<T>(path: string, options?: GunCollectionOptions<T>): GunCollectionResult<T>;
|
|
59
|
+
useGunConnection(path: string): {
|
|
60
|
+
isConnected: boolean;
|
|
61
|
+
lastSeen: Date;
|
|
62
|
+
error: string;
|
|
63
|
+
};
|
|
64
|
+
useGunDebug(path: string, enabled?: boolean): void;
|
|
65
|
+
useGunRealtime<T>(path: string, callback?: (data: T, key: string) => void): {
|
|
66
|
+
data: T;
|
|
67
|
+
key: string;
|
|
68
|
+
};
|
|
69
|
+
put(path: string, data: any): Promise<void>;
|
|
70
|
+
get(path: string): any;
|
|
71
|
+
remove(path: string): Promise<void>;
|
|
72
|
+
cleanup(): void;
|
|
73
|
+
getStats(): {
|
|
74
|
+
activeConnections: number;
|
|
75
|
+
cachedCollections: number;
|
|
76
|
+
debugEnabled: boolean;
|
|
77
|
+
config: GunAdvancedPluginConfig;
|
|
78
|
+
};
|
|
79
|
+
}
|