-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
197 lines (176 loc) · 6.6 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import './App.css';
import {TonConnectButton} from '@tonconnect/ui-react';
import {useMainContract} from './hooks/useMainContract';
import {useTonConnect} from './hooks/useTonConnect';
import {fromNano} from 'ton-core';
import {useCallback, useEffect, useState} from 'react';
import WebApp from '@twa-dev/sdk';
function App() {
const {
contract_address,
counter_value,
recent_sender,
owner_address,
contract_balance,
sendIncrement,
sendDeposit,
sendWithdrawal,
} = useMainContract();
const {tonConnectUI} = useTonConnect();
const [connected, setConnected] = useState<boolean>(false);
const [platform, setPlatform] = useState<string | null>(null);
const [code, setCode] = useState<string | null>(null);
const [color, setColor] = useState<string | null>(null);
const userPlatform = useCallback(() => {
setPlatform(WebApp.platform === "unknown" ? null : WebApp.platform);
}, []);
const showPlatformInAlert = useCallback(() => {
if (platform) {
WebApp.showAlert(platform);
}
}, [platform]);
const softHaptic = useCallback(() => {
WebApp.HapticFeedback.impactOccurred("soft");
}, []);
const successHaptic = useCallback(() => {
WebApp.HapticFeedback.notificationOccurred("success");
}, []);
const openScan = useCallback(() => {
WebApp.showScanQrPopup({text: "Scan some QR code"}, (code) => {
setCode(code);
WebApp.closeScanQrPopup();
});
}, []);
const userColor = useCallback(() => {
setColor(WebApp.colorScheme);
}, []);
const showColorAlert = useCallback(() => {
if (color) {
WebApp.showAlert(color);
}
}, [color]);
useEffect(() => {
WebApp.expand();
userPlatform();
userColor();
setConnected(tonConnectUI.connected);
tonConnectUI.onStatusChange((status) => {
setConnected(status !== null);
});
}, [tonConnectUI, userPlatform]);
return (
<div>
<div className="container">
<div className="button-container">
<h3>Contract Data:</h3>
<TonConnectButton/>
</div>
<div className="data-container">
<b>Our contract Address:</b>
<p>{contract_address}</p>
<hr/>
<b>Our contract Owner:</b>
<p>{owner_address?.toString()}</p>
<hr/>
{contract_balance && (
<>
<b>Our contract Balance:</b>
<p>{fromNano(contract_balance)}</p>
<hr/>
</>
)}
{recent_sender && (
<>
<b>Recent sender:</b>
<p>{recent_sender.toString()}</p>
<hr/>
</>
)}
<div>
<b>Counter Value:</b>
<p>{counter_value ?? "Loading..."}</p>
<hr/>
</div>
</div>
<h3>App actions: </h3>
<div className="data-container">
{platform && (
<>
<div className="button-container">
<b>Show platform</b>
<button onClick={showPlatformInAlert}>Show</button>
</div>
<hr/>
</>
)}
{color && (
<>
<div className="button-container">
<b>Show color</b>
<button onClick={showColorAlert}>Show</button>
</div>
<hr/>
</>
)}
<div className="button-container">
<b>Show scanner</b>
<button onClick={openScan}>Start</button>
</div>
<hr/>
{code && (
<div>
<b>Scanned code</b>
<p>{code}</p>
</div>
)}
<div className="button-container">
<b>Soft haptic</b>
<button onClick={softHaptic}>Start</button>
</div>
<hr/>
<div className="button-container">
<b>Success haptic</b>
<button onClick={successHaptic}>Start</button>
</div>
</div>
<h3>Contract actions: </h3>
<div className="data-container">
{connected ? (
<>
<div className="button-container">
<p>Increment counter by 1</p>
<button onClick={sendIncrement}>Start</button>
</div>
<hr/>
<div className="button-container">
<p>Deposit contract by 1 TON</p>
<button onClick={sendDeposit}>Start</button>
</div>
<hr/>
<div className="button-container">
<p>Withdrawal 0.2 TON</p>
<button onClick={sendWithdrawal}>Start</button>
</div>
</>
) : (
<p>Connect wallet to start action</p>
)}
</div>
<div>
<a
href="https://testnet.tonscan.org/address/EQCKgvXZVtec2EHhAXYwTdY65owG4xgR6-th6TxgtZbnPyrd"
target="_blank"
>
explorer
</a>
<br/>
<a href="https://github.com/cronnoss/counter-front-end" target="_blank">
github
</a>
<div>{platform}</div>
</div>
</div>
</div>
);
}
export default App;