|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | +import { useHasVoteTimeExpired } from '../hooks/useHasVoteTimeExpired' |
| 4 | +import useRealm from '../hooks/useRealm' |
| 5 | +import { getSignatoryRecordAddress, ProposalState } from '../models/accounts' |
| 6 | +import useWalletStore from '../stores/useWalletStore' |
| 7 | +import Button, { SecondaryButton } from './Button' |
| 8 | +import CancelProposalModal from './CancelProposalModal' |
| 9 | +import FinalizeVotesModal from './FinalizeVotesModal' |
| 10 | +import SignOffProposalModal from './SignOffProposalModal' |
| 11 | + |
| 12 | +const ProposalActionsPanel = () => { |
| 13 | + const { governance, proposal, proposalOwner } = useWalletStore( |
| 14 | + (s) => s.selectedProposal |
| 15 | + ) |
| 16 | + const { realmInfo } = useRealm() |
| 17 | + const wallet = useWalletStore((s) => s.current) |
| 18 | + const connected = useWalletStore((s) => s.connected) |
| 19 | + const hasVoteTimeExpired = useHasVoteTimeExpired(governance, proposal!) |
| 20 | + const signatories = useWalletStore((s) => s.selectedProposal.signatories) |
| 21 | + |
| 22 | + const [showSignOffModal, setShowSignOffModal] = useState(false) |
| 23 | + const [signatoryRecord, setSignatoryRecord] = useState<any>(undefined) |
| 24 | + const [showFinalizeVoteModal, setShowFinalizeVoteModal] = useState(false) |
| 25 | + const [showCancelModal, setShowCancelModal] = useState(false) |
| 26 | + |
| 27 | + const canFinalizeVote = |
| 28 | + hasVoteTimeExpired === true && |
| 29 | + connected && |
| 30 | + proposal?.info.state === ProposalState.Voting |
| 31 | + |
| 32 | + const walletPk = wallet?.publicKey |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const setup = async () => { |
| 36 | + if (proposal && realmInfo && walletPk) { |
| 37 | + const signatoryRecordPk = await getSignatoryRecordAddress( |
| 38 | + realmInfo.programId, |
| 39 | + proposal.pubkey, |
| 40 | + walletPk |
| 41 | + ) |
| 42 | + |
| 43 | + if (signatoryRecordPk && signatories) { |
| 44 | + setSignatoryRecord(signatories[signatoryRecordPk.toBase58()]) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + setup() |
| 50 | + }, [proposal, realmInfo, walletPk]) |
| 51 | + |
| 52 | + const canSignOff = |
| 53 | + signatoryRecord && |
| 54 | + (proposal?.info.state === ProposalState.Draft || |
| 55 | + proposal?.info.state === ProposalState.SigningOff) |
| 56 | + |
| 57 | + const canCancelProposal = |
| 58 | + proposal && |
| 59 | + governance && |
| 60 | + proposalOwner && |
| 61 | + wallet?.publicKey && |
| 62 | + proposal.info.canWalletCancel( |
| 63 | + governance.info, |
| 64 | + proposalOwner.info, |
| 65 | + wallet.publicKey |
| 66 | + ) |
| 67 | + |
| 68 | + return ( |
| 69 | + <> |
| 70 | + {ProposalState.Cancelled === proposal?.info.state || |
| 71 | + ProposalState.Succeeded === proposal?.info.state || |
| 72 | + ProposalState.Defeated === proposal?.info.state || |
| 73 | + (!canCancelProposal && !canSignOff && canFinalizeVote) ? null : ( |
| 74 | + <div> |
| 75 | + <div className="bg-bkg-2 rounded-lg p-6 space-y-6 flex justify-center items-center text-center flex-col w-full mt-4"> |
| 76 | + {canSignOff && ( |
| 77 | + <Button |
| 78 | + className="w-1/2" |
| 79 | + onClick={() => setShowSignOffModal(true)} |
| 80 | + disabled={!connected || !canSignOff} |
| 81 | + > |
| 82 | + Sign Off |
| 83 | + </Button> |
| 84 | + )} |
| 85 | + |
| 86 | + {canCancelProposal && ( |
| 87 | + <SecondaryButton |
| 88 | + className="w-1/2" |
| 89 | + onClick={() => setShowCancelModal(true)} |
| 90 | + disabled={!connected} |
| 91 | + > |
| 92 | + Cancel |
| 93 | + </SecondaryButton> |
| 94 | + )} |
| 95 | + |
| 96 | + {canFinalizeVote && ( |
| 97 | + <Button |
| 98 | + className="w-1/2" |
| 99 | + onClick={() => setShowFinalizeVoteModal(true)} |
| 100 | + disabled={!connected || !canFinalizeVote} |
| 101 | + > |
| 102 | + Finalize |
| 103 | + </Button> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + |
| 107 | + {showSignOffModal && ( |
| 108 | + <SignOffProposalModal |
| 109 | + isOpen={showSignOffModal && canSignOff} |
| 110 | + onClose={() => setShowSignOffModal(false)} |
| 111 | + signatoryRecord={signatoryRecord} |
| 112 | + /> |
| 113 | + )} |
| 114 | + |
| 115 | + {showFinalizeVoteModal && ( |
| 116 | + <FinalizeVotesModal |
| 117 | + isOpen={showFinalizeVoteModal && canFinalizeVote} |
| 118 | + onClose={() => setShowFinalizeVoteModal(false)} |
| 119 | + proposal={proposal} |
| 120 | + governance={governance} |
| 121 | + /> |
| 122 | + )} |
| 123 | + |
| 124 | + {showCancelModal && ( |
| 125 | + <CancelProposalModal |
| 126 | + // @ts-ignore |
| 127 | + isOpen={showCancelModal && canCancelProposal} |
| 128 | + onClose={() => setShowCancelModal(false)} |
| 129 | + /> |
| 130 | + )} |
| 131 | + </div> |
| 132 | + )} |
| 133 | + </> |
| 134 | + ) |
| 135 | +} |
| 136 | + |
| 137 | +export default ProposalActionsPanel |
0 commit comments