@explorins/pers-sdk-react-native 2.1.21 → 2.1.22
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/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -1
- package/dist/hooks/useIPFSUrl.d.ts +51 -1
- package/dist/hooks/useIPFSUrl.d.ts.map +1 -1
- package/dist/hooks/useIPFSUrl.js +63 -12
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +10 -6
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +288 -57
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useIPFSUrl.ts +66 -13
- package/src/hooks/useWeb3.ts +10 -6
- package/src/index.ts +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@explorins/pers-sdk-react-native",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.22",
|
|
4
4
|
"description": "React Native SDK for PERS Platform - Tourism Loyalty System with Blockchain Transaction Signing and WebAuthn Authentication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"author": "eXplorins",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@explorins/pers-sdk": "
|
|
40
|
+
"@explorins/pers-sdk": "file:.yalc/@explorins/pers-sdk",
|
|
41
41
|
"@explorins/pers-signer": "^1.0.33",
|
|
42
42
|
"buffer": "^6.0.3",
|
|
43
43
|
"ethers": "^6.15.0",
|
package/src/hooks/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ export { useBusiness } from './useBusiness';
|
|
|
8
8
|
export { useCampaigns } from './useCampaigns';
|
|
9
9
|
export { useRedemptions } from './useRedemptions';
|
|
10
10
|
export { useWeb3 } from './useWeb3';
|
|
11
|
-
export { useResolvedUrl, useMetadata, useIPFSUrl } from './useIPFSUrl';
|
|
11
|
+
export { useResolvedUrl, useMetadata, useIPFSUrl, useTransactionMetadata } from './useIPFSUrl';
|
|
12
12
|
export { usePurchases } from './usePurchases';
|
|
13
13
|
export { useTenants } from './useTenants';
|
|
14
14
|
export { useUsers } from './useUsers';
|
package/src/hooks/useIPFSUrl.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useState, useEffect } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import type { TokenMetadata } from '@explorins/pers-sdk
|
|
1
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { TokenMetadata } from '@explorins/pers-sdk';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* React hook for resolving media URLs (IPFS or HTTPS).
|
|
@@ -8,6 +8,8 @@ import type { TokenMetadata } from '@explorins/pers-sdk/web3';
|
|
|
8
8
|
* Automatically handles IPFS URLs by resolving them to HTTPS via tenant's gateway.
|
|
9
9
|
* Non-IPFS URLs are returned as-is. Handles loading and error states.
|
|
10
10
|
*
|
|
11
|
+
* Uses sdk.tenants for IPFS resolution (lightweight, no Web3 deps).
|
|
12
|
+
*
|
|
11
13
|
* @param url - The URL to resolve (can be ipfs://, https://, or undefined)
|
|
12
14
|
* @returns Object with resolved URL, loading state, and error
|
|
13
15
|
*
|
|
@@ -22,13 +24,13 @@ import type { TokenMetadata } from '@explorins/pers-sdk/web3';
|
|
|
22
24
|
* ```
|
|
23
25
|
*/
|
|
24
26
|
export function useResolvedUrl(url: string | undefined | null) {
|
|
25
|
-
const {
|
|
27
|
+
const { sdk, isInitialized } = usePersSDK();
|
|
26
28
|
const [resolvedUrl, setResolvedUrl] = useState<string | null>(null);
|
|
27
29
|
const [isLoading, setIsLoading] = useState(false);
|
|
28
30
|
const [error, setError] = useState<string | null>(null);
|
|
29
31
|
|
|
30
32
|
useEffect(() => {
|
|
31
|
-
if (!url || !
|
|
33
|
+
if (!url || !isInitialized || !sdk) {
|
|
32
34
|
setResolvedUrl(null);
|
|
33
35
|
return;
|
|
34
36
|
}
|
|
@@ -42,7 +44,7 @@ export function useResolvedUrl(url: string | undefined | null) {
|
|
|
42
44
|
setIsLoading(true);
|
|
43
45
|
setError(null);
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
sdk.tenants.resolveIpfsUrl(url)
|
|
46
48
|
.then((resolved) => {
|
|
47
49
|
setResolvedUrl(resolved);
|
|
48
50
|
})
|
|
@@ -53,7 +55,7 @@ export function useResolvedUrl(url: string | undefined | null) {
|
|
|
53
55
|
.finally(() => {
|
|
54
56
|
setIsLoading(false);
|
|
55
57
|
});
|
|
56
|
-
}, [url,
|
|
58
|
+
}, [url, isInitialized, sdk]);
|
|
57
59
|
|
|
58
60
|
return {
|
|
59
61
|
/** Resolved HTTPS URL (or original if not IPFS) */
|
|
@@ -71,6 +73,8 @@ export function useResolvedUrl(url: string | undefined | null) {
|
|
|
71
73
|
* Handles IPFS resolution automatically and returns parsed metadata
|
|
72
74
|
* with all nested IPFS URLs already resolved to HTTPS.
|
|
73
75
|
*
|
|
76
|
+
* Uses sdk.tenants for metadata fetching (lightweight, no Web3 deps).
|
|
77
|
+
*
|
|
74
78
|
* @param metadataUri - The metadata URI to fetch (can be ipfs:// or https://)
|
|
75
79
|
* @returns Object with metadata, loading state, error, and refresh function
|
|
76
80
|
*
|
|
@@ -94,13 +98,13 @@ export function useResolvedUrl(url: string | undefined | null) {
|
|
|
94
98
|
* ```
|
|
95
99
|
*/
|
|
96
100
|
export function useMetadata(metadataUri: string | undefined | null) {
|
|
97
|
-
const {
|
|
101
|
+
const { sdk, isInitialized } = usePersSDK();
|
|
98
102
|
const [metadata, setMetadata] = useState<TokenMetadata | null>(null);
|
|
99
103
|
const [isLoading, setIsLoading] = useState(false);
|
|
100
104
|
const [error, setError] = useState<string | null>(null);
|
|
101
105
|
|
|
102
|
-
const fetchMetadata = async () => {
|
|
103
|
-
if (!metadataUri || !
|
|
106
|
+
const fetchMetadata = useCallback(async () => {
|
|
107
|
+
if (!metadataUri || !isInitialized || !sdk) {
|
|
104
108
|
setMetadata(null);
|
|
105
109
|
return;
|
|
106
110
|
}
|
|
@@ -109,7 +113,7 @@ export function useMetadata(metadataUri: string | undefined | null) {
|
|
|
109
113
|
setError(null);
|
|
110
114
|
|
|
111
115
|
try {
|
|
112
|
-
const result = await fetchAndProcessMetadata(metadataUri);
|
|
116
|
+
const result = await sdk.tenants.fetchAndProcessMetadata(metadataUri);
|
|
113
117
|
setMetadata(result);
|
|
114
118
|
} catch (err) {
|
|
115
119
|
setError(err instanceof Error ? err.message : 'Failed to fetch metadata');
|
|
@@ -117,11 +121,11 @@ export function useMetadata(metadataUri: string | undefined | null) {
|
|
|
117
121
|
} finally {
|
|
118
122
|
setIsLoading(false);
|
|
119
123
|
}
|
|
120
|
-
};
|
|
124
|
+
}, [metadataUri, isInitialized, sdk]);
|
|
121
125
|
|
|
122
126
|
useEffect(() => {
|
|
123
127
|
fetchMetadata();
|
|
124
|
-
}, [
|
|
128
|
+
}, [fetchMetadata]);
|
|
125
129
|
|
|
126
130
|
return {
|
|
127
131
|
/** Parsed metadata with resolved IPFS URLs (imageUrl, animationUrl, etc.) */
|
|
@@ -137,3 +141,52 @@ export function useMetadata(metadataUri: string | undefined | null) {
|
|
|
137
141
|
|
|
138
142
|
// Re-export with descriptive aliases
|
|
139
143
|
export { useResolvedUrl as useIPFSUrl };
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* React hook for loading transaction/token metadata.
|
|
147
|
+
*
|
|
148
|
+
* Convenience wrapper around useMetadata specifically for transaction items.
|
|
149
|
+
* Pass a transaction's metadataUri to get the NFT image, name, description, etc.
|
|
150
|
+
*
|
|
151
|
+
* @param transaction - Transaction object with optional metadataUri field
|
|
152
|
+
* @returns Object with metadata, loading state, and image URL
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```tsx
|
|
156
|
+
* import { useTransactionMetadata } from '@explorins/pers-sdk-react-native';
|
|
157
|
+
*
|
|
158
|
+
* function TransactionItem({ tx }: { tx: TransactionDTO }) {
|
|
159
|
+
* const { metadata, isLoading, imageUrl } = useTransactionMetadata(tx);
|
|
160
|
+
*
|
|
161
|
+
* if (isLoading) return <ActivityIndicator />;
|
|
162
|
+
*
|
|
163
|
+
* return (
|
|
164
|
+
* <View>
|
|
165
|
+
* {imageUrl && <Image source={{ uri: imageUrl }} style={{ width: 60, height: 60 }} />}
|
|
166
|
+
* <Text>{metadata?.name || tx.type}</Text>
|
|
167
|
+
* <Text>{tx.amount} tokens</Text>
|
|
168
|
+
* </View>
|
|
169
|
+
* );
|
|
170
|
+
* }
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function useTransactionMetadata(transaction: { metadataUri?: string | null } | null | undefined) {
|
|
174
|
+
const { metadata, isLoading, error, refresh } = useMetadata(transaction?.metadataUri);
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
/** Parsed metadata with resolved URLs */
|
|
178
|
+
metadata,
|
|
179
|
+
/** Whether metadata is loading */
|
|
180
|
+
isLoading,
|
|
181
|
+
/** Error message if fetch failed */
|
|
182
|
+
error,
|
|
183
|
+
/** Convenience: resolved image URL (or undefined) */
|
|
184
|
+
imageUrl: metadata?.imageUrl,
|
|
185
|
+
/** Convenience: token/NFT name (or undefined) */
|
|
186
|
+
name: metadata?.name,
|
|
187
|
+
/** Convenience: token/NFT description (or undefined) */
|
|
188
|
+
description: metadata?.description,
|
|
189
|
+
/** Manually refresh metadata */
|
|
190
|
+
refresh,
|
|
191
|
+
};
|
|
192
|
+
}
|
package/src/hooks/useWeb3.ts
CHANGED
|
@@ -169,6 +169,8 @@ export const useWeb3 = () => {
|
|
|
169
169
|
* Converts an ipfs:// URL to an HTTPS URL using the tenant's configured IPFS gateway.
|
|
170
170
|
* Non-IPFS URLs are returned unchanged.
|
|
171
171
|
*
|
|
172
|
+
* Note: Uses sdk.tenants for IPFS resolution (chain-agnostic).
|
|
173
|
+
*
|
|
172
174
|
* @param url - IPFS URL to resolve (ipfs://...)
|
|
173
175
|
* @returns Promise resolving to HTTP gateway URL
|
|
174
176
|
*
|
|
@@ -180,38 +182,40 @@ export const useWeb3 = () => {
|
|
|
180
182
|
* ```
|
|
181
183
|
*/
|
|
182
184
|
const resolveIPFSUrl = useCallback(async (url: string): Promise<string> => {
|
|
183
|
-
if (!isInitialized || !
|
|
185
|
+
if (!isInitialized || !sdk) {
|
|
184
186
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
try {
|
|
188
|
-
const result = await
|
|
190
|
+
const result = await sdk.tenants.resolveIpfsUrl(url);
|
|
189
191
|
return result;
|
|
190
192
|
} catch (error) {
|
|
191
193
|
console.error('Failed to resolve IPFS URL:', error);
|
|
192
194
|
throw error;
|
|
193
195
|
}
|
|
194
|
-
}, [
|
|
196
|
+
}, [sdk, isInitialized]);
|
|
195
197
|
|
|
196
198
|
/**
|
|
197
199
|
* Fetch and process token metadata from a URI
|
|
198
200
|
*
|
|
201
|
+
* Note: Uses sdk.tenants for metadata fetching (chain-agnostic IPFS).
|
|
202
|
+
*
|
|
199
203
|
* @param tokenUri - Token URI to fetch metadata from
|
|
200
204
|
* @returns Promise resolving to processed metadata or null
|
|
201
205
|
*/
|
|
202
206
|
const fetchAndProcessMetadata = useCallback(async (tokenUri: string): Promise<TokenMetadata | null> => {
|
|
203
|
-
if (!isInitialized || !
|
|
207
|
+
if (!isInitialized || !sdk) {
|
|
204
208
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
205
209
|
}
|
|
206
210
|
|
|
207
211
|
try {
|
|
208
|
-
const result = await
|
|
212
|
+
const result = await sdk.tenants.fetchAndProcessMetadata(tokenUri);
|
|
209
213
|
return result;
|
|
210
214
|
} catch (error) {
|
|
211
215
|
console.error('Failed to fetch and process metadata:', error);
|
|
212
216
|
throw error;
|
|
213
217
|
}
|
|
214
|
-
}, [
|
|
218
|
+
}, [sdk, isInitialized]);
|
|
215
219
|
|
|
216
220
|
const getChainDataById = useCallback(async (chainId: number): Promise<ChainData> => {
|
|
217
221
|
if (!isInitialized || !web3) {
|
package/src/index.ts
CHANGED
|
@@ -263,7 +263,12 @@ export {
|
|
|
263
263
|
useAnalytics,
|
|
264
264
|
useDonations,
|
|
265
265
|
useEvents,
|
|
266
|
-
useTriggerSources
|
|
266
|
+
useTriggerSources,
|
|
267
|
+
// IPFS hooks - for resolving ipfs:// URLs and loading metadata
|
|
268
|
+
useResolvedUrl,
|
|
269
|
+
useMetadata,
|
|
270
|
+
useIPFSUrl, // alias for useResolvedUrl
|
|
271
|
+
useTransactionMetadata // convenience for transaction items
|
|
267
272
|
} from './hooks';
|
|
268
273
|
|
|
269
274
|
// Re-export signing status types for convenience
|