diff --git a/example/package-lock.json b/example/package-lock.json index 95a6585..c4d16c2 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -8,6 +8,7 @@ "name": "dapp-cookie-cutter", "version": "0.1.0", "dependencies": { + "@tanstack/react-query": "^5.50.1", "bip322-js": "^2.0.0", "bitcoinjs-message": "^2.2.0", "eslint-plugin-react": "^7.34.3", @@ -1298,6 +1299,30 @@ "win32" ] }, + "node_modules/@tanstack/query-core": { + "version": "5.50.1", + "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.50.1.tgz", + "integrity": "sha512-lpfhKPrJlyV2DSVcQb/HuozH3Av3kws4ge22agx+lNGpFkS4vLZ7St0l3GLwlAD+bqB+qXGex3JdRKUNtMviEQ==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + } + }, + "node_modules/@tanstack/react-query": { + "version": "5.50.1", + "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.50.1.tgz", + "integrity": "sha512-s0DW3rVBDPReDDovUjVqItVa3R2nPfUANK9nqGvarO2DwTiY9U4EBTsqizMxItRCoGgK5apeM7D3mxlHrSKpdQ==", + "dependencies": { + "@tanstack/query-core": "5.50.1" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + }, + "peerDependencies": { + "react": "^18.0.0" + } + }, "node_modules/@types/babel__core": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", diff --git a/example/package.json b/example/package.json index 5abc396..13dc42e 100644 --- a/example/package.json +++ b/example/package.json @@ -16,11 +16,12 @@ "prepare": "cd .. && npm i && npm run build && cd example" }, "dependencies": { + "@tanstack/react-query": "^5.50.1", "bip322-js": "^2.0.0", "bitcoinjs-message": "^2.2.0", "eslint-plugin-react": "^7.34.3", - "react": "^18.3.1", "react-dom": "^18.3.1", + "react": "^18.3.1", "sats-connect": "file:..", "styled-components": "6.1.11" }, diff --git a/example/src/App.tsx b/example/src/App.tsx index 8e36475..b76c07c 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -13,9 +13,13 @@ import GetBtcBalance from './components/GetBtcBalance'; import GetRunesBalance from './components/GetRunesBalance'; import { Container, ConnectButtonsContainer, Header, Logo, Body, Button } from './App.styles'; import GetInscriptions from './components/GetInscriptions'; +import { QueryClient, QueryClientProvider, useQueryClient } from '@tanstack/react-query'; +import { WalletType } from './components/wallet/WalletType'; +import { GetAccounts } from './components/bitcoin/GetAccounts'; import { SignMessage } from './components/SignMessage'; -function App() { +function AppWithProviders() { + const queryClient = useQueryClient(); const [network, setNetwork] = useLocalStorage( 'network', BitcoinNetworkType.Mainnet @@ -81,8 +85,9 @@ function App() { setBtcAddressInfo([]); setStxAddressInfo([]); setLegacyAddressInfo([]); + queryClient.clear(); })().catch(console.error); - }, [setBtcAddressInfo, setLegacyAddressInfo, setStxAddressInfo]); + }, [queryClient, setBtcAddressInfo, setLegacyAddressInfo, setStxAddressInfo]); if (!isConnected) { return ( @@ -111,6 +116,8 @@ function App() { addresses={[...legacyAddressInfo, ...btcAddressInfo, ...stxAddressInfo]} onDisconnect={onDisconnect} /> + + @@ -124,4 +131,13 @@ function App() { ); } +const queryClient = new QueryClient(); + +function App() { + return ( + + + + ); +} export default App; diff --git a/example/src/components/bitcoin/GetAccounts.tsx b/example/src/components/bitcoin/GetAccounts.tsx new file mode 100644 index 0000000..7afb61b --- /dev/null +++ b/example/src/components/bitcoin/GetAccounts.tsx @@ -0,0 +1,58 @@ +import Wallet, { AddressPurpose } from 'sats-connect'; +import { Button, Card } from '../../App.styles'; +import { useQuery } from '@tanstack/react-query'; +import styled from 'styled-components'; + +const ErrorMessage = styled.div({ + color: 'red', +}); + +export function GetAccounts() { + const { refetch, error, data, isFetching, isError, isSuccess } = useQuery({ + queryKey: ['getAccounts'], + queryFn: async () => { + const res = await Wallet.request('getAccounts', { + purposes: [AddressPurpose.Payment, AddressPurpose.Ordinals, AddressPurpose.Stacks], + }); + if (res.status === 'error') { + throw new Error('Error getting wallet type', { cause: res.error }); + } + return res.result; + }, + enabled: false, + }); + + return ( + +

Get accounts

+ + + + {(() => { + if (isFetching) { + return

Loading...

; + } + + if (isError) { + console.error(error); + return Error. Check console for details.; + } + + if (isSuccess) { + console.log(data); + return ( +
+

Check console for data.

+
+ ); + } + })()} +
+ ); +} diff --git a/example/src/components/wallet/WalletType.tsx b/example/src/components/wallet/WalletType.tsx new file mode 100644 index 0000000..4810d09 --- /dev/null +++ b/example/src/components/wallet/WalletType.tsx @@ -0,0 +1,55 @@ +import Wallet from 'sats-connect'; +import { Button, Card } from '../../App.styles'; +import { useQuery } from '@tanstack/react-query'; +import styled from 'styled-components'; + +const ErrorMessage = styled.div({ + color: 'red', +}); + +export function WalletType() { + const { refetch, error, data, isFetching, isError, isSuccess } = useQuery({ + queryKey: ['wallet_getWalletType'], + queryFn: async () => { + const res = await Wallet.request('wallet_getWalletType', undefined); + if (res.status === 'error') { + throw new Error('Error getting wallet type', { cause: res.error }); + } + return res.result; + }, + enabled: false, + }); + + return ( + +

Wallet type

+ + + + {(() => { + if (isFetching) { + return

Loading...

; + } + + if (isError) { + console.error(error); + return Error. Check console for details.; + } + + if (isSuccess) { + return ( +
+

Wallet type: {data}

+
+ ); + } + })()} +
+ ); +} diff --git a/example/tsconfig.app.json b/example/tsconfig.app.json index d739292..e6b0d35 100644 --- a/example/tsconfig.app.json +++ b/example/tsconfig.app.json @@ -2,9 +2,9 @@ "compilerOptions": { "composite": true, "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", - "target": "ES2020", + "target": "ESNext", "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], + "lib": ["ESNext", "DOM", "DOM.Iterable"], "module": "ESNext", "skipLibCheck": true,