-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGallery.tsx
339 lines (287 loc) Β· 13.5 KB
/
Gallery.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import './Gallery.css';
import { getFileIcon, formatFileSize, downloadFile, formatAddress } from './helpers';
import { ActionButtonHelper } from './ActionButtonHelper';
import { useState, useEffect, useCallback } from 'react';
import { Container, Row, Col, Table, Button, Pagination } from 'react-bootstrap';
import * as contract from '../Blockchain/contract';
import { decryptFile, getCiphFileCidHash, DecryptedFileMetaData } from '../Utils/utils';
import { importCryptoKey } from '../Utils/keyencrypt';
import { useFhevm } from '../Contexts/useFhevm';
import { NFTContent } from '../Contexts/NFTContext';
import { useNFTs } from '../Contexts/useNFTs';
import { toast } from 'react-toastify';
import { ArrowClockwise, Download } from 'react-bootstrap-icons';
import { getSignature } from '../../fhevmjs';
export const Gallery = () => {
const [page, setPage] = useState(0);
const { instance, createInstance } = useFhevm();
const [myTotalNFTs, setMyTotalNFTs] = useState(0);
const [totalNFTsSharedWithMe, setTotalNFTsSharedWithMe] = useState(0);
const [showGallery, setShowGallery] = useState(false);
const { nfts, removeNFT, updateNFTs, removeAllNFTs } = useNFTs();
const [nftsSharedWithMe, setNFTsSharedWithMe] = useState<NFTContent[]>([]);
const [account, setAccount] = useState<string>('');
const ITEMS_PERPAGE = 5;
const handleShare = useCallback(async (tokenId: number, to: string) => {
const response = await contract.shareToken(to, tokenId);
if (response) {
toast.success(`The NFT#${tokenId} has been shared with: ${formatAddress(to)}`);
} else {
toast.error(`Could not share NFT #${tokenId}! Please check if it is already shared with ${formatAddress(to)}.`);
}
}, []);
const handleTransfer = useCallback(async (tokenId: number, to: string) => {
const response = await contract.transferToken(to, tokenId);
if (response) {
toast.success(`The NFT#${tokenId} has been transferred and will be no more accessible!`);
removeNFT(tokenId);
} else {
toast.error(`Could not transfer the NFT#${tokenId}!`);
}
}, [removeNFT]);
const handleDelete = useCallback(async (tokenId: number) => {
const maxToRemove = await contract.getMaxUsersToRemove();
const response = await contract.burnToken(tokenId, maxToRemove);
if (response) {
toast.success(`The NFT#${tokenId} has been deleted and will be no more accessible!`);
removeNFT(tokenId);
} else {
toast.error(`Could not delete the NFT#${tokenId}!`);
}
}, [removeNFT]);
useEffect(() => {
if (!instance) {
createInstance().catch(console.error);
}
}, [instance, createInstance]);
const fetchAccount = useCallback(async () => {
try {
const fetchedAccount = await contract.getAccount();
if (fetchedAccount) {
setAccount(fetchedAccount);
return fetchedAccount;
}
} catch (error) {
console.error(error);
toast.info('Please connect to continue.');
}
return '';
}, []);
useEffect(() => {
const initializeAccount = async () => {
const fetchedAccount = await fetchAccount();
if (fetchedAccount) {
setAccount(fetchedAccount);
}
};
void initializeAccount();
}, [fetchAccount]);
const accessFile = useCallback(async (cidHash: string, publicKey: Uint8Array, signature: string, tokenId: number): Promise<DecryptedFileMetaData> => {
try {
if (!instance) throw new Error('Instance retrieval failed.');
const ciphFile = await getCiphFileCidHash(cidHash);
if (!ciphFile) throw new Error('Decrypting data failed.');
const reEncryptedFileKey = await contract.reencrypt(tokenId, publicKey, signature);
const decryptedKey: bigint[] = [];
for (const element of reEncryptedFileKey) {
if (element) {
const result = instance.decrypt(contract.contractAddress, element);
decryptedKey.push(result);
}
}
const fileKey = await importCryptoKey(decryptedKey);
const decryptedFile = await decryptFile(ciphFile, fileKey);
return decryptedFile;
} catch (error) {
toast.error(`Error while trying to access the NFT#${tokenId}. Could not fetch ${cidHash}`);
throw error;
}
}, [instance]);
const displayMyNFTs = useCallback(async (currentAccount: string) => {
if (!currentAccount) return;
try {
if (!instance) throw new Error('Instance retrieval failed.');
const total = await contract.getSupply();
setMyTotalNFTs(total);
if (total <= 0) {
toast.info('You have no NFTs to display!');
removeAllNFTs();
return;
}
const myNFTs = await contract.getTokensInRange(0, ITEMS_PERPAGE);
const reencryption = await getSignature(contract.contractAddress, currentAccount);
const updatedNFTs = await Promise.all(
myNFTs.map(async (token) => {
const decryptedFile = await accessFile(token.cidHash, reencryption.publicKey, reencryption.signature, token.tokenId);
return {
id: Number(token.tokenId),
file: decryptedFile.file,
};
})
);
updateNFTs(updatedNFTs);
toast.success('Gallery updated successfully!');
} catch (error) {
console.error('Error displaying NFTs:', error);
toast.error('Error displaying NFTs.');
}
}, [instance, updateNFTs, removeAllNFTs, accessFile]);
const displaySharedWithMeNFTs = useCallback(async (currentAccount: string) => {
if (!currentAccount) return;
try {
const totalShareWith = await contract.getSharedWithSupply();
setTotalNFTsSharedWithMe(totalShareWith);
if (totalShareWith <= 0) {
toast.info('You have no NFTs shared with you to display!');
setNFTsSharedWithMe([]);
return;
}
const nftsSharedWithMe = await contract.getSharedTokensInRange(0, totalShareWith);
const reencryption = await getSignature(contract.contractAddress, currentAccount);
const updatedTokens = await Promise.all(
nftsSharedWithMe.map(async (token) => {
const decryptedFile = await accessFile(token.cidHash, reencryption.publicKey, reencryption.signature, token.tokenId);
return {
id: Number(token.tokenId),
file: decryptedFile.file,
};
})
);
setNFTsSharedWithMe(updatedTokens);
toast.success('Shared NFTs updated successfully!');
} catch (error) {
console.error('Error displaying shared NFTs:', error);
toast.error('Error displaying shared NFTs.');
}
}, [accessFile]);
const displayGallery = useCallback(async () => {
let currentAccount = account;
if (!currentAccount) {
currentAccount = await fetchAccount();
if (currentAccount) {
setAccount(currentAccount);
}
}
if (!currentAccount) {
toast.info('Please connect to continue.');
return; // Exit if no account is fetched
}
setShowGallery(true);
try {
await Promise.all([displayMyNFTs(currentAccount), displaySharedWithMeNFTs(currentAccount)]);
} catch (error) {
console.error('Error displaying gallery:', error);
toast.error('Error displaying gallery.');
}
}, [account, fetchAccount, displayMyNFTs, displaySharedWithMeNFTs]);
return (
<Container className="mt-4 gallery-container" id="gallery">
<Row className="mb-4">
<Col>
<hr className="header-divider" />
<h1 className="gallery-header">My NFTs Gallery</h1>
<hr className="header-divider" />
</Col>
</Row>
{!showGallery && (
<Row>
<Col className="d-flex justify-content-center">
<Button className="modern-button" onClick={() => displayGallery()}
>
Show Private Content
</Button>
</Col>
</Row>
)}
{showGallery && (
<div>
<Table striped hover>
<thead>
<tr>
<th className="nft-num">NFT #</th>
<th className="name">Name</th>
<th className="size">Size</th>
<th className="actions">
<button
onClick={() => displayMyNFTs(account)}
title="Refresh Gallery"
className="icon-button" // Custom CSS class
>
<ArrowClockwise />
</button>
</th>
</tr>
</thead>
<tbody>
{/* Your NFTs data */}
{nfts.map(token => (
<tr key={token.id}>
<td>{token.id}</td>
<td>{getFileIcon(token.file.type)} {token.file.name}</td>
<td >{formatFileSize(token.file.size)}</td>
<td >
<ActionButtonHelper
onDownload={() => downloadFile(token.file)}
onShare={(to) => handleShare(token.id, to)}
onTransfer={(to) => handleTransfer(token.id, to)}
onDelete={() => handleDelete(token.id)}
tokenId={token.id}
/>
</td>
</tr>
))}
</tbody>
</Table>
<Pagination className="justify-content-center mt-4">
{[...Array(Math.ceil(myTotalNFTs / ITEMS_PERPAGE)).keys()].map(number => (
<Pagination.Item key={number} active={number + 1 === page} onClick={() => setPage(number + 1)}>
{number + 1}
</Pagination.Item>
))}
</Pagination>
<h3 className='shared-separator'>NFTs Shared With Me</h3>
<Table striped hover>
{/* Reusing the <thead> section */}
<thead>
<tr>
<th className="nft-num"></th>
<th className="name"></th>
<th className="size"></th>
<th className="actions">
<button
onClick={() => displaySharedWithMeNFTs(account)}
title="Refresh Gallery"
className="icon-button" // Custom CSS class
>
<ArrowClockwise />
</button>
</th>
</tr>
</thead>
<tbody>
{/* NFTs shared with you data */}
{nftsSharedWithMe.map(token => (
<tr key={token.id}>
<td>{token.id}</td>
<td>{getFileIcon(token.file.type)} {token.file.name}</td>
<td >{formatFileSize(token.file.size)}</td>
<td >
<Download onClick={() => downloadFile(token.file)} />
</td>
</tr>
))}
</tbody>
</Table>
<Pagination className="justify-content-center mt-4">
{[...Array(Math.ceil(totalNFTsSharedWithMe / ITEMS_PERPAGE)).keys()].map(number => (
<Pagination.Item key={number} active={number + 1 === page} onClick={() => setPage(number + 1)}>
{number + 1}
</Pagination.Item>
))}
</Pagination>
</div>
)
}
</Container >
);
};