2022-05-18 16:05:25 +00:00
|
|
|
import noop from "lodash/noop";
|
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-05-18 16:05:25 +00:00
|
|
|
type contractsContextType = {
|
|
|
|
contracts: Record<string, string>;
|
|
|
|
addContract: (payload: addContractsPayload) => void;
|
|
|
|
};
|
2022-02-01 21:48:40 +00:00
|
|
|
|
2022-05-18 16:05:25 +00:00
|
|
|
const contractsContextDefaultValue: contractsContextType = {
|
|
|
|
contracts: {},
|
|
|
|
addContract: noop,
|
|
|
|
};
|
2022-02-01 21:48:40 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-05-18 16:05:25 +00:00
|
|
|
return <ContractsContext.Provider value={value}>{children}</ContractsContext.Provider>;
|
2022-02-01 21:48:40 +00:00
|
|
|
}
|