Skip to content

Commit

Permalink
hotfix: projects search results fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
aziolek committed Oct 11, 2024
1 parent 21d3de7 commit 34612c3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Img from 'components/ui/Img';
import useSortedProjects from 'hooks/helpers/useIdsInAllocation/useSortedProjects';
import useCurrentEpoch from 'hooks/queries/useCurrentEpoch';
import useIsDecisionWindowOpen from 'hooks/queries/useIsDecisionWindowOpen';
import { ProjectsIpfsWithRewardsAndEpochs } from 'hooks/queries/useSearchedProjectsDetails';

import styles from './ProjectsSearchResults.module.scss';
import ProjectsSearchResultsProps from './types';
Expand All @@ -29,6 +30,15 @@ const ProjectsSearchResults: FC<ProjectsSearchResultsProps> = ({
orderOption,
);

const getEpochNumberToFetchData = (
epochNumber: ProjectsIpfsWithRewardsAndEpochs['epoch'],
): number | undefined => {
if (isDecisionWindowOpen) {
return epochNumber === currentEpoch! - 1 ? undefined : epochNumber;
}
return epochNumber === currentEpoch ? undefined : epochNumber;
};

return (
<div className={styles.list}>
{projectsIpfsWithRewardsAndEpochs.length === 0 && !isLoading && (
Expand All @@ -44,25 +54,28 @@ const ProjectsSearchResults: FC<ProjectsSearchResultsProps> = ({
<Grid>
{projectsIpfsWithRewardsAndEpochs.length > 0 &&
!isLoading &&
projectsIpfsWithRewardsSorted.map((projectIpfsWithRewards, index) => (
<ProjectsListItem
key={`${projectIpfsWithRewards.address}--${projectIpfsWithRewards.epoch}`}
dataTest={
projectIpfsWithRewards.epoch
? `ProjectsView__ProjectsListItem--archive--${index}`
: `ProjectsView__ProjectsListItem--${index}`
}
epoch={projectIpfsWithRewards.epoch}
projectIpfsWithRewards={projectIpfsWithRewards}
searchResultsLabel={
isDecisionWindowOpen && currentEpoch === projectIpfsWithRewards.epoch
? ''
: t('searchResultsLabel', { epochNumber: projectIpfsWithRewards.epoch })
}
/>
))}
{projectsIpfsWithRewardsAndEpochs.length === 0 &&
isLoading &&
projectsIpfsWithRewardsSorted.map((projectIpfsWithRewards, index) => {
const epochNumberToFetchData = getEpochNumberToFetchData(projectIpfsWithRewards.epoch);

return (
<ProjectsListItem
key={`${projectIpfsWithRewards.address}--${projectIpfsWithRewards.epoch}`}
dataTest={
projectIpfsWithRewards.epoch
? `ProjectsView__ProjectsListItem--archive--${index}`
: `ProjectsView__ProjectsListItem--${index}`
}
epoch={epochNumberToFetchData}
projectIpfsWithRewards={projectIpfsWithRewards}
searchResultsLabel={
epochNumberToFetchData
? t('searchResultsLabel', { epochNumber: projectIpfsWithRewards.epoch })
: ''
}
/>
);
})}
{isLoading &&
[...Array(5).keys()].map((_, index) => (
// eslint-disable-next-line react/no-array-index-key
<ProjectsListSkeletonItem key={index} className={styles.element} />
Expand Down
64 changes: 53 additions & 11 deletions client/src/hooks/queries/useSearchedProjectsDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { apiGetProjectIpfsData, apiGetProjects } from 'api/calls/projects';
import {
apiGetEstimatedMatchedProjectRewards,
apiGetMatchedProjectRewards,
Response as ResponseRewards,
} from 'api/calls/rewards';
import { apiGetAllocationsPerProject } from 'api/calls/userAllocations';
import { QUERY_KEYS } from 'api/queryKeys';
import { parseUnitsBigInt } from 'utils/parseUnitsBigInt';

import useCurrentEpoch from './useCurrentEpoch';
import useIsDecisionWindowOpen from './useIsDecisionWindowOpen';
import { ProjectIpfsWithRewards } from './useProjectsIpfsWithRewards';
import { ProjectsSearchResults } from './useSearchedProjects';

Expand All @@ -25,23 +27,52 @@ export interface ProjectsDetails {
refetch: () => void;
}

const getRewards = ({
currentEpoch,
isDecisionWindowOpen,
epoch,
rewardsEstimated,
rewardsPast,
}: {
currentEpoch: number | undefined;
epoch: number | undefined;
isDecisionWindowOpen: boolean | undefined;
rewardsEstimated: ResponseRewards | undefined;
rewardsPast: ResponseRewards | undefined;
}): ResponseRewards['rewards'] | undefined => {
if (epoch === currentEpoch && isDecisionWindowOpen) {
return rewardsEstimated?.rewards;
}
if (epoch !== currentEpoch) {
return rewardsPast?.rewards;
}
return [];
};

export default function useSearchedProjectsDetails(
projectsSearchResults: ProjectsSearchResults | undefined,
): ProjectsDetails {
const { data: currentEpoch } = useCurrentEpoch();
const { data: isDecisionWindowOpen } = useIsDecisionWindowOpen();

const queries = useQueries({
queries: (projectsSearchResults || []).map(projectsSearchResult => ({
queryFn: async () => {
const projectsEpoch = await apiGetProjects(Number(projectsSearchResult.epoch));
return Promise.all([
apiGetProjectIpfsData(`${projectsEpoch?.projectsCid}/${projectsSearchResult.address}`),
projectsSearchResult.epoch === currentEpoch
? apiGetEstimatedMatchedProjectRewards()
: apiGetMatchedProjectRewards(projectsSearchResult.epoch),
apiGetAllocationsPerProject(projectsSearchResult.address, projectsSearchResult.epoch),
projectsSearchResult.epoch,
projectsSearchResult.address,
apiGetProjectIpfsData(`${projectsEpoch?.projectsCid}/${projectsSearchResult.address}`),
projectsSearchResult.epoch !== currentEpoch ||
(projectsSearchResult.epoch === currentEpoch && isDecisionWindowOpen)
? apiGetAllocationsPerProject(projectsSearchResult.address, projectsSearchResult.epoch)
: undefined,
projectsSearchResult.epoch === currentEpoch && isDecisionWindowOpen
? apiGetEstimatedMatchedProjectRewards()
: undefined,
projectsSearchResult.epoch !== currentEpoch
? apiGetMatchedProjectRewards(projectsSearchResult.epoch)
: undefined,
]);
},
queryKey: QUERY_KEYS.searchResultsDetails(
Expand Down Expand Up @@ -69,9 +100,20 @@ export default function useSearchedProjectsDetails(

return {
data: queries.map(({ data }) => {
const rewards = data?.[1]?.rewards ?? [];
const address = data?.[4];
const rewardsOfProject = rewards.find(element => element.address === address);
const epoch = data?.[0];
const address = data?.[1];
const rewardsEstimated = data?.[4] ?? undefined;
const rewardsPast = data?.[5] ?? undefined;
const rewards = getRewards({
currentEpoch,
epoch,
isDecisionWindowOpen,
rewardsEstimated,
rewardsPast,
});
const rewardsOfProject = rewards
? rewards.find(element => element.address === address)
: undefined;
const rewardsOfProjectMatched = rewardsOfProject
? parseUnitsBigInt(rewardsOfProject.matched, 'wei')
: BigInt(0);
Expand All @@ -82,11 +124,11 @@ export default function useSearchedProjectsDetails(
return {
address,
donations: rewardsOfProjectAllocated,
epoch: data?.[3],
epoch: data?.[0],
matchedRewards: rewardsOfProjectMatched,
numberOfDonors: data?.[2].length ?? 0,
numberOfDonors: data?.[3]?.length ?? 0,
totalValueOfAllocations: rewardsOfProjectMatched + rewardsOfProjectAllocated,
...(data?.[0] ?? {}),
...(data?.[2] ?? {}),
};
}),
isFetching: false,
Expand Down

0 comments on commit 34612c3

Please # to comment.