46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
|
import { createContext, ReactNode, useContext } from "react";
|
||
|
|
||
|
type contractsContextType = Record<string, string>;
|
||
|
|
||
|
const contractsContextDefaultValue: contractsContextType = {};
|
||
|
|
||
|
const ContractsContext = createContext<contractsContextType>(contractsContextDefaultValue);
|
||
|
|
||
|
export function useContracts() {
|
||
|
return useContext(ContractsContext);
|
||
|
}
|
||
|
|
||
|
type Props = {
|
||
|
children: ReactNode;
|
||
|
};
|
||
|
|
||
|
interface addContractsPayload {
|
||
|
address: string;
|
||
|
signature: string;
|
||
|
}
|
||
|
|
||
|
export function ContractsProvider({ children }: Props) {
|
||
|
const addContract = (payload: addContractsPayload) => {
|
||
|
window.localStorage.setItem(
|
||
|
"contracts",
|
||
|
JSON.stringify({
|
||
|
...JSON.parse(window.localStorage.getItem("contracts") || "{}"),
|
||
|
[payload.address]: payload.signature,
|
||
|
})
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const value = {
|
||
|
contracts:
|
||
|
typeof window !== "undefined" ? JSON.parse(window.localStorage.getItem("contracts") || "{}") : {},
|
||
|
addContract,
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<>
|
||
|
{/* @ts-ignore */}
|
||
|
<ContractsContext.Provider value={value}>{children}</ContractsContext.Provider>
|
||
|
</>
|
||
|
);
|
||
|
}
|