Skip to content

Commit

Permalink
refactor: refactor: create dedicated composable for vote voting power
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 28, 2024
1 parent 1b269b6 commit 595dc13
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 54 deletions.
8 changes: 5 additions & 3 deletions apps/ui/src/components/Modal/Vote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const emit = defineEmits<{
const { vote } = useActions();
const { web3 } = useWeb3();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
const proposalsStore = useProposalsStore();
Expand All @@ -55,6 +55,8 @@ const formValidator = getValidator({
}
});
const votingPower = computed(() => getProposalVp(props.proposal));
const formattedVotingPower = computed(() =>
getFormattedVotingPower(votingPower.value)
);
Expand Down Expand Up @@ -124,7 +126,7 @@ async function handleConfirmed(tx?: string | null) {
}
function handleFetchVotingPower() {
fetchVotingPower(props.proposal);
fetchProposalVp(props.proposal);
}
watch(
Expand Down
80 changes: 38 additions & 42 deletions apps/ui/src/composables/useVotingPower.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,57 @@
import { supportsNullCurrent } from '@/networks';
import { getIndex as getVotingPowerIndex } from '@/stores/votingPowers';
import { Proposal, Space } from '@/types';
import { getIndex } from '@/stores/votingPowers';
import { NetworkID, Proposal, Space } from '@/types';

export function useVotingPower() {
const votingPowersStore = useVotingPowersStore();
const { web3 } = useWeb3();
const { getCurrent } = useMetaStore();

const item = ref<Space | Proposal | undefined>();
const block = ref<number | null>(null);

const space = computed(() =>
item.value && 'space' in item.value
? (item.value?.space as Space)
: item.value
);
function latestBlock(network: NetworkID) {
return supportsNullCurrent(network) ? null : getCurrent(network) || 0;
}

const proposal = computed(() =>
item.value && 'snapshot' in item.value
? (item.value as Proposal)
: undefined
);
function proposalSnapshot(proposal: Proposal) {
return (
(proposal.state === 'pending' ? null : proposal.snapshot) ||
latestBlock(proposal.network)
);
}

const proposalSnapshot = computed<number | null>(() => {
if (!proposal.value) return null;
function fetchProposalVp(proposal: Proposal) {
if (!web3.value.account) return;

return proposal.value.state === 'pending'
? latestBlock(proposal.value)
: proposal.value.snapshot;
});
votingPowersStore.fetch(
proposal,
web3.value.account,
proposalSnapshot(proposal)
);
}

const votingPower = computed(
() =>
space.value &&
votingPowersStore.votingPowers.get(
getVotingPowerIndex(space.value, block.value)
)
);
function fetchSpaceVp(space: Space) {
if (!web3.value.account) return;

function latestBlock(spaceOrProposal: Space | Proposal) {
return supportsNullCurrent(spaceOrProposal.network)
? null
: getCurrent(spaceOrProposal.network) ?? 0;
votingPowersStore.fetch(
space,
web3.value.account,
latestBlock(space.network)
);
}

function reset() {
votingPowersStore.reset();
function getProposalVp(proposal: Proposal) {
return votingPowersStore.votingPowers.get(
getIndex(proposal.space, proposalSnapshot(proposal))
);
}

function fetch(spaceOrProposal: Space | Proposal) {
if (!web3.value.account) return;
item.value = spaceOrProposal;
block.value = proposal.value
? proposalSnapshot.value
: latestBlock(space.value as Space);
function getSpaceVp(space: Space) {
return votingPowersStore.votingPowers.get(
getIndex(space, latestBlock(space.network))
);
}

votingPowersStore.fetch(item.value, web3.value.account, block.value);
function reset() {
votingPowersStore.reset();
}

watch(
Expand All @@ -65,5 +61,5 @@ export function useVotingPower() {
}
);

return { votingPower, fetch, reset };
return { getProposalVp, getSpaceVp, fetchProposalVp, fetchSpaceVp, reset };
}
12 changes: 9 additions & 3 deletions apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const props = defineProps<{
const route = useRoute();
const proposalsStore = useProposalsStore();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
const { setTitle } = useTitle();
Expand Down Expand Up @@ -39,6 +39,12 @@ const discussion = computed(() => {
return sanitizeUrl(proposal.value.discussion);
});
const votingPower = computed(() => {
if (!proposal.value) return;
return getProposalVp(proposal.value);
});
const votingPowerDecimals = computed(() => {
if (!proposal.value) return 0;
return Math.max(
Expand Down Expand Up @@ -73,7 +79,7 @@ async function handleVoteSubmitted() {
function handleFetchVotingPower() {
if (!proposal.value) return;
fetchVotingPower(proposal.value);
fetchProposalVp(proposal.value);
}
watch(
Expand Down
10 changes: 4 additions & 6 deletions apps/ui/src/views/Space/Proposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { Space } from '@/types';
const props = defineProps<{ space: Space }>();
const { setTitle } = useTitle();
const {
votingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
const { getSpaceVp, fetchSpaceVp, reset: resetVotingPower } = useVotingPower();
const { web3 } = useWeb3();
const router = useRouter();
const route = useRoute();
Expand All @@ -27,14 +23,16 @@ const proposalsRecord = computed(
() => proposalsStore.proposals[`${props.space.network}:${props.space.id}`]
);
const votingPower = computed(() => getSpaceVp(props.space));
async function handleEndReached() {
if (!proposalsRecord.value?.hasMoreProposals) return;
proposalsStore.fetchMore(props.space.id, props.space.network);
}
function handleFetchVotingPower() {
fetchVotingPower(props.space);
fetchSpaceVp(props.space);
}
watch(
Expand Down

0 comments on commit 595dc13

Please sign in to comment.