-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
examples/minimal/packages/client-react-external-wallet/src/MUDContext.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
examples/minimal/packages/client-react-external-wallet/src/mud/store.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createContext, type ReactNode, useContext } from "react"; | ||
import { type PublicClient } from "wagmi"; | ||
import { syncToZustand } from "@latticexyz/store-sync/zustand"; | ||
import { type NetworkConfig } from "./networkConfig"; | ||
import mudConfig from "contracts/mud.config"; | ||
|
||
export type Store = Awaited<ReturnType<typeof syncStore>>; | ||
|
||
export const syncStore = async (networkConfig: NetworkConfig, publicClient: PublicClient) => { | ||
const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({ | ||
config: mudConfig, | ||
address: networkConfig.worldAddress, | ||
publicClient, | ||
startBlock: BigInt(networkConfig.initialBlockNumber), | ||
}); | ||
|
||
return { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction }; | ||
}; | ||
|
||
const StoreContext = createContext<Store | null>(null); | ||
|
||
type Props = { | ||
children: ReactNode; | ||
store: Store; | ||
}; | ||
|
||
export const StoreProvider = ({ children, store }: Props) => { | ||
const currentValue = useContext(StoreContext); | ||
if (currentValue) throw new Error("StoreProvider can only be used once"); | ||
return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>; | ||
}; | ||
|
||
export const useStore = () => { | ||
const value = useContext(StoreContext); | ||
if (!value) throw new Error("Must be used within a StoreProvider"); | ||
return value; | ||
}; |