2022-07-14 10:16:50 +00:00
|
|
|
import { useMinterContract, UseMinterContractProps } from 'contracts/minter'
|
|
|
|
import { useSG721Contract, UseSG721ContractProps } from 'contracts/sg721'
|
|
|
|
import {
|
|
|
|
useWhiteListContract,
|
|
|
|
useWhiteListContractProps,
|
|
|
|
} from 'contracts/whitelist'
|
|
|
|
|
|
|
|
import { Fragment, ReactNode, useEffect, VFC } from 'react'
|
|
|
|
import create, { State } from 'zustand'
|
|
|
|
|
2022-07-13 13:56:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contracts store type definitions
|
|
|
|
*/
|
|
|
|
export interface ContractsStore extends State {
|
2022-07-14 10:16:50 +00:00
|
|
|
sg721: UseSG721ContractProps | null
|
|
|
|
minter: UseMinterContractProps | null
|
|
|
|
whitelist: useWhiteListContractProps | null
|
2022-07-13 13:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contracts store default values as a separate variable for reusability
|
|
|
|
*/
|
|
|
|
export const defaultValues: ContractsStore = {
|
2022-07-14 10:16:50 +00:00
|
|
|
sg721: null,
|
|
|
|
minter: null,
|
|
|
|
whitelist: null,
|
2022-07-13 13:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entrypoint for contracts store using {@link defaultValues}
|
|
|
|
*/
|
|
|
|
export const useContracts = create<ContractsStore>(() => ({
|
|
|
|
...defaultValues,
|
|
|
|
}))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contracts store provider to easily mount {@link ContractsSubscription}
|
|
|
|
* to listen/subscribe to contract changes
|
|
|
|
*/
|
|
|
|
export const ContractsProvider = ({ children }: { children: ReactNode }) => {
|
|
|
|
return (
|
2022-07-14 10:16:50 +00:00
|
|
|
<Fragment>
|
2022-07-13 13:56:36 +00:00
|
|
|
{children}
|
|
|
|
<ContractsSubscription />
|
2022-07-14 10:16:50 +00:00
|
|
|
</Fragment>
|
2022-07-13 13:56:36 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contracts store subscriptions (side effects)
|
|
|
|
*
|
2022-07-14 10:16:50 +00:00
|
|
|
* @todo refactor all contract logics to zustand store
|
2022-07-13 13:56:36 +00:00
|
|
|
*/
|
2022-07-14 10:16:50 +00:00
|
|
|
const ContractsSubscription: VFC = () => {
|
|
|
|
const sg721 = useSG721Contract()
|
|
|
|
const minter = useMinterContract()
|
|
|
|
const whitelist = useWhiteListContract()
|
|
|
|
|
2022-07-13 13:56:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
useContracts.setState({
|
2022-07-14 10:16:50 +00:00
|
|
|
sg721,
|
|
|
|
minter,
|
|
|
|
whitelist,
|
2022-07-13 13:56:36 +00:00
|
|
|
})
|
|
|
|
}, [
|
2022-07-14 10:16:50 +00:00
|
|
|
sg721,
|
|
|
|
minter,
|
|
|
|
whitelist,
|
2022-07-13 13:56:36 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|