2022-02-03 22:31:22 +00:00
|
|
|
import { createContext, ReactNode, useContext } from "react";
|
2022-02-01 21:48:40 +00:00
|
|
|
|
2022-05-09 18:12:47 +00:00
|
|
|
import { localStorage } from "@calcom/lib/webstorage";
|
|
|
|
|
2022-02-01 21:48:40 +00:00
|
|
|
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) => {
|
2022-05-09 18:12:47 +00:00
|
|
|
localStorage.setItem(
|
2022-02-03 22:31:22 +00:00
|
|
|
"contracts",
|
|
|
|
JSON.stringify({
|
2022-05-09 18:12:47 +00:00
|
|
|
...JSON.parse(localStorage.getItem("contracts") || "{}"),
|
2022-02-03 22:31:22 +00:00
|
|
|
[payload.address]: payload.signature,
|
|
|
|
})
|
|
|
|
);
|
2022-02-01 21:48:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const value = {
|
2022-05-09 18:12:47 +00:00
|
|
|
contracts: typeof window !== "undefined" ? JSON.parse(localStorage.getItem("contracts") || "{}") : {},
|
2022-02-01 21:48:40 +00:00
|
|
|
addContract,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-05-18 15:04:50 +00:00
|
|
|
<ContractsContext.Provider value={value as any}>{children}</ContractsContext.Provider>
|
2022-02-01 21:48:40 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|