|
1 | 1 | import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister' |
2 | | -import { QueryClient } from '@tanstack/react-query' |
3 | | -import type { OneOf } from 'viem' |
4 | | -import { tempoLocalnet, tempoTestnet } from 'viem/chains' |
| 2 | +import { MutationCache, QueryCache, QueryClient } from '@tanstack/react-query' |
| 3 | +import { Json } from 'ox' |
| 4 | +import { KeyManager, webAuthn } from 'tempo.ts/wagmi' |
5 | 5 | import { createConfig, deserialize, http, serialize, webSocket } from 'wagmi' |
6 | | -import { hashFn } from 'wagmi/query' |
7 | | - |
8 | | -// biome-ignore lint/suspicious/noExplicitAny: TODO |
9 | | -type TODO = any |
| 6 | +import { tempoLocalnet, tempoTestnet } from 'wagmi/chains' |
10 | 7 |
|
11 | 8 | const browser = typeof window !== 'undefined' |
12 | 9 |
|
13 | | -export const DEFAULT_TESTNET_RPC_URL = 'https://rpc-orchestra.testnet.tempo.xyz' |
14 | 10 | export const DEFAULT_TESTNET_WS_URL = 'wss://rpc-orchestra.testnet.tempo.xyz' |
| 11 | +export const DEFAULT_TESTNET_RPC_URL = 'https://rpc-orchestra.testnet.tempo.xyz' |
15 | 12 |
|
16 | | -export const queryClient = new QueryClient({ |
| 13 | +export const queryClient: QueryClient = new QueryClient({ |
17 | 14 | defaultOptions: { |
18 | 15 | queries: { |
19 | | - staleTime: 60 * 1_000, // needed for SSR |
20 | | - queryKeyHashFn: hashFn, |
21 | | - refetchOnWindowFocus: false, |
22 | | - gcTime: 1_000 * 60 * 60 * 24, // 24 hours |
| 16 | + gcTime: 1000 * 60 * 60 * 24, // 24 hours |
| 17 | + queryKeyHashFn: Json.stringify, |
| 18 | + refetchOnReconnect: () => !queryClient.isMutating(), |
| 19 | + retry: 0, |
23 | 20 | }, |
24 | 21 | }, |
| 22 | + mutationCache: new MutationCache({ |
| 23 | + onError: (error) => { |
| 24 | + if (import.meta.env.MODE !== 'development') return |
| 25 | + console.error(error) |
| 26 | + }, |
| 27 | + }), |
| 28 | + queryCache: new QueryCache({ |
| 29 | + onError: (error, query) => { |
| 30 | + if (import.meta.env.MODE !== 'development') return |
| 31 | + if (query.state.data !== undefined) console.error('[tsq]', error) |
| 32 | + }, |
| 33 | + }), |
25 | 34 | }) |
26 | 35 |
|
27 | 36 | export const persister = createAsyncStoragePersister({ |
28 | 37 | // Cache key includes build version - automatically invalidates on new deploys |
29 | | - key: `tempo-query-cache-${__BUILD_VERSION__}`, |
30 | 38 | serialize, |
31 | | - storage: browser ? window.localStorage : undefined, |
32 | 39 | deserialize, |
| 40 | + key: `tempo-query-cache-${__BUILD_VERSION__}`, |
| 41 | + storage: browser ? window.localStorage : undefined, |
33 | 42 | }) |
34 | 43 |
|
35 | | -const chain = |
36 | | - import.meta.env.VITE_LOCALNET === 'true' ? tempoLocalnet : tempoTestnet |
| 44 | +export const config = createConfig({ |
| 45 | + ssr: true, |
| 46 | + chains: [ |
| 47 | + import.meta.env.VITE_LOCALNET === 'true' ? tempoLocalnet : tempoTestnet, |
| 48 | + ], |
| 49 | + connectors: [ |
| 50 | + webAuthn({ |
| 51 | + keyManager: KeyManager.http('psstkey'), |
| 52 | + }), |
| 53 | + ], |
| 54 | + batch: { multicall: false }, |
| 55 | + multiInjectedProviderDiscovery: false, |
| 56 | + transports: { |
| 57 | + [tempoTestnet.id]: browser |
| 58 | + ? webSocket(DEFAULT_TESTNET_WS_URL) |
| 59 | + : http(DEFAULT_TESTNET_RPC_URL), |
| 60 | + [tempoLocalnet.id]: http(undefined, { batch: true }), |
| 61 | + }, |
| 62 | +}) |
37 | 63 |
|
38 | | -export function getConfig( |
39 | | - parameters: OneOf<{ rpcUrl?: string | undefined }> = {}, |
40 | | -) { |
41 | | - const { rpcUrl } = parameters |
42 | | - return createConfig({ |
43 | | - chains: [chain], |
44 | | - ssr: true, |
45 | | - batch: { multicall: false }, |
46 | | - transports: { |
47 | | - [tempoTestnet.id]: browser |
48 | | - ? webSocket(DEFAULT_TESTNET_WS_URL) |
49 | | - : http(rpcUrl ?? DEFAULT_TESTNET_RPC_URL), |
50 | | - [tempoLocalnet.id]: http(undefined, { |
51 | | - batch: true, |
52 | | - }), |
53 | | - }, |
54 | | - }) |
| 64 | +declare module 'wagmi' { |
| 65 | + interface Register { |
| 66 | + config: typeof config |
| 67 | + } |
55 | 68 | } |
56 | | - |
57 | | -export const config = getConfig() as TODO |
|
0 commit comments