@@ -4,7 +4,9 @@ import { Typography } from "@atoms";
44import { useCardano , useDataActionsBar } from "@context" ;
55import {
66 useFetchNextPageDetector ,
7+ useGetDRepVotesQuery ,
78 useGetProposalsInfiniteQuery ,
9+ useGetVoterInfo ,
810 useSaveScrollPosition ,
911 useScreenDimension ,
1012 useTranslation ,
@@ -24,6 +26,7 @@ export const GovernanceActionsToVote = ({
2426 const { isMobile, screenWidth } = useScreenDimension ( ) ;
2527 const { debouncedSearchText, ...dataActionsBarProps } = useDataActionsBar ( ) ;
2628 const { chosenSorting, chosenFilters } = dataActionsBarProps ;
29+ const { voter } = useGetVoterInfo ( ) ;
2730 const { t } = useTranslation ( ) ;
2831
2932 const {
@@ -46,21 +49,59 @@ export const GovernanceActionsToVote = ({
4649 proposalsHaveNextPage ,
4750 ) ;
4851
52+ const {
53+ data : votes ,
54+ areDRepVotesLoading,
55+ isFetching : isFetchingVotes ,
56+ } = useGetDRepVotesQuery ( chosenFilters , chosenSorting , debouncedSearchText ) ;
57+
4958 const saveScrollPosition = useSaveScrollPosition (
5059 isProposalsLoading ,
5160 isProposalsFetching ,
5261 ) ;
5362
54- const mappedProposals = useMemo (
63+ const mappedData = useMemo (
5564 ( ) => removeDuplicatedProposals ( proposals ) ,
56- [ proposals , isProposalsFetchingNextPage ] ,
65+ [ proposals , voter ?. isRegisteredAsDRep , isProposalsFetchingNextPage ] ,
5766 ) ;
5867
68+ // TODO: Filtering here is some kind of craziness. It should be done on the backend.
69+ const filteredProposals = useMemo ( ( ) => {
70+ const list = mappedData ?? [ ] ;
71+ if ( ! votes ?. length ) return list ;
72+
73+ const proposalsFromVotes = votes
74+ . flatMap ( ( v ) => v ?. actions ?? [ ] )
75+ . map ( ( a ) => a ?. proposal )
76+ . filter ( Boolean ) ;
77+
78+ const votedKeys = new Set (
79+ proposalsFromVotes
80+ . map ( ( p ) => ( {
81+ id : p ?. id ?? p ?. id ,
82+ tx : p ?. txHash ?? p ?. txHash ,
83+ } ) )
84+ . filter ( ( { id, tx } ) => Boolean ( id && tx ) )
85+ . map ( ( { id, tx } ) => `${ id } :${ tx } ` ) ,
86+ ) ;
87+
88+ if ( votedKeys . size === 0 ) return list ;
89+
90+ return list . filter ( ( p ) => {
91+ const id = p ?. id ?? p ?. id ;
92+ const tx = p ?. txHash ?? p ?. txHash ;
93+ if ( ! id || ! tx ) return true ;
94+ return ! votedKeys . has ( `${ id } :${ tx } ` ) ;
95+ } ) ;
96+ } , [ mappedData , voter ?. isRegisteredAsDRep , isProposalsFetchingNextPage ] ) ;
97+
5998 return (
6099 < >
61- { ! mappedProposals ||
100+ { ! filteredProposals ||
62101 isEnableLoading ||
63- isProposalsLoading ? (
102+ isProposalsLoading ||
103+ areDRepVotesLoading ||
104+ isFetchingVotes ? (
64105 < Box
65106 sx = { {
66107 alignItems : "center" ,
@@ -72,7 +113,7 @@ export const GovernanceActionsToVote = ({
72113 >
73114 < CircularProgress />
74115 </ Box >
75- ) : ! mappedProposals ?. length ? (
116+ ) : ! filteredProposals ?. length ? (
76117 < Typography fontWeight = { 300 } sx = { { py : 4 } } >
77118 { t ( "govActions.noResultsForTheSearch" ) }
78119 </ Typography >
@@ -84,7 +125,7 @@ export const GovernanceActionsToVote = ({
84125 screenWidth < 420 ? "290px" : isMobile ? "324px" : "350px"
85126 } , 1fr))`}
86127 >
87- { mappedProposals . map ( ( item ) => (
128+ { filteredProposals . map ( ( item ) => (
88129 < Box pb = { 4.25 } key = { item . txHash + item . index } >
89130 < ValidatedGovernanceActionCard
90131 { ...item }
0 commit comments