From 30f2eb38ebffab9977553e95e1e6148c1fc5eb5e Mon Sep 17 00:00:00 2001 From: Slava Startsev Date: Tue, 8 Oct 2024 18:56:10 +0300 Subject: [PATCH] Replace hook library to be compatible with ssr (#99) * chore: replace hook library to be compatible with ssr * chore: add use client directive to hooks --- packages/sats-wagmi/package.json | 6 +- packages/sats-wagmi/src/hooks/useAccount.tsx | 2 + packages/sats-wagmi/src/hooks/useBalance.tsx | 2 + packages/sats-wagmi/src/hooks/useConnect.tsx | 2 + .../sats-wagmi/src/hooks/useDisconnect.tsx | 2 + .../sats-wagmi/src/hooks/useFeeEstimate.tsx | 2 + packages/sats-wagmi/src/hooks/useFeeRate.tsx | 2 + .../src/hooks/useSendGatewayTransaction.tsx | 2 + .../src/hooks/useSendTransaction.tsx | 2 + .../sats-wagmi/src/hooks/useSignMessage.tsx | 2 + .../hooks/useWaitForTransactionReceipt.tsx | 2 + packages/sats-wagmi/src/provider.tsx | 10 ++- pnpm-lock.yaml | 70 +++++++++---------- 13 files changed, 65 insertions(+), 41 deletions(-) diff --git a/packages/sats-wagmi/package.json b/packages/sats-wagmi/package.json index 96801aa..c3d96e3 100644 --- a/packages/sats-wagmi/package.json +++ b/packages/sats-wagmi/package.json @@ -36,9 +36,9 @@ "postpack": "clean-package restore" }, "peerDependencies": { + "@tanstack/react-query": ">=5.0.0", "react": ">=18", - "react-dom": ">=18", - "@tanstack/react-query": ">=5.0.0" + "react-dom": ">=18" }, "devDependencies": { "@types/async-retry": "^1.4.8", @@ -59,7 +59,7 @@ "bs58check": "^2.1.2", "clean-package": "^2.2.0", "sats-connect": "^1.1.2", - "@uidotdev/usehooks": "^2.4.1" + "usehooks-ts": "^3.1.0" }, "clean-package": "../../clean-package.config.json" } diff --git a/packages/sats-wagmi/src/hooks/useAccount.tsx b/packages/sats-wagmi/src/hooks/useAccount.tsx index 957082f..04c9525 100644 --- a/packages/sats-wagmi/src/hooks/useAccount.tsx +++ b/packages/sats-wagmi/src/hooks/useAccount.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useQuery } from '@tanstack/react-query'; import { getAddressInfo } from 'bitcoin-address-validation'; import { useEffect } from 'react'; diff --git a/packages/sats-wagmi/src/hooks/useBalance.tsx b/packages/sats-wagmi/src/hooks/useBalance.tsx index 5fdc9b1..9d258d2 100644 --- a/packages/sats-wagmi/src/hooks/useBalance.tsx +++ b/packages/sats-wagmi/src/hooks/useBalance.tsx @@ -1,3 +1,5 @@ +'use client'; + import { UseQueryOptions, useQuery } from '@tanstack/react-query'; import { EsploraClient } from '@gobob/bob-sdk'; diff --git a/packages/sats-wagmi/src/hooks/useConnect.tsx b/packages/sats-wagmi/src/hooks/useConnect.tsx index 7a69c72..fd16290 100644 --- a/packages/sats-wagmi/src/hooks/useConnect.tsx +++ b/packages/sats-wagmi/src/hooks/useConnect.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useMutation } from '@tanstack/react-query'; import { SatsConnector } from '../connectors/base'; diff --git a/packages/sats-wagmi/src/hooks/useDisconnect.tsx b/packages/sats-wagmi/src/hooks/useDisconnect.tsx index 81f40c0..31ab982 100644 --- a/packages/sats-wagmi/src/hooks/useDisconnect.tsx +++ b/packages/sats-wagmi/src/hooks/useDisconnect.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useMutation } from '@tanstack/react-query'; import { useSatsWagmi } from '../provider'; diff --git a/packages/sats-wagmi/src/hooks/useFeeEstimate.tsx b/packages/sats-wagmi/src/hooks/useFeeEstimate.tsx index 383727e..7cc922e 100644 --- a/packages/sats-wagmi/src/hooks/useFeeEstimate.tsx +++ b/packages/sats-wagmi/src/hooks/useFeeEstimate.tsx @@ -1,3 +1,5 @@ +'use client'; + import { estimateTxFee } from '@gobob/bob-sdk'; import { UndefinedInitialDataOptions, useQuery } from '@tanstack/react-query'; diff --git a/packages/sats-wagmi/src/hooks/useFeeRate.tsx b/packages/sats-wagmi/src/hooks/useFeeRate.tsx index 1f79040..d9970c6 100644 --- a/packages/sats-wagmi/src/hooks/useFeeRate.tsx +++ b/packages/sats-wagmi/src/hooks/useFeeRate.tsx @@ -1,3 +1,5 @@ +'use client'; + import { EsploraClient } from '@gobob/bob-sdk'; import { UndefinedInitialDataOptions, useQuery } from '@tanstack/react-query'; diff --git a/packages/sats-wagmi/src/hooks/useSendGatewayTransaction.tsx b/packages/sats-wagmi/src/hooks/useSendGatewayTransaction.tsx index 9000c14..481cac1 100644 --- a/packages/sats-wagmi/src/hooks/useSendGatewayTransaction.tsx +++ b/packages/sats-wagmi/src/hooks/useSendGatewayTransaction.tsx @@ -1,3 +1,5 @@ +'use client'; + import { Optional, useMutation, UseMutationOptions } from '@tanstack/react-query'; import { GatewayQuoteParams, GatewaySDK } from '@gobob/bob-sdk'; diff --git a/packages/sats-wagmi/src/hooks/useSendTransaction.tsx b/packages/sats-wagmi/src/hooks/useSendTransaction.tsx index 6a1770a..8294db6 100644 --- a/packages/sats-wagmi/src/hooks/useSendTransaction.tsx +++ b/packages/sats-wagmi/src/hooks/useSendTransaction.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useMutation, UseMutationOptions } from '@tanstack/react-query'; import { useSatsWagmi } from '../provider'; diff --git a/packages/sats-wagmi/src/hooks/useSignMessage.tsx b/packages/sats-wagmi/src/hooks/useSignMessage.tsx index 3d7a74d..799a0e2 100644 --- a/packages/sats-wagmi/src/hooks/useSignMessage.tsx +++ b/packages/sats-wagmi/src/hooks/useSignMessage.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useMutation, UseMutationOptions } from '@tanstack/react-query'; import { useSatsWagmi } from '../provider'; diff --git a/packages/sats-wagmi/src/hooks/useWaitForTransactionReceipt.tsx b/packages/sats-wagmi/src/hooks/useWaitForTransactionReceipt.tsx index 97d685d..03c90e2 100644 --- a/packages/sats-wagmi/src/hooks/useWaitForTransactionReceipt.tsx +++ b/packages/sats-wagmi/src/hooks/useWaitForTransactionReceipt.tsx @@ -1,3 +1,5 @@ +'use client'; + import { UseQueryOptions, useQuery } from '@tanstack/react-query'; import { EsploraClient } from '@gobob/bob-sdk'; diff --git a/packages/sats-wagmi/src/provider.tsx b/packages/sats-wagmi/src/provider.tsx index 608e770..d52130b 100644 --- a/packages/sats-wagmi/src/provider.tsx +++ b/packages/sats-wagmi/src/provider.tsx @@ -2,7 +2,7 @@ import { Network as BitcoinNetwork } from 'bitcoin-address-validation'; import { FC, ReactNode, createContext, useCallback, useContext, useEffect, useState } from 'react'; -import { useLocalStorage } from '@uidotdev/usehooks'; +import { useLocalStorage } from 'usehooks-ts'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { LeatherConnector, MMSnapConnector, UnisatConnector, XverseConnector } from './connectors'; @@ -44,7 +44,13 @@ const SatsWagmiConfig: FC = ({ children, queryClient, netw const [connectors, setConnectors] = useState([]); const [connector, setCurrentConnector] = useState(); - const [storedConnector, setStoredConnector] = useLocalStorage(LocalStorageKeys.CONNECTOR); + const [storedConnector, setStoredConnector] = useLocalStorage( + LocalStorageKeys.CONNECTOR, + undefined, + { + initializeWithValue: typeof window !== 'undefined' + } + ); useEffect(() => { const init = () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3f051b4..9ca8fb0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -176,7 +176,7 @@ importers: version: 7.3.0(@swc/core@1.7.23)(ts-node@10.9.2)(typescript@5.5.4) turbo: specifier: latest - version: 2.1.2 + version: 2.1.3 typescript: specifier: ^5.2.2 version: 5.5.4 @@ -201,9 +201,6 @@ importers: '@tanstack/react-query': specifier: '>=5.0.0' version: 5.54.1(react@18.3.1) - '@uidotdev/usehooks': - specifier: ^2.4.1 - version: 2.4.1(react-dom@18.3.1)(react@18.3.1) async-retry: specifier: ^1.3.3 version: 1.3.3 @@ -225,6 +222,9 @@ importers: sats-connect: specifier: ^1.1.2 version: 1.4.1 + usehooks-ts: + specifier: ^3.1.0 + version: 3.1.0(react@18.3.1) devDependencies: '@types/async-retry': specifier: ^1.4.8 @@ -6143,17 +6143,6 @@ packages: eslint-visitor-keys: 3.4.3 dev: true - /@uidotdev/usehooks@2.4.1(react-dom@18.3.1)(react@18.3.1): - resolution: {integrity: sha512-1I+RwWyS+kdv3Mv0Vmc+p0dPYH0DTRAo04HLyXReYBL9AeseDWUJyi4THuksBJcu9F0Pih69Ak150VDnqbVnXg==} - engines: {node: '>=16'} - peerDependencies: - react: ^18.2.0 - react-dom: ^18.2.0 - dependencies: - react: 18.3.1 - react-dom: 18.3.1(react@18.3.1) - dev: false - /@ungap/structured-clone@1.2.0: resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==} dev: true @@ -11272,7 +11261,6 @@ packages: /lodash.debounce@4.0.8: resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==} - dev: true /lodash.isfunction@3.0.9: resolution: {integrity: sha512-AirXNj15uRIMMPihnkInB4i3NHeb4iBtNg9WRWuK2o31S+ePwwNmDPaTL3o7dTJ+VXNZim7rFs4rxN4YU1oUJw==} @@ -13913,64 +13901,64 @@ packages: safe-buffer: 5.2.1 dev: true - /turbo-darwin-64@2.1.2: - resolution: {integrity: sha512-3TEBxHWh99h2yIzkuIigMEOXt/ItYQp0aPiJjPd1xN4oDcsKK5AxiFKPH9pdtfIBzYsY59kQhZiFj0ELnSP7Bw==} + /turbo-darwin-64@2.1.3: + resolution: {integrity: sha512-ouJOm0g0YyoBuhmikEujVCBGo3Zr0lbSOWFIsQtWUTItC88F2w2byhjtsYGPXQwMlTbXwmoBU2lOCfWNkeEwHQ==} cpu: [x64] os: [darwin] requiresBuild: true dev: true optional: true - /turbo-darwin-arm64@2.1.2: - resolution: {integrity: sha512-he0miWNq2WxJzsH82jS2Z4MXpnkzn9SH8a79iPXiJkq25QREImucscM4RPasXm8wARp91pyysJMq6aasD45CeA==} + /turbo-darwin-arm64@2.1.3: + resolution: {integrity: sha512-j2FOJsK4LAOtHQlb3Oom0yWB/Vi0nF1ljInr311mVzHoFAJRZtfW2fRvdZRb/lBUwjSp8be58qWHzANIcrA0OA==} cpu: [arm64] os: [darwin] requiresBuild: true dev: true optional: true - /turbo-linux-64@2.1.2: - resolution: {integrity: sha512-fKUBcc0rK8Vdqv5a/E3CSpMBLG1bzwv+Q0Q83F8fG2ZfNCNKGbcEYABdonNZkkx141Rj03cZQFCgxu3MVEGU+A==} + /turbo-linux-64@2.1.3: + resolution: {integrity: sha512-ubRHkI1gSel7H7wsmxKK8C9UlLWqg/2dkCC88LFupaK6TKgvBKqDqA0Z1M9C/escK0Jsle2k0H8bybV9OYIl4Q==} cpu: [x64] os: [linux] requiresBuild: true dev: true optional: true - /turbo-linux-arm64@2.1.2: - resolution: {integrity: sha512-sV8Bpmm0WiuxgbhxymcC7wSsuxfBBieI98GegSwbr/bs1ANAgzCg93urIrdKdQ3/b31zZxQwcaP4FBF1wx1Qdg==} + /turbo-linux-arm64@2.1.3: + resolution: {integrity: sha512-LffUL+e5wv7BtD6DgnM2kKOlDkMo2eRjhbAjVnrCD3wi2ug0tl6NDzajnHHjtaMyOnIf4AvzSKdLWsBxafGBQA==} cpu: [arm64] os: [linux] requiresBuild: true dev: true optional: true - /turbo-windows-64@2.1.2: - resolution: {integrity: sha512-wcmIJZI9ORT9ykHGliFE6kWRQrlH930QGSjSgWC8uFChFFuOyUlvC7ttcxuSvU9VqC7NF4C+GVAcFJQ8lTjN7g==} + /turbo-windows-64@2.1.3: + resolution: {integrity: sha512-S9SvcZZoaq5jKr6kA6eF7/xgQhVn8Vh7PVy5lono9zybvhyL4eY++y2PaLToIgL8G9IcbLmgOC73ExNjFBg9XQ==} cpu: [x64] os: [win32] requiresBuild: true dev: true optional: true - /turbo-windows-arm64@2.1.2: - resolution: {integrity: sha512-zdnXjrhk7YO6CP+Q5wPueEvOCLH4lDa6C4rrwiakcWcPgcQGbVozJlo4uaQ6awo8HLWQEvOwu84RkWTdLAc/Hw==} + /turbo-windows-arm64@2.1.3: + resolution: {integrity: sha512-twlEo8lRrGbrR6T/ZklUIquW3IlFCEtywklgVA81aIrSBm56+GEVpSrHhIlsx1hiYeSNrs+GpDwZGe+V7fvEVQ==} cpu: [arm64] os: [win32] requiresBuild: true dev: true optional: true - /turbo@2.1.2: - resolution: {integrity: sha512-Jb0rbU4iHEVQ18An/YfakdIv9rKnd3zUfSE117EngrfWXFHo3RndVH96US3GsT8VHpwTncPePDBT2t06PaFLrw==} + /turbo@2.1.3: + resolution: {integrity: sha512-lY0yj2GH2a2a3NExZ3rGe+rHUVeFE2aXuRAue57n+08E7Z7N7YCmynju0kPC1grAQzERmoLpKrmzmWd+PNiADw==} hasBin: true optionalDependencies: - turbo-darwin-64: 2.1.2 - turbo-darwin-arm64: 2.1.2 - turbo-linux-64: 2.1.2 - turbo-linux-arm64: 2.1.2 - turbo-windows-64: 2.1.2 - turbo-windows-arm64: 2.1.2 + turbo-darwin-64: 2.1.3 + turbo-darwin-arm64: 2.1.3 + turbo-linux-64: 2.1.3 + turbo-linux-arm64: 2.1.3 + turbo-windows-64: 2.1.3 + turbo-windows-arm64: 2.1.3 dev: true /tween-functions@1.2.0: @@ -14289,6 +14277,16 @@ packages: tslib: 2.7.0 dev: true + /usehooks-ts@3.1.0(react@18.3.1): + resolution: {integrity: sha512-bBIa7yUyPhE1BCc0GmR96VU/15l/9gP1Ch5mYdLcFBaFGQsdmXkvjV0TtOqW1yUd6VjIwDunm+flSciCQXujiw==} + engines: {node: '>=16.15.0'} + peerDependencies: + react: ^18.2.0 + dependencies: + lodash.debounce: 4.0.8 + react: 18.3.1 + dev: false + /util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}