forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useDaoProposalSinglePublishProposal.ts
53 lines (45 loc) · 1.81 KB
/
useDaoProposalSinglePublishProposal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useMemo } from 'react'
import { useChain, useDaoInfoContext } from '@dao-dao/stateless'
import { DaoProposalSingleAdapterId } from '@dao-dao/utils'
import {
matchAndLoadCommon,
matchAdapter as matchProposalModuleAdapter,
} from '../proposal-module-adapter'
import { makeUsePublishProposal } from '../proposal-module-adapter/adapters/DaoProposalSingle/common/hooks/makeUsePublishProposal'
import { PublishProposal } from '../proposal-module-adapter/adapters/DaoProposalSingle/types'
// Returns the publishProposal function for the DaoProposalSingle module on the
// current DAO. If the DAO does not have a DaoProposalSingle module, returns
// undefined. This hook makes it easy to publish a proposal from anywhere.
export const useDaoProposalSinglePublishProposal = ():
| PublishProposal
| undefined => {
const chain = useChain()
const { coreAddress, proposalModules } = useDaoInfoContext()
// Memoize hook getter since we don't want to create the hook more than once.
// `useDaoInfoContext` always returns the same instances of the data, so no
// hook rules are violated here.
const useProposalModule = useMemo(() => {
const daoProposalSingleModule = proposalModules.find(
({ contractName }) =>
matchProposalModuleAdapter(contractName)?.id ===
DaoProposalSingleAdapterId
)
if (!daoProposalSingleModule) {
return undefined
}
const common = matchAndLoadCommon(daoProposalSingleModule, {
chain,
coreAddress,
})
return makeUsePublishProposal({
options: {
chain,
proposalModule: daoProposalSingleModule,
coreAddress,
},
depositInfoSelector: common.selectors.depositInfo,
})
}, [proposalModules, chain, coreAddress])
const { publishProposal } = useProposalModule?.() ?? {}
return publishProposal
}