shogun-button-react 3.0.17 → 3.0.19

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.
@@ -0,0 +1,41 @@
1
+ import React from 'react';
2
+ interface RainbowKitShogunButtonProps {
3
+ /**
4
+ * Testo del pulsante quando non connesso
5
+ */
6
+ connectText?: string;
7
+ /**
8
+ * Testo del pulsante quando connesso ma non autenticato
9
+ */
10
+ loginText?: string;
11
+ /**
12
+ * Testo del pulsante quando connesso e autenticato
13
+ */
14
+ connectedText?: string;
15
+ /**
16
+ * Modalità: 'login' per login, 'signup' per registrazione, 'auto' per automatico
17
+ */
18
+ mode?: 'login' | 'signup' | 'auto';
19
+ /**
20
+ * Callback chiamato quando l'autenticazione ha successo
21
+ */
22
+ onSuccess?: (data: any) => void;
23
+ /**
24
+ * Callback chiamato quando si verifica un errore
25
+ */
26
+ onError?: (error: string) => void;
27
+ /**
28
+ * Stile personalizzato per il pulsante
29
+ */
30
+ className?: string;
31
+ /**
32
+ * Se true, mostra l'indirizzo del wallet quando connesso
33
+ */
34
+ showAddress?: boolean;
35
+ }
36
+ /**
37
+ * Componente che integra RainbowKit con Shogun
38
+ * Gestisce automaticamente la connessione wallet e l'autenticazione
39
+ */
40
+ export declare const RainbowKitShogunButton: React.FC<RainbowKitShogunButtonProps>;
41
+ export {};
@@ -0,0 +1,122 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import { useRainbowKitShogun } from '../hooks/useRainbowKitShogun';
3
+ /**
4
+ * Componente che integra RainbowKit con Shogun
5
+ * Gestisce automaticamente la connessione wallet e l'autenticazione
6
+ */
7
+ export const RainbowKitShogunButton = ({ connectText = "Connect Wallet", loginText = "Login with Wallet", connectedText = "Connected", mode = 'auto', onSuccess, onError, className = "shogun-connect-button", showAddress = true, }) => {
8
+ const { address, isConnected, isLoggedIn, connectAndLogin, connectAndSignUp, isReady, } = useRainbowKitShogun();
9
+ const [isLoading, setIsLoading] = useState(false);
10
+ const [error, setError] = useState(null);
11
+ // Gestisce automaticamente l'autenticazione quando l'indirizzo cambia
12
+ useEffect(() => {
13
+ if (isConnected && address && !isLoggedIn && mode === 'auto') {
14
+ handleAutoAuth();
15
+ }
16
+ }, [isConnected, address, isLoggedIn, mode]);
17
+ const handleAutoAuth = async () => {
18
+ var _a;
19
+ if (!address)
20
+ return;
21
+ setIsLoading(true);
22
+ setError(null);
23
+ try {
24
+ // Prova prima il login, se fallisce prova la registrazione
25
+ let result = await connectAndLogin();
26
+ if (!result.success && ((_a = result.error) === null || _a === void 0 ? void 0 : _a.includes('not found'))) {
27
+ // Se l'utente non esiste, prova la registrazione
28
+ result = await connectAndSignUp();
29
+ }
30
+ if (result.success) {
31
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(result);
32
+ }
33
+ else if (result.error) {
34
+ setError(result.error);
35
+ onError === null || onError === void 0 ? void 0 : onError(result.error);
36
+ }
37
+ }
38
+ catch (err) {
39
+ const errorMsg = err.message || "Authentication failed";
40
+ setError(errorMsg);
41
+ onError === null || onError === void 0 ? void 0 : onError(errorMsg);
42
+ }
43
+ finally {
44
+ setIsLoading(false);
45
+ }
46
+ };
47
+ const handleClick = async () => {
48
+ var _a;
49
+ if (!isReady) {
50
+ setError("RainbowKit or Shogun not ready");
51
+ return;
52
+ }
53
+ if (!isConnected) {
54
+ // Apri il modal di connessione RainbowKit
55
+ connectAndLogin();
56
+ return;
57
+ }
58
+ if (isLoggedIn) {
59
+ // Già autenticato, non fare nulla
60
+ return;
61
+ }
62
+ // Connesso ma non autenticato, procedi con l'autenticazione
63
+ setIsLoading(true);
64
+ setError(null);
65
+ try {
66
+ let result;
67
+ if (mode === 'login') {
68
+ result = await connectAndLogin();
69
+ }
70
+ else if (mode === 'signup') {
71
+ result = await connectAndSignUp();
72
+ }
73
+ else {
74
+ // Modalità auto: prova login, se fallisce prova signup
75
+ result = await connectAndLogin();
76
+ if (!result.success && ((_a = result.error) === null || _a === void 0 ? void 0 : _a.includes('not found'))) {
77
+ result = await connectAndSignUp();
78
+ }
79
+ }
80
+ if (result.success) {
81
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(result);
82
+ }
83
+ else if (result.error) {
84
+ setError(result.error);
85
+ onError === null || onError === void 0 ? void 0 : onError(result.error);
86
+ }
87
+ }
88
+ catch (err) {
89
+ const errorMsg = err.message || "Authentication failed";
90
+ setError(errorMsg);
91
+ onError === null || onError === void 0 ? void 0 : onError(errorMsg);
92
+ }
93
+ finally {
94
+ setIsLoading(false);
95
+ }
96
+ };
97
+ const getButtonText = () => {
98
+ if (isLoading)
99
+ return "Connecting...";
100
+ if (isLoggedIn)
101
+ return showAddress && address ? `${connectedText} (${address.slice(0, 6)}...${address.slice(-4)})` : connectedText;
102
+ if (isConnected)
103
+ return loginText;
104
+ return connectText;
105
+ };
106
+ const getButtonClass = () => {
107
+ let baseClass = className;
108
+ if (isLoggedIn)
109
+ baseClass += " shogun-connected";
110
+ if (isLoading)
111
+ baseClass += " shogun-loading";
112
+ if (error)
113
+ baseClass += " shogun-error";
114
+ return baseClass;
115
+ };
116
+ if (!isReady) {
117
+ return (React.createElement("button", { className: className, disabled: true }, "RainbowKit not available"));
118
+ }
119
+ return (React.createElement("div", { className: "rainbowkit-shogun-container" },
120
+ React.createElement("button", { className: getButtonClass(), onClick: handleClick, disabled: isLoading }, getButtonText()),
121
+ error && (React.createElement("div", { className: "shogun-error-message", style: { marginTop: '8px' } }, error))));
122
+ };
@@ -0,0 +1,16 @@
1
+ import React from 'react';
2
+ import { ShogunConnectorOptions } from '../interfaces/connector-options';
3
+ interface ShogunButtonWithRainbowKitProps {
4
+ core: any;
5
+ options: ShogunConnectorOptions;
6
+ onLoginSuccess?: (data: any) => void;
7
+ onSignupSuccess?: (data: any) => void;
8
+ onError?: (error: string) => void;
9
+ }
10
+ /**
11
+ * Componente wrapper che integra RainbowKit con ShogunButton
12
+ * Quando RainbowKit è disponibile, il pulsante MetaMask utilizzerà RainbowKit
13
+ * invece della connessione diretta a MetaMask
14
+ */
15
+ export declare const ShogunButtonWithRainbowKit: React.FC<ShogunButtonWithRainbowKitProps>;
16
+ export {};
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ import { ShogunButton, ShogunButtonProvider } from './ShogunButton';
3
+ import { useRainbowKitShogun } from '../hooks/useRainbowKitShogun';
4
+ /**
5
+ * Componente wrapper che integra RainbowKit con ShogunButton
6
+ * Quando RainbowKit è disponibile, il pulsante MetaMask utilizzerà RainbowKit
7
+ * invece della connessione diretta a MetaMask
8
+ */
9
+ export const ShogunButtonWithRainbowKit = ({ core, options, onLoginSuccess, onSignupSuccess, onError, }) => {
10
+ const rainbowKit = useRainbowKitShogun();
11
+ // Merge RainbowKit integration with options
12
+ const enhancedOptions = {
13
+ ...options,
14
+ rainbowKitIntegration: rainbowKit.isReady ? {
15
+ openConnectModal: rainbowKit.openConnectModal,
16
+ openAccountModal: rainbowKit.openAccountModal,
17
+ openChainModal: rainbowKit.openChainModal,
18
+ isConnected: rainbowKit.isConnected,
19
+ address: rainbowKit.address,
20
+ } : undefined,
21
+ };
22
+ return (React.createElement(ShogunButtonProvider, { core: core, options: enhancedOptions, onLoginSuccess: onLoginSuccess, onSignupSuccess: onSignupSuccess, onError: onError },
23
+ React.createElement(ShogunButton, null)));
24
+ };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Hook personalizzato per integrare RainbowKit con Shogun
3
+ * Questo hook gestisce la connessione wallet tramite RainbowKit
4
+ * e l'autenticazione tramite Shogun utilizzando il ShogunButton esistente
5
+ */
6
+ export declare const useRainbowKitShogun: () => {
7
+ address: any;
8
+ isConnected: any;
9
+ openConnectModal: any;
10
+ openAccountModal: any;
11
+ openChainModal: any;
12
+ isLoggedIn: boolean;
13
+ connectAndLogin: () => Promise<any>;
14
+ connectAndSignUp: () => Promise<any>;
15
+ loginWithAddress: (walletAddress: string) => Promise<any>;
16
+ signUpWithAddress: (walletAddress: string) => Promise<any>;
17
+ isReady: boolean;
18
+ };
@@ -0,0 +1,120 @@
1
+ import { useConnectModal, useAccountModal, useChainModal } from '@rainbow-me/rainbowkit';
2
+ import { useAccount } from 'wagmi';
3
+ import { useShogun } from '../components/ShogunButton';
4
+ /**
5
+ * Hook personalizzato per integrare RainbowKit con Shogun
6
+ * Questo hook gestisce la connessione wallet tramite RainbowKit
7
+ * e l'autenticazione tramite Shogun utilizzando il ShogunButton esistente
8
+ */
9
+ export const useRainbowKitShogun = () => {
10
+ const { openConnectModal } = useConnectModal();
11
+ const { openAccountModal } = useAccountModal();
12
+ const { openChainModal } = useChainModal();
13
+ const { address, isConnected } = useAccount();
14
+ const { login, signUp, core, isLoggedIn } = useShogun();
15
+ /**
16
+ * Connette il wallet tramite RainbowKit e autentica con Shogun
17
+ */
18
+ const connectAndLogin = async () => {
19
+ try {
20
+ // Se non c'è connessione, apri il modal di RainbowKit
21
+ if (!isConnected) {
22
+ openConnectModal === null || openConnectModal === void 0 ? void 0 : openConnectModal();
23
+ return { success: false, needsConnection: true };
24
+ }
25
+ // Se c'è già una connessione, procedi con l'autenticazione Shogun
26
+ if (address) {
27
+ const result = await login("web3", address);
28
+ return result;
29
+ }
30
+ throw new Error("No address available");
31
+ }
32
+ catch (error) {
33
+ console.error("RainbowKit Shogun connection failed:", error);
34
+ return {
35
+ success: false,
36
+ error: error.message || "Connection failed"
37
+ };
38
+ }
39
+ };
40
+ /**
41
+ * Registra un nuovo utente tramite RainbowKit e Shogun
42
+ */
43
+ const connectAndSignUp = async () => {
44
+ try {
45
+ // Se non c'è connessione, apri il modal di RainbowKit
46
+ if (!isConnected) {
47
+ openConnectModal === null || openConnectModal === void 0 ? void 0 : openConnectModal();
48
+ return { success: false, needsConnection: true };
49
+ }
50
+ // Se c'è già una connessione, procedi con la registrazione Shogun
51
+ if (address) {
52
+ const result = await signUp("web3", address);
53
+ return result;
54
+ }
55
+ throw new Error("No address available");
56
+ }
57
+ catch (error) {
58
+ console.error("RainbowKit Shogun signup failed:", error);
59
+ return {
60
+ success: false,
61
+ error: error.message || "Signup failed"
62
+ };
63
+ }
64
+ };
65
+ /**
66
+ * Autentica direttamente con un indirizzo (utile quando l'indirizzo è già noto)
67
+ */
68
+ const loginWithAddress = async (walletAddress) => {
69
+ try {
70
+ if (!walletAddress) {
71
+ throw new Error("Address is required");
72
+ }
73
+ const result = await login("web3", walletAddress);
74
+ return result;
75
+ }
76
+ catch (error) {
77
+ console.error("Login with address failed:", error);
78
+ return {
79
+ success: false,
80
+ error: error.message || "Login failed"
81
+ };
82
+ }
83
+ };
84
+ /**
85
+ * Registra direttamente con un indirizzo (utile quando l'indirizzo è già noto)
86
+ */
87
+ const signUpWithAddress = async (walletAddress) => {
88
+ try {
89
+ if (!walletAddress) {
90
+ throw new Error("Address is required");
91
+ }
92
+ const result = await signUp("web3", walletAddress);
93
+ return result;
94
+ }
95
+ catch (error) {
96
+ console.error("Signup with address failed:", error);
97
+ return {
98
+ success: false,
99
+ error: error.message || "Signup failed"
100
+ };
101
+ }
102
+ };
103
+ return {
104
+ // RainbowKit state
105
+ address,
106
+ isConnected,
107
+ openConnectModal,
108
+ openAccountModal,
109
+ openChainModal,
110
+ // Shogun state
111
+ isLoggedIn,
112
+ // Actions
113
+ connectAndLogin,
114
+ connectAndSignUp,
115
+ loginWithAddress,
116
+ signUpWithAddress,
117
+ // Utilities
118
+ isReady: !!core && !!openConnectModal,
119
+ };
120
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "shogun-button-react",
3
3
  "description": "Shogun connector button",
4
- "version": "3.0.17",
4
+ "version": "3.0.19",
5
5
  "files": [
6
6
  "dist",
7
7
  "src/styles/index.css"
@@ -34,7 +34,7 @@
34
34
  "ethers": "^6.13.5",
35
35
  "prettier": "^3.5.3",
36
36
  "rxjs": "^7.8.1",
37
- "shogun-core": "^3.0.18"
37
+ "shogun-core": "^3.0.19"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": "^18.0.0",