From afb3e4c7335d87ba530ce962a492e9e223632c7b Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Wed, 22 Nov 2023 11:09:03 +0100 Subject: [PATCH 1/2] Fix snapshot/shutter bugs, fix loading state when navigating between dao --- app/daos/[daoAddress]/layout.tsx | 6 +++--- .../Proposals/ProposalActions/ProposalAction.tsx | 2 +- src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts | 8 ++------ src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts | 1 + src/hooks/DAO/loaders/useFractalNode.ts | 3 ++- src/hooks/DAO/useDAOController.ts | 5 ++++- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/daos/[daoAddress]/layout.tsx b/app/daos/[daoAddress]/layout.tsx index c93d6198a2..edc8a86031 100644 --- a/app/daos/[daoAddress]/layout.tsx +++ b/app/daos/[daoAddress]/layout.tsx @@ -74,7 +74,7 @@ export default function DaoPageLayout({ params: { daoAddress?: string }; }) { const { node } = useFractal(); - const loading = useDAOController({ daoAddress }); + const { nodeLoading, reloadingDAO } = useDAOController({ daoAddress }); const { chain } = useNetwork(); const validSafe = node.safe; @@ -84,7 +84,7 @@ export default function DaoPageLayout({ display = children; } else if (!chain) { // if we're disconnected - if (loading || validSafe) { + if (nodeLoading || reloadingDAO || validSafe) { display = children; } else { display = ; @@ -94,7 +94,7 @@ export default function DaoPageLayout({ const invalidChain = !supportedChains.map(c => c.chainId).includes(chain.id); if (invalidChain) { display = ; - } else if (loading || validSafe) { + } else if (nodeLoading || reloadingDAO || validSafe) { display = children; } else { display = ; diff --git a/src/components/Proposals/ProposalActions/ProposalAction.tsx b/src/components/Proposals/ProposalActions/ProposalAction.tsx index bad0ed455a..c28c14db14 100644 --- a/src/components/Proposals/ProposalActions/ProposalAction.tsx +++ b/src/components/Proposals/ProposalActions/ProposalAction.tsx @@ -71,7 +71,7 @@ export function ProposalAction({ ); const showActionButton = - (isSnapshotProposal && canVote) || + (isSnapshotProposal && canVote && isActiveProposal) || (user.votingWeight.gt(0) && (isActiveProposal || proposal.state === FractalProposalState.EXECUTABLE || diff --git a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts index e162641749..793dfab355 100644 --- a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts +++ b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts @@ -27,11 +27,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u [snapshotProposal] ); - const getVoteWeight = useCallback( - (vote: SnapshotVote) => - vote.votingWeight * vote.votingWeightByStrategy.reduce((prev, curr) => prev + curr, 0), - [] - ); + const getVoteWeight = useCallback((vote: SnapshotVote) => vote.votingWeight, []); const loadProposal = useCallback(async () => { if (snapshotProposal?.snapshotProposalId) { @@ -78,7 +74,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u const votesQueryResult = await client .query({ query: gql`query SnapshotProposalVotes { - votes(where: {proposal: "${snapshotProposal.snapshotProposalId}"}) { + votes(where: {proposal: "${snapshotProposal.snapshotProposalId}"}, first: 100) { id voter vp diff --git a/src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts b/src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts index a8c76f888f..4d660805d9 100644 --- a/src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts +++ b/src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts @@ -19,6 +19,7 @@ export const useSnapshotProposals = () => { query: gql` query Proposals { proposals( + first: 50, where: { space_in: ["${daoSnapshotURL}"] }, diff --git a/src/hooks/DAO/loaders/useFractalNode.ts b/src/hooks/DAO/loaders/useFractalNode.ts index d02a446136..b9383cca7d 100644 --- a/src/hooks/DAO/loaders/useFractalNode.ts +++ b/src/hooks/DAO/loaders/useFractalNode.ts @@ -123,9 +123,10 @@ export const useFractalNode = ({ daoAddress }: { daoAddress?: string }) => { const chainId = chain ? chain.id : disconnectedChain.id; useEffect(() => { if (daoAddress && chainId + daoAddress !== currentValidSafe.current) { + setNodeLoading(true); setDAO(chainId, daoAddress); } - }, [daoAddress, setDAO, action, currentValidSafe, chainId]); + }, [daoAddress, setDAO, currentValidSafe, chainId]); return nodeLoading; }; diff --git a/src/hooks/DAO/useDAOController.ts b/src/hooks/DAO/useDAOController.ts index e24feddacf..a91af59f21 100644 --- a/src/hooks/DAO/useDAOController.ts +++ b/src/hooks/DAO/useDAOController.ts @@ -13,6 +13,7 @@ import { useGovernanceContracts } from './loaders/useGovernanceContracts'; export default function useDAOController({ daoAddress }: { daoAddress?: string }) { const [currentDAOAddress, setCurrentDAOAddress] = useState(); + const [reloadingDAO, setReloadingDAO] = useState(false); const { node: { nodeHierarchy: { parentAddress }, @@ -21,8 +22,10 @@ export default function useDAOController({ daoAddress }: { daoAddress?: string } } = useFractal(); useEffect(() => { if (daoAddress && currentDAOAddress !== daoAddress) { + setReloadingDAO(true); action.resetDAO().then(() => { setCurrentDAOAddress(daoAddress); + setReloadingDAO(false); }); } }, [daoAddress, currentDAOAddress, action]); @@ -35,5 +38,5 @@ export default function useDAOController({ daoAddress }: { daoAddress?: string } useFractalTreasury(); useERC20Claim(); useSnapshotProposals(); - return nodeLoading; + return { nodeLoading, reloadingDAO }; } From 8e0aa2e1367872d9a7aa2931a85f4fbb29a06949 Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Wed, 22 Nov 2023 11:16:33 +0100 Subject: [PATCH 2/2] Remove getVoteWeight from useSnapshotProposal --- .../SnapshotProposalVoteItem.tsx | 4 +--- .../hooks/useTotalVotes.ts | 6 ++---- .../DAO/loaders/snapshot/useSnapshotProposal.ts | 17 +++++++---------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVoteItem.tsx b/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVoteItem.tsx index 95df867cbf..17560334ec 100644 --- a/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVoteItem.tsx +++ b/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVoteItem.tsx @@ -1,6 +1,5 @@ import { Grid, GridItem, Text, Flex } from '@chakra-ui/react'; import { useTranslation } from 'react-i18next'; -import useSnapshotProposal from '../../../hooks/DAO/loaders/snapshot/useSnapshotProposal'; import useDisplayName from '../../../hooks/utils/useDisplayName'; import { useFractal } from '../../../providers/App/AppProvider'; import { @@ -17,7 +16,6 @@ interface ISnapshotProposalVoteItem { export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotProposalVoteItem) { const { t } = useTranslation(); - const { getVoteWeight } = useSnapshotProposal(proposal); const { displayName } = useDisplayName(vote.voter); const { readOnly: { user }, @@ -62,7 +60,7 @@ export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotPr - {getVoteWeight(vote)} {proposal.strategies[0].params.symbol} + {vote.votingWeight} {proposal.strategies[0].params.symbol} diff --git a/src/components/Proposals/SnapshotProposalDetails/hooks/useTotalVotes.ts b/src/components/Proposals/SnapshotProposalDetails/hooks/useTotalVotes.ts index f38fb85737..fcdf62d885 100644 --- a/src/components/Proposals/SnapshotProposalDetails/hooks/useTotalVotes.ts +++ b/src/components/Proposals/SnapshotProposalDetails/hooks/useTotalVotes.ts @@ -1,18 +1,16 @@ import { useState, useEffect } from 'react'; -import useSnapshotProposal from '../../../../hooks/DAO/loaders/snapshot/useSnapshotProposal'; import { ExtendedSnapshotProposal } from '../../../../types'; export default function useTotalVotes({ proposal }: { proposal?: ExtendedSnapshotProposal }) { const [totalVotesCasted, setTotalVotesCasted] = useState(0); - const { getVoteWeight } = useSnapshotProposal(proposal); useEffect(() => { if (proposal) { let newTotalVotesCasted = 0; - proposal.votes.forEach(vote => (newTotalVotesCasted += getVoteWeight(vote))); + proposal.votes.forEach(vote => (newTotalVotesCasted += vote.votingWeight)); setTotalVotesCasted(newTotalVotesCasted); } - }, [proposal, totalVotesCasted, getVoteWeight]); + }, [proposal, totalVotesCasted]); return { totalVotesCasted }; } diff --git a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts index 793dfab355..e6e0f15e7c 100644 --- a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts +++ b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts @@ -27,8 +27,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u [snapshotProposal] ); - const getVoteWeight = useCallback((vote: SnapshotVote) => vote.votingWeight, []); - const loadProposal = useCallback(async () => { if (snapshotProposal?.snapshotProposalId) { const proposalQueryResult = await client @@ -135,12 +133,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u const existingChoiceType = votesBreakdown[voteChoice]; if (existingChoiceType) { votesBreakdown[voteChoice] = { - total: existingChoiceType.total + getVoteWeight(vote), + total: existingChoiceType.total + vote.votingWeight, votes: [...existingChoiceType.votes, vote], }; } else { votesBreakdown[voteChoice] = { - total: getVoteWeight(vote), + total: vote.votingWeight, votes: [vote], }; } @@ -152,12 +150,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u const existingChoiceType = votesBreakdown[voteChoice]; if (existingChoiceType) { votesBreakdown[voteChoice] = { - total: existingChoiceType.total + getVoteWeight(vote), + total: existingChoiceType.total + vote.votingWeight, votes: [...existingChoiceType.votes, vote], }; } else { votesBreakdown[voteChoice] = { - total: getVoteWeight(vote), + total: vote.votingWeight, votes: [vote], }; } @@ -170,12 +168,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u if (existingChoiceType) { votesBreakdown[choiceKey] = { - total: existingChoiceType.total + getVoteWeight(vote), + total: existingChoiceType.total + vote.votingWeight, votes: [...existingChoiceType.votes, vote], }; } else { votesBreakdown[choiceKey] = { - total: getVoteWeight(vote), + total: vote.votingWeight, votes: [vote], }; } @@ -190,7 +188,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u votes: votesQueryResult, } as ExtendedSnapshotProposal); } - }, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state, getVoteWeight]); + }, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state]); const loadVotingWeight = useCallback(async () => { if (snapshotProposal?.snapshotProposalId) { @@ -230,7 +228,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u return { loadVotingWeight, loadProposal, - getVoteWeight, snapshotProposal, isSnapshotProposal, extendedSnapshotProposal,