generated from latticexyz/mud-template-react
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdevTools.ts
More file actions
48 lines (39 loc) · 1.35 KB
/
devTools.ts
File metadata and controls
48 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import mudConfig from "contracts/mud.config"
import { useEffect, useRef } from "react"
import { Destructor, MUDInterface, noop } from "./types"
const noopPromise = Promise.resolve(noop);
/**
* Mounts the dev tools in development mode.
*/
export function useDevTools(mud?: MUDInterface) {
const unmountDevtoolsPromise = useRef(noopPromise);
useEffect(() => {
if (!mud) return noop;
unmountDevtoolsPromise.current.then(async (unmount) => {
unmount()
unmountDevtoolsPromise.current = mountDevTools(mud);
})
return () => {
unmountDevtoolsPromise.current.then(async (unmount) => {
unmount()
unmountDevtoolsPromise.current = noopPromise;
});
}
}, [mud]);
}
async function mountDevTools(mud: MUDInterface): Promise<Destructor> {
if (!import.meta.env.DEV) return noop;
// Avoid loading when not in dev mode.
const { mount } = await import("@latticexyz/dev-tools");
return await mount({
config: mudConfig,
publicClient: mud.network.publicClient,
walletClient: mud.network.walletClient!, // this will work even if undefined
latestBlock$: mud.network.latestBlock$,
storedBlockLogs$: mud.network.storedBlockLogs$,
worldAddress: mud.network.worldContract.address,
worldAbi: mud.network.worldContract.abi,
write$: mud.network.write$,
recsWorld: mud.network.world,
}) ?? noop;
}