@ton/appkit-react 0.0.3 → 0.0.5-alpha.0

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,17 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { GetDefaultNetworkReturnType, Network } from '@ton/appkit';
9
+ export type UseDefaultNetworkReturnType = [
10
+ network: GetDefaultNetworkReturnType,
11
+ setNetwork: (network: Network | undefined) => void
12
+ ];
13
+ /**
14
+ * Hook to get and set the default network for wallet connections.
15
+ */
16
+ export declare const useDefaultNetwork: () => UseDefaultNetworkReturnType;
17
+ //# sourceMappingURL=use-default-network.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-default-network.d.ts","sourceRoot":"","sources":["../../../../../src/features/network/hooks/use-default-network.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,2BAA2B,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAIxE,MAAM,MAAM,2BAA2B,GAAG;IACtC,OAAO,EAAE,2BAA2B;IACpC,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,KAAK,IAAI;CACrD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,QAAO,2BAwBpC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"use-networks.d.ts","sourceRoot":"","sources":["../../../../../src/features/network/hooks/use-networks.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAIzD,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,WAAW,QAAO,qBAe9B,CAAC"}
1
+ {"version":3,"file":"use-networks.d.ts","sourceRoot":"","sources":["../../../../../src/features/network/hooks/use-networks.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAIzD,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,WAAW,QAAO,qBA0B9B,CAAC"}
@@ -7,4 +7,5 @@
7
7
  */
8
8
  export { useNetworks, type UseNetworksReturnType } from './hooks/use-networks';
9
9
  export { useNetwork, type UseNetworkReturnType } from './hooks/use-network';
10
+ export { useDefaultNetwork, type UseDefaultNetworkReturnType } from './hooks/use-default-network';
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/features/network/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,WAAW,EAAE,KAAK,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/features/network/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,WAAW,EAAE,KAAK,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,KAAK,2BAA2B,EAAE,MAAM,6BAA6B,CAAC"}
@@ -12,5 +12,5 @@ export type UseNFTsByAddressReturnType<selectData = GetNFTsData> = UseQueryRetur
12
12
  /**
13
13
  * Hook to get NFTs
14
14
  */
15
- export declare const useNFTsByAddress: <selectData = GetNFTsData>(parameters?: UseNFTsByAddressParameters<selectData>) => UseNFTsByAddressReturnType<selectData>;
15
+ export declare const useNftsByAddress: <selectData = GetNFTsData>(parameters?: UseNFTsByAddressParameters<selectData>) => UseNFTsByAddressReturnType<selectData>;
16
16
  //# sourceMappingURL=use-nfts-by-address.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ton/appkit-react",
3
- "version": "0.0.3",
3
+ "version": "0.0.5-alpha.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "clsx": "2.1.1",
37
37
  "radix-ui": "^1.4.3",
38
38
  "rosetta": "1.1.0",
39
- "@ton/appkit": "0.0.2"
39
+ "@ton/appkit": "0.0.4-alpha.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@storybook/addon-docs": "10.2.8",
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Copyright (c) TonTech.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import { useSyncExternalStore, useCallback } from 'react';
10
+ import { getDefaultNetwork, setDefaultNetwork, watchDefaultNetwork } from '@ton/appkit';
11
+ import type { GetDefaultNetworkReturnType, Network } from '@ton/appkit';
12
+
13
+ import { useAppKit } from '../../../hooks/use-app-kit';
14
+
15
+ export type UseDefaultNetworkReturnType = [
16
+ network: GetDefaultNetworkReturnType,
17
+ setNetwork: (network: Network | undefined) => void,
18
+ ];
19
+
20
+ /**
21
+ * Hook to get and set the default network for wallet connections.
22
+ */
23
+ export const useDefaultNetwork = (): UseDefaultNetworkReturnType => {
24
+ const appKit = useAppKit();
25
+
26
+ const subscribe = useCallback(
27
+ (onChange: () => void) => {
28
+ return watchDefaultNetwork(appKit, { onChange });
29
+ },
30
+ [appKit],
31
+ );
32
+
33
+ const getSnapshot = useCallback(() => {
34
+ return getDefaultNetwork(appKit);
35
+ }, [appKit]);
36
+
37
+ const network = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
38
+
39
+ const setNetwork = useCallback(
40
+ (newNetwork: Network | undefined) => {
41
+ setDefaultNetwork(appKit, { network: newNetwork });
42
+ },
43
+ [appKit],
44
+ );
45
+
46
+ return [network, setNetwork];
47
+ };
@@ -6,7 +6,7 @@
6
6
  *
7
7
  */
8
8
 
9
- import { useSyncExternalStore, useCallback } from 'react';
9
+ import { useSyncExternalStore, useCallback, useRef } from 'react';
10
10
  import { getNetworks, watchNetworks } from '@ton/appkit';
11
11
  import type { GetNetworksReturnType } from '@ton/appkit';
12
12
 
@@ -19,6 +19,7 @@ export type UseNetworksReturnType = GetNetworksReturnType;
19
19
  */
20
20
  export const useNetworks = (): UseNetworksReturnType => {
21
21
  const appKit = useAppKit();
22
+ const cachedRef = useRef<GetNetworksReturnType>([]);
22
23
 
23
24
  const subscribe = useCallback(
24
25
  (onChange: () => void) => {
@@ -28,8 +29,18 @@ export const useNetworks = (): UseNetworksReturnType => {
28
29
  );
29
30
 
30
31
  const getSnapshot = useCallback(() => {
31
- return getNetworks(appKit);
32
+ const networks = getNetworks(appKit);
33
+
34
+ if (
35
+ networks.length === cachedRef.current.length &&
36
+ networks.every((n, i) => n.chainId === cachedRef.current[i]?.chainId)
37
+ ) {
38
+ return cachedRef.current;
39
+ }
40
+
41
+ cachedRef.current = networks;
42
+ return networks;
32
43
  }, [appKit]);
33
44
 
34
- return useSyncExternalStore(subscribe, getSnapshot, () => []);
45
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
35
46
  };
@@ -9,3 +9,4 @@
9
9
  // Hooks
10
10
  export { useNetworks, type UseNetworksReturnType } from './hooks/use-networks';
11
11
  export { useNetwork, type UseNetworkReturnType } from './hooks/use-network';
12
+ export { useDefaultNetwork, type UseDefaultNetworkReturnType } from './hooks/use-default-network';
@@ -20,7 +20,7 @@ export type UseNFTsByAddressReturnType<selectData = GetNFTsData> = UseQueryRetur
20
20
  /**
21
21
  * Hook to get NFTs
22
22
  */
23
- export const useNFTsByAddress = <selectData = GetNFTsData>(
23
+ export const useNftsByAddress = <selectData = GetNFTsData>(
24
24
  parameters: UseNFTsByAddressParameters<selectData> = {},
25
25
  ): UseNFTsByAddressReturnType<selectData> => {
26
26
  const appKit = useAppKit();
@@ -9,7 +9,7 @@
9
9
  import type { GetNFTsData } from '@ton/appkit/queries';
10
10
 
11
11
  import { useAddress } from '../../wallets/hooks/use-address';
12
- import { useNFTsByAddress } from './use-nfts-by-address';
12
+ import { useNftsByAddress } from './use-nfts-by-address';
13
13
  import type { UseNFTsByAddressParameters, UseNFTsByAddressReturnType } from './use-nfts-by-address';
14
14
 
15
15
  export type UseNFTsParameters<selectData = GetNFTsData> = UseNFTsByAddressParameters<selectData>;
@@ -24,5 +24,5 @@ export const useNfts = <selectData = GetNFTsData>(
24
24
  ): UseNFTsReturnType<selectData> => {
25
25
  const address = useAddress();
26
26
 
27
- return useNFTsByAddress({ ...parameters, address });
27
+ return useNftsByAddress({ ...parameters, address });
28
28
  };