From 472834cbaf964e3b23f1bc6c97987fdc29407dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sza=C5=82owski?= Date: Wed, 19 Mar 2025 13:37:07 +0100 Subject: [PATCH 1/2] fix: filtering drep votes --- govtool/backend/src/VVA/API.hs | 16 +++++++++------- govtool/backend/src/VVA/DRep.hs | 6 ++---- govtool/backend/src/VVA/Types.hs | 17 +++++++++-------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/govtool/backend/src/VVA/API.hs b/govtool/backend/src/VVA/API.hs index 5bfbd304d..7b9d0fe4c 100644 --- a/govtool/backend/src/VVA/API.hs +++ b/govtool/backend/src/VVA/API.hs @@ -283,18 +283,20 @@ getVotes (unHexText -> dRepId) selectedTypes sortMode mSearch = do CacheEnv {dRepGetVotesCache} <- asks vvaCache (votes, proposals) <- cacheRequest dRepGetVotesCache dRepId $ DRep.getVotes dRepId [] - let voteMap = Map.fromList $ map (\vote@Types.Vote {..} -> (voteProposalId, vote)) votes - - processedProposals <- filter (isProposalSearchedFor mSearch) <$> mapSortAndFilterProposals selectedTypes sortMode proposals - + let voteMapByTxHash = Map.fromList $ + map (\vote -> (pack $ Prelude.takeWhile (/= '#') (unpack $ Types.voteGovActionId vote), vote)) votes + + processedProposals <- filter (isProposalSearchedFor mSearch) <$> + mapSortAndFilterProposals selectedTypes sortMode proposals + return $ [ VoteResponse { voteResponseVote = voteToResponse vote , voteResponseProposal = proposalResponse } - | proposalResponse@ProposalResponse{proposalResponseId} <- processedProposals - , let proposalIdInt = read (unpack proposalResponseId) :: Int - , Just vote <- [Map.lookup (toInteger proposalIdInt) voteMap] + | proposalResponse <- processedProposals + , let txHash = unHexText (proposalResponseTxHash proposalResponse) + , Just vote <- [Map.lookup txHash voteMapByTxHash] ] drepInfo :: App m => HexText -> m DRepInfoResponse diff --git a/govtool/backend/src/VVA/DRep.hs b/govtool/backend/src/VVA/DRep.hs index 187cdc73d..16f6d70d0 100644 --- a/govtool/backend/src/VVA/DRep.hs +++ b/govtool/backend/src/VVA/DRep.hs @@ -115,16 +115,14 @@ getVotes :: m ([Vote], [Proposal]) getVotes drepId selectedProposals = withPool $ \conn -> do results <- liftIO $ SQL.query conn getVotesSql (SQL.Only drepId) - + if null results then return ([], []) else do let proposalsToSelect = if null selectedProposals then [ govActionId | (_, govActionId, _, _, _, _, _, _, _) <- results] else selectedProposals - allProposals <- mapM (Proposal.getProposals . Just . (:[])) proposalsToSelect - let proposals = concat allProposals let proposalMap = M.fromList $ map (\x -> (proposalId x, x)) proposals @@ -132,7 +130,7 @@ getVotes drepId selectedProposals = withPool $ \conn -> do timeZone <- liftIO getCurrentTimeZone let votes = - [ Vote proposalId' drepId' vote' url' docHash' epochNo' (localTimeToUTC timeZone date') voteTxHash' + [ Vote proposalId' govActionId' drepId' vote' url' docHash' epochNo' (localTimeToUTC timeZone date') voteTxHash' | (proposalId', govActionId', drepId', vote', url', docHash', epochNo', date', voteTxHash') <- results , govActionId' `elem` proposalsToSelect ] diff --git a/govtool/backend/src/VVA/Types.hs b/govtool/backend/src/VVA/Types.hs index 5d1f3ca5f..5af17aa16 100644 --- a/govtool/backend/src/VVA/Types.hs +++ b/govtool/backend/src/VVA/Types.hs @@ -61,14 +61,15 @@ instance Exception AppError data Vote = Vote - { voteProposalId :: Integer - , voteDrepId :: Text - , voteVote :: Text - , voteUrl :: Maybe Text - , voteDocHash :: Maybe Text - , voteEpochNo :: Integer - , voteDate :: UTCTime - , voteTxHash :: Text + { voteProposalId :: Integer + , voteGovActionId :: Text + , voteDrepId :: Text + , voteVote :: Text + , voteUrl :: Maybe Text + , voteDocHash :: Maybe Text + , voteEpochNo :: Integer + , voteDate :: UTCTime + , voteTxHash :: Text } data DRepInfo From f510e4b18616a915b9fe36baf603a98a6cb4a41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sza=C5=82owski?= Date: Wed, 19 Mar 2025 15:06:46 +0100 Subject: [PATCH 2/2] fix(#3242): fix redirection after vote --- CHANGELOG.md | 3 +- .../organisms/DashboardGovernanceActions.tsx | 32 +++++++++++++++-- .../DashboardGovernanceActionsVotedOn.tsx | 34 ++++++++----------- .../src/hooks/queries/useGetDRepVotesQuery.ts | 2 ++ .../src/hooks/queries/useGetProposalsQuery.ts | 13 +++---- govtool/frontend/src/types/global.d.ts | 3 +- 6 files changed, 54 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5c444349..6853205ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,13 +14,14 @@ changes. ### Fixed +- Fix post-vote navigation to governance action list [Issue 3242](https://github.com/IntersectMBO/govtool/issues/3242) + ### Changed ### Removed ## [v2.0.17](https://github.com/IntersectMBO/govtool/releases/tag/v2.0.17) 2025-03-18 - ### Added ### Fixed diff --git a/govtool/frontend/src/components/organisms/DashboardGovernanceActions.tsx b/govtool/frontend/src/components/organisms/DashboardGovernanceActions.tsx index 6c246b929..b1b4f80e1 100644 --- a/govtool/frontend/src/components/organisms/DashboardGovernanceActions.tsx +++ b/govtool/frontend/src/components/organisms/DashboardGovernanceActions.tsx @@ -10,6 +10,7 @@ import { } from "@consts"; import { useCardano, useDataActionsBar, useFeatureFlag } from "@context"; import { + useGetDRepVotesQuery, useGetProposalsQuery, useGetVoterInfo, useScreenDimension, @@ -89,6 +90,31 @@ export const DashboardGovernanceActions = () => { searchPhrase: debouncedSearchText, enabled: !isAdjusting, }); + const { data: votes, areDRepVotesLoading } = useGetDRepVotesQuery( + queryFilters, + chosenSorting, + debouncedSearchText, + ); + + // TODO: Black magic - that filtering should be done on the backend + const filteredProposals = proposals + ?.map((proposalCategory) => { + const filteredActions = proposalCategory.actions.filter((action) => { + const hasVote = votes?.some((voteCategory) => + voteCategory.actions.some( + (voteAction) => voteAction.proposal.txHash === action.txHash, + ), + ); + + return !hasVote; + }); + + return { + ...proposalCategory, + actions: filteredActions, + }; + }) + .filter((category) => category.actions.length > 0); const { state } = useLocation(); const [content, setContent] = useState( @@ -189,14 +215,14 @@ export const DashboardGovernanceActions = () => { onDashboard searchPhrase={debouncedSearchText} sorting={chosenSorting} - proposals={proposals} + proposals={filteredProposals} /> diff --git a/govtool/frontend/src/components/organisms/DashboardGovernanceActionsVotedOn.tsx b/govtool/frontend/src/components/organisms/DashboardGovernanceActionsVotedOn.tsx index 5d4dcf134..552f63061 100644 --- a/govtool/frontend/src/components/organisms/DashboardGovernanceActionsVotedOn.tsx +++ b/govtool/frontend/src/components/organisms/DashboardGovernanceActionsVotedOn.tsx @@ -2,38 +2,34 @@ import { useMemo } from "react"; import { Box, Typography, CircularProgress } from "@mui/material"; import { useCardano } from "@context"; -import { - useGetDRepVotesQuery, - useScreenDimension, - useTranslation, -} from "@hooks"; +import { useScreenDimension, useTranslation } from "@hooks"; import { GovernanceVotedOnCard } from "@molecules"; import { Slider } from "@organisms"; import { getFullGovActionId, getProposalTypeLabel } from "@utils"; +import { VotedProposal } from "@/models"; type DashboardGovernanceActionsVotedOnProps = { - filters: string[]; searchPhrase?: string; - sorting: string; + votes: { + title: string; + actions: VotedProposal[]; + }[]; + areDRepVotesLoading: boolean; }; export const DashboardGovernanceActionsVotedOn = ({ - filters, searchPhrase, - sorting, + votes, + areDRepVotesLoading, }: DashboardGovernanceActionsVotedOnProps) => { - const { data, areDRepVotesLoading } = useGetDRepVotesQuery( - filters, - sorting, - searchPhrase, - ); const { isMobile } = useScreenDimension(); const { pendingTransaction } = useCardano(); const { t } = useTranslation(); + // TODO: Filtering here is some kind of craziness. It should be done on the backend. const filteredData = useMemo(() => { - if (data.length && searchPhrase) { - return data + if (votes.length && searchPhrase) { + return votes .map((entry) => ({ ...entry, actions: entry.actions.filter((action) => @@ -44,8 +40,8 @@ export const DashboardGovernanceActionsVotedOn = ({ })) .filter((entry) => entry.actions?.length > 0); } - return data; - }, [data, searchPhrase, pendingTransaction.vote]); + return votes; + }, [votes, searchPhrase, pendingTransaction.vote]); return areDRepVotesLoading ? ( @@ -53,7 +49,7 @@ export const DashboardGovernanceActionsVotedOn = ({ ) : ( <> - {!data.length ? ( + {!votes.length ? ( {t("govActions.youHaventVotedYet")} diff --git a/govtool/frontend/src/hooks/queries/useGetDRepVotesQuery.ts b/govtool/frontend/src/hooks/queries/useGetDRepVotesQuery.ts index bec791c61..0878191bc 100644 --- a/govtool/frontend/src/hooks/queries/useGetDRepVotesQuery.ts +++ b/govtool/frontend/src/hooks/queries/useGetDRepVotesQuery.ts @@ -30,6 +30,8 @@ export const useGetDRepVotesQuery = ( }, }), enabled: !!dRepID, + refetchOnWindowFocus: true, + keepPreviousData: true, }); const groupedByType = data?.reduce((groups, item) => { diff --git a/govtool/frontend/src/hooks/queries/useGetProposalsQuery.ts b/govtool/frontend/src/hooks/queries/useGetProposalsQuery.ts index 6843f76af..0ebbe8677 100644 --- a/govtool/frontend/src/hooks/queries/useGetProposalsQuery.ts +++ b/govtool/frontend/src/hooks/queries/useGetProposalsQuery.ts @@ -12,7 +12,7 @@ export const useGetProposalsQuery = ({ sorting, enabled, }: GetProposalsArguments) => { - const { dRepID, pendingTransaction } = useCardano(); + const { dRepID } = useCardano(); const { voter } = useGetVoterInfo(); const fetchProposals = async (): Promise => { @@ -34,17 +34,12 @@ export const useGetProposalsQuery = ({ }; const { data, isLoading } = useQuery( - [ - QUERY_KEYS.useGetProposalsKey, - filters, - searchPhrase, - sorting, - dRepID, - pendingTransaction.vote?.transactionHash, - ], + [QUERY_KEYS.useGetProposalsKey, filters, searchPhrase, sorting, dRepID], fetchProposals, { enabled, + refetchOnWindowFocus: true, + keepPreviousData: true, }, ); diff --git a/govtool/frontend/src/types/global.d.ts b/govtool/frontend/src/types/global.d.ts index 7f990a3d3..8e8ebaea5 100644 --- a/govtool/frontend/src/types/global.d.ts +++ b/govtool/frontend/src/types/global.d.ts @@ -14,6 +14,7 @@ declare global { type ActionTypeFromAPI = { id: string; + txHash: string; type: string; details: string; expiryDate: string; @@ -36,7 +37,7 @@ declare global { type ToVoteDataType = { title: string; - actions: ActionTypeToDsiplay[]; + actions: ProposalData[]; }[]; type NestedKeys = T extends Record