diff --git a/src/hooks/DAO/useSendAssetsActionModal.tsx b/src/hooks/DAO/useSendAssetsActionModal.tsx new file mode 100644 index 000000000..ecae202cf --- /dev/null +++ b/src/hooks/DAO/useSendAssetsActionModal.tsx @@ -0,0 +1,63 @@ +import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; +import { ModalType } from "../../components/ui/modals/ModalProvider"; +import { SendAssetsData } from "../../components/ui/modals/SendAssetsModal"; +import { useDecentModal } from "../../components/ui/modals/useDecentModal"; +import { DAO_ROUTES } from "../../constants/routes"; +import { useFractal } from "../../providers/App/AppProvider"; +import { useNetworkConfigStore } from "../../providers/NetworkConfig/useNetworkConfigStore"; +import { useProposalActionsStore } from "../../store/actions/useProposalActionsStore"; +import { useDaoInfoStore } from "../../store/daoInfo/useDaoInfoStore"; +import { ProposalActionType } from "../../types/proposalBuilder"; +import { isNativeAsset, prepareSendAssetsActionData } from "../../utils/dao/prepareSendAssetsActionData"; + +export default function useSendAssetsActionModal() { + const { safe } = useDaoInfoStore(); + const { addressPrefix } = useNetworkConfigStore(); + const { t } = useTranslation(['modals']); + const { addAction } = useProposalActionsStore(); + const navigate = useNavigate(); + const { governance: { isAzorius } } = useFractal(); + const sendAssetsAction = async (sendAssetsData: SendAssetsData) => { + if (!safe?.address) { + return; + } + const isNative = isNativeAsset(sendAssetsData.asset); + const transactionData = prepareSendAssetsActionData({ + transferAmount: sendAssetsData.transferAmount, + asset: sendAssetsData.asset, + destinationAddress: sendAssetsData.destinationAddress, + }); + addAction({ + actionType: ProposalActionType.TRANSFER, + content: <>, + transactions: [ + { + targetAddress: transactionData.calldata, + ethValue: { + bigintValue: transactionData.value, + value: transactionData.value.toString(), + }, + functionName: isNative ? '' : 'transfer', + parameters: isNative + ? [] + : [ + { signature: 'address', value: sendAssetsData.destinationAddress }, + { signature: 'uint256', value: sendAssetsData.transferAmount.toString() }, + ], + }, + ], + }); + navigate(DAO_ROUTES.proposalWithActionsNew.relative(addressPrefix, safe.address)); + }; + + const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, { + onSubmit: sendAssetsAction, + submitButtonText: t('submitProposal', { ns: 'modals' }), + showNonceInput: !isAzorius, + }); + + return { + openSendAssetsModal, + }; +} diff --git a/src/pages/dao/proposal-templates/SafeProposalTemplatesPage.tsx b/src/pages/dao/proposal-templates/SafeProposalTemplatesPage.tsx index 405738b66..ffb0942ae 100644 --- a/src/pages/dao/proposal-templates/SafeProposalTemplatesPage.tsx +++ b/src/pages/dao/proposal-templates/SafeProposalTemplatesPage.tsx @@ -8,12 +8,10 @@ import ExampleTemplateCard from '../../../components/ProposalTemplates/ExampleTe import ProposalTemplateCard from '../../../components/ProposalTemplates/ProposalTemplateCard'; import NoDataCard from '../../../components/ui/containers/NoDataCard'; import { InfoBoxLoader } from '../../../components/ui/loaders/InfoBoxLoader'; -import { ModalType } from '../../../components/ui/modals/ModalProvider'; -import { SendAssetsData } from '../../../components/ui/modals/SendAssetsModal'; -import { useDecentModal } from '../../../components/ui/modals/useDecentModal'; import PageHeader from '../../../components/ui/page/Header/PageHeader'; import Divider from '../../../components/ui/utils/Divider'; import { DAO_ROUTES } from '../../../constants/routes'; +import useSendAssetsActionModal from '../../../hooks/DAO/useSendAssetsActionModal'; import { useCanUserCreateProposal } from '../../../hooks/utils/useCanUserSubmitProposal'; import { analyticsEvents } from '../../../insights/analyticsEvents'; import { useFractal } from '../../../providers/App/AppProvider'; @@ -27,7 +25,7 @@ export function SafeProposalTemplatesPage() { const { t } = useTranslation(); const { - governance: { proposalTemplates, isAzorius }, + governance: { proposalTemplates }, } = useFractal(); const { safe } = useDaoInfoStore(); const { canUserCreateProposal } = useCanUserCreateProposal(); @@ -35,13 +33,7 @@ export function SafeProposalTemplatesPage() { const navigate = useNavigate(); const safeAddress = safe?.address; - const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, { - onSubmit: (sendAssetData: SendAssetsData) => { - console.log(sendAssetData); - }, - submitButtonText: t('submitProposal', { ns: 'modals' }), - showNonceInput: !isAzorius, - }); + const { openSendAssetsModal } = useSendAssetsActionModal(); const EXAMPLE_TEMPLATES = useMemo(() => { if (!safeAddress) return []; diff --git a/src/pages/dao/treasury/SafeTreasuryPage.tsx b/src/pages/dao/treasury/SafeTreasuryPage.tsx index c326f8580..05364b99d 100644 --- a/src/pages/dao/treasury/SafeTreasuryPage.tsx +++ b/src/pages/dao/treasury/SafeTreasuryPage.tsx @@ -2,7 +2,6 @@ import * as amplitude from '@amplitude/analytics-browser'; import { Box, Divider, Flex, Grid, GridItem, Show } from '@chakra-ui/react'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { useNavigate } from 'react-router-dom'; import { Assets } from '../../../components/DAOTreasury/components/Assets'; import { PaginationButton, @@ -10,39 +9,25 @@ import { Transactions, } from '../../../components/DAOTreasury/components/Transactions'; import { TitledInfoBox } from '../../../components/ui/containers/TitledInfoBox'; -import { ModalType } from '../../../components/ui/modals/ModalProvider'; -import { SendAssetsData } from '../../../components/ui/modals/SendAssetsModal'; -import { useDecentModal } from '../../../components/ui/modals/useDecentModal'; import PageHeader from '../../../components/ui/page/Header/PageHeader'; -import { DAO_ROUTES } from '../../../constants/routes'; +import useSendAssetsActionModal from '../../../hooks/DAO/useSendAssetsActionModal'; import { useCanUserCreateProposal } from '../../../hooks/utils/useCanUserSubmitProposal'; import { analyticsEvents } from '../../../insights/analyticsEvents'; import { useFractal } from '../../../providers/App/AppProvider'; -import { useNetworkConfigStore } from '../../../providers/NetworkConfig/useNetworkConfigStore'; -import { useProposalActionsStore } from '../../../store/actions/useProposalActionsStore'; import { useDaoInfoStore } from '../../../store/daoInfo/useDaoInfoStore'; -import { ProposalActionType } from '../../../types'; -import { - isNativeAsset, - prepareSendAssetsActionData, -} from '../../../utils/dao/prepareSendAssetsActionData'; export function SafeTreasuryPage() { useEffect(() => { amplitude.track(analyticsEvents.TreasuryPageOpened); }, []); - const { safe } = useDaoInfoStore(); const { - governance: { isAzorius }, treasury: { assetsFungible, transfers }, } = useFractal(); const { subgraphInfo } = useDaoInfoStore(); const [shownTransactions, setShownTransactions] = useState(20); const { t } = useTranslation(['treasury', 'modals']); const { canUserCreateProposal } = useCanUserCreateProposal(); - const { addAction } = useProposalActionsStore(); - const navigate = useNavigate(); - const { addressPrefix } = useNetworkConfigStore(); + const hasAnyBalanceOfAnyFungibleTokens = assetsFungible.reduce((p, c) => p + BigInt(c.balance), 0n) > 0n; @@ -50,44 +35,7 @@ export function SafeTreasuryPage() { const totalTransfers = transfers?.length || 0; const showLoadMoreTransactions = totalTransfers > shownTransactions && shownTransactions < 100; - const sendAssetsAction = async (sendAssetsData: SendAssetsData) => { - if (!safe?.address) { - return; - } - const isNative = isNativeAsset(sendAssetsData.asset); - const transactionData = prepareSendAssetsActionData({ - transferAmount: sendAssetsData.transferAmount, - asset: sendAssetsData.asset, - destinationAddress: sendAssetsData.destinationAddress, - }); - addAction({ - actionType: ProposalActionType.TRANSFER, - content: <>, - transactions: [ - { - targetAddress: transactionData.calldata, - ethValue: { - bigintValue: transactionData.value, - value: transactionData.value.toString(), - }, - functionName: isNative ? '' : 'transfer', - parameters: isNative - ? [] - : [ - { signature: 'address', value: sendAssetsData.destinationAddress }, - { signature: 'uint256', value: sendAssetsData.transferAmount.toString() }, - ], - }, - ], - }); - navigate(DAO_ROUTES.proposalWithActionsNew.relative(addressPrefix, safe.address)); - }; - - const openSendAssetsModal = useDecentModal(ModalType.SEND_ASSETS, { - onSubmit: sendAssetsAction, - submitButtonText: t('submitProposal', { ns: 'modals' }), - showNonceInput: !isAzorius, - }); + const { openSendAssetsModal } = useSendAssetsActionModal(); return (