Skip to content

Commit

Permalink
Fix proposal list items title (#91)
Browse files Browse the repository at this point in the history
* fix: ipfs error bug on the proposal list screen

* fix: ipfs data error

* fix: added max count
  • Loading branch information
Argeare5 authored May 27, 2024
1 parent 22509b4 commit 194428c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
41 changes: 32 additions & 9 deletions src/proposals/components/proposalList/ActiveProposalListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
} from '@bgd-labs/aave-governance-ui-helpers';
import { WalletType } from '@bgd-labs/frontend-web3-utils';
import { Box, useTheme } from '@mui/system';
import React, { useState } from 'react';
import { zeroAddress } from 'viem';
import React, { useEffect, useState } from 'react';
import { Hex, zeroAddress } from 'viem';

import { useStore } from '../../../store/ZustandStoreProvider';
import { Link } from '../../../ui';
Expand Down Expand Up @@ -43,9 +43,34 @@ export function ActiveProposalListItem({
const isRendered = useStore((state) => state.isRendered);
const appClients = useStore((state) => state.appClients);
const ipfsDataErrors = useStore((state) => state.ipfsDataErrors);
const ipfsData = useStore((state) => state.ipfsData);
const getIpfsData = useStore((state) => state.getIpfsData);
let activeWallet = useStore((state) => state.activeWallet);

const proposal = proposalData.proposal;
const loading = proposalData.loading;
const balanceLoading = proposalData.balanceLoading;

const [isClicked, setIsClicked] = useState(false);
const [isIPFSError, setIsIpfsError] = useState(
ipfsDataErrors[proposal.data.ipfsHash] && !ipfsData[proposal.data.ipfsHash],
);
const [ipfsErrorCount, setIpfsErrorCount] = useState(0);

const MAX_COUNT = 5;

useEffect(() => {
setIsIpfsError(
ipfsDataErrors[proposal.data.ipfsHash] &&
!ipfsData[proposal.data.ipfsHash],
);
if (isIPFSError && ipfsErrorCount <= MAX_COUNT) {
setTimeout(async () => {
getIpfsData([proposal.data.id], proposal.data.ipfsHash as Hex);
setIpfsErrorCount(ipfsErrorCount + 1);
}, 1000);
}
}, [ipfsErrorCount, isIPFSError, Object.keys(ipfsDataErrors).length]);

if (isForHelpModal) {
activeWallet = {
Expand All @@ -59,10 +84,6 @@ export function ActiveProposalListItem({
};
}

const proposal = proposalData.proposal;
const loading = proposalData.loading;
const balanceLoading = proposalData.balanceLoading;

const {
stateTimestamp,
estimatedState,
Expand Down Expand Up @@ -126,7 +147,7 @@ export function ActiveProposalListItem({
<Box
component={isForHelpModal ? Box : Link}
href={
ipfsDataErrors[proposal.data.ipfsHash]
isIPFSError
? ROUTES.proposalWithoutIpfs(proposal.data.id)
: ROUTES.proposal(proposal.data.id, proposal.data.ipfsHash)
}
Expand Down Expand Up @@ -193,8 +214,10 @@ export function ActiveProposalListItem({
? `${theme.palette.$text} !important`
: theme.palette.$text,
}}>
{ipfsDataErrors[proposal.data.ipfsHash] ? (
ipfsDataErrors[proposal.data.ipfsHash]
{isIPFSError && ipfsErrorCount > MAX_COUNT ? (
'Ipfs getting error'
) : isIPFSError ? (
<CustomSkeleton width={250} height={24} />
) : proposal.data.title ===
`Proposal #${proposal.data.id}` ? (
<CustomSkeleton width={250} height={24} />
Expand Down
32 changes: 24 additions & 8 deletions src/proposals/store/proposalsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export interface IProposalsSlice {

ipfsData: Record<string, ProposalMetadata>;
ipfsDataErrors: Record<string, string>;
setIpfsDataErrors: (ipfsHash: string, text?: string) => void;
setIpfsDataErrors: (
ipfsHash: string,
text?: string,
remove?: boolean,
) => void;
setIpfsData: (hash: string, data: ProposalMetadata) => void;
getIpfsData: (ids: number[], hash?: Hex) => Promise<void>;

Expand Down Expand Up @@ -459,13 +463,23 @@ export const createProposalsSlice: StoreSlice<

ipfsData: {},
ipfsDataErrors: {},
setIpfsDataErrors: (ipfsHash, text) => {
set((state) =>
produce(state, (draft) => {
draft.ipfsDataErrors[ipfsHash] =
text === '' ? '' : text || texts.other.fetchFromIpfsError;
}),
);
setIpfsDataErrors: (ipfsHash, text, remove) => {
if (remove) {
set((state) =>
produce(state, (draft) => {
if (!!draft.ipfsDataErrors[ipfsHash]) {
delete draft.ipfsDataErrors[ipfsHash];
}
}),
);
} else {
set((state) =>
produce(state, (draft) => {
draft.ipfsDataErrors[ipfsHash] =
text === '' ? '' : text || texts.other.fetchFromIpfsError;
}),
);
}
},
setIpfsData: (hash, data) => {
if (!get().ipfsData[hash]) {
Expand Down Expand Up @@ -507,6 +521,8 @@ export const createProposalsSlice: StoreSlice<
);

if (ipfsData) {
get().setIpfsDataErrors(hash, '', true);

set((state) =>
produce(state, (draft) => {
draft.ipfsData[hash] = ipfsData;
Expand Down

1 comment on commit 194428c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

Please sign in to comment.