Skip to content

Commit

Permalink
refactor: create dedicated composable for proposing power (#988)
Browse files Browse the repository at this point in the history
* refactor: extract proposing power into its own composable

* fix: use proposition variant everywhere
  • Loading branch information
wa0x6e authored Nov 28, 2024
1 parent 4b45ddd commit 6a8b3ed
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
40 changes: 40 additions & 0 deletions apps/ui/src/composables/usePropositionPower.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { supportsNullCurrent } from '@/networks';
import { getIndex } from '@/stores/votingPowers';
import { NetworkID, Space } from '@/types';

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

function latestBlock(network: NetworkID) {
return supportsNullCurrent(network) ? null : getCurrent(network) ?? 0;
}

function fetch(space: Space) {
votingPowersStore.fetch(
space,
web3.value.account,
latestBlock(space.network)
);
}

function get(space: Space) {
return votingPowersStore.votingPowers.get(
getIndex(space, latestBlock(space.network))
);
}

function reset() {
votingPowersStore.reset();
}

watch(
() => web3.value.account,
account => {
if (!account) reset();
}
);

return { fetch, get };
}
29 changes: 18 additions & 11 deletions apps/ui/src/views/Space/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const {
reset
} = useWalletConnectTransaction();
const proposalsStore = useProposalsStore();
const { votingPower, fetch: fetchVotingPower } = useVotingPower();
const { get: getPropositionPower, fetch: fetchPropositionPower } =
usePropositionPower();
const { strategiesWithTreasuries } = useTreasuries(props.space);
const termsStore = useTermsStore();
Expand Down Expand Up @@ -154,7 +155,7 @@ const canSubmit = computed(() => {
if (Object.keys(formErrors.value).length > 0) return false;
return web3.value.account
? votingPower.value?.canPropose
? propositionPower.value?.canPropose
: !web3.value.authLoading;
});
const spaceType = computed(() =>
Expand All @@ -167,6 +168,8 @@ const proposalLimitReached = computed(
(props.space.proposal_count_30d || 0) >= MAX_30D_PROPOSALS[spaceType.value]
);
const propositionPower = computed(() => getPropositionPower(props.space));
async function handleProposeClick() {
if (!proposal.value) return;
Expand Down Expand Up @@ -278,16 +281,16 @@ function handleTransactionAccept() {
reset();
}
function handleFetchVotingPower() {
fetchVotingPower(props.space);
function handleFetchPropositionPower() {
fetchPropositionPower(props.space);
}
watch(
() => web3.value.account,
toAccount => {
if (!toAccount) return;
handleFetchVotingPower();
handleFetchPropositionPower();
},
{ immediate: true }
);
Expand Down Expand Up @@ -358,7 +361,9 @@ watchEffect(() => {
class="primary min-w-[46px] flex gap-2 justify-center items-center !px-0 md:!px-3"
:loading="
!!web3.account &&
(sending || !votingPower || votingPower.status === 'loading')
(sending ||
!propositionPower ||
propositionPower.status === 'loading')
"
:disabled="!canSubmit"
@click="handleProposeClick"
Expand All @@ -373,14 +378,16 @@ watchEffect(() => {
<div class="md:mr-[340px]">
<UiContainer class="pt-5 !max-w-[710px] mx-0 md:mx-auto s-box">
<MessageVotingPower
v-if="votingPower"
v-if="propositionPower"
class="mb-4"
:voting-power="votingPower"
:voting-power="propositionPower"
action="propose"
@fetch-voting-power="handleFetchVotingPower"
@fetch-voting-power="handleFetchPropositionPower"
/>
<UiAlert
v-if="votingPower && spaceType === 'default' && proposalLimitReached"
v-if="
propositionPower && spaceType === 'default' && proposalLimitReached
"
type="error"
class="mb-4"
>
Expand All @@ -396,7 +403,7 @@ watchEffect(() => {
</UiAlert>
<UiAlert
v-else-if="
votingPower && spaceType !== 'turbo' && proposalLimitReached
propositionPower && spaceType !== 'turbo' && proposalLimitReached
"
type="error"
class="mb-4"
Expand Down

0 comments on commit 6a8b3ed

Please sign in to comment.